From 4b7c1d898be5538b3fc7457812614324638458fa Mon Sep 17 00:00:00 2001 From: Epinephrine Date: Thu, 26 Nov 2026 11:47:31 +0900 Subject: [PATCH] Fix security finding acf_d6cf0000372abf3947bcb6b422d14484 --- src/adapters/step-cli/adapter.ts | 29 +++++++--- src/adapters/step-cli/projection.ts | 41 +++++++++----- structure/providers/step-cli-transport.md | 3 + tests/adapters/step-cli-run-turn.test.ts | 67 +++++++++++++++++++++++ 4 files changed, 119 insertions(+), 21 deletions(-) diff --git a/src/adapters/step-cli/adapter.ts b/src/adapters/step-cli/adapter.ts index 11bfd85def7..5ff17d871da 100644 --- a/src/adapters/step-cli/adapter.ts +++ b/src/adapters/step-cli/adapter.ts @@ -11,6 +11,7 @@ import { composePrompt, hasImageInput, latestToolResult, + newConversationUserText, pendingUserText, stepModelId, stepThinkingTokens, @@ -158,9 +159,19 @@ export function createStepCliAdapter( ? { handle: identified, sessionId: "" } : sessions.nextRef(); + // A tool result is actionable only for a live conversation holding the matching call. Refuse + // empty, model-only and orphan-result requests before `open` can allocate a process; existing + // conversations still reach `driveTurn`, which performs the query/call ownership checks. + const userText = newConversationUserText(parsed); + const existingSession = userText ? undefined : sessions.get(ref); + if (!existingSession && !userText) { + emit(noUserTextRefusal()); + return; + } + let session!: StepConversationSession; try { - session = await sessions.open(ref); + session = existingSession ?? await sessions.open(ref); await session.handshake(); } catch (error) { await session?.close(); @@ -233,6 +244,14 @@ function emitPreDispatchAbort(emit: (event: AdapterEvent) => void): void { emit({ type: "error", message: "StepCode turn was aborted before start.", retryable: false }); } +function noUserTextRefusal(): AdapterEvent { + return refusal( + "StepCode carries conversation history inside its own process, so a turn must end with either user text or a tool result.", + 400, + "step_cli_no_user_text", + ); +} + async function driveTurn( parsed: OcxParsedRequest, incoming: IncomingMeta, @@ -277,13 +296,7 @@ async function driveTurn( // abandoned the call still carries the user's message and must keep it. const userText = continuedQuery ? undefined : pendingUserText(parsed); if (!continuedQuery && !userText) { - emit( - refusal( - "StepCode carries conversation history inside its own process, so a turn must end with either user text or a tool result.", - 400, - "step_cli_no_user_text", - ), - ); + emit(noUserTextRefusal()); return; } diff --git a/src/adapters/step-cli/projection.ts b/src/adapters/step-cli/projection.ts index cc603199a47..f109c8fc625 100644 --- a/src/adapters/step-cli/projection.ts +++ b/src/adapters/step-cli/projection.ts @@ -195,24 +195,39 @@ export function asRecord(value: unknown): Record { /** * The text StepCode should be asked for on this turn. * - * Only the final user message crosses `query.input`; earlier turns live in the process' session, + * Only the newest user message crosses `query.input`; earlier turns live in the process' session, * because the wire reduces every message to text and images. */ +function userMessageText(message: OcxParsedRequest["context"]["messages"][number]): string | undefined { + if (message.role !== "user") return undefined; + if (!Array.isArray(message.content)) { + return typeof message.content === "string" && message.content.trim().length > 0 + ? message.content + : undefined; + } + const text = message.content + .filter(part => part.type === "text") + .map(part => (part as { text: string }).text) + .join("") + .trim(); + return text.length > 0 ? text : undefined; +} + export function pendingUserText(parsed: OcxParsedRequest): string | undefined { for (let index = parsed.context.messages.length - 1; index >= 0; index -= 1) { const message = parsed.context.messages[index]; - if (!message || message.role !== "user") continue; - if (!Array.isArray(message.content)) { - return typeof message.content === "string" && message.content.length > 0 - ? message.content - : undefined; - } - const text = message.content - .filter(part => part.type === "text") - .map(part => (part as { text: string }).text) - .join("") - .trim(); - if (text.length > 0) return text; + if (message?.role === "user") return userMessageText(message); + } + return undefined; +} + +/** User text that starts a new process, excluding closed assistant turns and orphaned tool results. */ +export function newConversationUserText(parsed: OcxParsedRequest): string | undefined { + for (let index = parsed.context.messages.length - 1; index >= 0; index -= 1) { + const message = parsed.context.messages[index]; + if (!message) continue; + if (message.role === "assistant" || message.role === "toolResult") return undefined; + if (message.role === "user") return userMessageText(message); } return undefined; } diff --git a/structure/providers/step-cli-transport.md b/structure/providers/step-cli-transport.md index bd974780d10..5d09f055528 100644 --- a/structure/providers/step-cli-transport.md +++ b/structure/providers/step-cli-transport.md @@ -43,6 +43,9 @@ permission denials. each session before workspace setup, so shutdown can close an opening session before it spawns. At eight owned sessions a home pool refuses new conversations with retryable `step_cli_pool_full`. Existing handles and coalesced opens still work. Only confirmed cleanup releases capacity. +Before `sessions.open` can allocate a process, a new handle must carry non-whitespace user text. +Empty, model-only and orphan-result requests receive `step_cli_no_user_text`; an existing live handle +still reaches held-call matching, so a legitimate tool result can continue its original query. The pool retains ownership of adapter, idle-timer and sweep closes until termination is confirmed. Shutdown drains those receipts and propagates failures. Closing handles refuse replacement. Session close is idempotent and clears held-request timers. diff --git a/tests/adapters/step-cli-run-turn.test.ts b/tests/adapters/step-cli-run-turn.test.ts index 83f54f91421..797ccd7f4cf 100644 --- a/tests/adapters/step-cli-run-turn.test.ts +++ b/tests/adapters/step-cli-run-turn.test.ts @@ -176,6 +176,23 @@ function host(session: FakeSession): StepConversationHost & { ref: StepConversat }; } +/** A host with no session until `open`, used to distinguish admission from existing continuation. */ +function unopenedHost(session: FakeSession): StepConversationHost & { openCount(): number } { + const base = host(session); + let openedHandle: string | undefined; + let opens = 0; + return { + ...base, + get: ref => ref?.handle === openedHandle ? session : undefined, + open: async ref => { + opens += 1; + openedHandle = ref.handle; + return session; + }, + openCount: () => opens, + }; +} + function provider() { return { adapter: "step-cli", @@ -200,6 +217,56 @@ function parsed(messages: unknown[]): OcxParsedRequest { const USER_ASK = [{ role: "user", content: "run it", timestamp: 0 }]; +describe("StepCode pre-allocation admission", () => { + test("non-actionable new conversations are refused without opening a session", async () => { + const cases: unknown[][] = [ + [], + [ + { role: "user", content: "historical question", timestamp: 0 }, + { role: "assistant", content: "model-only history", timestamp: 1 }, + ], + [{ role: "user", content: " \t\n ", timestamp: 0 }], + [ + { role: "user", content: "historical question", timestamp: 0 }, + { role: "toolResult", toolCallId: "orphan", toolName: "shell", content: "result", timestamp: 1 }, + ], + ]; + for (const messages of cases) { + const session = new FakeSession(); + const conversation = unopenedHost(session); + const events: AdapterEvent[] = []; + await createStepCliAdapter(provider(), { openSession: conversation }).runTurn!( + parsed(messages), + THREAD_META, + event => events.push(event), + ); + expect(conversation.openCount()).toBe(0); + expect(session.handshakeCount).toBe(0); + expect(events).toMatchObject([{ type: "error", code: "step_cli_no_user_text", status: 400 }]); + } + }); + + test("ordinary user text still opens a session and runs the query", async () => { + const session = new FakeSession(); + const conversation = unopenedHost(session); + const events: AdapterEvent[] = []; + session.stream = [TURN_COMPLETE]; + await createStepCliAdapter(provider(), { openSession: conversation }).runTurn!( + parsed([ + { role: "user", content: "historical question", timestamp: 0 }, + { role: "assistant", content: "historical answer", timestamp: 1 }, + { role: "user", content: "run it", timestamp: 2 }, + ]), + THREAD_META, + event => events.push(event), + ); + expect(conversation.openCount()).toBe(1); + expect(session.handshakeCount).toBe(1); + expect(session.inputs[0]?.text).toContain("run it"); + expect(events.find(event => event.type === "done")).toMatchObject({ stopReason: "stop" }); + }); +}); + describe("StepCode reverse requests are always answered", () => { test("a permission request with no turn is denied", () => { const session = new FakeSession();