Skip to content

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

TypeScript

fix(mcp-host): withhold a generated file whose record keys print a secret once cleaned

marcoteb06b115aa23d4
diff --git a/mcp-host/src/core/tools/__tests__/generatedArtifactAttachments.test.ts b/mcp-host/src/core/tools/__tests__/generatedArtifactAttachments.test.ts
index 06e43f2bc..14a0e166e 100644
--- a/mcp-host/src/core/tools/__tests__/generatedArtifactAttachments.test.ts
+++ b/mcp-host/src/core/tools/__tests__/generatedArtifactAttachments.test.ts
@@ -2,6 +2,7 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
+import { zipEntries } from '../../../workflow/__tests__/support/zipEntries'
import type { ArtifactMetadata } from '../../../workflow/types'
import type { NativeToolConfig } from '../../interfaces'
import type { Attachment } from '../../types'
@@ -353,6 +354,70 @@ describe('generated internal artifact attachments', () => {
expect(leaked.attachments ?? []).toHaveLength(0)
})
+ describe('XLSX record keys, which the sheet prints as its header', () => {
+ const secret = 'probe-redaction-value'
+
+ function xlsxTool() {
+ 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: secret }],
+ }
+ )
+ return registry.get('clerum__generate_xlsx')!
+ }
+
+ function sheetXml(file: string): string {
+ return [...zipEntries(file)]
+ .filter(([name]) => name.startsWith('xl/') && name.endsWith('.xml'))
+ .map(([, data]) => data.toString('utf8'))
+ .join('\n')
+ }
+
+ it('attaches a sheet whose keys hold no secret', async () => {
+ const result = await xlsxTool().execute({
+ filename: 'ordinary.xlsx',
+ sheets: [{ name: 'S', rows: [{ Region: 'North', Total: 5 }] }],
+ })
+ expect(result.is_error).toBe(false)
+ expect(result.attachments ?? []).toHaveLength(1)
+ const attached = path.join(outputDir, 'attached.xlsx')
+ fs.writeFileSync(attached, decoded(result.attachments![0]!))
+ expect(sheetXml(attached)).toContain('Region')
+ })
+
+ it.each([
+ ['as sent', secret],
+ ['split by a control character', 'probe-red\u0007action-value'],
+ ['split by an ANSI escape', 'probe-red\u001b[31maction-value'],
+ ['split by half a surrogate pair', 'probe-red\uD800action-value'],
+ ])('does not attach a sheet whose key prints a secret %s', async (_label, key) => {
+ const result = await xlsxTool().execute({
+ filename: 'keyed.xlsx',
+ sheets: [{ name: 'S', rows: [{ [key]: 'ordinary' }] }],
+ })
+ expect(result.is_error).toBe(false)
+ expect(sheetXml(path.join(outputDir, 'keyed.xlsx'))).toContain(secret)
+ expect(result.attachments ?? []).toHaveLength(0)
+ })
+ })
+
it('native internal tools return generated artifact attachments through the adapter', async () => {
const config: NativeToolConfig = {
workspacePath: outputDir,
diff --git a/mcp-host/src/workflow/__tests__/toolText.test.ts b/mcp-host/src/workflow/__tests__/toolText.test.ts
index 839717dab..cf1d009b9 100644
--- a/mcp-host/src/workflow/__tests__/toolText.test.ts
+++ b/mcp-host/src/workflow/__tests__/toolText.test.ts
@@ -3,7 +3,7 @@ import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
import { INTERNAL_TOOLS } from '../internalTools'
-import { cleanText, cleanToolArgs } from '../toolText'
+import { cleanText, cleanToolArgs, printedForms } from '../toolText'
import { XML_FORBIDDEN_CHARS, zipEntries } from './support/zipEntries'
describe('cleanText', () => {
@@ -98,6 +98,55 @@ describe('cleanToolArgs', () => {
})
})
+/** Every string and object key `value` holds. */
+function textsOf(value: unknown, out: string[] = []): string[] {
+ if (typeof value === 'string') out.push(value)
+ else if (Array.isArray(value)) value.forEach(item => textsOf(item, out))
+ else if (value && typeof value === 'object') {
+ for (const [key, item] of Object.entries(value)) {
+ out.push(key)
+ textsOf(item, out)
+ }
+ }
+ return out
+}
+
+describe('printedForms', () => {
+ it('holds every string and key of the cleaned arguments', () => {
+ const args = {
+ body: 'a\u0007b',
+ sheets: [
+ {
+ rows: [
+ { 'probe-red\u0007action-value': 'ordinary', 'Cost\u001b[31m': 3 },
+ { '<b>Region</b>': ['x\u0000y'] },
+ ],
+ },
+ ],
+ columnFormats: { 'Total\uD800': 'currency' },
+ nested: { 'in\u0007ner': { 'deep\u001b[0mest': 'value\u0007' } },
+ }
+ const forms = new Set(printedForms(args))
+ for (const text of textsOf(cleanToolArgs(args))) expect(forms, text).toContain(text)
+ })
+
+ it('reads a key in the same printed forms as a value', () => {
+ const key = 'probe-<b>red</b>[act](https://a.example)ion&#45;value'
+ const asKey = printedForms({ rows: [{ [key]: 1 }] })
+ const asValue = printedForms({ rows: [[key]] })
+ for (const form of asValue) expect(asKey).toContain(form)
+ expect(asKey).toContain('probe-redaction-value')
+ })
+
+ it('reads keys nested absurdly deep without overflowing the stack', () => {
+ let deep: unknown = { 'k\u0007': 'v' }
+ for (let i = 0; i < 10_000; i++) deep = { n: deep }
+ const forms = printedForms({ body: 'a\u0007', extra: deep })
+ expect(forms).toContain('a')
+ expect(forms).toContain('extra')
+ })
+})
+
describe('generated Office files', () => {
const dirty = 'build \u001b[31mFAILED\u001b[0m\f page\v two\u0000'
let outputDir: string
diff --git a/mcp-host/src/workflow/toolText.ts b/mcp-host/src/workflow/toolText.ts
index 4947aafc8..0e19ae9b7 100644
--- a/mcp-host/src/workflow/toolText.ts
+++ b/mcp-host/src/workflow/toolText.ts
@@ -72,12 +72,15 @@ const INVISIBLE = /[­​-‏⁠-⁤]/g
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,
- * with markdown markers taken out too, and read as inline markdown, where a
- * link prints its label. Text split by any of these prints whole, so a check
- * for leaked secrets that reads every form sees it whole.
+ * The text of every string in `args`, object keys included, 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, with markdown markers taken out too, and read as
+ * inline markdown, where a link prints its label. Text split by any of these
+ * prints whole, so a check for leaked secrets that reads every form sees it
+ * whole. Keys count because a generator can print one, as an XLSX sheet prints
+ * the keys of record rows as its header. Like cleanToolArgs, it reads no
+ * deeper than MAX_DEPTH.
*/
export function printedForms(args: unknown): string[] {
const sent: string[] = []
@@ -85,7 +88,12 @@ export function printedForms(args: unknown): string[] {
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))
+ else if (isPlainObject(value)) {
+ for (const [key, item] of Object.entries(value)) {
+ sent.push(key)
+ collect(item, depth + 1)
+ }
+ }
}
collect(args, 0)
const cleaned = sent.map(cleanText)