Skip to content

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

TypeScript

fix(mcp-host): bound an SVG sized with an exponent and refuse unreadable sizes

marcotebfb0695db62c5
diff --git a/mcp-host/src/workflow/__tests__/embeddedImages.test.ts b/mcp-host/src/workflow/__tests__/embeddedImages.test.ts
index 44344d271..1fb4541fa 100644
--- a/mcp-host/src/workflow/__tests__/embeddedImages.test.ts
+++ b/mcp-host/src/workflow/__tests__/embeddedImages.test.ts
@@ -183,6 +183,26 @@ describe('loadEmbeddableImage', () => {
})
})
+ it('bounds an SVG size written with an exponent and refuses one it cannot read', async () => {
+ const svg = (attrs: string) =>
+ `<svg xmlns="http://www.w3.org/2000/svg" ${attrs}><rect width="10" height="10"/></svg>`
+ fs.writeFileSync(path.join(outputDir, 'exp.svg'), svg('width="1e9" height="5e8"'))
+ fs.writeFileSync(path.join(outputDir, 'calc.svg'), svg('width="calc(1e9px)" height="10"'))
+ fs.writeFileSync(
+ path.join(outputDir, 'pct.svg'),
+ svg('width="100%" height="100%" viewBox="0 0 50 40"')
+ )
+ await predecodeImages(['exp.svg', 'calc.svg', 'pct.svg'], outputDir)
+ expect(loadEmbeddableImage('exp.svg', outputDir, [])).toMatchObject({
+ width: 2048,
+ height: 1024,
+ })
+ const warnings: string[] = []
+ expect(loadEmbeddableImage('calc.svg', outputDir, warnings)).toBeUndefined()
+ expect(warnings[0]).toMatch(/calc\.svg' is not an image this tool can read/)
+ expect(loadEmbeddableImage('pct.svg', outputDir, [])).toMatchObject({ width: 50, height: 40 })
+ })
+
it('does not decode an image whose header declares an enormous size', async () => {
const gif = Buffer.from(
'R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==',
diff --git a/mcp-host/src/workflow/embeddedImages.ts b/mcp-host/src/workflow/embeddedImages.ts
index 2b4f616d0..f5d593901 100644
--- a/mcp-host/src/workflow/embeddedImages.ts
+++ b/mcp-host/src/workflow/embeddedImages.ts
@@ -268,7 +268,8 @@ export interface EmbeddableImage {
/**
* Largest image decoded for conversion, in pixels (64 MB of RGBA). The canvas
* library aborts the whole process, uncatchably, when it cannot allocate a
- * bitmap, so every size is checked from the file header before decoding.
+ * bitmap, so every size is checked before decoding: a raster's from its file
+ * header, an SVG's from its root element, which is refused when unreadable.
*/
const MAX_DECODE_PIXELS = 16_000_000
const MAX_SVG_SIDE = 2048
@@ -313,10 +314,20 @@ const SVG_UNIT_PX: Record<string, number> = {
ex: 8,
}
-function svgLength(tag: string, name: string): number | undefined {
- const m = new RegExp(`\\s${name}\\s*=\\s*["']\\s*([0-9.]+)\\s*([a-z]*)\\s*["']`, 'i').exec(tag)
+const SVG_NUMBER = '\\+?(?:\\d+(?:\\.\\d*)?|\\.\\d+)(?:e[+-]?\\d+)?'
+
+/**
+ * The root's `name` attribute in pixels: undefined when it is absent or a
+ * percentage, which leaves the size to the viewBox, and null when it is set in
+ * a form this reader cannot size, so the image is not decoded at all.
+ */
+function svgLength(tag: string, name: string): number | null | undefined {
+ const attr = new RegExp(`\\s${name}\\s*=\\s*["']([^"']*)["']`, 'i').exec(tag)
+ if (!attr) return undefined
+ const m = new RegExp(`^\\s*(${SVG_NUMBER})\\s*([a-z%]*)\\s*$`, 'i').exec(attr[1])
+ if (m?.[2] === '%') return undefined
const factor = m ? SVG_UNIT_PX[m[2].toLowerCase()] : undefined
- return m && factor ? Number(m[1]) * factor : undefined
+ return m && factor ? Number(m[1]) * factor : null
}
/**
@@ -331,8 +342,11 @@ function boundedSvg(buf: Buffer): Buffer | undefined {
const tag = open[0]
const box =
/\sviewBox\s*=\s*["']\s*[-0-9.e]+[\s,]+[-0-9.e]+[\s,]+([0-9.e]+)[\s,]+([0-9.e]+)/i.exec(tag)
- const width = svgLength(tag, 'width') ?? (box ? Number(box[1]) : undefined)
- const height = svgLength(tag, 'height') ?? (box ? Number(box[2]) : undefined)
+ const givenWidth = svgLength(tag, 'width')
+ const givenHeight = svgLength(tag, 'height')
+ if (givenWidth === null || givenHeight === null) return undefined
+ const width = givenWidth ?? (box ? Number(box[1]) : undefined)
+ const height = givenHeight ?? (box ? Number(box[2]) : undefined)
if (!width || !height || !Number.isFinite(width) || !Number.isFinite(height)) return buf
const scale = Math.min(
1,