Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): read a protected number as a locale writes it, not as bare digits
marcotebd88a1c39d93d
diff --git a/mcp-host/src/__tests__/artifacts/officePackageText.test.ts b/mcp-host/src/__tests__/artifacts/officePackageText.test.tsindex 0031d4c08..2ad2a2d1e 100644--- a/mcp-host/src/__tests__/artifacts/officePackageText.test.ts+++ b/mcp-host/src/__tests__/artifacts/officePackageText.test.ts@@ -9,7 +9,7 @@ import {officePackageHasText,officePackageTexts,} from '../../artifacts/officePackageText'-import { withoutSeparators } from '../../artifacts/shownNumbers'+import { showsNumber } from '../../artifacts/shownNumbers'const needle = '3141592653'const holds = (text: string) => text.includes(needle)@@ -143,12 +143,15 @@ function workbook(})}-/** Matches nothing as stored, and `shown` (without separators) as shown. */+/** Matches nothing as stored, and `shown` as shown: its text, or the number it writes. */function shownOnly(shown: string): [(text: string) => boolean, typeof OFFICE_PACKAGE_LIMITS, (text: string) => boolean] {- const target = withoutSeparators(shown)- return [() => false, OFFICE_PACKAGE_LIMITS, text => text.includes(target)]+ return [+ () => false,+ OFFICE_PACKAGE_LIMITS,+ text => text.includes(shown) || showsNumber(text, shown),+ ]}describe('officePackageHasText, numbers as they are shown', () => {diff --git a/mcp-host/src/__tests__/artifacts/shownNumbers.test.ts b/mcp-host/src/__tests__/artifacts/shownNumbers.test.tsindex be87dc1cc..8e4e07ee1 100644--- a/mcp-host/src/__tests__/artifacts/shownNumbers.test.ts+++ b/mcp-host/src/__tests__/artifacts/shownNumbers.test.ts@@ -4,6 +4,7 @@ import {isModelledFormatCode,renderCount,shownForms,+ showsNumber,withoutSeparators,} from '../../artifacts/shownNumbers'import { CURRENCY_SYMBOLS } from '../../workflow/xlsxCells'@@ -34,12 +35,17 @@ describe('isModelledFormatCode', () => {expect(isModelledFormatCode(code)).toBe(true)})- it.each(['[$-409]yyyy-mm-dd', '[$€-407]#,##0', '[$-de-DE]dd.mm.yyyy', '[$USD-409]0', '[$€]0'])(- 'reads %s, a currency or a plain locale',- code => {- expect(isModelledFormatCode(code)).toBe(true)- }- )+ it.each([+ '[$-409]yyyy-mm-dd',+ '[$€-407]#,##0',+ '[$-de-DE]dd.mm.yyyy',+ '[$USD-409]0',+ '[$€]0',+ '[$€-x-euro2] #,##0.00',+ '#,##0.00 [$€-x-euro1]',+ ])('reads %s, a currency or a plain locale', code => {+ expect(isModelledFormatCode(code)).toBe(true)+ })it('reads built-in 47 as Excel shows it', () => {expect(builtinFormatCode(47)).toBe('mm:ss.0')@@ -75,27 +81,53 @@ describe('isModelledFormatCode', () => {})describe('shownForms', () => {- it('shows a date serial as its date, in the code and without separators', () => {- expect(shownForms(JAN_2_2026, 'yyyy-mm-dd')).toContain('2026-01-02'.replace(/[.,]/g, ''))+ it('shows a date serial as its date in the code', () => {+ expect(shownForms(JAN_2_2026, 'yyyy-mm-dd')).toContain('2026-01-02')expect(shownForms(JAN_2_2026, 'dd/mm/yyyy')).toContain('02/01/2026')- expect(shownForms(46024.62783564815, 'yyyy-mm-dd hh:mm:ss')).toContain('2026-01-0215:04:05')+ expect(shownForms(46024.62783564815, 'yyyy-mm-dd hh:mm:ss')).toContain('2026-01-02 15:04:05')})it('shows a built-in short date in the patterns locales use', () => {const forms = shownForms(JAN_2_2026, 'mm-dd-yy', true)- for (const shown of ['1/2/2026', '2/1/2026', '02012026', '2026-01-02']) {- expect(forms).toContain(withoutSeparators(shown))+ for (const shown of ['1/2/2026', '2/1/2026', '02.01.2026', '2026-01-02']) {+ expect(forms).toContain(shown)}})it('shows grouped digits, and quoted text beside them', () => {- expect(shownForms(3141592653, '#,##0')).toEqual(['3141592653'])+ expect(shownForms(3141592653, '#,##0')).toEqual(['3,141,592,653'])expect(shownForms(123456, '"sk-"0')).toEqual(['sk-123456'])})it('shows month names in other languages', () => {const forms = shownForms(JAN_2_2026, 'mmm d, yyyy')- expect(forms).toEqual(expect.arrayContaining(['Jan22026', 'janv22026', 'ene22026']))+ expect(forms).toEqual(expect.arrayContaining(['Jan 2, 2026', 'janv. 2, 2026', 'ene 2, 2026']))+ })+})++describe('showsNumber', () => {+ it.each([+ ['grouped with commas', '3,141,592,653', '3141592653'],+ ['grouped the German way', '3.141.592.653', '3141592653'],+ ['grouped with apostrophes', "3'141'592'653", '3141592653'],+ ['grouped with narrow spaces', '3\u202f141\u202f592\u202f653', '3141592653'],+ ['grouped the Indian way', '3,14,15,92,653', '3141592653'],+ ['before a decimal part', '3,141,592,653.00', '3141592653'],+ ['written in blocks, as a card number is', 'Card 4111 1111 1111 1111', '4111111111111111'],+ ['inside a longer grouped number', 'Q1 15,432 units', '5432'],+ ['after a decimal point', '0.1234', '1234'],+ ['as the secret writes it, separators and all', '1,234.50', '1.234,50'],+ ])('finds a number shown %s', (_label, text, secret) => {+ expect(showsNumber(text, secret)).toBe(true)+ })++ it.each([+ ['across a decimal point', '54.32%', '5432'],+ ['from a value that rounds elsewhere', '3,141,592,652.6', '3141592653'],+ ['across two numbers in a list', '54, 32', '5432'],+ ['in a secret that is not a number', 'sk-123456', 'sk-123456'],+ ])('does not read a number %s', (_label, text, secret) => {+ expect(showsNumber(text, secret)).toBe(false)})})diff --git a/mcp-host/src/artifacts/officePackageText.ts b/mcp-host/src/artifacts/officePackageText.tsindex bcb91a346..98c0e5655 100644--- a/mcp-host/src/artifacts/officePackageText.ts+++ b/mcp-host/src/artifacts/officePackageText.ts@@ -66,7 +66,7 @@ interface ZipEntry {* nodes run together, so a value split across runs or cells reads whole, and* each attribute value on its own, with character references decoded; then* each number a sheet cell or a chart holds, in every form it may be shown,- * passed to `matchesShown` without separators (shownNumbers.withoutSeparators);+ * passed to `matchesShown` as shown (match numbers with shownNumbers.showsNumber);* null skips them.* Embedded packages are read too. Null when the bytes are not a package this* reader can read within `limits`, or show a number in a way it does not modeldiff --git a/mcp-host/src/artifacts/shownNumbers.ts b/mcp-host/src/artifacts/shownNumbers.tsindex a00dd8022..d6f74e99b 100644--- a/mcp-host/src/artifacts/shownNumbers.ts+++ b/mcp-host/src/artifacts/shownNumbers.ts@@ -2,10 +2,10 @@* What a spreadsheet shows for a number under a number format code, so a* protected value it displays is found although the file only stores the* number: a date serial under a date format, digits under a grouping format or- * beside quoted text. A spreadsheet renders in its viewer's locale, so a value- * is compared without separators (any locale's grouping reads the same), with- * month and day names in English, German, French and Spanish, and, for the- * built-in short date, in the date patterns locales commonly use.+ * beside quoted text. A spreadsheet renders in its viewer's locale, so a+ * protected number is looked for as any locale may write it (showsNumber),+ * with month and day names in English, German, French and Spanish, and, for+ * the built-in short date, in the date patterns locales commonly use.*/import type * as Numfmt from 'numfmt'@@ -97,6 +97,8 @@ function isModelledLocale(value: string): boolean {const dash = value.indexOf('-')if (dash < 0) return trueconst tag = value.slice(dash + 1)+ // Excel's euro placement tags, which move the symbol and nothing else.+ if (/^x-euro[12]$/i.test(tag)) return true// Ids from 0xF000 are system formats; bytes above 0xFFFF pick a calendar or digits.if (/^[0-9A-F]{1,8}$/i.test(tag)) return parseInt(tag, 16) < 0xf000return /^[a-z]{2,3}(?:-[a-z0-9]{2,8})*$/i.test(tag) && !/-[a-z](?:-|$)/i.test(tag)@@ -183,9 +185,10 @@ export function renderCount(code: string, shortDate = false): number {const memo = new Map<string, string[]>()/**- * The ways a viewer may show `value` under `code` (`code` modelled), each- * without separators. For a built-in short date id, pass `shortDate` to render- * the common date patterns instead of `code`.+ * The ways a viewer may show `value` under `code` (`code` modelled), as shown;+ * compare a protected number with showsNumber, which reads any locale's+ * grouping. For a built-in short date id, pass `shortDate` to render the+ * common date patterns instead of `code`.*/export function shownForms(value: number, code: string, shortDate = false): string[] {const key = `${shortDate ? 'S' : 'C'}\u0000${code}\u0000${value}`@@ -194,10 +197,10 @@ export function shownForms(value: number, code: string, shortDate = false): striconst nf = numfmt()const forms = new Set<string>()for (const each of renderedCodes(code, shortDate)) {- forms.add(withoutSeparators(nf.format(each, value, { locale: 'en', throws: false })))+ forms.add(nf.format(each, value, { locale: 'en', throws: false }))if (NAMED.test(each)) {for (const locale of NAME_LOCALES) {- forms.add(withoutSeparators(nf.format(each, value, { locale, throws: false })))+ forms.add(nf.format(each, value, { locale, throws: false }))}}}@@ -206,3 +209,97 @@ export function shownForms(value: number, code: string, shortDate = false): strimemo.set(key, out)return out}++/** A run of digits with single separators between them, as a number is shown. */+const NUMBER_RUN = /\d+(?:[,.'\u2019\u00A0\u202F ]\d+)*/g+const RUN_PART = /\d+|[,.'\u2019\u00A0\u202F ]/g+/** Separators that only ever group digits: spaces and apostrophes, never a decimal mark. */+const BLOCK_SEPARATOR = /^[ \u00A0\u202F'\u2019]$/++/** Whether `groups` are the digit groups of one number as a locale groups thousands. */+function groupedValidly(groups: string[]): boolean {+ if (groups.length < 2) return true+ const [first, ...rest] = groups+ const last = rest[rest.length - 1]+ const western = first.length <= 3 && rest.every(group => group.length === 3)+ // Indian grouping: 3 digits last, 2 in each group before them.+ const indian =+ first.length <= 2 &&+ rest.length >= 2 &&+ last.length === 3 &&+ rest.slice(0, -1).every(group => group.length === 2)+ return western || indian+}++/** Whether every separator in `separators` is `separators[0]`. */+function oneSeparator(separators: string[]): boolean {+ return separators.every(sep => sep === separators[0])+}++/** The most groups one number is read across: 24 digits in groups of three. */+const MAX_GROUPS = 8++/**+ * The digits a stretch of digit groups and the separators between them may+ * stand for: all of them, where the separators group thousands as some locale+ * does; those before the last separator, where that one is a decimal point+ * and the rest group thousands; and, for groups set apart by spaces or+ * apostrophes only (a card or phone number), all of them as a whole number.+ * A decimal part is never joined to what comes before it: "54.32" shows 54+ * and 32, not 5432.+ */+function readStretch(groups: string[], separators: string[], within: string[], whole: string[]) {+ if (oneSeparator(separators) && groupedValidly(groups)) within.push(groups.join(''))+ const decimal = separators[separators.length - 1]+ const grouping = separators.slice(0, -1)+ if (+ (decimal === '.' || decimal === ',') &&+ (grouping.length === 0 || (oneSeparator(grouping) && grouping[0] !== decimal)) &&+ groupedValidly(groups.slice(0, -1))+ ) {+ within.push(groups.slice(0, -1).join(''))+ }+ if (separators.every(sep => BLOCK_SEPARATOR.test(sep))) whole.push(groups.join(''))+}++/**+ * Every reading of a shown run of digits and separators: each group on its+ * own, and each stretch of up to MAX_GROUPS groups read as one number (a space+ * may group thousands or just stand between two numbers, as in "Q1 15,432").+ */+function readings(run: string): { within: string[]; whole: string[] } {+ const groups: string[] = []+ const separators: string[] = []+ for (const part of run.match(RUN_PART) ?? []) {+ if (/\d/.test(part)) groups.push(part)+ else separators.push(part)+ }+ const within = [...groups]+ const whole: string[] = []+ for (let from = 0; from < groups.length; from++) {+ for (let to = from + 1; to < Math.min(groups.length, from + MAX_GROUPS); to++) {+ readStretch(groups.slice(from, to + 1), separators.slice(from, to), within, whole)+ }+ }+ return { within, whole }+}++/**+ * Whether `text` shows `secret` as a number, written as any locale writes it.+ * Only a secret that is a number counts: digits, with separators or not, at+ * least four of them; any other secret is found by its exact text instead.+ */+export function showsNumber(text: string, secret: string): boolean {+ if (!/^[\d,.'\u2019\u00A0\u202F ]+$/.test(secret)) return false+ const digits = withoutSeparators(secret)+ if (digits.length < 4 || !/\d/.test(text)) return false+ // A secret written with separators may use another locale's decimal mark.+ const asWritten = digits !== secret+ for (const run of text.match(NUMBER_RUN) ?? []) {+ const { within, whole } = readings(run)+ if (within.some(reading => reading.includes(digits))) return true+ if (whole.includes(digits)) return true+ if (asWritten && withoutSeparators(run).includes(digits)) return true+ }+ return false+}diff --git a/mcp-host/src/core/tools/__tests__/generatedArtifactAttachments.test.ts b/mcp-host/src/core/tools/__tests__/generatedArtifactAttachments.test.tsindex 4c2f98326..56400bda7 100644--- a/mcp-host/src/core/tools/__tests__/generatedArtifactAttachments.test.ts+++ b/mcp-host/src/core/tools/__tests__/generatedArtifactAttachments.test.ts@@ -714,6 +714,44 @@ describe('generated internal artifact attachments', () => {expect(calm.attachments ?? []).toHaveLength(1)})+ it.each([+ [+ 'markdown',+ 'clerum__generate_markdown',+ { filename: 'card.md', content: 'Card 4111 1111 1111 1111' },+ ],+ ['a PDF', 'clerum__generate_pdf', { filename: 'card.pdf', body: 'Card 4111 1111 1111 1111' }],+ ])(+ 'does not attach %s that writes a protected number in groups',+ async (_label, name, args) => {+ const result = await toolWith(name, ['4111111111111111']).execute(args)+ expect(result.is_error).toBe(false)+ expect(result.attachments ?? []).toHaveLength(0)+ }+ )++ it('attaches a chart whose decimal label only runs the digits of a protected number together', async () => {+ const result = await toolWith('clerum__generate_chart', ['5432']).execute({+ filename: 'pct.png',+ type: 'bar',+ data: { labels: ['54.32%', 'other'], datasets: [{ label: 'S', data: [1, 2] }] },+ })+ expect(result.is_error).toBe(false)+ expect(result.attachments ?? []).toHaveLength(1)+ })++ it('attaches a document whose only match is the time the generator stamped on it', async () => {+ // core.xml holds the moment of writing; a protected value of the year is not in the arguments.+ const year = String(new Date().getUTCFullYear())+ const result = await toolWith('clerum__generate_docx', [year]).execute({+ filename: 'stamped.docx',+ title: 'Report',+ body: 'Hello',+ })+ expect(result.is_error).toBe(false)+ expect(result.attachments ?? []).toHaveLength(1)+ })+it("attaches ordinary documents when a protected value is also the generators' own text", async () => {const common = ['Office', '1000', 'Calibri', 'General', 'main', 'false', 'solid']const calls: Array<[string, Record<string, unknown>]> = [diff --git a/mcp-host/src/core/tools/generatedArtifactAttachments.ts b/mcp-host/src/core/tools/generatedArtifactAttachments.tsindex 01483cbef..288f1a6f8 100644--- a/mcp-host/src/core/tools/generatedArtifactAttachments.ts+++ b/mcp-host/src/core/tools/generatedArtifactAttachments.ts@@ -7,13 +7,14 @@ import {} from '../../artifacts/artifactBytes'import type { ArtifactSecretEntry } from '../../artifacts/artifactRedaction'import { OFFICE_PACKAGE_LIMITS, officePackageHasText } from '../../artifacts/officePackageText'-import { withoutSeparators } from '../../artifacts/shownNumbers'+import { showsNumber } from '../../artifacts/shownNumbers'import { logger } from '../../logger'import {ArtifactPathError,isSafeArtifactFilename,openExistingArtifactFile,} from '../../workflow/artifactPaths'+import { withoutWriteTimes } from '../../workflow/officeBoilerplate'import { printedForms } from '../../workflow/toolText'import type { ArtifactMetadata } from '../../workflow/types'import type { Attachment } from '../types'@@ -69,7 +70,7 @@ type BuildGeneratedArtifactAttachmentParams = {packageBoilerplate?: readonly string[]/*** What the file shows that its arguments do not spell (a chart's drawn- * labels), each string as drawn; compared without separators.+ * labels), each string as drawn; a number is read as any locale writes it.*/shownText?: readonly string[]/**@@ -133,6 +134,11 @@ function warnSkip()}+/**+ * Whether `content` holds an entry: its exact text, or, for an entry that is a+ * number, that number as any locale writes it ("4111 1111 1111 1111",+ * "3.141.592.653").+ */function textContainsRedactableSecretEntry(content: string,entries: ArtifactSecretEntry[]@@ -140,27 +146,17 @@ function textContainsRedactableSecretEntry(if (entries.length === 0) return falsereturn entries.some(entry => {if (!entry.value || entry.value.length < 4) return false- return content.includes(entry.value)+ return content.includes(entry.value) || showsNumber(content, entry.value)})}-/**- * Whether a string the file shows holds an entry, both without separators: a- * label drawn "3,141,592,653" shows "3141592653" and "3.141.592.653" alike.- */+/** Whether a string the file shows (a chart's drawn label) holds an entry. */function shownTextHoldsSecretEntry(shown: readonly string[] | undefined,entries: ArtifactSecretEntry[]): boolean {if (!shown?.length) return false- const values = entries- .map(entry => withoutSeparators(entry.value))- .filter(value => value.length >= 4)- if (values.length === 0) return false- return shown.some(text => {- const bare = withoutSeparators(text)- return values.some(value => bare.includes(value))- })+ return shown.some(text => textContainsRedactableSecretEntry(text, entries))}/**@@ -302,24 +298,22 @@ export function buildGeneratedArtifactAttachment(// the package is read as delivered. One that cannot be read is withheld.const boilerplate = params.packageBoilerplate ?? []const packageEntries = secretEntries.filter(- entry => !boilerplate.some(text => text.includes(entry.value))+ entry => !boilerplate.some(text => textContainsRedactableSecretEntry(text, [entry])))- // Numbers are compared as shown without separators, so any locale's- // grouping of a protected value matches. Every entry counts here: a- // number a cell or a chart shows comes from the arguments, never from- // the generator's own text.- const shownValues = secretEntries- .map(entry => withoutSeparators(entry.value))- .filter(value => value.length >= 4)+ // Every entry counts for the numbers a cell or a chart shows: they come+ // from the arguments, never from the generator's own text.+ const shownEntries = secretEntries.filter(entry => entry.value && entry.value.length >= 4)if (- (packageEntries.length > 0 || shownValues.length > 0) &&+ (packageEntries.length > 0 || shownEntries.length > 0) &&OFFICE_PACKAGE_FORMATS.has(expectedFormat)) {const found = officePackageHasText(fileBuffer,- text => textContainsRedactableSecretEntry(text, packageEntries),+ text => textContainsRedactableSecretEntry(withoutWriteTimes(text), packageEntries),OFFICE_PACKAGE_LIMITS,- shownValues.length > 0 ? shown => shownValues.some(value => shown.includes(value)) : null+ shownEntries.length > 0+ ? shown => textContainsRedactableSecretEntry(shown, shownEntries)+ : null)if (found !== false) {warnSkip(diff --git a/mcp-host/src/workflow/officeBoilerplate.ts b/mcp-host/src/workflow/officeBoilerplate.tsindex fed319970..120b289f0 100644--- a/mcp-host/src/workflow/officeBoilerplate.ts+++ b/mcp-host/src/workflow/officeBoilerplate.ts@@ -128,11 +128,18 @@ export function boilerplateTexts(format: OfficeFormat): Promise<string[]> {return Promise.race([reference, waited]).finally(() => clearTimeout(timer))}+/** When a package was written, which its document properties hold. */+const WRITE_TIME = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z/g+/**- * When the sample was written (its document properties), which no other- * package shares, so it is no part of the reference.+ * `text` without the times a generator stamps on the package it writes. They+ * are no part of the reference, which no other package shares them with, and+ * no part of what a delivered package is checked for: the generator wrote+ * them, not the arguments.*/-const TIMESTAMP = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z/g+export function withoutWriteTimes(text: string): string {+ return text.replace(WRITE_TIME, '\u0000')+}async function writeSample(format: OfficeFormat): Promise<string[]> {const sample = SAMPLES[format]@@ -143,7 +150,7 @@ async function writeSample(format: OfficeFormat): Promise<string[]> {if (!result.success || !result.artifact) throw new Error(result.error ?? 'no artifact')const texts = officePackageTexts(fs.readFileSync(result.artifact.path))if (!texts) throw new Error('the reference package could not be read')- return texts.map(text => text.replace(TIMESTAMP, '\u0000'))+ return texts.map(withoutWriteTimes)} finally {fs.rmSync(dir, { recursive: true, force: true })}