Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(mentions): no regex built from page or query text (semgrep detect-non-literal-regexp)

garrytan766951932935
diff --git a/src/core/mentions/aliases.ts b/src/core/mentions/aliases.ts
index 5a6df69391..949552e987 100644
--- a/src/core/mentions/aliases.ts
+++ b/src/core/mentions/aliases.ts
@@ -50,7 +50,7 @@ const COMMON_WORDS = new Set([
'june', 'july', 'august', 'september', 'october', 'november', 'december', 'board', 'finance', 'legal', 'sales', 'marketing',
'engineering', 'operations', 'support', 'hr', 'it', 'plan', 'report', 'team', 'project', 'update', 'review', 'company',
]);
-const CORPORATE_SUFFIX = '(?:,?\\s+(?:Inc|LLC|Ltd|Limited|Corp|Corporation|Co|GmbH|PLC|plc|LP|LLP|Holdings|S\\.A)\\.?)*';
+const CORPORATE_SUFFIX_RE = /,?\s+(?:Inc|LLC|Ltd|Limited|Corp|Corporation|Co|GmbH|PLC|LP|LLP|Holdings|S\.A)\.?$/i;
const PAREN_TERM_RE = /\(\s*(?:the\s+)?["\u201c']([^"\u201d'()\n]{1,60})["\u201d']\s*\)/g;
const MAX_NAME_TOKENS = 4;
@@ -132,9 +132,21 @@ export function declarationsIn(text: string, opts: DeclarationOpts = {}): Declar
const out: Declaration[] = [];
const push = (alias: string | null, line: number) => { if (alias && !out.some(d => d.alias === alias)) out.push({ alias, line }); };
const ownNames = (opts.ownNames ?? []).filter(Boolean);
- const parenOwner = ownNames.length
- ? new RegExp(`(?:${ownNames.map(n => n.replace(/[.*+?^\${}()|[\]\\]/g, '\\$&')).join('|')})${CORPORATE_SUFFIX},?\\s*$`, 'i')
- : null;
+ const ownLower = ownNames.map(n => n.toLowerCase());
+ /** Whether `before` ends with one of the page's own names, after any corporate suffixes and a trailing comma. */
+ const endsWithOwnName = (before: string): boolean => {
+ const endsWithName = (t: string) => {
+ const lower = t.toLowerCase();
+ return ownLower.some(n => lower.endsWith(n) && !/[\p{L}\p{N}]/u.test(lower.charAt(lower.length - n.length - 1)));
+ };
+ let t = before.trimEnd().replace(/,$/, '');
+ for (let i = 0; i < 4; i++) {
+ if (endsWithName(t)) return true;
+ if (!CORPORATE_SUFFIX_RE.test(t)) return false;
+ t = t.replace(CORPORATE_SUFFIX_RE, '');
+ }
+ return endsWithName(t);
+ };
for (let i = 0; i < lines.length; i++) {
const line = lines[i]!;
if (line.length > 2000) continue;
@@ -158,9 +170,9 @@ export function declarationsIn(text: string, opts: DeclarationOpts = {}): Declar
if (RELATIONAL_NOUN_RE.test(sentence)) continue;
push(captureName(plain.slice(m.index! + m[0].length), { multiword }), i);
}
- if (parenOwner) {
+ if (ownLower.length) {
for (const m of plain.matchAll(PAREN_TERM_RE)) {
- if (!parenOwner.test(plain.slice(0, m.index))) continue;
+ if (!endsWithOwnName(plain.slice(0, m.index))) continue;
push(captureName(`"${m[1]}"`, { multiword }), i);
}
}
diff --git a/src/core/search/alias-fanout.ts b/src/core/search/alias-fanout.ts
index 3bc4f1834e..405e1d1528 100644
--- a/src/core/search/alias-fanout.ts
+++ b/src/core/search/alias-fanout.ts
@@ -169,8 +169,12 @@ export async function withAliasFanOut(engine: BrainEngine, results: SearchResult
try {
const max = await aliasFanoutMax(engine);
if (max === 0) return { results };
- const queryLc = ` ${queryText.toLowerCase()} `;
- const inQuery = (name: string) => new RegExp(`(?<![\\p{L}\\p{N}])${normalizeAlias(name).replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(?![\\p{L}\\p{N}])`, 'u').test(normalizeAlias(queryLc));
+ const words = (text: string) => normalizeAlias(text).split(/[^\p{L}\p{N}]+/u).filter(Boolean);
+ const queryWords = words(queryText);
+ const inQuery = (name: string) => {
+ const w = words(name);
+ return w.length > 0 && queryWords.some((_, i) => w.every((t, k) => queryWords[i + k] === t));
+ };
const resolved = await resolveQueryEntity(engine, queryText, scope);
let names: Array<{ alias: string; page: string }>;
let usedTokens: [number, number] | null = null;
diff --git a/src/core/verbs/entity-card-identity.ts b/src/core/verbs/entity-card-identity.ts
index 9c1d309df8..037915da4c 100644
--- a/src/core/verbs/entity-card-identity.ts
+++ b/src/core/verbs/entity-card-identity.ts
@@ -34,7 +34,17 @@ export interface IdentityExcerptLine { slug: string; line: string }
/** Whether `text` holds a name other than `subject`: a capitalized multi-word run or a code token. */
export function hasOtherName(text: string, subject: string): boolean {
const own = subject.toLowerCase();
- const stripped = own ? text.replace(new RegExp(own.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi'), ' ') : text;
+ let stripped = text;
+ if (own) {
+ const lower = text.toLowerCase();
+ const parts: string[] = [];
+ let from = 0;
+ for (let at = lower.indexOf(own); at >= 0; at = lower.indexOf(own, from)) {
+ parts.push(text.slice(from, at));
+ from = at + own.length;
+ }
+ stripped = parts.join(' ') + ' ' + text.slice(from);
+ }
for (const m of stripped.matchAll(NAME_RUN_RE)) if (m[0].toLowerCase() !== own) return true;
for (const m of stripped.matchAll(CODE_RE)) if (!NOT_NAMES.has(m[0]) && !/^\d[\d-]*$/.test(m[0])) return true;
return false;