mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-15 19:52:50 +03:00
* fix(deps): bump nanoid, dompurify for 2 new Dependabot alerts (#189, #190) Bumps: nanoid ^3.3.17 (was transitive, now overridden), dompurify ^3.4.13 (with monaco-editor scoped override). Closes Dependabot #189, #190. Remaining #182-#188 (js-yaml + mermaid) already closed by #9651 merge — awaiting Dependabot re-scan. npm audit → 0 vulnerabilities. * fix(repo): harden .gitignore to also ignore a _tasks symlink (/_tasks) _tasks is a SEPARATE nested git repo (gitignored). The pattern _tasks/ (trailing slash) ignores only a directory, not a SYMLINK named _tasks. A self-referential _tasks symlink can slip in via git add -A and, once pulled, checkout materializes it over the real _tasks repo (destroying plans/specs/hands-off). Anchored /_tasks ignores the symlink too, preventing re-capture. * feat(codex): converge OAuth fingerprints * test(codex): preserve identity assertions * fix(codex): preserve explicit off identity * fix(codex): close fingerprint transport gaps --------- Co-authored-by: diegosouzapw <diegosouzapw@users.noreply.github.com> Co-authored-by: Diego Rodrigues de Sa e Souza <diegosouza.pw@outlook.com> Co-authored-by: diegosouzapw <diegosouza.pw@gmail.com> Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
374 lines
13 KiB
TypeScript
374 lines
13 KiB
TypeScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
import {
|
|
applyCodexClientIdentityHeaders,
|
|
applyCodexClientMetadata,
|
|
applyCodexOriginalIdentityHeaders,
|
|
createCodexClientIdentity,
|
|
getCodexClientSessionId,
|
|
getCodexConvergedSessionId,
|
|
getCodexConvergedThreadId,
|
|
getCodexFingerprintMode,
|
|
getCodexInstallationId,
|
|
resolveCodexFingerprintIdentity,
|
|
resolveCodexOriginalIdentityHeaders,
|
|
withCodexFingerprintCredentials,
|
|
} from "../../open-sse/config/codexIdentity.ts";
|
|
|
|
const oauthCredentials = {
|
|
accessToken: "oauth-token",
|
|
connectionId: "connection-42",
|
|
providerSpecificData: { workspaceId: "workspace-42" },
|
|
};
|
|
|
|
test("Codex fingerprint mode defaults to session and only explicit off disables it", () => {
|
|
assert.equal(getCodexFingerprintMode(undefined), "session");
|
|
assert.equal(getCodexFingerprintMode({ codexFingerprintMode: "invalid" }), "session");
|
|
assert.equal(getCodexFingerprintMode({ codexFingerprintMode: "off" }), "off");
|
|
assert.equal(
|
|
resolveCodexFingerprintIdentity({
|
|
credentials: { ...oauthCredentials, providerSpecificData: { codexFingerprintMode: "off" } },
|
|
clientHeaders: { "session-id": "client-session" },
|
|
body: {},
|
|
}),
|
|
null
|
|
);
|
|
});
|
|
|
|
test("Codex off mode preserves original OAuth identity headers", () => {
|
|
const clientHeaders = {
|
|
"session-id": "client-session",
|
|
"thread-id": "client-thread",
|
|
"x-client-request-id": "client-request",
|
|
"x-codex-window-id": "client-thread:0",
|
|
"x-codex-turn-metadata": '{"turn_id":"client-turn"}',
|
|
};
|
|
const credentials = {
|
|
...oauthCredentials,
|
|
providerSpecificData: { codexFingerprintMode: "off" },
|
|
};
|
|
const original = resolveCodexOriginalIdentityHeaders({
|
|
credentials,
|
|
clientHeaders,
|
|
});
|
|
const wrapped = withCodexFingerprintCredentials(credentials, clientHeaders, {});
|
|
assert.deepEqual(wrapped.providerSpecificData.codexOriginalIdentityHeaders, original);
|
|
assert.equal(wrapped.providerSpecificData.codexClientIdentity, undefined);
|
|
|
|
const resolvedAgain = resolveCodexOriginalIdentityHeaders({
|
|
credentials,
|
|
clientHeaders,
|
|
});
|
|
assert.ok(resolvedAgain);
|
|
|
|
const headers: Record<string, string> = { session_id: "generated-session" };
|
|
applyCodexOriginalIdentityHeaders(headers, resolvedAgain);
|
|
assert.equal(headers["session-id"], "client-session");
|
|
assert.equal(headers["thread-id"], "client-thread");
|
|
assert.equal(headers["x-client-request-id"], "client-request");
|
|
assert.equal(headers["x-codex-window-id"], "client-thread:0");
|
|
assert.equal(headers["x-codex-turn-metadata"], '{"turn_id":"client-turn"}');
|
|
assert.equal(
|
|
resolveCodexOriginalIdentityHeaders({
|
|
credentials: {
|
|
...oauthCredentials,
|
|
requestEndpointPath: "/responses/compact",
|
|
providerSpecificData: { codexFingerprintMode: "off" },
|
|
},
|
|
clientHeaders: { "session-id": "compact-session" },
|
|
}),
|
|
null
|
|
);
|
|
});
|
|
|
|
test("Codex device/session/full modes preserve their Sub2API convergence boundaries", () => {
|
|
const providerSpecificData = { workspaceId: "workspace-42" };
|
|
const device = createCodexClientIdentity("client-session-a", providerSpecificData, {
|
|
mode: "device",
|
|
});
|
|
const sessionA = createCodexClientIdentity("client-session-a", providerSpecificData, {
|
|
mode: "session",
|
|
accountKey: "connection-42",
|
|
});
|
|
const sessionB = createCodexClientIdentity("client-session-b", providerSpecificData, {
|
|
mode: "session",
|
|
accountKey: "connection-42",
|
|
});
|
|
const fullA = createCodexClientIdentity("client-session-a", providerSpecificData, {
|
|
mode: "full",
|
|
accountKey: "connection-42",
|
|
});
|
|
const fullB = createCodexClientIdentity("client-session-b", providerSpecificData, {
|
|
mode: "full",
|
|
accountKey: "connection-42",
|
|
});
|
|
|
|
assert.ok(device && sessionA && sessionB && fullA && fullB);
|
|
assert.equal(device.installationId, getCodexInstallationId(providerSpecificData, undefined));
|
|
assert.notEqual(
|
|
getCodexInstallationId({}, "connection-a"),
|
|
getCodexInstallationId({}, "connection-b")
|
|
);
|
|
assert.equal(device.sessionId, "");
|
|
assert.equal(sessionA.sessionId, sessionB.sessionId);
|
|
assert.notEqual(sessionA.threadId, sessionB.threadId);
|
|
assert.equal(sessionA.windowId, `${sessionA.threadId}:0`);
|
|
assert.equal(fullA.sessionId, fullB.sessionId);
|
|
assert.equal(fullA.threadId, fullA.sessionId);
|
|
assert.equal(fullA.threadId, fullB.threadId);
|
|
assert.notEqual(sessionA.turnId, sessionB.turnId);
|
|
assert.equal(
|
|
getCodexConvergedSessionId(providerSpecificData, "connection-42"),
|
|
sessionA.sessionId
|
|
);
|
|
assert.equal(
|
|
getCodexConvergedThreadId("client-session-a", providerSpecificData, "connection-42"),
|
|
sessionA.threadId
|
|
);
|
|
});
|
|
|
|
test("Codex client session extraction prefers hyphenated session-id and rejects unsafe values", () => {
|
|
assert.equal(
|
|
getCodexClientSessionId({ "session-id": "hyphen", session_id: "underscore" }),
|
|
"hyphen"
|
|
);
|
|
assert.equal(getCodexClientSessionId({ session_id: "underscore" }), "underscore");
|
|
assert.equal(getCodexClientSessionId({ "session-id": "bad\\r\\nheader" }), null);
|
|
});
|
|
|
|
test("One Codex identity is shared by headers, body metadata, and nested turn metadata", () => {
|
|
const identity = resolveCodexFingerprintIdentity({
|
|
credentials: oauthCredentials,
|
|
clientHeaders: { "session-id": "client-session" },
|
|
body: {},
|
|
});
|
|
assert.ok(identity);
|
|
|
|
const headers: Record<string, string> = {};
|
|
applyCodexClientIdentityHeaders(headers, identity);
|
|
const body: Record<string, unknown> = {
|
|
client_metadata: { "x-codex-turn-metadata": '{"sandbox":"none"}' },
|
|
};
|
|
applyCodexClientMetadata(body, identity);
|
|
|
|
const headerMetadata = JSON.parse(headers["x-codex-turn-metadata"]);
|
|
const clientMetadata = body.client_metadata as Record<string, unknown>;
|
|
const bodyMetadata = JSON.parse(clientMetadata["x-codex-turn-metadata"] as string);
|
|
|
|
assert.equal(headers["session-id"], clientMetadata.session_id);
|
|
assert.equal(headers["thread-id"], clientMetadata.thread_id);
|
|
assert.equal(headers["x-codex-window-id"], clientMetadata["x-codex-window-id"]);
|
|
assert.equal(identity.installationId, headers["x-codex-installation-id"]);
|
|
assert.equal(identity.turnId, clientMetadata.turn_id);
|
|
assert.equal(identity.turnId, headerMetadata.turn_id);
|
|
assert.equal(identity.turnId, bodyMetadata.turn_id);
|
|
assert.equal(bodyMetadata.sandbox, "none");
|
|
});
|
|
|
|
test("Codex compact requests do not resolve a fingerprint identity", () => {
|
|
assert.equal(
|
|
resolveCodexFingerprintIdentity({
|
|
credentials: {
|
|
...oauthCredentials,
|
|
requestEndpointPath: "/responses/compact",
|
|
},
|
|
clientHeaders: { "session-id": "client-session" },
|
|
body: {},
|
|
}),
|
|
null
|
|
);
|
|
});
|
|
|
|
test("Codex HTTP off mode preserves original identity headers and body metadata", async () => {
|
|
const { CodexExecutor } = await import("../../open-sse/executors/codex.ts");
|
|
const executor = new CodexExecutor();
|
|
const originalFetch = globalThis.fetch;
|
|
let upstreamHeaders = new Headers();
|
|
let upstreamBody: Record<string, unknown> = {};
|
|
globalThis.fetch = async (_url, init) => {
|
|
upstreamHeaders = new Headers(init?.headers);
|
|
upstreamBody = JSON.parse(String(init?.body || "{}"));
|
|
return new Response(JSON.stringify({ id: "resp-off", object: "response" }), {
|
|
status: 200,
|
|
headers: { "Content-Type": "application/json" },
|
|
});
|
|
};
|
|
|
|
try {
|
|
await executor.execute({
|
|
model: "gpt-5.5",
|
|
body: {
|
|
model: "gpt-5.5",
|
|
input: [{ role: "user", content: "hello" }],
|
|
client_metadata: {
|
|
session_id: "client-session",
|
|
thread_id: "client-thread",
|
|
turn_id: "client-turn",
|
|
"x-codex-window-id": "client-thread:0",
|
|
"x-codex-turn-metadata": '{"turn_id":"client-turn"}',
|
|
},
|
|
_nativeCodexPassthrough: true,
|
|
},
|
|
stream: true,
|
|
clientHeaders: {
|
|
"session-id": "client-session",
|
|
"thread-id": "client-thread",
|
|
"x-client-request-id": "client-request",
|
|
"x-codex-window-id": "client-thread:0",
|
|
"x-codex-turn-metadata": '{"turn_id":"client-turn"}',
|
|
},
|
|
credentials: {
|
|
accessToken: "codex-token",
|
|
connectionId: "conn-http-off",
|
|
providerSpecificData: { workspaceId: "http-off", codexFingerprintMode: "off" },
|
|
},
|
|
});
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
|
|
const metadata = upstreamBody.client_metadata as Record<string, unknown>;
|
|
assert.equal(upstreamHeaders.get("session-id"), "client-session");
|
|
assert.equal(upstreamHeaders.get("thread-id"), "client-thread");
|
|
assert.equal(upstreamHeaders.get("x-client-request-id"), "client-request");
|
|
assert.equal(upstreamHeaders.get("x-codex-window-id"), "client-thread:0");
|
|
assert.equal(upstreamHeaders.get("x-codex-turn-metadata"), '{"turn_id":"client-turn"}');
|
|
assert.equal(metadata.session_id, "client-session");
|
|
assert.equal(metadata.thread_id, "client-thread");
|
|
assert.equal(metadata.turn_id, "client-turn");
|
|
assert.equal(metadata["x-codex-window-id"], "client-thread:0");
|
|
assert.equal(metadata["x-codex-turn-metadata"], '{"turn_id":"client-turn"}');
|
|
});
|
|
|
|
test("Codex websocket off mode preserves original identity headers and body metadata", async () => {
|
|
const { CodexExecutor, __setCodexWebSocketTransportForTesting } =
|
|
await import("../../open-sse/executors/codex.ts");
|
|
const executor = new CodexExecutor();
|
|
let sent: string | null = null;
|
|
let wsHeaders: Record<string, string> = {};
|
|
__setCodexWebSocketTransportForTesting(async (_url, opts) => {
|
|
wsHeaders = (opts?.headers as Record<string, string>) || {};
|
|
return {
|
|
send(data: string) {
|
|
sent = data;
|
|
queueMicrotask(() => {
|
|
this.onmessage?.({
|
|
data: JSON.stringify({
|
|
type: "response.completed",
|
|
response: { status: "completed" },
|
|
}),
|
|
});
|
|
});
|
|
},
|
|
close() {},
|
|
onmessage: null,
|
|
onerror: null,
|
|
onclose: null,
|
|
};
|
|
});
|
|
|
|
try {
|
|
const result = await executor.execute({
|
|
model: "gpt-5.5",
|
|
body: {
|
|
model: "gpt-5.5",
|
|
input: [{ role: "user", content: "hello" }],
|
|
client_metadata: {
|
|
session_id: "client-session",
|
|
thread_id: "client-thread",
|
|
turn_id: "client-turn",
|
|
},
|
|
},
|
|
stream: true,
|
|
clientHeaders: {
|
|
"session-id": "client-session",
|
|
"thread-id": "client-thread",
|
|
"x-client-request-id": "client-request",
|
|
"x-codex-window-id": "client-thread:0",
|
|
"x-codex-turn-metadata": '{"turn_id":"client-turn"}',
|
|
},
|
|
credentials: {
|
|
accessToken: "codex-token",
|
|
connectionId: "conn-ws-off",
|
|
providerSpecificData: {
|
|
workspaceId: "ws-off",
|
|
codexTransport: "websocket",
|
|
codexFingerprintMode: "off",
|
|
},
|
|
},
|
|
});
|
|
await result.response.text();
|
|
} finally {
|
|
__setCodexWebSocketTransportForTesting(undefined);
|
|
}
|
|
|
|
assert.ok(sent);
|
|
const payload = JSON.parse(sent as string) as Record<string, unknown>;
|
|
const metadata = payload.client_metadata as Record<string, unknown>;
|
|
assert.equal(wsHeaders["session-id"], "client-session");
|
|
assert.equal(wsHeaders["thread-id"], "client-thread");
|
|
assert.equal(wsHeaders["x-client-request-id"], "client-request");
|
|
assert.equal(wsHeaders["x-codex-window-id"], "client-thread:0");
|
|
assert.equal(wsHeaders["x-codex-turn-metadata"], '{"turn_id":"client-turn"}');
|
|
assert.equal(metadata.session_id, "client-session");
|
|
assert.equal(metadata.thread_id, "client-thread");
|
|
assert.equal(metadata.turn_id, "client-turn");
|
|
});
|
|
|
|
test("Codex websocket headers and payload share one fingerprint identity", async () => {
|
|
const { CodexExecutor, __setCodexWebSocketTransportForTesting } =
|
|
await import("../../open-sse/executors/codex.ts");
|
|
const executor = new CodexExecutor();
|
|
let sent: string | null = null;
|
|
let wsHeaders: Record<string, string> = {};
|
|
__setCodexWebSocketTransportForTesting(async (_url, opts) => {
|
|
wsHeaders = (opts?.headers as Record<string, string>) || {};
|
|
return {
|
|
send(data: string) {
|
|
sent = data;
|
|
queueMicrotask(() => {
|
|
this.onmessage?.({
|
|
data: JSON.stringify({
|
|
type: "response.completed",
|
|
response: { status: "completed" },
|
|
}),
|
|
});
|
|
});
|
|
},
|
|
close() {},
|
|
onmessage: null,
|
|
onerror: null,
|
|
onclose: null,
|
|
};
|
|
});
|
|
|
|
try {
|
|
const result = await executor.execute({
|
|
model: "gpt-5.5",
|
|
body: {
|
|
model: "gpt-5.5",
|
|
session_id: "client-ws",
|
|
input: [{ role: "user", content: "hello" }],
|
|
},
|
|
stream: true,
|
|
credentials: {
|
|
accessToken: "codex-token",
|
|
connectionId: "conn-ws",
|
|
providerSpecificData: { workspaceId: "ws-ws", codexTransport: "websocket" },
|
|
},
|
|
});
|
|
await result.response.text();
|
|
} finally {
|
|
__setCodexWebSocketTransportForTesting(undefined);
|
|
}
|
|
|
|
assert.ok(sent);
|
|
const payload = JSON.parse(sent as string) as Record<string, unknown>;
|
|
const metadata = (payload.client_metadata as Record<string, unknown>) || {};
|
|
assert.equal(wsHeaders.session_id, metadata.session_id);
|
|
assert.equal(wsHeaders["x-client-request-id"], metadata.thread_id);
|
|
assert.equal(wsHeaders["x-codex-window-id"], metadata["x-codex-window-id"]);
|
|
assert.equal(payload.type, "response.create");
|
|
});
|