Skip to content

Commit 6d8225f

Browse files
fix(logs): drop query metadata and bound unwrap of codex://threads links
Review feedback on #711: codex://threads/<id>?hostId=… pastes kept the query string in the candidate id so neither raw-id nor digest matching worked, and the lazy-wildcard regex had quadratic backtracking on malformed slash-flooded inputs run per log row. unwrapLogConversationQuery is now a bounded (512-char) linear scan: cut at ? or #, strip trailing slashes, and only return a single-segment id. Regression cases cover durable and percent-encoded remote-control hostId values across the dashboard matcher, GET /api/logs conversationId, and ocx logs filter --conversation. Co-Authored-By: Epinephrine <[email protected]>
1 parent 19a0f35 commit 6d8225f

6 files changed

Lines changed: 90 additions & 7 deletions

File tree

‎docs-site/src/content/docs/guides/web-dashboard.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,9 @@ speed, and conversation ID over the currently loaded request ring. Provider and
148148
choices also include fallback attempts; model matching ignores case and surrounding spaces
149149
but does not match partial names. Choices that disappear from the ring reset to All.
150150
The conversation field also accepts a pasted `codex://threads/<id>` deep link, the form a
151-
Codex "copy session link" puts on the clipboard — the wrapper is ignored and the bare
152-
thread id is matched, the same value an `ocx logs filter --conversation` search unwraps.
151+
Codex "copy session link" puts on the clipboard — the wrapper and any link metadata such
152+
as `?hostId=…` are ignored and the bare thread id is matched, the same value an
153+
`ocx logs filter --conversation` search unwraps.
153154

154155
Time windows cover the last 15 minutes, hour, or day and refresh every 30 seconds while the
155156
Logs tab is active, even with auto-refresh off. Windows use the proxy timestamp from

‎gui/src/log-conversation-id.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,27 @@ function hasControlChars(value: string): boolean {
1717
return false;
1818
}
1919

20+
const CODEX_THREAD_LINK_PREFIX = "codex://threads/";
21+
const LOG_CONVERSATION_LINK_MAX = 512;
22+
2023
/**
21-
* Unwrap a pasted `codex://threads/<id>` deep link to the bare thread id.
24+
* Unwrap a pasted `codex://threads/<id>` deep link to the bare thread id; query/fragment
25+
* metadata (e.g. `?hostId=…`) is dropped. Bounded linear scan — no ambiguous regex.
2226
* Mirrors src/server/request-log-conversation.unwrapLogConversationQuery.
2327
*/
2428
export function unwrapLogConversationQuery(query: string): string {
25-
const unwrapped = /^codex:\/\/threads\/(.*?)\/*$/i.exec(query.trim())?.[1]?.trim();
26-
return unwrapped || query.trim();
29+
const trimmed = query.trim();
30+
if (
31+
trimmed.length > LOG_CONVERSATION_LINK_MAX ||
32+
trimmed.length <= CODEX_THREAD_LINK_PREFIX.length ||
33+
!trimmed.toLowerCase().startsWith(CODEX_THREAD_LINK_PREFIX)
34+
) {
35+
return trimmed;
36+
}
37+
const rest = trimmed.slice(CODEX_THREAD_LINK_PREFIX.length);
38+
const metaIndex = rest.search(/[?#]/);
39+
const segment = (metaIndex === -1 ? rest : rest.slice(0, metaIndex)).replace(/\/+$/, "");
40+
return segment !== "" && !/[/\s]/.test(segment) ? segment : trimmed;
2741
}
2842

2943
/** SHA-256 hex prefix used as the persisted conversation id. */

‎gui/tests/logs-filter.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,24 @@ describe("codex://threads paste in the conversation filter", () => {
202202
expect(unwrapLogConversationQuery(CODEX_THREAD_ID)).toBe(CODEX_THREAD_ID);
203203
expect(unwrapLogConversationQuery("codex://other/x")).toBe("codex://other/x");
204204
expect(unwrapLogConversationQuery("codex://threads/")).toBe("codex://threads/");
205+
expect(unwrapLogConversationQuery(`codex://threads/${CODEX_THREAD_ID}?hostId=durable`)).toBe(CODEX_THREAD_ID);
206+
expect(unwrapLogConversationQuery(`codex://threads/${CODEX_THREAD_ID}?hostId=remote-control%3Aexample-environment`)).toBe(CODEX_THREAD_ID);
207+
expect(unwrapLogConversationQuery(`codex://threads/${CODEX_THREAD_ID}#frag`)).toBe(CODEX_THREAD_ID);
205208
expect(await hashLogConversationQuery(`codex://threads/${CODEX_THREAD_ID}`)).toBe(digest32(CODEX_THREAD_ID));
209+
expect(await hashLogConversationQuery(`codex://threads/${CODEX_THREAD_ID}?hostId=durable`)).toBe(digest32(CODEX_THREAD_ID));
210+
});
211+
212+
test("malformed or oversized pastes stay plain queries and never match", () => {
213+
const slashFlood = `codex://threads/${"/".repeat(4000)}\u2028x`;
214+
expect(unwrapLogConversationQuery(slashFlood)).toBe(slashFlood.trim());
215+
expect(unwrapLogConversationQuery("codex://threads/id/extra")).toBe("codex://threads/id/extra");
216+
const rows = [{ id: "hashed", conversationId: digest32(CODEX_THREAD_ID) }];
217+
expect(filterLogs(rows, { ...DEFAULT_LOG_FILTER_STATE, conversationId: slashFlood }, NOW)).toEqual([]);
218+
expect(filterLogs(rows, {
219+
...DEFAULT_LOG_FILTER_STATE,
220+
conversationId: `codex://threads/${CODEX_THREAD_ID}?hostId=durable`,
221+
conversationQueryHash: digest32(CODEX_THREAD_ID),
222+
}, NOW).map(row => row.id)).toEqual(["hashed"]);
206223
});
207224

208225
test("filterLogs matches a stored digest or raw id behind the pasted link", async () => {

‎src/server/request-log-conversation.ts‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,33 @@ export function normalizeLogConversationId(raw: string | undefined | null): stri
3737
return createHash("sha256").update(trimmed).digest("hex").slice(0, LOG_CONVERSATION_ID_LEN);
3838
}
3939

40+
const CODEX_THREAD_LINK_PREFIX = "codex://threads/";
41+
/** Real thread links stay well under this bound; larger pastes skip unwrapping so per-row matching stays cheap. */
42+
const LOG_CONVERSATION_LINK_MAX = 512;
43+
4044
/**
4145
* Unwrap a pasted `codex://threads/<id>` deep link to the bare thread id. Codex's
4246
* "copy session link" clipboard carries that URI form while clients send the bare id
4347
* on the wire, so Logs search accepts either paste without changing what is persisted.
48+
* Query/fragment metadata (e.g. `?hostId=…`) belongs to the link, not the id, and is
49+
* dropped. The scan is a bounded linear walk — no ambiguous regex — because this runs
50+
* per log row on every filter evaluation.
4451
*/
4552
export function unwrapLogConversationQuery(query: string): string {
46-
const unwrapped = /^codex:\/\/threads\/(.*?)\/*$/i.exec(query.trim())?.[1]?.trim();
47-
return unwrapped || query.trim();
53+
const trimmed = query.trim();
54+
if (
55+
trimmed.length > LOG_CONVERSATION_LINK_MAX ||
56+
trimmed.length <= CODEX_THREAD_LINK_PREFIX.length ||
57+
!trimmed.toLowerCase().startsWith(CODEX_THREAD_LINK_PREFIX)
58+
) {
59+
return trimmed;
60+
}
61+
const rest = trimmed.slice(CODEX_THREAD_LINK_PREFIX.length);
62+
const metaIndex = rest.search(/[?#]/);
63+
const segment = (metaIndex === -1 ? rest : rest.slice(0, metaIndex)).replace(/\/+$/, "");
64+
// A thread id is a single path segment; deeper paths or embedded whitespace keep the
65+
// whole paste as the query, so foreign `codex://` URIs still fail to match.
66+
return segment !== "" && !/[/\s]/.test(segment) ? segment : trimmed;
4867
}
4968

5069
/**

‎tests/cli/cli-log-view-filter.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ test("conversation digest and preimage use the existing owner, including alias s
135135
expect(filterLogs(logs, { ...DEFAULT_LOG_FILTER_STATE, conversationId: "hello", conversationQueryHash: await hashLogConversationQuery("hello") }, NOW).map(row => row.id)).toEqual(["digest", "direct"]);
136136
expect(await run(logs, ["--conversationId", digest])).toBe(0);
137137
expect(ids()).toEqual(["digest"]);
138+
expect(await run(logs, ["--conversation=codex://threads/hello?hostId=durable"])).toBe(0);
139+
expect(ids()).toEqual(["digest", "direct"]);
138140
});
139141

140142
test("modern scope counts ignore server totals and unknown metadata, preserving recent ordered duplicates", async () => {

‎tests/usage/request-log-conversation.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
reasoningReplayConversationIdFromResponsesRequest,
1010
sessionIdHeaderFromRequest,
1111
summarizeConversationLogs,
12+
unwrapLogConversationQuery,
1213
} from "../../src/server/request-log-conversation";
1314
import {
1415
filterRequestLogs,
@@ -71,6 +72,29 @@ describe("matchesLogConversationId", () => {
7172
expect(matchesLogConversationId(stored, "codex://other/x")).toBe(false);
7273
expect(matchesLogConversationId(stored, "codex://threads/")).toBe(false);
7374
});
75+
76+
test("unwraps codex://threads links carrying query or fragment metadata", () => {
77+
const threadId = "019f6482-67d5-77c2-a643-02daddaa7115";
78+
const stored = digest32(threadId);
79+
for (const paste of [
80+
`codex://threads/${threadId}?hostId=durable`,
81+
`codex://threads/${threadId}?hostId=remote-control%3Aexample-environment`,
82+
`codex://threads/${threadId}/?hostId=durable&view=full`,
83+
`codex://threads/${threadId}#section`,
84+
]) {
85+
expect(matchesLogConversationId(stored, paste)).toBe(true);
86+
expect(matchesLogConversationId(threadId, paste)).toBe(true);
87+
}
88+
});
89+
90+
test("rejects malformed or oversized codex://threads pastes without backtracking", () => {
91+
const stored = digest32("019f6482-67d5-77c2-a643-02daddaa7115");
92+
const slashFlood = `codex://threads/${"/".repeat(4000)}\u2028x`;
93+
expect(unwrapLogConversationQuery(slashFlood)).toBe(slashFlood.trim());
94+
expect(matchesLogConversationId(stored, slashFlood)).toBe(false);
95+
expect(matchesLogConversationId(stored, "codex://threads/id/extra")).toBe(false);
96+
expect(matchesLogConversationId(stored, `codex://threads/${"a".repeat(600)}`)).toBe(false);
97+
});
7498
});
7599

76100
describe("sessionIdHeaderFromRequest", () => {
@@ -211,6 +235,12 @@ describe("request log conversation persistence / filter", () => {
211235
];
212236
const params = new URLSearchParams(`conversationId=${encodeURIComponent(`codex://threads/${threadId}`)}`);
213237
expect(filterRequestLogs(logs, params).map(e => e.requestId)).toEqual(["a"]);
238+
for (const hostId of ["durable", "remote-control%3Aexample-environment"]) {
239+
const withHost = new URLSearchParams(
240+
`conversationId=${encodeURIComponent(`codex://threads/${threadId}?hostId=${hostId}`)}`,
241+
);
242+
expect(filterRequestLogs(logs, withHost).map(e => e.requestId)).toEqual(["a"]);
243+
}
214244
});
215245

216246
test("hydrated usage rows keep conversationId", () => {

0 commit comments

Comments
 (0)