Skip to content

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

TypeScript

fix(host): redact every password value and survive rules with many matches

Alfredo Lopez986de23fc68f
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index abfaeaddd..72e547824 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.496",
+ "version": "0.1.497",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.496",
+ "version": "0.1.497",
"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 4c3a21aba..986221fc3 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.496",
+ "version": "0.1.497",
"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 e076c12a2..40e4d29b5 100644
--- a/mcp-host/src/core/safety/__tests__/safety.test.ts
+++ b/mcp-host/src/core/safety/__tests__/safety.test.ts
@@ -343,10 +343,11 @@ describe('BasicSafety', () => {
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.
+ // text, so that password is redacted as well; the match that runs to
+ // the next separator joins it to the second value.
const plain = 'xoxb-1password=aaaaaaaapwd: SuperSecretValue123'
const value = safety.sanitizeFreeformContent(plain, { secretWarning: WARNING }).content
- expect(value).toBe('[REDACTED][REDACTED]')
+ expect(value).toBe('[REDACTED]')
const output = JSON.stringify({ log: plain })
// Witness: the text pass breaks this output, so the in-place path runs.
@@ -454,6 +455,69 @@ describe('BasicSafety', () => {
'ConfigStore secret value redacted (MIXED)',
])
})
+
+ it('redacts a configured secret that holds an injection pattern in JSON', () => {
+ const s = new BasicSafety(() => [{ name: 'MIXED', value: 'abc<system>defghi' }])
+ // `b` makes the text pass break the JSON, so the in-place path runs.
+ const output = JSON.stringify({ a: 'x abc<system>defghi y', b: 'password=abcdefgh' })
+ const textPass = s.sanitizeFreeformContent(output, { secretWarning: SECRET_WARNING })
+ expect(() => JSON.parse(textPass.content)).toThrow()
+
+ const result = s.sanitizeOutput('mcp__config', output)
+ expect(result.content).toBe(JSON.stringify({ a: 'x [REDACTED] y', b: '[REDACTED]' }))
+ expect(result.warnings).toEqual([
+ 'Potential prompt injection pattern filtered',
+ SECRET_WARNING,
+ 'ConfigStore secret value redacted (MIXED)',
+ ])
+ })
+
+ it('reports no warning for a rule whose every match an earlier rule already redacted', () => {
+ // The configured value lies inside the Bearer token, which the secret
+ // rule replaces first, so the configured rule redacts nothing of its own.
+ const s = new BasicSafety(() => [{ name: 'INNER', value: 'ghijklmn' }])
+ const result = s.sanitizeOutput('shell_exec', `x Bearer ${SYNTHETIC_BEARER} y`)
+ expect(result.content).toBe('x [REDACTED] y')
+ expect(result.was_modified).toBe(true)
+ expect(result.warnings).toEqual(['Potential secret detected in shell_exec output'])
+ })
+ })
+
+ describe('a configured secret made only of JSON punctuation', () => {
+ it('keeps the text-pass result after a password whose match ends inside it', () => {
+ // Only the match on the original text finds the value, since the
+ // password match swallowed part of it. The value cannot be removed
+ // without breaking the JSON structure, so the redacted text pass is
+ // returned rather than leaving the value.
+ const output = '[{"a":"pwd=abcdefgh"},{"b":1}]'
+ const s = new BasicSafety(() => [{ name: 'S', value: '"},{"' }])
+ const textPass = s.sanitizeFreeformContent(output, {
+ secretWarning: 'Potential secret detected in mcp__config output',
+ })
+ const result = s.sanitizeOutput('mcp__config', output)
+ // Witness: the configured rule matched.
+ expect(result.warnings).toContain('ConfigStore secret value redacted (S)')
+ expect(result.content).toBe(textPass.content)
+ expect(result.content).not.toContain('"},{"')
+ })
+ })
+
+ describe('was_modified', () => {
+ it('is false when every replacement equals the text it replaces', () => {
+ const s = new BasicSafety(() => [{ name: 'TOK', value: '[REDACTED:TOK]' }])
+ const result = s.sanitizeOutput('shell_exec', 'x [REDACTED:TOK] y')
+ expect(result.content).toBe('x [REDACTED:TOK] y')
+ // Witness: the configured rule matched.
+ expect(result.warnings).toEqual(['ConfigStore secret value redacted (TOK)'])
+ expect(result.was_modified).toBe(false)
+ })
+
+ it('is true when a replacement changes the text', () => {
+ const s = new BasicSafety(() => [{ name: 'TOK', value: 'tok-value-1234' }])
+ const result = s.sanitizeOutput('shell_exec', 'x tok-value-1234 y')
+ expect(result.content).toBe('x [REDACTED:TOK] y')
+ expect(result.was_modified).toBe(true)
+ })
})
describe('password values next to another password label', () => {
@@ -499,6 +563,43 @@ describe('BasicSafety', () => {
expect(result.content).not.toContain('abc')
})
+ it.each([
+ ['a label whose short value is the next label', 'pwd=AAAAAAAApwd=pwd= S3cretValue1'],
+ [
+ 'a label with spaces around its separator',
+ 'password=hunter2hunter2password=pwd : S3cretValue1',
+ ],
+ ])('redacts the last value after %s', (_label, text) => {
+ const result = safety.sanitizeOutput('shell_exec', text)
+ expect(result.was_modified).toBe(true)
+ expect(result.content).not.toContain('S3cretValue1')
+
+ const output = JSON.stringify({ log: text })
+ const inJson = safety.sanitizeOutput('mcp__config', output)
+ expect(inJson.was_modified).toBe(true)
+ expect(inJson.content).not.toContain('S3cretValue1')
+ })
+
+ it.each([
+ // `PASSWORD:` has a short value, so the value of `pwd :` that runs to
+ // the next separator swallows `passwd =`, whose value starts with a
+ // label of its own.
+ ['starts with a label', 'PASSWORD:pwd :Zq8Lm2Xvpasswd =pwd=Ab3x tail', 'Ab3x'],
+ ['starts with a label and a colon', 'PASSWORD:pwd :Zq8Lm2Xvpasswd =Pwd:7Kq2', '7Kq2'],
+ ])(
+ 'redacts the value of a label inside an earlier value when it %s',
+ (_label, text, secret) => {
+ const result = safety.sanitizeOutput('shell_exec', text)
+ expect(result.was_modified).toBe(true)
+ expect(result.content).not.toContain(secret)
+
+ const output = JSON.stringify({ log: text })
+ const inJson = safety.sanitizeOutput('mcp__config', output)
+ expect(inJson.was_modified).toBe(true)
+ expect(inJson.content).not.toContain(secret)
+ }
+ )
+
it('matches a long run of labels in linear time', () => {
const content = 'pwd:'.repeat(250_000)
const started = performance.now()
@@ -509,6 +610,18 @@ describe('BasicSafety', () => {
}, 60_000)
})
+ describe('a rule with many matches', () => {
+ it('redacts 200,000 injection markers without exceeding the call stack', () => {
+ const content = '[INST] '.repeat(200_000)
+ let result: ReturnType<BasicSafety['sanitizeOutput']> | undefined
+ expect(() => {
+ result = safety.sanitizeOutput('shell_exec', content)
+ }).not.toThrow()
+ expect(result?.content).toBe('[filtered] '.repeat(200_000))
+ expect(result?.warnings).toEqual(['Potential prompt injection pattern filtered'])
+ }, 60_000)
+ })
+
describe('redaction patterns that can match nothing', () => {
it('refuses a filter that matches an empty string', () => {
const filter = (pattern: RegExp) => ({
diff --git a/mcp-host/src/core/safety/safety.ts b/mcp-host/src/core/safety/safety.ts
index 8971045ba..c9b739fd2 100644
--- a/mcp-host/src/core/safety/safety.ts
+++ b/mcp-host/src/core/safety/safety.ts
@@ -201,6 +201,34 @@ function patternMatches(text: string, pattern: RegExp): Array<[number, number]>
return matches
}
+type MatchFinder = (text: string) => Array<[number, number]>
+
+/**
+ * Each password label with its value, which runs to the next whitespace, `,`
+ * or `;` and must be at least 8 code units long; values that overlap are
+ * joined. Unlike one global regex, a label inside an earlier label's value
+ * still starts a value of its own (`pwd :abcdefghpasswd =pwd=Ab3x`).
+ */
+function passwordValueMatches(text: string): Array<[number, number]> {
+ const matches: Array<[number, number]> = []
+ const separatorPattern = /[\s,;]/g
+ let separator = 0
+ for (const label of text.matchAll(/(?:password|passwd|pwd)\s*[:=]\s*/gi)) {
+ const valueStart = label.index + label[0].length
+ // Value starts only grow, so a separator found for an earlier value is
+ // still the next one when it is not before this value's start.
+ if (separator < valueStart) {
+ separatorPattern.lastIndex = valueStart
+ separator = separatorPattern.exec(text)?.index ?? text.length
+ }
+ if (separator - valueStart < 8) continue
+ const last = matches[matches.length - 1]
+ if (last && label.index < last[1]) last[1] = separator
+ else matches.push([label.index, separator])
+ }
+ 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]> = []
@@ -268,16 +296,23 @@ export class BasicSafety implements Safety {
/<<\/SYS>>/gi,
]
- private static readonly SECRET_PATTERNS: RegExp[] = [
+ // Each entry is one rule; an array entry is one rule matched with each of
+ // its patterns or finders, so its warning is reported once.
+ private static readonly SECRET_PATTERNS: Array<RegExp | Array<RegExp | MatchFinder>> = [
/(?:sk|pk|api)[_-](?:live|test|prod)[_-][a-zA-Z0-9]{16,}/g,
/(?:ghp|gho|ghs|ghr)_[a-zA-Z0-9]{36,}/g,
/(?: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 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,
+ // Password values, matched twice so the coverage is the union of both:
+ // - stopping before the next password label, so a label glued to the
+ // value still starts a match of its own (`password=Sup3rS3cretPwd:x1`);
+ // - running to the next separator from every label, so a value that
+ // holds a label (`password=abc=pwd=defghijk`) is covered whole.
+ [
+ /(?:password|passwd|pwd)\s*[:=]\s*(?:(?!(?:password|passwd|pwd)\s*[:=])[^\s,;]){8,}/gi,
+ passwordValueMatches,
+ ],
// AWS access keys
/AKIA[0-9A-Z]{16}/g,
// Slack webhook URLs
@@ -637,7 +672,7 @@ export class BasicSafety implements Safety {
const rules: Array<{
replacement: string
warning: string
- find: Array<(text: string) => Array<[number, number]>>
+ find: MatchFinder[]
}> = []
for (const pattern of BasicSafety.INJECTION_PATTERNS) {
rules.push({
@@ -653,11 +688,14 @@ export class BasicSafety implements Safety {
find: [text => patternMatches(text, filter.pattern)],
})
}
- for (const pattern of BasicSafety.SECRET_PATTERNS) {
+ for (const entry of BasicSafety.SECRET_PATTERNS) {
+ const patterns = Array.isArray(entry) ? entry : [entry]
rules.push({
replacement: BasicSafety.SECRET_REPLACEMENT,
warning: options.secretWarning,
- find: [text => patternMatches(text, pattern)],
+ find: patterns.map(pattern =>
+ typeof pattern === 'function' ? pattern : (text: string) => patternMatches(text, pattern)
+ ),
})
}
for (const secret of this.configuredSecretForms()) {
@@ -678,7 +716,9 @@ export class BasicSafety implements Safety {
const matches = find(replay.text)
if (matches.length > 0) found = true
replay.replace(matches, rule.replacement)
- original.push(...find(content))
+ // A loop, not `push(...)`: spreading one argument per match overflows
+ // the call stack at about 120k matches.
+ for (const match of find(content)) original.push(match)
}
replayed.push(found)
direct.push(original)