Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(trust): escape envelope tags with one global replace; annotate vetted write-gate regex construction for semgrep

garrytan5b9c814b55bd
diff --git a/scripts/check-write-gate-regex.ts b/scripts/check-write-gate-regex.ts
index ce8529af0b..c2cfd70693 100644
--- a/scripts/check-write-gate-regex.ts
+++ b/scripts/check-write-gate-regex.ts
@@ -47,6 +47,7 @@ export function checkWriteGatePatterns(patterns: readonly WriteGatePattern[] = W
function fixturePatterns(root: string): WriteGatePattern[] {
const rows = JSON.parse(readFileSync(join(root, 'patterns.json'), 'utf8')) as Array<{ name: string; source: string; flags?: string; anchors: string[]; preceded?: string }>;
+ // nosemgrep: javascript.lang.security.audit.detect-non-literal-regexp.detect-non-literal-regexp -- self-test fixture patterns this guard exists to vet; never runs on user text
return rows.map(r => ({ name: r.name, family: 'override', rx: new RegExp(r.source, r.flags ?? 'i'), anchors: r.anchors, ...(r.preceded ? { preceded: new RegExp(r.preceded, 'i') } : {}) }));
}
diff --git a/src/core/eligibility/labels.ts b/src/core/eligibility/labels.ts
index 46a553e6af..c228e58952 100644
--- a/src/core/eligibility/labels.ts
+++ b/src/core/eligibility/labels.ts
@@ -109,11 +109,12 @@ export function needsDataEnvelope(tier: TrustTier): boolean {
}
const ENVELOPE_TAG = 'external-data';
+const ENVELOPE_TAG_RE = /<(\/?external-data)/gi;
/** One item of text context: a compact label line, or for external content a data envelope. */
export function renderTrustedText(text: string, fields: TrustFields, opts: LabelOpts = {}): string {
if (!needsDataEnvelope(fields.trust_tier)) return `${compactTrustLabel(fields, opts)} ${text}`;
- const body = text.replace(new RegExp(`</?${ENVELOPE_TAG}`, 'gi'), m => m.replace('<', '&lt;'));
+ const body = text.replace(ENVELOPE_TAG_RE, '&lt;$1');
return `<${ENVELOPE_TAG} trust="${fields.trust_tier}" origin="${fields.origin}">\n${body}\n</${ENVELOPE_TAG}>`;
}
@@ -134,6 +135,6 @@ export function withTrustLabel<T extends object>(row: T): T & TrustFields {
/** One-line variant for list items (pointers, volunteered pages, fact lines): label, or an inline data envelope below `unknown`. */
export function renderTrustedInline(text: string, fields: TrustFields, opts: LabelOpts = {}): string {
if (!needsDataEnvelope(fields.trust_tier)) return text ? `${compactTrustLabel(fields, opts)} ${text}` : compactTrustLabel(fields, opts);
- const body = text.replace(new RegExp(`</?${ENVELOPE_TAG}`, 'gi'), m => m.replace('<', '&lt;'));
+ const body = text.replace(ENVELOPE_TAG_RE, '&lt;$1');
return `<${ENVELOPE_TAG} trust="${fields.trust_tier}" origin="${fields.origin}">${body}</${ENVELOPE_TAG}>`;
}
diff --git a/src/core/think/sanitize.ts b/src/core/think/sanitize.ts
index 333dece220..c7f8bacda9 100644
--- a/src/core/think/sanitize.ts
+++ b/src/core/think/sanitize.ts
@@ -72,6 +72,7 @@ const DETECTION_FAMILY: Readonly<Record<string, 'override' | 'exfiltration'>> =
};
export const INJECTION_DETECTION_PATTERNS: ReadonlyArray<{ name: string; family: 'override' | 'exfiltration'; rx: RegExp }> = INJECTION_PATTERNS
.filter(p => DETECTION_FAMILY[p.name])
+ // nosemgrep: javascript.lang.security.audit.detect-non-literal-regexp.detect-non-literal-regexp -- non-global copies of this module's own literal INJECTION_PATTERNS
.map(p => ({ name: p.name, family: DETECTION_FAMILY[p.name], rx: new RegExp(p.rx.source, p.rx.flags.replace('g', '')) }));
/**
diff --git a/src/core/write-gate-patterns.ts b/src/core/write-gate-patterns.ts
index 7dee9e1a67..f4c5304387 100644
--- a/src/core/write-gate-patterns.ts
+++ b/src/core/write-gate-patterns.ts
@@ -147,6 +147,7 @@ export function spaced(source: string): string {
function own(name: string, family: WriteGateReasonFamily, source: string, anchors: readonly string[],
extra: Pick<WriteGatePattern, 'requires' | 'negatable'> & { preceded?: string } = {}): WriteGatePattern {
const { preceded, ...rest } = extra;
+ // nosemgrep: javascript.lang.security.audit.detect-non-literal-regexp.detect-non-literal-regexp -- sources are this module's literal table, vetted by scripts/check-write-gate-regex.ts (bounded, no nesting, no backrefs)
return { name, family, rx: new RegExp(spaced(source), 'i'), anchors, ...rest, ...(preceded ? { preceded: new RegExp(`${spaced(preceded)}$`, 'i') } : {}) };
}
@@ -228,10 +229,12 @@ const OWN_PATTERNS: readonly WriteGatePattern[] = [
/** The full detector table: bounded clones first, then the detector's own patterns. */
export const WRITE_GATE_PATTERNS: readonly WriteGatePattern[] = [
...INJECTION_DETECTION_PATTERNS.map(p => ({
+ // nosemgrep: javascript.lang.security.audit.detect-non-literal-regexp.detect-non-literal-regexp -- bounded clone of a literal pattern, vetted by scripts/check-write-gate-regex.ts
name: p.name, family: p.family, rx: new RegExp(boundQuantifiers(p.rx.source), p.rx.flags), anchors: CLONE_ANCHORS[p.name] ?? [],
...(CLONE_REQUIRES[p.name] ? { requires: CLONE_REQUIRES[p.name] } : {}),
})),
...IMPERATIVE_RES.map((rx, i) => ({
+ // nosemgrep: javascript.lang.security.audit.detect-non-literal-regexp.detect-non-literal-regexp -- bounded clone of a literal pattern, vetted by scripts/check-write-gate-regex.ts
name: IMPERATIVE_META[i]!.name, family: 'override' as const, rx: new RegExp(boundQuantifiers(rx.source), rx.flags.replace('g', '')), anchors: IMPERATIVE_META[i]!.anchors,
...(IMPERATIVE_META[i]!.requires ? { requires: IMPERATIVE_META[i]!.requires } : {}),
})),
diff --git a/src/core/write-gate.ts b/src/core/write-gate.ts
index 82d54591ff..53d0732e73 100644
--- a/src/core/write-gate.ts
+++ b/src/core/write-gate.ts
@@ -194,6 +194,7 @@ export const WRITE_GATE_WINDOW_CHARS = 4096;
const NEGATED_BEFORE_RE = /\b(?:never|not|n't|no)\s{1,4}$/i;
/** Global clones, for searching from an offset and walking every match (the table's own regexes stay stateless). */
+// nosemgrep: javascript.lang.security.audit.detect-non-literal-regexp.detect-non-literal-regexp -- global copies of WRITE_GATE_PATTERNS, vetted by scripts/check-write-gate-regex.ts
const ITERATORS: ReadonlyMap<string, RegExp> = new Map(WRITE_GATE_PATTERNS.map(p => [p.name, new RegExp(p.rx.source, `${p.rx.flags}g`)]));
/**