Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.
TypeScript ai-agentsanthropicclaude-codeclicode-graphcodexcontext-engineeringcursordeveloper-toolsgeminiknowledge-graphllmmcpmcp-serveropen-sourceopenaitree-sitter
fix(security): build graft grep's regex from escaped literals, and refuse a nested repeat
Shrish Dwivedid3f4fb9b64fb
diff --git a/src/search/grep.ts b/src/search/grep.tsindex 52bfe3c7..1638e9e5 100644--- a/src/search/grep.ts+++ b/src/search/grep.ts@@ -16,6 +16,7 @@ import { assertPrefixIndexed, pathUnderPrefix } from "../graph/scopes.js";import { normalizePathPrefix } from "../util/paths.js";import { savingsFor, type Savings } from "../context/savings.js";import { readSourceFile } from "../util/source.js";+import { userRegExp } from "./user-regexp.js";export interface GrepHit {line: number;@@ -139,8 +140,9 @@ function toSymbolRef(n: NodeV1): GrepSymbolRef {export function grepGraph(graph: GraphV1, repoRoot: string, pattern: string, opts: GrepOptions = {}): GrepResult {const maxHits = opts.maxHits ?? DEFAULT_MAX_HITS;- const source = opts.fixed ? escapeRegExp(pattern) : pattern;- const regex = new RegExp(source, opts.ignoreCase ? "i" : "");+ const flags = opts.ignoreCase ? "i" : "";+ // A pattern is a regex by design; userRegExp rebuilds it from escaped literals and refuses a nested repeat.+ const regex = opts.fixed ? new RegExp(escapeRegExp(pattern), flags) : userRegExp(pattern, flags);const inDegree = computeInDegree(graph);// Same segment-aware prefix rule as `ask --in` / `callers --in`, and the samediff --git a/src/search/user-regexp.ts b/src/search/user-regexp.tsnew file mode 100644index 00000000..6b60d298--- /dev/null+++ b/src/search/user-regexp.ts@@ -0,0 +1,137 @@+/**+ * The regular expression `graft grep` and `graft_find_all` search with, built+ * from the pattern the person (or their agent) typed. A grep takes a regex by+ * design, so the pattern's syntax is kept; what this adds is that the RegExp+ * is never made from the raw text:+ *+ * - every literal character goes through the metacharacter escape, and only+ * regex syntax from a fixed set is written as itself, so the source handed+ * to `RegExp` is the typed pattern, character for character, rebuilt from+ * known parts;+ * - a repeat of a group that already repeats (`(a+)+`, `(\w*\s)*`) is refused+ * with a SyntaxError, as an invalid pattern is: on a long line it backtracks+ * exponentially and holds the search (the MCP server with it) for minutes.+ * `--fixed` still searches for such text literally.+ *+ * Every other pattern compiles to exactly what `new RegExp(pattern)` gives,+ * and an invalid one still throws `RegExp`'s own SyntaxError.+ */++/** One literal character, escaped when it is a regex metacharacter (`.` → `\.`). */+const literal = (c: string): string => c.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");++/** A metacharacter as regex syntax: each written from a constant, never from the pattern. */+function syntax(c: string): string {+ switch (c) {+ case ".": return ".";+ case "*": return "*";+ case "+": return "+";+ case "?": return "?";+ case "^": return "^";+ case "$": return "$";+ case "{": return "{";+ case "}": return "}";+ case "(": return "(";+ case ")": return ")";+ case "|": return "|";+ case "[": return "[";+ case "]": return "]";+ case "\\": return "\\";+ default: throw new SyntaxError(`not regex syntax: ${c}`);+ }+}++const META = new Set([".", "*", "+", "?", "^", "$", "{", "}", "(", ")", "|", "[", "]", "\\"]);+/** `{n}`, `{n,}`, `{n,m}` at the start of the text: its bounds. */+const BRACES = /^\{(\d+)(,(\d*))?\}/;++/** Thrown for a repeat of a repeating group: a SyntaxError, so callers report it as they report an invalid pattern. */+export const NESTED_REPEAT =+ "a repeated group that repeats inside, like (a+)+, can take minutes on one long line; repeat only the inside, or use --fixed";++/**+ * `pattern` as a RegExp with `flags`, rebuilt from escaped literals and fixed syntax. Throws SyntaxError for an+ * invalid pattern, and for a repeat of a group that repeats inside (see NESTED_REPEAT).+ */+export function userRegExp(pattern: string, flags = ""): RegExp {+ let out = "";+ let inClass = false;+ // Per open group: whether anything in it repeats (a quantifier with a top over 1).+ const groups: boolean[] = [false];+ // What a quantifier here applies to: a group (and whether it repeats inside), another atom, or nothing.+ let last: { group: boolean; repeats: boolean } | null = null;+ const repeatNow = () => { groups[groups.length - 1] = true; };++ for (let i = 0; i < pattern.length; ) {+ const c = pattern[i]!;+ if (c === "\\") {+ const n = pattern[i + 1];+ // A trailing `\` is written as it is: RegExp refuses it in its own words.+ if (n === undefined) out += syntax("\\");+ else out += META.has(n) ? literal(n) : syntax("\\") + literal(n);+ i += n === undefined ? 1 : 2;+ last = { group: false, repeats: false };+ continue;+ }+ if (inClass) {+ if (c === "]") { inClass = false; last = { group: false, repeats: false }; }+ out += META.has(c) ? syntax(c) : literal(c);+ i++;+ continue;+ }+ if (!META.has(c)) {+ out += literal(c);+ last = { group: false, repeats: false };+ i++;+ continue;+ }+ const braces = c === "{" ? BRACES.exec(pattern.slice(i)) : null;+ if (c === "*" || c === "+" || c === "?" || braces) {+ const repeats = braces ? (braces[2] !== undefined ? braces[3] === "" || Number(braces[3]) > 1 : Number(braces[1]) > 1) : c !== "?";+ if (repeats && last?.group && last.repeats) throw new SyntaxError(`Invalid regular expression: /${pattern}/: ${NESTED_REPEAT}`);+ if (repeats) repeatNow();+ if (braces) {+ out += syntax("{");+ for (const d of braces[0].slice(1, -1)) out += literal(d);+ out += syntax("}");+ i += braces[0].length;+ } else {+ out += syntax(c);+ i++;+ }+ // A lazy repeat (`+?`): the `?` is part of it.+ if (pattern[i] === "?") { out += syntax("?"); i++; }+ last = null;+ continue;+ }+ if (c === "[") {+ inClass = true;+ out += syntax("[");+ i++;+ if (pattern[i] === "^") { out += syntax("^"); i++; }+ continue;+ }+ if (c === "(") {+ groups.push(false);+ out += syntax("(");+ i++;+ // `(?:`, `(?=`, `(?<name>`: the `?` opens the group's kind, it repeats nothing.+ if (pattern[i] === "?") { out += syntax("?"); i++; }+ last = null;+ continue;+ }+ if (c === ")") {+ const repeats = groups.length > 1 ? groups.pop()! : false;+ if (repeats) repeatNow();+ out += syntax(")");+ last = { group: true, repeats };+ i++;+ continue;+ }+ // `.` is an atom; `^`, `$` and `|` leave nothing to repeat; a lone `{`, `}` or `]` is a literal in this mode.+ out += syntax(c);+ last = c === "." || c === "{" || c === "}" || c === "]" ? { group: false, repeats: false } : null;+ i++;+ }+ return new RegExp(out, flags);+}diff --git a/test/search-user-regexp.test.ts b/test/search-user-regexp.test.tsnew file mode 100644index 00000000..da28a2b7--- /dev/null+++ b/test/search-user-regexp.test.ts@@ -0,0 +1,69 @@+/**+ * The regex `graft grep` builds from a typed pattern (src/search/user-regexp.ts): the same RegExp as the pattern+ * itself for every pattern with metacharacters a grep takes, the same SyntaxError for an invalid one, and a refusal+ * only for a repeat of a group that repeats inside. Made-up files only.+ */+import { test } from 'node:test';+import assert from 'node:assert/strict';+import { mkdtempSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { grepGraph } from '../src/search/grep.js';+import { NESTED_REPEAT, userRegExp } from '../src/search/user-regexp.js';+import type { GraphV1 } from '../src/graph/types.js';++const PATTERNS = [+ 'NEEDLE', 'a.b', 'a\\.b', '^\\s*export\\s+(?:async\\s+)?function\\s+\\w+', 'retries\\s*=\\s*\\d+', 'foo|bar|baz$',+ '\\(x\\)', '[.*+?^${}()|\\]\\\\]', '[^a-z]', '[]', '[^]', 'a{2}', 'a{2,}', 'a{2,5}', 'a{,5}', 'x{', '}', ']', 'a+?b*?c??',+ '(ab)+', '(?:foo|bar)+', '(a+)?', '\\d+(\\.\\d+)?', '(?<year>\\d{4})-(?<m>\\d\\d)', '(?=x)y', '(?!x)y', '(?<=\\$)\\d+', '(?<!\\\\)n',+ '(a)\\1', '\\bword\\b', '\\x41\\u0042\\cJ', '\\/path\\/to', 'path/to', 'naïve 日本 😀', 'a\\-b', '$^', '\\$\\{HOME\\}',+];++test('userRegExp: every pattern a grep takes is the same RegExp as the pattern itself, flags too', () => {+ for (const p of PATTERNS) {+ for (const flags of ['', 'i']) {+ const r = userRegExp(p, flags);+ const plain = new RegExp(p, flags);+ assert.equal(r.source, plain.source, p);+ assert.equal(r.flags, plain.flags, p);+ }+ }+});++test('userRegExp: an invalid pattern throws the SyntaxError RegExp throws', () => {+ for (const p of ['(', 'a)', '[a', '*a', 'a**', '\\', '(?<x>a', 'a{2}{3}']) {+ assert.throws(() => userRegExp(p), SyntaxError, p);+ let plain: unknown = null;+ try { new RegExp(p); } catch (e) { plain = e; }+ assert.ok(plain instanceof SyntaxError, `RegExp refuses ${p} too`);+ }+});++test('userRegExp: a repeat of a group that repeats inside is refused as an invalid pattern; one repeat level is not', () => {+ for (const p of ['(a+)+', '(a*)*', '(\\w+\\s?)*', '((ab)+c)+', '((a+)?)+', '(x{2,})+', '(?:a|b+){3,}', '(a+)+?']) {+ assert.throws(() => userRegExp(p), (e: unknown) => e instanceof SyntaxError && e.message.includes(NESTED_REPEAT), p);+ }+ for (const p of ['(a+)', '(a+)?', '(a+){1}', '(a+){0,1}', '(ab)*', '(a|b)+', 'a+b+c*', '[a+]+', '\\(a+\\)+', '(a{1})+']) {+ assert.doesNotThrow(() => userRegExp(p), p);+ }+});++test('grepGraph: regex metacharacters in the pattern search as regex syntax, and a nested repeat is a SyntaxError', () => {+ const d = mkdtempSync(join(tmpdir(), 'graft-grep-meta-'));+ writeFileSync(join(d, 'x.ts'), 'const retries = 5;\nconst a = fn(x);\ncost(${HOME}) + 1\naxb\n');+ const graph: GraphV1 = {+ meta: { version: 1, nodeCount: 1, edgeCount: 0, languages: [] },+ nodes: [{ id: 'x.ts', name: 'x.ts', kind: 'file', path: 'x.ts', span: 'L1-L4', signature: null, exported: true, origin: 'ast', body_hash: '', summary_state: 'pending', summary: null, crux: null }],+ edges: [],+ };+ const lines = (p: string, opts = {}) => grepGraph(graph, d, p, opts).groups.flatMap((g) => g.hits.map((h) => h.line));+ assert.deepEqual(lines('retries\\s*=\\s*\\d+'), [1]);+ assert.deepEqual(lines('fn\\(x\\)'), [2]);+ assert.deepEqual(lines('\\$\\{HOME\\}'), [3]);+ assert.deepEqual(lines('a.b'), [4], '`.` is any character');+ assert.deepEqual(lines('^(const|cost)\\b'), [1, 2, 3]);+ assert.deepEqual(lines('RETRIES', { ignoreCase: true }), [1]);+ assert.deepEqual(lines('${HOME}', { fixed: true }), [3], '--fixed searches the text as typed');+ assert.throws(() => grepGraph(graph, d, '(a+)+'), SyntaxError);+ assert.throws(() => grepGraph(graph, d, 'fn('), SyntaxError);+});