fix(filter): use isolated linear-time patterns

Backtracking patterns can exhaust worker CPU on untrusted payloads.
Use linear-time matching and separate bounded cache namespaces so
glob and regex entries cannot corrupt each other.
This commit is contained in:
翅膀 committed 2026-10-23 23:21:20 +08:00
1 parent 50966a4c50
commit 125d5eee1f
4 files changed
+102 -23

No files matched your search

+23
View File
@@ -168,3 +168,26 @@ describe("matchRoute AST integration", () => {
expect(matchRoute({ ...baseRoute, enabled: false }, event())).toBe(false);
});
});
describe("filter-ast pattern engine", () => {
test("runs backtracking-prone patterns in linear time", () => {
const started = Date.now();
const text = `${"a".repeat(50_000)}!`;
for (const match of ["/(a+)+$/", "/a+a+a+a+$/", "*a*a*a*a*b"]) {
expect(
evaluateFilterNode(
{ type: "field", path: "body", match },
event({ payload: { body: text } }),
),
).toBe(false);
}
expect(Date.now() - started).toBeLessThan(2000);
});
test("keeps glob ? and * semantics", () => {
const ev = event();
expect(evaluateFilterNode({ type: "repo", match: "acme/w?dget" }, ev)).toBe(true);
expect(evaluateFilterNode({ type: "repo", match: "acme/*" }, ev)).toBe(true);
expect(evaluateFilterNode({ type: "repo", match: "acme" }, ev)).toBe(false);
});
});