Skip to content

Commit 9697e89

Browse files
committed
fix(replay): wire durable lookup at adapter serialization; trust only pool account handles; harden salt perms
Security-review fold-back: (1) the parser's durable lookup ran before the credential scope was bound, so the store was write-only at runtime — the google adapter now falls back to the durable store at serialization time, when the scope identity exists (regression-pinned); (2) the codex-forward durable handle no longer accepts the client-supplied chatgpt-account-id header (trusted pool context only; direct-forward fails closed); (3) the salt file re-asserts 0600 on every load.
1 parent 1d1d8c2 commit 9697e89

4 files changed

Lines changed: 44 additions & 5 deletions

File tree

‎src/adapters/google.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { identifyRoutedModel } from "./identity";
2626
import { antigravityUsesReplayCache, applyAntigravityReplay, clearAntigravityReplay, observeAntigravityReplay } from "./google-antigravity-replay";
2727
import { resolveAntigravityEffortWireModel } from "../providers/antigravity-models";
2828
import { googleVertexLocationConfigError } from "../providers/google-vertex-location";
29+
import { lookupReplayThoughtSignature } from "../responses/thought-signature-replay";
2930
import {
3031
isTranslatorBudgetExceededError,
3132
retainTranslatedEventBatch,
@@ -215,7 +216,13 @@ function messagesToGeminiFormat(
215216
const part: Record<string, unknown> = { functionCall };
216217
// Prefer the metadata that travelled with this exact call; fall back to the legacy
217218
// field for callers that have not been migrated. Never merge or synthesize.
218-
const signature = tc.providerMetadata?.google?.thoughtSignature ?? tc.thoughtSignature;
219+
// Final fallback (#1926): the durable store, read AT SERIALIZATION TIME. The
220+
// Responses parser runs before the route/credential scope is bound, so its
221+
// parse-time lookup can never hit; by the time this adapter serializes, the
222+
// credential-scoped identity is bound and the durable lookup is meaningful.
223+
const signature = tc.providerMetadata?.google?.thoughtSignature
224+
?? tc.thoughtSignature
225+
?? lookupReplayThoughtSignature(tc.id, parsed._reasoningReplayScope);
219226
if (isLikelyRealThoughtSignature(signature)) part.thoughtSignature = signature;
220227
parts.push(part);
221228
}

‎src/responses/thought-signature-replay.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* destination, adapter, model and credential — so a signature can only ever be replayed into
2121
* the turn that produced it.
2222
*/
23-
import { readFileSync, writeFileSync } from "node:fs";
23+
import { chmodSync, readFileSync, writeFileSync } from "node:fs";
2424
import { randomBytes } from "node:crypto";
2525
import { join } from "node:path";
2626
import { atomicWriteFileAsync, getConfigDir } from "../config";
@@ -87,6 +87,9 @@ export function thoughtSignatureReplaySalt(): Buffer | undefined {
8787
try {
8888
const raw = readFileSync(saltPath());
8989
if (raw.length >= 16) {
90+
// Re-assert owner-only permissions on every load: a pre-existing file may have
91+
// been created before this guard or loosened by external tooling.
92+
try { chmodSync(saltPath(), 0o600); } catch { /* best effort on exotic filesystems */ }
9093
cachedSalt = raw;
9194
return cachedSalt;
9295
}

‎src/server/responses/core.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,13 @@ function bindRouteReasoningReplayScope(args: {
361361
writerGeneration: poolContext?.writerGeneration,
362362
headers: provider.headers,
363363
});
364-
// Durable identity requires a STABLE account handle. A bearer alone is rotating
365-
// material; without an account id the durable store fails closed (audit blocker 2).
364+
// Durable identity requires a STABLE, TRUSTED account handle. Pool context comes from
365+
// our own account store; a client-supplied chatgpt-account-id header is attacker
366+
// -influenceable bucket selection and a bearer alone is rotating material — both are
367+
// refused, so direct-forward turns get no durable scope (fail closed; the in-process
368+
// cache still covers same-process replay).
366369
const codexDurableHandle = poolContext?.accountId
367370
?? poolContext?.chatgptAccountId
368-
?? args.forwardHeaders?.get("chatgpt-account-id")
369371
?? undefined;
370372
credentialDurableIdentity = durableReplayCredentialIdentity(
371373
"codex",

‎tests/google-signature-history-roundtrip.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,31 @@ describe("#1735 thought signature survives history replay", () => {
337337
resetThoughtSignatureReplayForTests();
338338
expect(lookupReplayThoughtSignature("call_dest_restart", scopeFor())).toBe(SIGNATURE);
339339
});
340+
341+
test("adapter serialization reads the durable store with the post-parse bound scope (#1926 wiring)", async () => {
342+
// The server parses BEFORE the route/credential scope exists, so the parser's own
343+
// lookup cannot hit; the google adapter's serialization-time fallback must read the
344+
// durable store once the scope identity has been bound.
345+
rememberThoughtSignatureForReplay("call_wire_1", SIGNATURE, scopeFor());
346+
await flushThoughtSignatureReplayForTests();
347+
const scope: { clientThreadId: string; current?: unknown } = { clientThreadId: "thread-a" };
348+
const parsed = parseRequestScoped({
349+
model: MODEL,
350+
stream: false,
351+
input: [
352+
{ role: "user", content: [{ type: "input_text", text: "run pwd" }] },
353+
{ type: "function_call", call_id: "call_wire_1", name: "shell_command", arguments: "{}" },
354+
{ type: "function_call_output", call_id: "call_wire_1", output: "ok" },
355+
],
356+
tools: [{ type: "function", name: "shell_command", description: "run", parameters: { type: "object" } }],
357+
}, scope as never);
358+
// Identity binds after parse, as bindRouteReasoningReplayScope does server-side.
359+
scope.current = scopeFor().current;
360+
parsed._reasoningReplayScope = scope as never;
361+
const adapter = createGoogleAdapter(provider);
362+
const req = await adapter.buildRequest(parsed, { headers: new Headers() });
363+
const parts = modelParts(String(req.body));
364+
const fnPart = parts.find(p => (p as { functionCall?: unknown }).functionCall) as { thoughtSignature?: string } | undefined;
365+
expect(fnPart?.thoughtSignature).toBe(SIGNATURE);
366+
});
340367
});

0 commit comments

Comments
 (0)