Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(edge-extractor): match receiver names literally instead of compiling them into a RegExp (semgrep detect-non-literal-regexp)

garrytand0c7e7447c44
diff --git a/src/core/chunkers/edge-extractor.ts b/src/core/chunkers/edge-extractor.ts
index 4f1e877c69..5eb9918f64 100644
--- a/src/core/chunkers/edge-extractor.ts
+++ b/src/core/chunkers/edge-extractor.ts
@@ -335,7 +335,7 @@ function statementBindingPrefix(stmt: any, language: SupportedCodeLanguage, rece
// Check named imports + default + namespace imports for receiverName.
const importText = (stmt.text ?? '') as string;
// Cheap pre-filter: must mention receiverName as an identifier.
- if (!new RegExp(`\\b${receiverName}\\b`).test(importText)) return null;
+ if (!mentionsWord(importText, receiverName)) return null;
return source;
}
// Pattern 1 (Python): `from pkg import obj` / `import pkg`
@@ -345,13 +345,13 @@ function statementBindingPrefix(stmt: any, language: SupportedCodeLanguage, rece
const module = moduleNode?.text;
const importText = (stmt.text ?? '') as string;
if (!module) return null;
- if (!new RegExp(`\\b${receiverName}\\b`).test(importText)) return null;
+ if (!mentionsWord(importText, receiverName)) return null;
return module;
}
if (stmt.type === 'import_statement') {
// `import pkg` — receiver matches module name directly.
const importText = (stmt.text ?? '') as string;
- if (!new RegExp(`\\b${receiverName}\\b`).test(importText)) return null;
+ if (!mentionsWord(importText, receiverName)) return null;
return receiverName;
}
}
@@ -362,7 +362,7 @@ function statementBindingPrefix(stmt: any, language: SupportedCodeLanguage, rece
stmt.type === 'assignment'
) {
const stmtText = (stmt.text ?? '') as string;
- if (!new RegExp(`\\b${receiverName}\\b`).test(stmtText)) return null;
+ if (!mentionsWord(stmtText, receiverName)) return null;
// Look for `new ClassName(...)` (JS/TS) or `ClassName(...)` (Python).
const newMatch = stmtText.match(/=\s*new\s+([A-Za-z_][A-Za-z0-9_]*)/);
if (newMatch) return newMatch[1]!;
@@ -389,6 +389,31 @@ const BINDING_STATEMENT_TYPES = new Set([
]);
const WORD_NAME = /^\w+$/;
+function isWordChar(code: number): boolean {
+ return (code >= 48 && code <= 57) || (code >= 65 && code <= 90) || (code >= 97 && code <= 122) || code === 95;
+}
+
+function isWordBoundary(text: string, at: number): boolean {
+ const before = at > 0 && isWordChar(text.charCodeAt(at - 1));
+ const after = at < text.length && isWordChar(text.charCodeAt(at));
+ return before !== after;
+}
+
+/**
+ * True when `name` occurs in `text` with a word boundary on both sides (JS
+ * `\b`, ASCII word characters). The receiver name comes from indexed source,
+ * so it is matched literally instead of being compiled into a RegExp.
+ *
+ * @internal exported for tests.
+ */
+export function mentionsWord(text: string, name: string): boolean {
+ if (name.length === 0) return false;
+ for (let i = text.indexOf(name); i !== -1; i = text.indexOf(name, i + 1)) {
+ if (isWordBoundary(text, i) && isWordBoundary(text, i + name.length)) return true;
+ }
+ return false;
+}
+
/**
* Top-level bindings of one file, built on first use. Every binding pattern
* requires `\b<name>\b` in the statement text, which for a `\w+` name means
diff --git a/test/edge-extractor-mentions-word.test.ts b/test/edge-extractor-mentions-word.test.ts
new file mode 100644
index 0000000000..60f76be315
--- /dev/null
+++ b/test/edge-extractor-mentions-word.test.ts
@@ -0,0 +1,39 @@
+import { describe, expect, test } from 'bun:test';
+import { mentionsWord } from '../src/core/chunkers/edge-extractor.ts';
+
+function lcg(seed: number): () => number {
+ let s = seed >>> 0;
+ return () => {
+ s = (Math.imul(s, 1664525) + 1013904223) >>> 0;
+ return s / 0x100000000;
+ };
+}
+
+const ALPHABET = 'ab_1 ,.{}()=\n\t$é';
+
+describe('mentionsWord', () => {
+ test('matches new RegExp(`\\b${name}\\b`) for word names over a seeded corpus', () => {
+ const rand = lcg(6715);
+ const names = ['a', 'ab', 'b_1', '_', 'a1b', 'ba'];
+ let checked = 0;
+ for (let n = 0; n < 4000; n++) {
+ const len = Math.floor(rand() * 40);
+ let text = '';
+ for (let i = 0; i < len; i++) text += ALPHABET[Math.floor(rand() * ALPHABET.length)];
+ for (const name of names) {
+ expect(mentionsWord(text, name)).toBe(new RegExp(`\\b${name}\\b`).test(text));
+ checked++;
+ }
+ }
+ expect(checked).toBe(24000);
+ });
+
+ test('matches a receiver name literally instead of as a pattern', () => {
+ expect(mentionsWord("import { $http } from 'ng'", '$http')).toBe(false);
+ expect(mentionsWord("import { a$http } from 'ng'", '$http')).toBe(true);
+ expect(mentionsWord('const xay = 1', 'a.y')).toBe(false);
+ expect(mentionsWord('const obj = new Foo()', 'obj')).toBe(true);
+ expect(mentionsWord('const objx = new Foo()', 'obj')).toBe(false);
+ expect(mentionsWord('anything', '')).toBe(false);
+ });
+});