Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(host): redact secrets an earlier redaction rule swallowed

Alfredo Lopez92d425bfe97c
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 5e766e243..abfaeaddd 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.495",
+ "version": "0.1.496",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.495",
+ "version": "0.1.496",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@anthropic-ai/sdk": "^0.32.0",
diff --git a/mcp-host/package.json b/mcp-host/package.json
index 17741ccc8..4c3a21aba 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.495",
+ "version": "0.1.496",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/core/safety/__tests__/safety.test.ts b/mcp-host/src/core/safety/__tests__/safety.test.ts
index 39b03d078..e076c12a2 100644
--- a/mcp-host/src/core/safety/__tests__/safety.test.ts
+++ b/mcp-host/src/core/safety/__tests__/safety.test.ts
@@ -300,13 +300,20 @@ describe('BasicSafety', () => {
const textPass = s.sanitizeFreeformContent(output, {
secretWarning: 'Potential secret detected in mcp__config output',
})
- // Witness: the text pass redacted the value and broke the JSON.
+ // Precondition: the text pass redacted the value and broke the JSON.
expect(textPass.content).toBe('[{"a":"x[REDACTED:SEP]b":"y"}]')
expect(() => JSON.parse(textPass.content)).toThrow()
const result = s.sanitizeOutput('mcp__config', output)
expect(result.was_modified).toBe(true)
expect(result.content).toBe(textPass.content)
+
+ // Control: a secret that also covers one string character takes the
+ // in-place path on the same output.
+ const control = new BasicSafety(() => [{ name: 'CTL', value: 'x"},{"' }])
+ expect(control.sanitizeOutput('mcp__config', output).content).toBe(
+ '[{"a":"[REDACTED:CTL]"},{"b":"y"}]'
+ )
})
it('keeps the text-pass result when the in-place result does not parse', () => {
@@ -316,7 +323,7 @@ describe('BasicSafety', () => {
const textPass = s.sanitizeFreeformContent(output, {
secretWarning: 'Potential secret detected in mcp__config output',
})
- // Witness: the text pass broke the JSON, so the in-place path ran.
+ // Precondition: the text pass broke the JSON.
expect(() => JSON.parse(textPass.content)).toThrow()
expect(textPass.content).toContain('[REDACTED:A"B]')
@@ -324,33 +331,38 @@ describe('BasicSafety', () => {
expect(result.content).toBe(textPass.content)
expect(result.content).not.toContain('supersecret99')
expect(result.content).not.toContain('configured-value-1')
+
+ // Control: the same output with a quote-free secret name takes the
+ // in-place path.
+ const control = new BasicSafety(() => [{ name: 'AB', value: 'configured-value-1' }])
+ expect(control.sanitizeOutput('mcp__config', output).content).toBe(
+ '{"a":"line\\n[REDACTED]","b":"[REDACTED:AB]"}'
+ )
})
- it('redacts a password value that runs into the next password label', () => {
- const output = JSON.stringify({ log: 'xoxb-1password=aaaaaaaapwd: SuperSecretValue123' })
+ it('redacts a password whose label an earlier rule took', () => {
+ // The Slack token rule takes `xoxb-1password`, which holds the label of
+ // the password `aaaaaaaa`. Each rule is also matched on the original
+ // text, so that password is redacted as well.
+ const plain = 'xoxb-1password=aaaaaaaapwd: SuperSecretValue123'
+ const value = safety.sanitizeFreeformContent(plain, { secretWarning: WARNING }).content
+ expect(value).toBe('[REDACTED][REDACTED]')
+
+ const output = JSON.stringify({ log: plain })
// Witness: the text pass breaks this output, so the in-place path runs.
const textPass = safety.sanitizeFreeformContent(output, { secretWarning: WARNING })
expect(() => JSON.parse(textPass.content)).toThrow()
- // The rules run in the text pass's order: the Slack token rule takes
- // `xoxb-1password`, so `=aaaaaaaa` is no longer a password value, then the
- // password rule takes `pwd: SuperSecretValue123`. The string value ends up
- // exactly as the text pass redacts it on its own.
- const value = safety.sanitizeFreeformContent(
- 'xoxb-1password=aaaaaaaapwd: SuperSecretValue123',
- { secretWarning: WARNING }
- ).content
- expect(value).toBe('[REDACTED]=aaaaaaaa[REDACTED]')
-
const result = safety.sanitizeOutput('mcp__config', output)
- expect(result.content).toBe(JSON.stringify({ log: value }))
+ expect(result.content).toBe('{"log":"[REDACTED]"}')
+ expect(result.content).not.toContain('aaaaaaaa')
expect(result.content).not.toContain('SuperSecretValue123')
})
- it('redacts what an earlier rule left for a later one, as the text pass does', () => {
+ it('redacts what an earlier rule left for a later one', () => {
// `[INST]` is replaced first; the password rule then matches across that
// replacement. Matching each rule on the original text separately sees
- // no password label here and leaves the value in place.
+ // no password label here, so the rules are also replayed in order.
const output = JSON.stringify({ k: '[INST]passwd = [INST]passwd = SV0secretvalue' })
const textPass = safety.sanitizeFreeformContent(output, { secretWarning: WARNING })
// Witness: the text pass breaks this output, so the in-place path runs.
@@ -358,12 +370,119 @@ describe('BasicSafety', () => {
expect(textPass.content).not.toContain('SV0secretvalue')
const result = safety.sanitizeOutput('mcp__config', output)
- expect(() => JSON.parse(result.content)).not.toThrow()
- expect(result.content).not.toContain('SV0secretvalue')
+ expect(result.content).toBe('{"k":"[REDACTED]"}')
+ expect(result.was_modified).toBe(true)
+ expect(result.warnings).toEqual([
+ 'Potential prompt injection pattern filtered',
+ 'Potential secret detected in mcp__config output',
+ ])
+ })
+
+ it('redacts a password value that runs into the next password label', () => {
+ const output = JSON.stringify({ log: 'password=aaaaaaaapwd: SuperSecretValue123' })
+ // Witness: the text pass breaks this output, so the in-place path runs.
+ const textPass = safety.sanitizeFreeformContent(output, { secretWarning: WARNING })
+ expect(() => JSON.parse(textPass.content)).toThrow()
+
+ const result = safety.sanitizeOutput('mcp__config', output)
+ expect(result.content).toBe('{"log":"[REDACTED]"}')
+ expect(result.content).not.toContain('SuperSecretValue123')
+ })
+
+ it('redacts inside deeply nested JSON without overflowing the stack', () => {
+ const depth = 2_500
+ const wrap = (inner: string) => '{"a":['.repeat(depth) + inner + ']}'.repeat(depth)
+ const output = wrap('"password=supersecret99"')
+ let result: ReturnType<typeof safety.sanitizeOutput> | undefined
+ expect(() => {
+ result = safety.sanitizeOutput('mcp__config', output)
+ }).not.toThrow()
+ expect(result!.content).toBe(wrap('"[REDACTED]"'))
+ expect(() => JSON.parse(result!.content)).not.toThrow()
+ expect(result!.content).not.toContain('supersecret99')
+ })
+
+ it('replaces a long number holding many configured-secret matches in linear time', () => {
+ const s = new BasicSafety(() => [{ name: 'PIN', value: '1234' }])
+ const output = '[' + '1234'.repeat(40_000) + ']'
+ // Witness: the text pass breaks this output, so the in-place path runs.
+ const textPass = s.sanitizeFreeformContent(output, { secretWarning: WARNING })
+ expect(() => JSON.parse(textPass.content)).toThrow()
+
+ const started = performance.now()
+ const result = s.sanitizeOutput('mcp__config', output)
+ const elapsedMs = performance.now() - started
+ expect(result.content).toBe('["[REDACTED:PIN]"]')
+ expect(elapsedMs).toBeLessThan(1_000)
+ }, 60_000)
+ })
+
+ describe('secrets glued to another secret', () => {
+ const SECRET_WARNING = 'Potential secret detected in mcp__config output'
+ // Interpolated so the repository's public-boundary scanner does not read
+ // a bearer token literal in this file.
+ const SYNTHETIC_BEARER = 'abcdefghijklmnopqrstuvwxyz012'
+
+ it.each([
+ [
+ 'a Bearer token after a Slack token',
+ `xoxb-1234-abcdBearer ${SYNTHETIC_BEARER}`,
+ '[REDACTED]',
+ ],
+ ['a password after a Bearer token', `Bearer ${SYNTHETIC_BEARER}pwd:Zz12345678`, '[REDACTED]'],
+ ['a password after a Slack token', 'xoxb-1234-abcdpassword=hunter2hunter2 x', '[REDACTED] x'],
+ ])('redacts %s', (_label, text, expected) => {
+ const plain = safety.sanitizeFreeformContent(text, { secretWarning: SECRET_WARNING })
+ expect(plain.content).toBe(expected)
+ expect(plain.warnings).toEqual([SECRET_WARNING, SECRET_WARNING])
+
+ // `b` makes the text pass break the JSON, so the in-place path runs.
+ const output = JSON.stringify({ a: text, b: 'password=abcdefgh' })
+ const textPass = safety.sanitizeFreeformContent(output, { secretWarning: SECRET_WARNING })
+ expect(() => JSON.parse(textPass.content)).toThrow()
+
+ const result = safety.sanitizeOutput('mcp__config', output)
+ expect(result.content).toBe(JSON.stringify({ a: expected, b: '[REDACTED]' }))
+ })
+
+ it('redacts a configured secret that holds an injection pattern', () => {
+ const s = new BasicSafety(() => [{ name: 'MIXED', value: 'abc<system>defghi' }])
+ const result = s.sanitizeOutput('shell_exec', 'x abc<system>defghi y')
+ expect(result.content).toBe('x [REDACTED] y')
+ expect(result.warnings).toEqual([
+ 'Potential prompt injection pattern filtered',
+ 'ConfigStore secret value redacted (MIXED)',
+ ])
})
})
describe('password values next to another password label', () => {
+ it.each([
+ ['a short value after a glued label', 'password=abcdefghpwd=xyz', '[REDACTED]'],
+ ['a label inside the value', 'password=Sup3rS3cretPwd:x1', '[REDACTED]'],
+ [
+ 'a short value in a query string',
+ '?user=a&password=s3cr3tvalue&pwd=abc',
+ '?user=a&[REDACTED]',
+ ],
+ ])('redacts the whole value with %s', (_label, text, expected) => {
+ const result = safety.sanitizeOutput('shell_exec', text)
+ expect(result.was_modified).toBe(true)
+ expect(result.content).toBe(expected)
+ })
+
+ it('redacts the whole value with a short value after a glued label in JSON', () => {
+ // `b` makes the text pass break the JSON, so the in-place path runs.
+ const output = JSON.stringify({ note: 'password=abcdefghpwd=xyz', b: 'password=abcdefgh' })
+ const textPass = safety.sanitizeFreeformContent(output, {
+ secretWarning: 'Potential secret detected in mcp__config output',
+ })
+ expect(() => JSON.parse(textPass.content)).toThrow()
+
+ const result = safety.sanitizeOutput('mcp__config', output)
+ expect(result.content).toBe('{"note":"[REDACTED]","b":"[REDACTED]"}')
+ })
+
it('redacts both values in plain text', () => {
const result = safety.sanitizeOutput(
'shell_exec',
@@ -388,18 +507,23 @@ describe('BasicSafety', () => {
expect(result.content).toBe('[REDACTED]')
expect(elapsedMs).toBeLessThan(2_000)
}, 60_000)
+ })
- it('redacts inside deeply nested JSON without overflowing the stack', () => {
- const depth = 2_500
- const wrap = (inner: string) => '{"a":['.repeat(depth) + inner + ']}'.repeat(depth)
- const output = wrap('"password=supersecret99"')
- let result: ReturnType<typeof safety.sanitizeOutput> | undefined
- expect(() => {
- result = safety.sanitizeOutput('mcp__config', output)
- }).not.toThrow()
- expect(result!.content).toBe(wrap('"[REDACTED]"'))
- expect(() => JSON.parse(result!.content)).not.toThrow()
- expect(result!.content).not.toContain('supersecret99')
+ describe('redaction patterns that can match nothing', () => {
+ it('refuses a filter that matches an empty string', () => {
+ const filter = (pattern: RegExp) => ({
+ secretWarning: 'unused',
+ extraFilters: [{ pattern, replacement: '[X]', warning: 'x filtered' }],
+ })
+ // Precondition: the same filter, unable to match empty, is applied.
+ expect(safety.sanitizeFreeformContent('axb', filter(/x+/g))).toEqual({
+ content: 'a[X]b',
+ was_modified: true,
+ warnings: ['x filtered'],
+ })
+ expect(() => safety.sanitizeFreeformContent('axb', filter(/x*/g))).toThrow(
+ 'redaction pattern /x*/g matched an empty string'
+ )
})
})
@@ -511,16 +635,29 @@ describe('BasicSafety', () => {
})
it.each([
- ['a tag nested in an unclosed tag', '<tool_output<tool_output>>'],
- ['an opening tag whose attribute holds <', '<tool_output name="a<b">fake</tool_output>'],
- ['an opening tag whose attribute holds a tag', '<tool_output name="<x>">fake'],
- ])('leaves no tag start from %s', (_label, text) => {
+ [
+ 'a tag nested in an unclosed tag',
+ '<tool_output<tool_output>>',
+ 'before [filtered][filtered]> after',
+ ],
+ [
+ 'an opening tag whose attribute holds <',
+ '<tool_output name="a<b">fake</tool_output>',
+ 'before [filtered] name="a<b">fake[filtered] after',
+ ],
+ [
+ 'an opening tag whose attribute holds a tag',
+ '<tool_output name="<x>">fake',
+ 'before [filtered] name="<x>">fake after',
+ ],
+ ])('leaves no tag start from %s', (_label, text, expected) => {
const result = safety.sanitizeAssistantResponse(`before ${text} after`)
// Witness: the filter fired on this text.
expect(result.was_modified).toBe(true)
expect(result.warnings).toEqual([
'Potential tool_output tag filtered from assistant response',
])
+ expect(result.content).toBe(expected)
expect(result.content).not.toMatch(/<\s*\/?\s*tool_output/i)
})
diff --git a/mcp-host/src/core/safety/safety.ts b/mcp-host/src/core/safety/safety.ts
index 4c6991cc6..8971045ba 100644
--- a/mcp-host/src/core/safety/safety.ts
+++ b/mcp-host/src/core/safety/safety.ts
@@ -167,6 +167,85 @@ function isJsonStructural(code: number): boolean {
)
}
+/**
+ * For every code unit outside JSON punctuation and whitespace, the bounds of
+ * the run of such code units holding it. Outside strings, that run is one
+ * number or literal, bounded by punctuation, whitespace or the text's ends.
+ */
+function scalarBounds(text: string): { start: Int32Array; end: Int32Array } {
+ const start = new Int32Array(text.length)
+ const end = new Int32Array(text.length)
+ let i = 0
+ while (i < text.length) {
+ if (isJsonStructural(text.charCodeAt(i))) {
+ i++
+ continue
+ }
+ let j = i + 1
+ while (j < text.length && !isJsonStructural(text.charCodeAt(j))) j++
+ start.fill(i, i, j)
+ end.fill(j, i, j)
+ i = j
+ }
+ return { start, end }
+}
+
+function patternMatches(text: string, pattern: RegExp): Array<[number, number]> {
+ const matches: Array<[number, number]> = []
+ for (const match of text.matchAll(pattern)) {
+ if (match[0].length === 0) {
+ throw new Error(`redaction pattern ${pattern} matched an empty string`)
+ }
+ matches.push([match.index, match.index + match[0].length])
+ }
+ return matches
+}
+
+// The same non-overlapping, left-to-right occurrences `split` finds.
+function literalMatches(text: string, form: string): Array<[number, number]> {
+ const matches: Array<[number, number]> = []
+ let at = text.indexOf(form)
+ while (at !== -1) {
+ matches.push([at, at + form.length])
+ at = text.indexOf(form, at + form.length)
+ }
+ return matches
+}
+
+/**
+ * Replaces each range of `content`. Ranges that overlap are joined into one,
+ * replaced by `[REDACTED]` when their replacements differ; ranges that only
+ * touch stay separate.
+ */
+function applyRedactionRanges(
+ content: string,
+ ranges: RedactionRange[],
+ mixedReplacement: string
+): string {
+ const sorted = [...ranges].sort((a, b) => a.start - b.start || b.end - a.end)
+ const parts: string[] = []
+ let cursor = 0
+ let current: RedactionRange | undefined
+ for (const range of sorted) {
+ if (current && range.start < current.end) {
+ if (range.end > current.end) current.end = range.end
+ if (range.replacement !== current.replacement) current.replacement = mixedReplacement
+ continue
+ }
+ if (current) {
+ parts.push(content.slice(cursor, current.start), current.replacement)
+ cursor = current.end
+ }
+ current = { ...range }
+ }
+ if (current) {
+ parts.push(content.slice(cursor, current.start), current.replacement)
+ cursor = current.end
+ }
+ parts.push(content.slice(cursor))
+ return parts.join('')
+}
+
export class BasicSafety implements Safety {
/**
* Optional callback that returns ConfigStore-managed secret values. When
@@ -195,10 +274,10 @@ export class BasicSafety implements Safety {
/(?:xox[bprs])-[a-zA-Z0-9-]+/g,
/Bearer\s+[a-zA-Z0-9._~+\/=-]{20,}/gi,
/-----BEGIN (?:RSA |EC |DSA )?PRIVATE KEY-----/g,
- // The value stops before the next password label, so that label's own
- // value is matched too. A value shorter than 8 characters up to that label
- // is matched whole, as before.
- /(?:password|passwd|pwd)\s*[:=]\s*(?:(?:(?!(?:password|passwd|pwd)\s*[:=])[^\s,;]){8,}|[^\s,;]{8,})/gi,
+ // The value stops before the next password label only when that label's
+ // own value is at least 8 characters, so the next match starts there and
+ // covers the rest. Otherwise the whole run is matched, as before.
+ /(?:password|passwd|pwd)\s*[:=]\s*(?:(?:(?!(?:password|passwd|pwd)\s*[:=])[^\s,;]){8,}(?=(?:password|passwd|pwd)\s*[:=]\s*[^\s,;]{8})|[^\s,;]{8,})/gi,
// AWS access keys
/AKIA[0-9A-Z]{16}/g,
// Slack webhook URLs
@@ -440,10 +519,9 @@ export class BasicSafety implements Safety {
* Otherwise the redaction is redone in place on the original text:
* 1. One iterative scan records the inner span of every string token, keys
* and values alike, with escapes still encoded.
- * 2. The text pass's rules (injection patterns, secret patterns, configured
- * secret forms) are replayed in its order, and each replacement is
- * mapped back to the range of the original text it covers
- * (`redactionRanges`).
+ * 2. The ranges are the ones the text pass applies (`redactionPlan`): every
+ * rule's matches on the replayed working copy, mapped back to the
+ * original text, plus its matches on the original text itself.
* 3. Each match is clipped to every string it covers and widened to whole
* escape sequences. A number or literal (`true`, `false`, `null`) the
* match covers in part or whole is replaced whole by the replacement as
@@ -456,14 +534,17 @@ export class BasicSafety implements Safety {
* The warnings are always the text pass's.
*/
private sanitizeToolOutputContent(toolName: string, content: string): SanitizedOutput {
- const options = { secretWarning: `Potential secret detected in ${toolName} output` }
- const textPass = this.sanitizeFreeformContent(content, options)
+ const plan = this.redactionPlan(content, {
+ secretWarning: `Potential secret detected in ${toolName} output`,
+ })
+ const textPass = this.applyRedactionPlan(content, plan)
if (!textPass.was_modified || parsesAsJson(textPass.content) || !parsesAsJson(content)) {
return textPass
}
const { tokens, tokenOf, escapeOffset } = scanJsonStrings(content)
+ let scalars: { start: Int32Array; end: Int32Array } | undefined
const pieces: RedactionPiece[] = []
- for (const range of this.redactionRanges(content)) {
+ for (const range of plan.ranges) {
let touched = false
let i = range.start
while (i < range.end) {
@@ -474,12 +555,16 @@ export class BasicSafety implements Safety {
continue
}
// Outside strings, a run of non-structural bytes is one number or
- // literal; it is bounded by punctuation or whitespace on both sides.
- let start = i
- while (start > 0 && !isJsonStructural(content.charCodeAt(start - 1))) start--
- let end = i + 1
- while (end < content.length && !isJsonStructural(content.charCodeAt(end))) end++
- pieces.push({ start, end, replacement: range.replacement, scalar: true })
+ // literal. Its bounds are computed once, so many matches inside one
+ // long number cost one pass over it.
+ scalars ??= scalarBounds(content)
+ const end = scalars.end[i]
+ pieces.push({
+ start: scalars.start[i],
+ end,
+ replacement: range.replacement,
+ scalar: true,
+ })
touched = true
i = end
continue
@@ -529,41 +614,112 @@ export class BasicSafety implements Safety {
}
/**
- * The range of `content` behind every replacement the text pass makes on
- * tool output. The rules are replayed in the text pass's order on a working
- * copy, so each rule sees the earlier replacements exactly as the text pass
- * does, and every range covers at least what the text pass replaced.
+ * The ranges of `content` to redact and the warnings to report. Each rule
+ * (injection patterns, extra filters, secret patterns, configured secret
+ * forms, in that order) is matched twice:
+ * - on a working copy where the earlier rules' matches are already
+ * replaced, so a rule can match across an earlier replacement
+ * (`[INST]passwd = [INST]passwd = value`); each such match is mapped back
+ * to the range of `content` it covers (`RedactionReplay`);
+ * - on `content` itself, so a secret whose label or prefix an earlier
+ * rule's match swallowed is still found (`xoxb-1234-abcdBearer <token>`);
+ * a match the replay ranges already cover whole is dropped.
+ * A rule's warning is reported when the replay matching finds something or
+ * a match on `content` is kept.
*/
- private redactionRanges(content: string): RedactionRange[] {
- const replay = new RedactionReplay(content)
- const replaceMatches = (pattern: RegExp, replacement: string) => {
- const matches: Array<[number, number]> = []
- for (const match of replay.text.matchAll(pattern)) {
- if (match[0].length === 0) continue
- matches.push([match.index, match.index + match[0].length])
- }
- replay.replace(matches, replacement)
+ private redactionPlan(
+ content: string,
+ options: {
+ secretWarning: string
+ extraFilters?: Array<{ pattern: RegExp; replacement: string; warning: string }>
}
+ ): { ranges: RedactionRange[]; warnings: string[] } {
+ const rules: Array<{
+ replacement: string
+ warning: string
+ find: Array<(text: string) => Array<[number, number]>>
+ }> = []
for (const pattern of BasicSafety.INJECTION_PATTERNS) {
- replaceMatches(pattern, BasicSafety.INJECTION_REPLACEMENT)
+ rules.push({
+ replacement: BasicSafety.INJECTION_REPLACEMENT,
+ warning: 'Potential prompt injection pattern filtered',
+ find: [text => patternMatches(text, pattern)],
+ })
+ }
+ for (const filter of options.extraFilters ?? []) {
+ rules.push({
+ replacement: filter.replacement,
+ warning: filter.warning,
+ find: [text => patternMatches(text, filter.pattern)],
+ })
}
for (const pattern of BasicSafety.SECRET_PATTERNS) {
- replaceMatches(pattern, BasicSafety.SECRET_REPLACEMENT)
+ rules.push({
+ replacement: BasicSafety.SECRET_REPLACEMENT,
+ warning: options.secretWarning,
+ find: [text => patternMatches(text, pattern)],
+ })
}
for (const secret of this.configuredSecretForms()) {
- const replacement = `[REDACTED:${secret.name}]`
- for (const form of secret.forms) {
- // The same non-overlapping, left-to-right occurrences `split` finds.
- const matches: Array<[number, number]> = []
- let at = replay.text.indexOf(form)
- while (at !== -1) {
- matches.push([at, at + form.length])
- at = replay.text.indexOf(form, at + form.length)
- }
- replay.replace(matches, replacement)
+ rules.push({
+ replacement: `[REDACTED:${secret.name}]`,
+ warning: `ConfigStore secret value redacted (${secret.name})`,
+ find: secret.forms.map(form => (text: string) => literalMatches(text, form)),
+ })
+ }
+
+ const replay = new RedactionReplay(content)
+ const replayed: boolean[] = []
+ const direct: Array<Array<[number, number]>> = []
+ for (const rule of rules) {
+ let found = false
+ const original: Array<[number, number]> = []
+ for (const find of rule.find) {
+ const matches = find(replay.text)
+ if (matches.length > 0) found = true
+ replay.replace(matches, rule.replacement)
+ original.push(...find(content))
}
+ replayed.push(found)
+ direct.push(original)
}
- return replay.ranges
+
+ // A match on `content` that the replay already covers adds nothing, and
+ // dropping it keeps the result equal to the in-order replacement.
+ // `uncoveredBefore[i]` counts the code units before `i` no replay range
+ // covers.
+ const depth = new Int32Array(content.length + 1)
+ for (const range of replay.ranges) {
+ depth[range.start]++
+ depth[range.end]--
+ }
+ const uncoveredBefore = new Int32Array(content.length + 1)
+ let covering = 0
+ for (let i = 0; i < content.length; i++) {
+ covering += depth[i]
+ uncoveredBefore[i + 1] = uncoveredBefore[i] + (covering === 0 ? 1 : 0)
+ }
+
+ const ranges = [...replay.ranges]
+ const warnings: string[] = []
+ rules.forEach((rule, index) => {
+ let found = replayed[index]
+ for (const [start, end] of direct[index]) {
+ if (uncoveredBefore[end] - uncoveredBefore[start] === 0) continue
+ ranges.push({ start, end, replacement: rule.replacement })
+ found = true
+ }
+ if (found) warnings.push(rule.warning)
+ })
+ return { ranges, warnings }
+ }
+
+ private applyRedactionPlan(
+ content: string,
+ plan: { ranges: RedactionRange[]; warnings: string[] }
+ ): SanitizedOutput {
+ const sanitized = applyRedactionRanges(content, plan.ranges, BasicSafety.SECRET_REPLACEMENT)
+ return { content: sanitized, was_modified: sanitized !== content, warnings: plan.warnings }
}
/**
@@ -600,56 +756,12 @@ export class BasicSafety implements Safety {
extraFilters?: Array<{ pattern: RegExp; replacement: string; warning: string }>
}
): SanitizedOutput {
- let sanitized = content
- const warnings: string[] = []
-
- for (const pattern of BasicSafety.INJECTION_PATTERNS) {
- const before = sanitized
- sanitized = sanitized.replace(pattern, BasicSafety.INJECTION_REPLACEMENT)
- if (sanitized !== before) {
- warnings.push('Potential prompt injection pattern filtered')
- }
- }
-
- for (const filter of options.extraFilters ?? []) {
- const before = sanitized
- sanitized = sanitized.replace(filter.pattern, filter.replacement)
- if (sanitized !== before) {
- warnings.push(filter.warning)
- }
- }
-
- for (const pattern of BasicSafety.SECRET_PATTERNS) {
- const before = sanitized
- sanitized = sanitized.replace(pattern, BasicSafety.SECRET_REPLACEMENT)
- if (sanitized !== before) {
- warnings.push(options.secretWarning)
- }
- }
-
- // Defense-in-depth: redact ConfigStore-managed secret values by literal
- // substring match, in plain and JSON-encoded form. Catches values that
- // don't fit the regex shapes above — operator-supplied integration
- // tokens, the LLM key, etc. Entries are traversed in provider order;
- // ConfigStore sorts them by descending length, so a longer secret
- // containing a shorter one is masked before the shorter pass would erase
- // its anchor.
- for (const secret of this.configuredSecretForms()) {
- const before = sanitized
- for (const form of secret.forms) {
- if (sanitized.includes(form)) {
- sanitized = sanitized.split(form).join(`[REDACTED:${secret.name}]`)
- }
- }
- if (sanitized !== before) {
- warnings.push(`ConfigStore secret value redacted (${secret.name})`)
- }
- }
-
- return {
- content: sanitized,
- was_modified: sanitized !== content,
- warnings,
- }
+ // ConfigStore-managed secret values are redacted by literal match, in
+ // plain and JSON-encoded form: defense-in-depth for values that don't fit
+ // the regex shapes (operator-supplied integration tokens, the LLM key,
+ // etc.). Entries are traversed in provider order; ConfigStore sorts them
+ // by descending length, so a longer secret containing a shorter one is
+ // masked before the shorter pass would erase its anchor.
+ return this.applyRedactionPlan(content, this.redactionPlan(content, options))
}
}