Skip to content

Commit 1a67e40

Browse files
PierrunoYTclaude
andcommitted
fix(history): reject task records whose id does not match the task
readTaskFile accepted any truthy id, so a history_item.json holding a non-string id or another task's id (e.g. a copied task directory) could replace the cached owner during invalidate(). Require a string id equal to the requested taskId; invalid records now throw and keep the cache. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent d59a7fe commit 1a67e40

2 files changed

Lines changed: 43 additions & 26 deletions

File tree

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -872,9 +872,13 @@ export class TaskHistoryStore {
872872

873873
try {
874874
const raw = await fs.readFile(filePath, "utf8")
875-
const item: HistoryItem = JSON.parse(raw)
876-
if (!item?.id) throw new Error(`Invalid task history record: ${filePath}`)
877-
return item
875+
const item: unknown = JSON.parse(raw)
876+
// Reject records that belong to another task (e.g. a copied task directory)
877+
// so they can never replace this task's cached ownership.
878+
if (typeof item !== "object" || item === null || (item as { id?: unknown }).id !== taskId) {
879+
throw new Error(`Invalid task history record: ${filePath}`)
880+
}
881+
return item as HistoryItem
878882
} catch (error) {
879883
if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") return null
880884
throw error

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

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -94,30 +94,43 @@ describe("TaskHistoryStore", () => {
9494
})
9595

9696
describe("invalidate()", () => {
97-
it.each(["malformed", "invalid-record", "read-error", "missing"] as const)(
98-
"preserves cached ownership unless the record is missing (%s)",
99-
async (scenario) => {
100-
await store.initialize()
101-
store.dispose() // Keep filesystem watcher reconciliation out of this explicit refresh test.
102-
const owner = makeHistoryItem({ id: "owner", status: "delegated", awaitingChildId: "child" })
103-
await store.upsert(owner)
104-
const filePath = path.join(tmpDir, "tasks", "owner", GlobalFileNames.historyItem)
105-
if (scenario === "malformed" || scenario === "invalid-record") {
106-
await fs.writeFile(filePath, scenario === "malformed" ? "{" : "{}")
107-
} else {
108-
await fs.unlink(filePath)
109-
if (scenario === "read-error") await fs.mkdir(filePath)
110-
}
97+
const invalidRecords = {
98+
malformed: "{",
99+
"invalid-record": "{}",
100+
"non-object-record": "null",
101+
"non-string-id": JSON.stringify({ id: 42, status: "active" }),
102+
"other-task-id": JSON.stringify({ id: "other-task", status: "active" }),
103+
} as const
104+
105+
it.each([
106+
"malformed",
107+
"invalid-record",
108+
"non-object-record",
109+
"non-string-id",
110+
"other-task-id",
111+
"read-error",
112+
"missing",
113+
] as const)("preserves cached ownership unless the record is missing (%s)", async (scenario) => {
114+
await store.initialize()
115+
store.dispose() // Keep filesystem watcher reconciliation out of this explicit refresh test.
116+
const owner = makeHistoryItem({ id: "owner", status: "delegated", awaitingChildId: "child" })
117+
await store.upsert(owner)
118+
const filePath = path.join(tmpDir, "tasks", "owner", GlobalFileNames.historyItem)
119+
if (scenario in invalidRecords) {
120+
await fs.writeFile(filePath, invalidRecords[scenario as keyof typeof invalidRecords])
121+
} else {
122+
await fs.unlink(filePath)
123+
if (scenario === "read-error") await fs.mkdir(filePath)
124+
}
111125

112-
if (scenario === "missing") {
113-
await expect(store.invalidate("owner")).resolves.toBeUndefined()
114-
expect(store.get("owner")).toBeUndefined()
115-
} else {
116-
await expect(store.invalidate("owner")).rejects.toThrow()
117-
expect(store.get("owner")).toEqual(owner)
118-
}
119-
},
120-
)
126+
if (scenario === "missing") {
127+
await expect(store.invalidate("owner")).resolves.toBeUndefined()
128+
expect(store.get("owner")).toBeUndefined()
129+
} else {
130+
await expect(store.invalidate("owner")).rejects.toThrow()
131+
expect(store.get("owner")).toEqual(owner)
132+
}
133+
})
121134
})
122135

123136
describe("pending action persistence", () => {

0 commit comments

Comments
 (0)