Skip to content

Commit 6c3b329

Browse files
PierrunoYTampagent
andcommitted
fix(history): reject unsafe task IDs before file access
Validate task directory components before strict delegation refreshes and other history-file access. Cover traversal, Windows aliases, and an escaped record with a matching ID. Amp-Thread-ID: https://ampcode.com/threads/T-01a10c95-779b-756d-9944-c14dcc2f955e Co-authored-by: Amp <[email protected]>
1 parent 22e4316 commit 6c3b329

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

‎src/core/task-persistence/TaskHistoryStore.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,10 @@ export class TaskHistoryStore {
801801
value !== "." &&
802802
value !== ".." &&
803803
!value.includes("/") &&
804-
!value.includes("\\")
804+
!value.includes("\\") &&
805+
!value.includes(":") &&
806+
!value.includes("\u0000") &&
807+
!/[. ]$/.test(value)
805808
)
806809
}
807810

@@ -1271,6 +1274,11 @@ export class TaskHistoryStore {
12711274
* Get the path to a task's `history_item.json` file.
12721275
*/
12731276
private async getTaskFilePath(taskId: string): Promise<string> {
1277+
// IDs must be single directory names on every supported OS. This also rejects Windows
1278+
// drive/stream syntax and trailing-dot/space aliases before any history-file access.
1279+
if (!this.isSafeTaskId(taskId)) {
1280+
throw new Error(`Invalid task ID: ${JSON.stringify(taskId)}`)
1281+
}
12741282
const tasksDir = await this.getTasksDir()
12751283
return path.join(tasksDir, taskId, GlobalFileNames.historyItem)
12761284
}

‎src/core/task-persistence/__tests__/TaskHistoryStore.spec.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,38 @@ describe("TaskHistoryStore", () => {
529529
})
530530

531531
describe("refreshStrict()", () => {
532+
it.each([
533+
"",
534+
".",
535+
"..",
536+
"../outside",
537+
"..\\outside",
538+
"child/../owner",
539+
"/outside",
540+
"C:outside",
541+
".. ",
542+
"child\u0000",
543+
])("rejects unsafe task ID %j", async (taskId) => {
544+
await store.initialize()
545+
store.dispose()
546+
await expect(store.refreshStrict(taskId)).rejects.toThrow("Invalid task ID")
547+
})
548+
549+
it("does not load an escaped record even when its ID matches the traversal", async () => {
550+
await store.initialize()
551+
store.dispose()
552+
const taskId = "../outside"
553+
const outsideDir = path.join(tmpDir, "outside")
554+
await fs.mkdir(outsideDir)
555+
await fs.writeFile(
556+
path.join(outsideDir, GlobalFileNames.historyItem),
557+
JSON.stringify(makeHistoryItem({ id: taskId, status: "completed" })),
558+
)
559+
560+
await expect(store.refreshStrict(taskId)).rejects.toThrow("Invalid task ID")
561+
expect(store.get(taskId)).toBeUndefined()
562+
})
563+
532564
it.each([
533565
["missing", undefined],
534566
["read-error", { code: "EISDIR" }],

0 commit comments

Comments
 (0)