fix(filter): read only own payload properties

Inherited object properties are not webhook payload data.
Read own properties only so JSONPath cannot traverse prototypes.
This commit is contained in:
翅膀 committed 2026-10-23 23:21:25 +08:00
1 parent 900962e003
commit 1b6b30066f
2 files changed
+17 -3

No files matched your search

+7 -3
View File
@@ -172,7 +172,8 @@ function stringify(value: unknown): string {
if (typeof value === "string") return value;
if (value === undefined || value === null) return "";
if (typeof value === "number" || typeof value === "boolean") return String(value);
return JSON.stringify(value);
// Payload values are JSON; anything else (should it appear) compares as "".
return JSON.stringify(value) ?? "";
}
function toNumber(value: unknown): number | null {
@@ -203,8 +204,11 @@ function walkPath(node: unknown, parts: string[], index: number, out: unknown[])
return;
}
if (node === null || node === undefined || typeof node !== "object") return;
const next = (node as Record<string, unknown>)[parts[index]!];
walkPath(next, parts, index + 1, out);
// Own properties only: `constructor`, `__proto__`, `toString`... are not
// payload data and must never reach the comparison.
const key = parts[index]!;
if (!Object.prototype.hasOwnProperty.call(node, key)) return;
walkPath((node as Record<string, unknown>)[key], parts, index + 1, out);
}
function valueList(filter: Filter, event: WebhookEvent): unknown[] {
+10
View File
@@ -191,3 +191,13 @@ describe("filter-ast pattern engine", () => {
expect(evaluateFilterNode({ type: "repo", match: "acme" }, ev)).toBe(false);
});
});
describe("filter-ast payload paths", () => {
test("never reads inherited properties", () => {
const node = { type: "field" as const, path: "constructor", match: "x" };
expect(evaluateFilterNode(node, event())).toBe(false);
expect(evaluateFilterNode({ type: "field", path: "__proto__", op: "exists" }, event())).toBe(
false,
);
});
});