Skip to content

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

TypeScript

fix(mcp-host): check chat artifacts for secrets in the text they print

marcoteb82c5ff979a7d
diff --git a/mcp-host/src/core/tools/__tests__/generatedArtifactAttachments.test.ts b/mcp-host/src/core/tools/__tests__/generatedArtifactAttachments.test.ts
index 05a5737ae..4b3154bb8 100644
--- a/mcp-host/src/core/tools/__tests__/generatedArtifactAttachments.test.ts
+++ b/mcp-host/src/core/tools/__tests__/generatedArtifactAttachments.test.ts
@@ -249,6 +249,43 @@ describe('generated internal artifact attachments', () => {
expect(isInternalGeneratedArtifactAttachment({ ...base, filename: 'report.html' })).toBe(false)
})
+ it.each([
+ ['an ANSI escape', 'probe-redaction\u001b[0m-value'],
+ ['an HTML tag', 'probe-redaction<b></b>-value'],
+ ['a character reference', 'probe&#45;redaction-value'],
+ ['a zero-width space', 'probe-redaction\u200B-value'],
+ ['markdown emphasis', 'probe-**redaction**-value'],
+ ])('does not attach a PDF that prints a secret split by %s', async (_label, body) => {
+ process.env.CLERUM_OUTPUT_DIR = outputDir
+ const registry = new NativeToolRegistry(
+ {
+ workspacePath: outputDir,
+ shellTimeout: 5000,
+ toolTimeout: 60000,
+ toolProgressInterval: 30000,
+ httpAllowlist: [],
+ envAllowlist: ['PATH'],
+ memoryMaxSize: 1048576,
+ },
+ 'conv-1',
+ undefined,
+ undefined,
+ undefined,
+ undefined,
+ null,
+ {
+ maxBytes: 52_428_800,
+ secretEntriesProvider: () => [{ name: 'PROBE_VALUE', value: 'probe-redaction-value' }],
+ }
+ )
+ const pdf = registry.get('clerum__generate_pdf')!
+ const leaked = await pdf.execute({ filename: 'split.pdf', body })
+ expect(leaked.is_error).toBe(false)
+ expect(leaked.attachments ?? []).toHaveLength(0)
+ const safe = await pdf.execute({ filename: 'safe.pdf', body: 'nothing secret here' })
+ expect(safe.attachments ?? []).toHaveLength(1)
+ })
+
it('native internal tools return generated artifact attachments through the adapter', async () => {
const config: NativeToolConfig = {
workspacePath: outputDir,
diff --git a/mcp-host/src/core/tools/nativeToolRegistry.ts b/mcp-host/src/core/tools/nativeToolRegistry.ts
index 5312238d3..aa1cbd8e5 100644
--- a/mcp-host/src/core/tools/nativeToolRegistry.ts
+++ b/mcp-host/src/core/tools/nativeToolRegistry.ts
@@ -11,6 +11,7 @@ import type { LlmProvider } from '../../llm/registryCore'
import type { McpManager } from '../../mcp/manager'
import type { IncomingMessage } from '../../server'
import { getOutputDir, resolveInternalTools } from '../../workflow/internalTools'
+import { printedForms } from '../../workflow/toolText'
import type { InternalToolDefinition } from '../../workflow/types'
import { ScopedWorkspace } from '../../workspace/scopedWorkspace'
import type { Workspace } from '../../workspace/service'
@@ -95,7 +96,9 @@ class InternalToolAdapter implements Tool {
artifact: result.artifact,
outputDir: this.outputDir,
maxBytes: this.attachmentOptions.maxBytes,
- sourcePayload: params,
+ // The file shows the arguments as the generator prints them, so a
+ // secret split by a tag or an escape is checked whole too.
+ sourcePayload: [params, printedForms(params)],
secretEntriesProvider: this.attachmentOptions.secretEntriesProvider,
})
: null
diff --git a/mcp-host/src/workflow/toolText.ts b/mcp-host/src/workflow/toolText.ts
index 0e39dca65..6457eebbe 100644
--- a/mcp-host/src/workflow/toolText.ts
+++ b/mcp-host/src/workflow/toolText.ts
@@ -6,6 +6,7 @@
* boxes, and the markdown parsers split on '\n' only. Cleaning the arguments
* once, before a generator reads them, covers every string that can reach a file.
*/
+import { htmlToPlainText } from './inlineMarkup'
/**
* CSI and terminated OSC sequences, then any other escape with its
@@ -63,3 +64,33 @@ export function cleanToolArgs<T>(value: T, depth = 0): T {
}
return out as T
}
+
+/** Characters the document renderers draw as nothing. */
+const INVISIBLE = /[­​-‏⁠-⁤]/g
+
+/** A backslash before ASCII punctuation, which markdown prints as that character. */
+const MARKDOWN_ESCAPE = /\\([!-/:-@[-`{-~])/g
+
+/**
+ * The text of every string in `args` in the forms a document generator may
+ * print it: as sent, cleaned as cleanToolArgs cleans it, with HTML tags,
+ * character references, backslash escapes and invisible characters taken out,
+ * and with markdown markers taken out too. Text split by any of these prints
+ * whole, so a check for leaked secrets that reads every form sees it whole.
+ */
+export function printedForms(args: unknown): string[] {
+ const sent: string[] = []
+ const collect = (value: unknown, depth: number): void => {
+ if (depth > MAX_DEPTH) return
+ if (typeof value === 'string') sent.push(value)
+ else if (Array.isArray(value)) value.forEach(item => collect(item, depth + 1))
+ else if (isPlainObject(value)) Object.values(value).forEach(item => collect(item, depth + 1))
+ }
+ collect(args, 0)
+ const cleaned = sent.map(cleanText)
+ const printed = cleaned.map(text =>
+ htmlToPlainText(text).replace(MARKDOWN_ESCAPE, '$1').replace(INVISIBLE, '')
+ )
+ const bare = printed.map(text => text.replace(/[*_~`]/g, ''))
+ return [...sent, ...cleaned, ...printed, ...bare]
+}