Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(semgrep): string matching instead of built regexes in harness wiring and fix-routing prose; justify build-time and stderr-only sites

garrytan6cd04ca91ddf
diff --git a/scripts/build-agent-protocol.ts b/scripts/build-agent-protocol.ts
index c8e1c30f65..1cd0758149 100644
--- a/scripts/build-agent-protocol.ts
+++ b/scripts/build-agent-protocol.ts
@@ -245,6 +245,7 @@ function renderTranscript(t: Transcript, i: number): string {
}
function replaceRegion(text: string, region: string, inner: string): string {
+ // nosemgrep: javascript.lang.security.audit.detect-non-literal-regexp.detect-non-literal-regexp -- `region` is a constant region name defined in this build script, never input
const begin = new RegExp(`(<!-- BEGIN GENERATED ${region}[^>]*-->\\n)[\\s\\S]*?(<!-- END GENERATED ${region} -->)`);
if (!begin.test(text)) throw new Error(`region ${region} not found`);
return text.replace(begin, (_m, a: string, b: string) => `${a}${inner}${b}`);
diff --git a/scripts/generate-plugin-tree.ts b/scripts/generate-plugin-tree.ts
index ff77a61d2b..c1c53d8570 100644
--- a/scripts/generate-plugin-tree.ts
+++ b/scripts/generate-plugin-tree.ts
@@ -185,6 +185,7 @@ function copySkill(slug: string, destSkillsDir: string): void {
cpSync(join(ROOT, 'skills', slug), join(destSkillsDir, slug), { recursive: true });
const gaps = starterGaps(slug);
if (gaps.length === 0) return;
+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- build-time generator; slug comes from this repository's own plugin definition
const skillMd = join(destSkillsDir, slug, 'SKILL.md');
writeFileSync(skillMd, readFileSync(skillMd, 'utf8').replace(/\n*$/, '\n') + surfaceNote(gaps));
}
@@ -283,6 +284,7 @@ function emitVariant(variantsDir: string, personaName: string, def: PersonaDef,
mkdirSync(join(root, 'skills'), { recursive: true });
const slugs = Object.keys(def.skills).sort();
+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- build-time generator over this repository's own plugin definition
for (const slug of slugs) copySkill(slug, join(root, 'skills'));
// nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal
copySharedDeps(join(root, 'skills'));
diff --git a/src/cli.ts b/src/cli.ts
index f58c9fc0c1..a0f928626c 100755
--- a/src/cli.ts
+++ b/src/cli.ts
@@ -1090,7 +1090,7 @@ export function parseOpArgs(op: Operation, args: string[]): Record<string, unkno
let fileHint = '';
try {
if (typeof prevValue === 'string' && prevValue && existsSync(prevValue)) {
- fileHint = ` If '${prevValue}' is a file you meant to ingest, use: gbrain capture --file ${prevValue} --slug <slug>`;
+ fileHint = ` If '${prevValue}' is a file you meant to ingest, use: gbrain capture --file ${prevValue} --slug <slug>`; // nosemgrep: javascript.express.security.injection.raw-html-format.raw-html-format -- stderr text, never HTML
}
} catch { /* best-effort hint */ }
process.stderr.write(
diff --git a/src/commands/doctor/checks/harness-wiring.ts b/src/commands/doctor/checks/harness-wiring.ts
index f2ae172b21..df9bf903a3 100644
--- a/src/commands/doctor/checks/harness-wiring.ts
+++ b/src/commands/doctor/checks/harness-wiring.ts
@@ -81,13 +81,13 @@ function claudeRegistration(name: string): HarnessRegistration | null {
/** `[mcp_servers.<name>]` from Codex's config.toml: the `command`/`args`/`url`/`env` keys only. */
export function parseCodexRegistration(text: string, name: string, source: string): HarnessRegistration | null {
const lines = text.replace(/\r\n/g, '\n').split('\n');
- const header = new RegExp(`^\\[mcp_servers\\.(?:${name}|"${name}")\\]\\s*$`);
- const start = lines.findIndex((l) => header.test(l.trim()));
+ const headers = new Set([`[mcp_servers.${name}]`, `[mcp_servers."${name}"]`]);
+ const start = lines.findIndex((l) => headers.has(l.trim()));
if (start < 0) return null;
const body: string[] = [];
for (let i = start + 1; i < lines.length && !lines[i].trim().startsWith('['); i++) body.push(lines[i].trim());
const value = (key: string): string | undefined => {
- const line = body.find((l) => new RegExp(`^${key}\\s*=`).test(l));
+ const line = body.find((l) => l.startsWith(key) && l.slice(key.length).trimStart().startsWith('='));
return line?.slice(line.indexOf('=') + 1).trim();
};
const parse = (raw: string | undefined): unknown => {
diff --git a/src/core/agent-output.ts b/src/core/agent-output.ts
index 0b19f6e8e0..319dd10e52 100644
--- a/src/core/agent-output.ts
+++ b/src/core/agent-output.ts
@@ -353,6 +353,19 @@ export interface EnvelopeParts {
retryable?: boolean;
}
+/** Replace each occurrence of `bare` that is a whole command (not followed by a word, path or more flags). */
+function replaceWholeCommand(text: string, bare: string, step: string): string {
+ let out = '';
+ let from = 0;
+ for (let at = text.indexOf(bare); at >= 0; at = text.indexOf(bare, at + bare.length)) {
+ const rest = text.slice(at + bare.length);
+ if (/^[\w/-]/.test(rest) || /^\s+-/.test(rest)) continue;
+ out += text.slice(from, at) + step;
+ from = at + bare.length;
+ }
+ return from === 0 ? text : out + text.slice(from);
+}
+
function appendFixToSuggestion(suggestion: string, fix: RenderedAction | undefined, unpinned?: readonly string[]): string {
if (!fix) return suggestion;
const step = renderedStep(fix);
@@ -360,8 +373,8 @@ function appendFixToSuggestion(suggestion: string, fix: RenderedAction | undefin
// Prose that already quotes the fix before its A1 routing pin quotes the pinned command instead (never both).
const bare = unpinned?.length && !fix.mcp ? shellQuote(unpinned) : undefined;
if (bare && bare !== step) {
- const quoted = new RegExp(`${bare.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(?![\\w/-]|\\s+-)`, 'g');
- if (quoted.test(suggestion)) return suggestion.replace(quoted, () => step);
+ const replaced = replaceWholeCommand(suggestion, bare, step);
+ if (replaced !== suggestion) return replaced;
}
const prose = suggestion.trim();
return prose ? `${prose.replace(/[.\s]*$/, '.')} Next: ${step}` : `Next: ${step}`;