Skip to content

Commit 5e06cf0

Browse files
author
yfge
committed
fix: validate reset transcript paths
1 parent 6f86f1a commit 5e06cf0

2 files changed

Lines changed: 74 additions & 24 deletions

File tree

‎src/gateway/server.sessions.gateway-server-sessions-a.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,40 @@ describe("gateway server sessions", () => {
14751475
ws.close();
14761476
});
14771477

1478+
test("sessions.reset falls back from unsafe outside sessionFile paths", async () => {
1479+
const { dir } = await createSessionStoreDir();
1480+
const outsideDir = path.join(sharedSessionStoreDir, "outside-transcripts");
1481+
const unsafeSessionFile = path.join(outsideDir, "pinned-transcript.jsonl");
1482+
await writeSessionStore({
1483+
entries: {
1484+
main: {
1485+
sessionId: "sess-unsafe-path",
1486+
sessionFile: unsafeSessionFile,
1487+
updatedAt: Date.now(),
1488+
},
1489+
},
1490+
});
1491+
1492+
const { ws } = await openClient();
1493+
const reset = await rpcReq<{
1494+
ok: true;
1495+
entry: {
1496+
sessionId: string;
1497+
sessionFile?: string;
1498+
};
1499+
}>(ws, "sessions.reset", { key: "main" });
1500+
1501+
expect(reset.ok).toBe(true);
1502+
expect(reset.payload?.entry.sessionFile).toBe(
1503+
path.join(dir, `${reset.payload?.entry.sessionId}.jsonl`),
1504+
);
1505+
expect(reset.payload?.entry.sessionFile?.startsWith(outsideDir)).toBe(false);
1506+
await expect(fs.stat(reset.payload?.entry.sessionFile as string)).resolves.toBeTruthy();
1507+
await expect(fs.stat(unsafeSessionFile)).rejects.toThrow();
1508+
1509+
ws.close();
1510+
});
1511+
14781512
test("sessions.preview resolves legacy mixed-case main alias with custom mainKey", async () => {
14791513
const { dir, storePath } = await createSessionStoreDir();
14801514
testState.agentsConfig = { list: [{ id: "ops", default: true }] };

‎src/gateway/session-reset-service.ts‎

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ import {
1414
type SessionEntry,
1515
updateSessionStore,
1616
} from "../config/sessions.js";
17-
import {
18-
resolveSessionFilePath,
19-
resolveSessionFilePathOptions,
20-
resolveSessionTranscriptPathInDir,
21-
} from "../config/sessions/paths.js";
17+
import { resolveSessionFilePath, resolveSessionFilePathOptions } from "../config/sessions/paths.js";
2218
import { logVerbose } from "../globals.js";
2319
import { createInternalHookEvent, triggerInternalHook } from "../hooks/internal-hooks.js";
2420
import { closeTrackedBrowserTabsForSessions } from "../plugin-sdk/browser-runtime.js";
@@ -46,6 +42,38 @@ function getChannelRuntime() {
4642
return cachedChannelRuntime;
4743
}
4844

45+
function extractGeneratedTranscriptSessionId(sessionFile?: string): string | undefined {
46+
const trimmed = sessionFile?.trim();
47+
if (!trimmed) {
48+
return undefined;
49+
}
50+
const base = path.basename(trimmed);
51+
if (!base.endsWith(".jsonl")) {
52+
return undefined;
53+
}
54+
const withoutExt = base.slice(0, -".jsonl".length);
55+
const topicIndex = withoutExt.indexOf("-topic-");
56+
if (topicIndex > 0) {
57+
const topicSessionId = withoutExt.slice(0, topicIndex);
58+
return looksLikeGeneratedSessionId(topicSessionId) ? topicSessionId : undefined;
59+
}
60+
const forkMatch = withoutExt.match(
61+
/^(\d{4}-\d{2}-\d{2}T[\w-]+(?:Z|[+-]\d{2}(?:-\d{2})?)?)_(.+)$/,
62+
);
63+
if (forkMatch?.[2]) {
64+
return looksLikeGeneratedSessionId(forkMatch[2]) ? forkMatch[2] : undefined;
65+
}
66+
return looksLikeGeneratedSessionId(withoutExt) ? withoutExt : undefined;
67+
}
68+
69+
function looksLikeGeneratedSessionId(value: string): boolean {
70+
return /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value);
71+
}
72+
73+
function isGeneratedTranscriptPath(sessionFile: string): boolean {
74+
return Boolean(extractGeneratedTranscriptSessionId(sessionFile));
75+
}
76+
4977
function stripRuntimeModelState(entry?: SessionEntry): SessionEntry | undefined {
5078
if (!entry) {
5179
return entry;
@@ -319,26 +347,14 @@ export async function performGatewaySessionReset(params: {
319347
storePath,
320348
agentId: sessionAgentId,
321349
});
322-
// Determine whether the current entry uses a custom transcript directory
323-
// (i.e. one outside the default sessions dir). If so, preserve that
324-
// directory and only rotate the filename (new sessionId) so spawned/owned
325-
// sessions don't silently relocate their transcripts. See #55474.
326-
const defaultSessionFile = resolveSessionFilePath(
327-
currentEntry?.sessionId ?? nextSessionId,
328-
undefined,
329-
pathOpts,
330-
);
331-
const defaultSessionsDir = path.dirname(defaultSessionFile);
332-
const explicitSessionFileDir = currentEntry?.sessionFile
333-
? path.dirname(path.resolve(currentEntry.sessionFile))
350+
const defaultSessionFile = resolveSessionFilePath(nextSessionId, undefined, pathOpts);
351+
const explicitSessionFile = currentEntry?.sessionFile
352+
? resolveSessionFilePath(nextSessionId, { sessionFile: currentEntry.sessionFile }, pathOpts)
334353
: undefined;
335-
const hasCustomSessionFileDir =
336-
explicitSessionFileDir !== undefined &&
337-
path.relative(defaultSessionsDir, explicitSessionFileDir).startsWith("..");
338-
339-
const sessionFile = hasCustomSessionFileDir
340-
? resolveSessionTranscriptPathInDir(nextSessionId, explicitSessionFileDir)
341-
: resolveSessionFilePath(nextSessionId, undefined, pathOpts);
354+
const sessionFile =
355+
explicitSessionFile && !isGeneratedTranscriptPath(explicitSessionFile)
356+
? explicitSessionFile
357+
: defaultSessionFile;
342358
const nextEntry: SessionEntry = {
343359
sessionId: nextSessionId,
344360
sessionFile,

0 commit comments

Comments
 (0)