Skip to content

Commit ab1a797

Browse files
committed
fix(plugin-sdk): guard legacy dedupe JSON parse against malformed files
1 parent 738b2be commit ab1a797

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

src/plugin-sdk/memory-host-events.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,20 @@ describe("createPersistentDedupe", () => {
232232
]);
233233
});
234234

235+
it("treats malformed legacy JSON cache files as empty", async () => {
236+
const root = await createTempDir("openclaw-legacy-dedupe-malformed-");
237+
const legacyPath = path.join(root, "legacy.json");
238+
await fs.writeFile(legacyPath, "{not valid json");
239+
240+
await expect(
241+
listPersistentDedupeLegacyJsonFileEntries({
242+
filePath: legacyPath,
243+
ttlMs: 500,
244+
now: 1_100,
245+
}),
246+
).resolves.toStrictEqual([]);
247+
});
248+
235249
it("warms empty namespaces and ignores retired JSON cache files", async () => {
236250
const root = await createTempDir("openclaw-dedupe-");
237251
const emptyReader = createDedupe(root, { ttlMs: 10_000 });

src/plugin-sdk/persistent-dedupe.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,12 @@ function parseLegacyDedupeData(raw: string): {
326326
data: Record<string, number>;
327327
invalidCount: number;
328328
} {
329-
const parsed = JSON.parse(raw) as unknown;
329+
let parsed: unknown;
330+
try {
331+
parsed = JSON.parse(raw) as unknown;
332+
} catch {
333+
return { data: {}, invalidCount: 0 };
334+
}
330335
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
331336
return { data: {}, invalidCount: 0 };
332337
}

0 commit comments

Comments
 (0)