diff --git a/bun.lock b/bun.lock index 28df17b..6311577 100644 --- a/bun.lock +++ b/bun.lock @@ -1,6 +1,5 @@ { "lockfileVersion": 1, - "configVersion": 1, "workspaces": { "": { "name": "webhooker", @@ -10,6 +9,7 @@ "markdown-it": "^15.0.0", "nuxt": "^4.1.0", "octokit": "^4.1.0", + "re2js": "^2.8.8", "valibot": "^1.4.2", "vue": "^3.5.13", }, @@ -1557,6 +1557,8 @@ "rc9": ["rc9@3.0.1", "", { "dependencies": { "defu": "^6.1.6", "destr": "^2.0.5" } }, "sha512-gMDyleLWVE+i6Sgtc0QbbY6pEKqYs97NGi6isHQPqYlLemPoO8dxQ3uGi0f4NiP98c+jMW6cG1Kx9dDwfvqARQ=="], + "re2js": ["re2js@2.8.8", "https://registry.npmmirror.com/re2js/-/re2js-2.8.8.tgz", {}, "sha512-E2FVgqD2mzJ+ZmMSHLnkOvZ7PiZ4FgMdfVCA3YsJWs9gtyoaKmUMxyVPCXS1yK209/mz3XkdT7kr3OlMWW2Fww=="], + "readable-stream": ["readable-stream@4.7.0", "", { "dependencies": { "abort-controller": "^3.0.0", "buffer": "^6.0.3", "events": "^3.3.0", "process": "^0.11.10", "string_decoder": "^1.3.0" } }, "sha512-oIGGmcpTLwPga8Bn6/Z75SVaH1z5dUut2ibSyAMVhmUggWpmDn2dapB0n7f8nwaSiRtepAsfJyfXIO5DCVAODg=="], "readdir-glob": ["readdir-glob@1.1.3", "", { "dependencies": { "minimatch": "^5.1.0" } }, "sha512-v05I2k7xN8zXvPD9N+z/uhXPaj0sUFCe2rcWZIpBsqxfP7xXFQ0tipAd/wjj1YxWyWtUS5IDJpOG82JKt2EAVA=="], diff --git a/package.json b/package.json index 9fac216..4fde906 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "markdown-it": "^15.0.0", "nuxt": "^4.1.0", "octokit": "^4.1.0", + "re2js": "^2.8.8", "valibot": "^1.4.2", "vue": "^3.5.13" }, diff --git a/server/lib/events/filter-ast.ts b/server/lib/events/filter-ast.ts index caa3cdf..a69acd8 100644 --- a/server/lib/events/filter-ast.ts +++ b/server/lib/events/filter-ast.ts @@ -1,40 +1,93 @@ +import { RE2JS } from "re2js"; import type { WebhookEvent, Filter, FilterNode, FilterOp } from "../types"; -const regexCache = new Map(); +/** A compiled user pattern; `test` searches (like `RegExp#test`). */ +interface Pattern { + test(text: string): boolean; +} + +const patternCache = new Map(); const keywordBodyCache = new WeakMap(); const MAX_PATTERN_LENGTH = 200; +const MAX_CACHED_PATTERNS = 500; function isWrappedRegex(pattern: string): boolean { return pattern.length >= 2 && pattern.startsWith("/") && pattern.endsWith("/"); } -function compileRegex(pattern: string): RegExp | null { - if (pattern.length > MAX_PATTERN_LENGTH) return null; - const cached = regexCache.get(pattern); - if (cached) return cached; +/** + * Compile with RE2 (re2js): matching runs in time linear in the input, so no + * pattern — however it nests or repeats — can stall a Worker on a crafted + * payload. RE2 has no backreferences or lookaround; such patterns are invalid. + */ +function compileRe2(source: string): Pattern { + const re = RE2JS.compile(source, RE2JS.CASE_INSENSITIVE); + return { test: (text: string): boolean => re.matcher(text).find() }; +} + +/** + * Why a user-supplied regex cannot be used, or null when it is fine. Patterns + * run on the linear-time RE2 engine, so only the length limit and the RE2 + * syntax (no backreferences or lookaround) constrain them. + */ +export function regexSafetyIssue(source: string): string | null { + if (source.length > MAX_PATTERN_LENGTH) return "pattern is too long"; try { - const re = new RegExp(pattern, "i"); - regexCache.set(pattern, re); - return re; - } catch { + compileRe2(source); return null; + } catch { + return "invalid or unsupported regular expression (RE2 syntax: no backreferences or lookaround)"; } } -function compileGlob(pattern: string, anchored: boolean): RegExp | null { - if (pattern.length > MAX_PATTERN_LENGTH) return null; - const cacheKey = `${anchored ? "a" : "s"}:${pattern}`; - const cached = regexCache.get(cacheKey); - if (cached) return cached; - const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&"); - const body = escaped.replace(/\*/g, ".*").replace(/\?/g, "."); - try { - const re = new RegExp(anchored ? `^${body}$` : body, "i"); - regexCache.set(cacheKey, re); - return re; - } catch { - return null; +function cached(key: string, build: () => Pattern | null): Pattern | null { + if (patternCache.has(key)) { + // Most recently used last: the oldest unused pattern is evicted first. + const hit = patternCache.get(key) ?? null; + patternCache.delete(key); + patternCache.set(key, hit); + return hit; } + // Bounded: patterns come from every group's routes and test requests. One + // pattern makes room at a time — clearing everything would recompile every + // pattern in use once the limit is reached. + if (patternCache.size >= MAX_CACHED_PATTERNS) { + patternCache.delete(patternCache.keys().next().value!); + } + const re = build(); + patternCache.set(key, re); + return re; +} + +function compileRegex(pattern: string): Pattern | null { + // Regex and glob entries live under distinct prefixes: the regex `a:foo*` + // and the anchored glob `foo*` must never share a cache slot. + return cached(`r:${pattern}`, () => { + if (pattern.length > MAX_PATTERN_LENGTH) return null; + try { + return compileRe2(pattern); + } catch { + return null; + } + }); +} + +function compileGlob(pattern: string, anchored: boolean): Pattern | null { + if (pattern.length > MAX_PATTERN_LENGTH) return null; + // Repeated `*` match the same as one; an unanchored search needs no leading + // or trailing `*` at all. + let glob = pattern.replace(/\*+/g, "*"); + if (!anchored) glob = glob.replace(/^\*+|\*+$/g, ""); + return cached(`${anchored ? "a" : "s"}:${glob}`, () => { + const escaped = glob.replace(/[.+^${}()|[\]\\]/g, "\\$&"); + // One pass: a sequential replace would rewrite the `?` of `(?s:` again. + const body = escaped.replace(/[*?]/g, (c) => (c === "*" ? "(?s:.*)" : "(?s:.)")); + try { + return compileRe2(anchored ? `^${body}$` : body); + } catch { + return null; + } + }); } function isGlob(pattern: string): boolean { diff --git a/tests/filter-ast.test.ts b/tests/filter-ast.test.ts index 487434f..ac67f2c 100644 --- a/tests/filter-ast.test.ts +++ b/tests/filter-ast.test.ts @@ -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); + }); +});