Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): read an Office package's delivered text before attaching it
marcoteb6d37ff927004
diff --git a/mcp-host/src/__tests__/artifacts/officePackageText.test.ts b/mcp-host/src/__tests__/artifacts/officePackageText.test.tsnew file mode 100644index 000000000..4e25822a2--- /dev/null+++ b/mcp-host/src/__tests__/artifacts/officePackageText.test.ts@@ -0,0 +1,111 @@+/**+ * Reading the text of an Office package back from its bytes, within bounds:+ * anything the reader cannot read is reported as such, never as clean.+ */+import { describe, expect, it } from 'vitest'+import JSZip from 'jszip'+import { OFFICE_PACKAGE_LIMITS, officePackageHasText } from '../../artifacts/officePackageText'++const needle = '3141592653'+const holds = (text: string) => text.includes(needle)++function pack(+ parts: Record<string, string | Buffer>,+ compression: 'DEFLATE' | 'STORE' = 'DEFLATE'+): Promise<Buffer> {+ const zip = new JSZip()+ for (const [name, data] of Object.entries(parts)) zip.file(name, data)+ return zip.generateAsync({ type: 'nodebuffer', compression })+}++/** The package with a 16-bit field of every central directory entry set to `value`. */+function withCentralField(buffer: Buffer, offset: number, value: number): Buffer {+ const out = Buffer.from(buffer)+ for (let at = 0; at + 46 <= out.length; at++) {+ if (out.readUInt32LE(at) === 0x02014b50) out.writeUInt16LE(value, at + offset)+ }+ return out+}++describe('officePackageHasText', () => {+ it('finds a number a sheet stores', async () => {+ const xlsx = await pack({+ 'xl/worksheets/sheet1.xml': `<sheetData><c><v>${needle}</v></c></sheetData>`,+ })+ expect(officePackageHasText(xlsx, holds)).toBe(true)+ })++ it('reads text split across runs whole', async () => {+ const xlsx = await pack({+ 'xl/sharedStrings.xml': '<sst><si><r><t>3141</t></r><r><t>592653</t></r></si></sst>',+ })+ expect(officePackageHasText(xlsx, holds)).toBe(true)+ })++ it('reads attribute values', async () => {+ const xlsx = await pack({+ 'xl/workbook.xml': `<workbook><sheet name="${needle}" sheetId="1"/></workbook>`,+ })+ expect(officePackageHasText(xlsx, holds)).toBe(true)+ })++ it('decodes character references', async () => {+ const docx = await pack({ 'word/document.xml': '<w:t>3141592653 & more</w:t>' })+ expect(officePackageHasText(docx, holds)).toBe(true)+ expect(officePackageHasText(docx, text => text.includes('3 & more'))).toBe(true)+ })++ it('reads a package embedded in a package, such as the workbook behind a chart', async () => {+ const workbook = await pack({ 'xl/worksheets/sheet1.xml': `<c><v>${needle}</v></c>` })+ const deck = await pack({+ 'ppt/slides/slide1.xml': '<p:sld>Slide</p:sld>',+ 'ppt/embeddings/Microsoft_Excel_Worksheet1.xlsx': workbook,+ })+ expect(officePackageHasText(deck, holds)).toBe(true)+ })++ it('reads stored entries', async () => {+ const xlsx = await pack({ 'xl/worksheets/sheet1.xml': `<v>${needle}</v>` }, 'STORE')+ expect(officePackageHasText(xlsx, holds)).toBe(true)+ })++ it('finds nothing in a package that does not hold the text, media included', async () => {+ const docx = await pack({+ '[Content_Types].xml': '<Types/>',+ 'word/document.xml': '<w:t>3,141,592,653</w:t>',+ 'word/media/image1.png': Buffer.from(needle),+ })+ expect(officePackageHasText(docx, holds)).toBe(false)+ })++ it.each([+ ['bytes that are not a zip', async () => Buffer.from('PK not a package')],+ ['an encrypted entry', async () => withCentralField(await pack({ 'a.xml': '<a/>' }), 8, 1)],+ [+ 'a compression method it does not read',+ async () => withCentralField(await pack({ 'a.xml': '<a/>' }), 10, 12),+ ],+ ['a truncated archive', async () => (await pack({ 'a.xml': '<a/>' })).subarray(8)],+ ])('reports %s as unreadable', async (_label, make) => {+ expect(officePackageHasText(await make(), holds)).toBeNull()+ })++ it('reports a package past its bounds as unreadable', async () => {+ const parts = { 'a.xml': `<a>${'x'.repeat(2048)}</a>`, 'b.xml': '<b/>' }+ const nested = await pack({ 'inner.xlsx': await pack({ 'c.xml': '<c/>' }) })+ expect(officePackageHasText(await pack(parts), holds)).toBe(false)+ expect(+ officePackageHasText(await pack(parts), holds, {+ ...OFFICE_PACKAGE_LIMITS,+ maxInflatedBytes: 1024,+ })+ ).toBeNull()+ expect(+ officePackageHasText(await pack(parts), holds, { ...OFFICE_PACKAGE_LIMITS, maxEntries: 1 })+ ).toBeNull()+ expect(officePackageHasText(nested, holds)).toBe(false)+ expect(+ officePackageHasText(nested, holds, { ...OFFICE_PACKAGE_LIMITS, maxDepth: 0 })+ ).toBeNull()+ })+})diff --git a/mcp-host/src/artifacts/officePackageText.ts b/mcp-host/src/artifacts/officePackageText.tsnew file mode 100644index 000000000..f0a41d6c5--- /dev/null+++ b/mcp-host/src/artifacts/officePackageText.ts@@ -0,0 +1,157 @@+/**+ * The text an Office package (DOCX, XLSX, PPTX) holds, read from the bytes+ * that are delivered rather than predicted from the arguments that produced+ * them: a generator may write a value in a form its arguments never spelled,+ * as a sheet stores the text "3,141,592,653" as the number 3141592653.+ */+import * as zlib from 'zlib'++/** Bounds on one inspection; a package past them is reported as unreadable. */+export const OFFICE_PACKAGE_LIMITS = {+ /** Entries in one package; an embedded package counts its own. */+ maxEntries: 4096,+ /** Bytes inflated across the package and every package inside it. */+ maxInflatedBytes: 64 * 1024 * 1024,+ /** Packages inside packages, such as the workbook behind a deck's chart. */+ maxDepth: 2,+}++export type OfficePackageLimits = typeof OFFICE_PACKAGE_LIMITS++const END_OF_CENTRAL_DIRECTORY = 0x06054b50+const CENTRAL_DIRECTORY_ENTRY = 0x02014b50+const LOCAL_FILE_HEADER = 0x04034b50+const TEXT_PART = /\.(?:xml|rels|vml)$/i+const EMBEDDED_PACKAGE = /\.(?:docx|docm|xlsx|xlsm|pptx|pptm)$/i+const TAG = /<[^>]*>/g+const ATTRIBUTE_VALUE = /\s[^\s=<>"'/]+\s*=\s*(?:"([^"]*)"|'([^']*)')/g+const CHARACTER_REFERENCE = /&(?:#(\d{1,7})|#[xX]([0-9a-fA-F]{1,6})|(lt|gt|amp|quot|apos));/g+const NAMED_REFERENCES: Record<string, string> = {+ lt: '<',+ gt: '>',+ amp: '&',+ quot: '"',+ apos: "'",+}++interface ZipEntry {+ name: string+ flags: number+ method: number+ compressedSize: number+ localHeaderOffset: number+}++/**+ * Whether any text in the package satisfies `matches`: per XML part, its text+ * nodes run together, so a value split across runs or cells reads whole, and+ * each attribute value on its own, with character references decoded.+ * Embedded packages are read too. Null when the bytes are not a package this+ * reader can read within `limits`; the caller then treats the file as unsafe.+ */+export function officePackageHasText(+ buffer: Buffer,+ matches: (text: string) => boolean,+ limits: OfficePackageLimits = OFFICE_PACKAGE_LIMITS+): boolean | null {+ return searchPackage(buffer, matches, limits, { inflated: 0 }, 0)+}++function searchPackage(+ buffer: Buffer,+ matches: (text: string) => boolean,+ limits: OfficePackageLimits,+ budget: { inflated: number },+ depth: number+): boolean | null {+ const entries = centralDirectory(buffer, limits.maxEntries)+ if (!entries) return null+ for (const entry of entries) {+ const embedded = EMBEDDED_PACKAGE.test(entry.name)+ if (!embedded && !TEXT_PART.test(entry.name)) continue+ if (embedded && depth >= limits.maxDepth) return null+ const data = entryData(buffer, entry, limits.maxInflatedBytes - budget.inflated)+ if (!data) return null+ budget.inflated += data.length+ const found = embedded+ ? searchPackage(data, matches, limits, budget, depth + 1)+ : xmlHasText(data.toString('utf8'), matches)+ if (found !== false) return found+ }+ return false+}++function xmlHasText(xml: string, matches: (text: string) => boolean): boolean {+ if (matches(decodeReferences(xml.replace(TAG, '')))) return true+ // One pass over the whole part: text that merely looks like an attribute is+ // text the first check already read, so reading it again costs nothing.+ for (const attribute of xml.matchAll(ATTRIBUTE_VALUE)) {+ if (matches(decodeReferences(attribute[1] ?? attribute[2] ?? ''))) return true+ }+ return false+}++function decodeReferences(text: string): string {+ return text.replace(CHARACTER_REFERENCE, (reference, decimal, hex, name) => {+ if (name) return NAMED_REFERENCES[name]+ const codePoint = decimal ? Number(decimal) : parseInt(hex, 16)+ return codePoint <= 0x10ffff ? String.fromCodePoint(codePoint) : reference+ })+}++/** The entries the central directory lists; null for anything but a plain zip. */+function centralDirectory(buffer: Buffer, maxEntries: number): ZipEntry[] | null {+ // The end record is the last 22 bytes, followed by a comment of at most 65535.+ let end = -1+ const earliest = Math.max(0, buffer.length - 22 - 0xffff)+ for (let at = buffer.length - 22; at >= earliest; at--) {+ if (buffer.readUInt32LE(at) === END_OF_CENTRAL_DIRECTORY) {+ end = at+ break+ }+ }+ if (end < 0) return null+ const count = buffer.readUInt16LE(end + 10)+ const size = buffer.readUInt32LE(end + 12)+ let at = buffer.readUInt32LE(end + 16)+ // ZIP64 markers: the generators never write one, so it is not read here.+ if (count === 0xffff || size === 0xffffffff || at === 0xffffffff) return null+ if (count > maxEntries || at + size > end) return null+ const entries: ZipEntry[] = []+ for (let n = 0; n < count; n++) {+ if (at + 46 > end || buffer.readUInt32LE(at) !== CENTRAL_DIRECTORY_ENTRY) return null+ const nameLength = buffer.readUInt16LE(at + 28)+ const next = at + 46 + nameLength + buffer.readUInt16LE(at + 30) + buffer.readUInt16LE(at + 32)+ if (next > end) return null+ entries.push({+ name: buffer.toString('utf8', at + 46, at + 46 + nameLength),+ flags: buffer.readUInt16LE(at + 8),+ method: buffer.readUInt16LE(at + 10),+ // From the central directory: writers that stream (ExcelJS) leave the+ // local header sizes zero.+ compressedSize: buffer.readUInt32LE(at + 20),+ localHeaderOffset: buffer.readUInt32LE(at + 42),+ })+ at = next+ }+ return entries+}++/** The entry's bytes, inflated within `remaining`; null when they cannot be read. */+function entryData(buffer: Buffer, entry: ZipEntry, remaining: number): Buffer | null {+ // Bit 0 marks an encrypted entry.+ if (entry.flags & 1 || remaining <= 0) return null+ const local = entry.localHeaderOffset+ if (local + 30 > buffer.length || buffer.readUInt32LE(local) !== LOCAL_FILE_HEADER) return null+ const start = local + 30 + buffer.readUInt16LE(local + 26) + buffer.readUInt16LE(local + 28)+ const stop = start + entry.compressedSize+ if (stop > buffer.length) return null+ const data = buffer.subarray(start, stop)+ if (entry.method === 0) return data.length <= remaining ? data : null+ if (entry.method !== 8) return null+ try {+ return zlib.inflateRawSync(data, { maxOutputLength: remaining })+ } catch {+ return null+ }+}diff --git a/mcp-host/src/core/tools/__tests__/generatedArtifactAttachments.test.ts b/mcp-host/src/core/tools/__tests__/generatedArtifactAttachments.test.tsindex a7675ace6..6a3f999df 100644--- a/mcp-host/src/core/tools/__tests__/generatedArtifactAttachments.test.ts+++ b/mcp-host/src/core/tools/__tests__/generatedArtifactAttachments.test.ts@@ -1,5 +1,6 @@import { afterEach, beforeEach, describe, expect, it } from 'vitest'import * as fs from 'fs'+import JSZip from 'jszip'import * as os from 'os'import * as path from 'path'import { zipEntries } from '../../../workflow/__tests__/support/zipEntries'@@ -43,6 +44,14 @@ function decoded(attachment: Attachment): Buffer {return Buffer.from(attachment[dataKey], b64)}+/** A minimal Office package whose one XML part holds `text`. */+function officePackage(text: string): Promise<Buffer> {+ return new JSZip()+ .file('[Content_Types].xml', '<Types/>')+ .file('part.xml', `<part>${text}</part>`)+ .generateAsync({ type: 'nodebuffer', compression: 'DEFLATE' })+}+describe('generated internal artifact attachments', () => {let outputDir: stringlet originalOutputDir: string | undefined@@ -108,14 +117,20 @@ describe('generated internal artifact attachments', () => {})it.each([- ['clerum__generate_pdf', 'report.pdf', 'pdf', Buffer.from('%PDF-1.7\n%%EOF')],- ['clerum__generate_docx', 'report.docx', 'docx', Buffer.from('PK docx bytes')],- ['clerum__generate_xlsx', 'report.xlsx', 'xlsx', Buffer.from('PK xlsx bytes')],- ['clerum__generate_pptx', 'report.pptx', 'pptx', Buffer.from('PK pptx bytes')],- ['clerum__generate_chart', 'chart.png', 'png', Buffer.from([0x89, 0x50, 0x4e, 0x47])],+ ['clerum__generate_pdf', 'report.pdf', 'pdf', async () => Buffer.from('%PDF-1.7\n%%EOF')],+ ['clerum__generate_docx', 'report.docx', 'docx', () => officePackage('safe generated content')],+ ['clerum__generate_xlsx', 'report.xlsx', 'xlsx', () => officePackage('safe generated content')],+ ['clerum__generate_pptx', 'report.pptx', 'pptx', () => officePackage('safe generated content')],+ [+ 'clerum__generate_chart',+ 'chart.png',+ 'png',+ async () => Buffer.from([0x89, 0x50, 0x4e, 0x47]),+ ],] as const)('builds a %s binary attachment when the source payload has no configured secrets',- (sourceTool, filename, format, body) => {+ async (sourceTool, filename, format, make) => {+ const body = await make()fs.writeFileSync(path.join(outputDir, filename), body)const attachment = buildGeneratedArtifactAttachment({@@ -185,6 +200,54 @@ describe('generated internal artifact attachments', () => {})+ it.each([+ ['clerum__generate_docx', 'report.docx', 'docx'],+ ['clerum__generate_xlsx', 'report.xlsx', 'xlsx'],+ ['clerum__generate_pptx', 'report.pptx', 'pptx'],+ ] as const)(+ 'does not auto-attach a %s whose delivered text holds a secret its payload never spelled',+ async (sourceTool, filename, format) => {+ fs.writeFileSync(path.join(outputDir, filename), await officePackage('<v>3141592653</v>'))++ const attachment = buildGeneratedArtifactAttachment({+ sourceTool,+ artifact: metadata(outputDir, filename, format),+ outputDir,+ maxBytes: 52_428_800,+ sourcePayload: { cell: '3,141,592,653' },+ secretEntriesProvider: () => [{ name: 'PROBE_VALUE', value: '3141592653' }],+ })++ expect(attachment).toBeNull()+ }+ )++ it.each([+ ['clerum__generate_docx', 'report.docx', 'docx'],+ ['clerum__generate_xlsx', 'report.xlsx', 'xlsx'],+ ['clerum__generate_pptx', 'report.pptx', 'pptx'],+ ] as const)(+ 'does not auto-attach a %s it cannot read while secrets are configured, and attaches it when none are',+ (sourceTool, filename, format) => {+ fs.writeFileSync(path.join(outputDir, filename), Buffer.from('PK not a package'))+ const params = {+ sourceTool,+ artifact: metadata(outputDir, filename, format),+ outputDir,+ maxBytes: 52_428_800,+ sourcePayload: { content: 'safe generated content' },+ }++ expect(+ buildGeneratedArtifactAttachment({+ ...params,+ secretEntriesProvider: () => [{ name: 'PROBE_VALUE', value: 'probe-redaction-value' }],+ })+ ).toBeNull()+ expect(buildGeneratedArtifactAttachment(params)).toMatchObject({ kind: 'file', filename })+ }+ )+it('does not auto-attach unsupported or unsafe artifacts', () => {fs.writeFileSync(path.join(outputDir, 'dashboard.html'), '<html></html>')fs.writeFileSync(path.join(outputDir, 'report.pdf'), '%PDF')@@ -418,6 +481,99 @@ describe('generated internal artifact attachments', () => {})})+ describe('values a generator stores in a form its arguments never spelled', () => {+ const protectedValue = '3141592653'++ function tool(name: string) {+ 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: protectedValue }],+ }+ )+ return registry.get(name)!+ }++ function packageXml(file: string): string {+ return [...zipEntries(file)]+ .filter(([name]) => name.endsWith('.xml'))+ .map(([, data]) => data.toString('utf8'))+ .join('\n')+ }++ it.each([+ ['grouped digits under an integer format', '3,141,592,653', { Amount: '0' }],+ ['an amount of money', '$3,141,592,653', undefined],+ ])(+ 'does not attach a sheet that stores %s as the protected digits',+ async (_label, cell, formats) => {+ const result = await tool('clerum__generate_xlsx').execute({+ filename: 'stored.xlsx',+ sheets: [+ {+ name: 'S',+ rows: [['Amount'], [cell]],+ ...(formats ? { columnFormats: formats } : {}),+ },+ ],+ })+ expect(result.is_error).toBe(false)+ // Witness: the sheet holds, as a number, digits the arguments never spelled.+ expect(packageXml(path.join(outputDir, 'stored.xlsx'))).toContain(+ `<v>${protectedValue}</v>`+ )+ expect(result.attachments ?? []).toHaveLength(0)+ }+ )++ it('attaches a sheet whose stored numbers hold no secret', async () => {+ const result = await tool('clerum__generate_xlsx').execute({+ filename: 'ordinary.xlsx',+ sheets: [{ name: 'S', rows: [['Amount'], ['1,234']], columnFormats: { Amount: '0' } }],+ })+ expect(result.is_error).toBe(false)+ expect(packageXml(path.join(outputDir, 'ordinary.xlsx'))).toContain('<v>1234</v>')+ expect(result.attachments ?? []).toHaveLength(1)+ })++ it('does not attach a deck whose chart stores grouped digits as the protected value', async () => {+ const deck = (data: string[]) => ({+ filename: 'chart.pptx',+ slides: [+ {+ layout: 'title-chart',+ title: 'Revenue',+ chart: { type: 'bar', labels: ['A'], datasets: [{ label: 'S', data }] },+ },+ ],+ })+ const leaked = await tool('clerum__generate_pptx').execute(deck(['3,141,592,653']))+ expect(leaked.is_error).toBe(false)+ expect(packageXml(path.join(outputDir, 'chart.pptx'))).toContain(protectedValue)+ expect(leaked.attachments ?? []).toHaveLength(0)++ const ordinary = await tool('clerum__generate_pptx').execute(deck(['1,234']))+ expect(ordinary.is_error).toBe(false)+ expect(ordinary.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/generatedArtifactAttachments.ts b/mcp-host/src/core/tools/generatedArtifactAttachments.tsindex 36017c956..7cbb0614f 100644--- a/mcp-host/src/core/tools/generatedArtifactAttachments.ts+++ b/mcp-host/src/core/tools/generatedArtifactAttachments.ts@@ -5,6 +5,8 @@ import {redactArtifactForDelivery,} from '../../artifacts/artifactBytes'import type { ArtifactSecretEntry } from '../../artifacts/artifactRedaction'+import { officePackageHasText } from '../../artifacts/officePackageText'+import { logger } from '../../logger'import {ArtifactPathError,isSafeArtifactFilename,@@ -30,6 +32,13 @@ export const INTERNAL_GENERATED_ARTIFACT_FORMAT_BY_TOOL: ReadonlyMap<['clerum__generate_chart', 'png'],] as const)+/** Zip packages of XML parts, whose delivered text can be read back. */+const OFFICE_PACKAGE_FORMATS: ReadonlySet<InternalGeneratedArtifactFormat> = new Set([+ 'docx',+ 'xlsx',+ 'pptx',+])+export const INTERNAL_GENERATED_ARTIFACT_MIME_BY_FORMAT: ReadonlyMap<InternalGeneratedArtifactFormat,string@@ -87,9 +96,15 @@ function closeFd(fd: number): void {}function warnSkip(reason: string, sourceTool: string, artifact: ArtifactMetadata): void {- console.warn(- `[GeneratedArtifactAttachment] Skipping artifact attachment: reason=${reason}, ` +- `tool=${sourceTool}, format=${artifact.format}, filename=${artifact.name}`+ logger.warn(+ {+ component: 'GeneratedArtifactAttachment',+ reason,+ tool: sourceTool,+ format: artifact.format,+ filename: artifact.name,+ },+ 'Skipping artifact attachment')}@@ -202,8 +217,9 @@ export function buildGeneratedArtifactAttachment(const fileBuffer = readOpenedArtifactBuffer(opened, params.maxBytes)const secretEntries = params.secretEntriesProvider?.() ?? []if (isBinaryArtifactFormat(expectedFormat)) {- // Binary artifacts are not redacted byte-by-byte here. Trust is limited to- // native generator provenance plus the source payload that produced them.+ // Binary artifacts are not redacted byte-by-byte here, so one that may+ // hold a secret is withheld: when the source payload that produced it+ // holds one, and, for an Office package, when the text it delivers does.if (secretEntries.length > 0 && params.sourcePayload === undefined) {warnSkip('binary_source_payload_unavailable', params.sourceTool, params.artifact)return null@@ -212,6 +228,22 @@ export function buildGeneratedArtifactAttachment(warnSkip('binary_redaction_unavailable', params.sourceTool, params.artifact)return null}+ // A generator can write a value in a form the payload never spelled, as+ // a sheet stores the text "3,141,592,653" as the number 3141592653, so+ // the package is read as delivered. One that cannot be read is withheld.+ if (secretEntries.length > 0 && OFFICE_PACKAGE_FORMATS.has(expectedFormat)) {+ const found = officePackageHasText(fileBuffer, text =>+ textContainsRedactableSecretEntry(text, secretEntries)+ )+ if (found !== false) {+ warnSkip(+ found ? 'binary_redaction_unavailable' : 'office_package_unreadable',+ params.sourceTool,+ params.artifact+ )+ return null+ }+ }}const redacted = redactArtifactForDelivery(expectedFormat, fileBuffer, secretEntries)if (redacted.buffer.byteLength > params.maxBytes) {