fix(plugin-sdk): guard legacy dedupe JSON parse against malformed files (#98125)

This commit is contained in:
pick-cat
2026-07-01 00:56:20 +08:00
committed by GitHub
parent 6cb82eaab8
commit 169acd1e4e
2 changed files with 20 additions and 1 deletions

View File

@@ -232,6 +232,20 @@ describe("createPersistentDedupe", () => {
]);
});
it("treats malformed legacy JSON cache files as empty", async () => {
const root = await createTempDir("openclaw-legacy-dedupe-malformed-");
const legacyPath = path.join(root, "legacy.json");
await fs.writeFile(legacyPath, "{not valid json");
await expect(
listPersistentDedupeLegacyJsonFileEntries({
filePath: legacyPath,
ttlMs: 500,
now: 1_100,
}),
).resolves.toStrictEqual([]);
});
it("warms empty namespaces and ignores retired JSON cache files", async () => {
const root = await createTempDir("openclaw-dedupe-");
const emptyReader = createDedupe(root, { ttlMs: 10_000 });

View File

@@ -326,7 +326,12 @@ function parseLegacyDedupeData(raw: string): {
data: Record<string, number>;
invalidCount: number;
} {
const parsed = JSON.parse(raw) as unknown;
let parsed: unknown;
try {
parsed = JSON.parse(raw) as unknown;
} catch {
return { data: {}, invalidCount: 0 };
}
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
return { data: {}, invalidCount: 0 };
}