Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): read an SVG prolog in one pass
marcotebbeb53a0f811a
diff --git a/mcp-host/src/workflow/__tests__/embeddedImages.test.ts b/mcp-host/src/workflow/__tests__/embeddedImages.test.tsindex 1fb4541fa..0bfc21a2b 100644--- a/mcp-host/src/workflow/__tests__/embeddedImages.test.ts+++ b/mcp-host/src/workflow/__tests__/embeddedImages.test.ts@@ -203,6 +203,59 @@ describe('loadEmbeddableImage', () => {expect(loadEmbeddableImage('pct.svg', outputDir, [])).toMatchObject({ width: 50, height: 40 })})+ it('reads an SVG after the declaration, comments and doctype an XML file may open with', async () => {+ const root =+ '<svg xmlns="http://www.w3.org/2000/svg" width="12" height="8"><rect width="12" height="8"/></svg>'+ const files: Record<string, string> = {+ 'decl.svg': `<?xml version="1.0" encoding="UTF-8"?>\n${root}`,+ 'comments.svg': `\n <!-- Generator: x -->\n<!---->\t<!-- <svg> named in a comment -->${root}`,+ 'doctype.svg':+ '<?xml version="1.0" standalone="no"?>\n<!-- a -->\n' ++ '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" ' ++ `"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n${root}`,+ }+ for (const [name, text] of Object.entries(files)) {+ fs.writeFileSync(path.join(outputDir, name), text)+ }+ await predecodeImages(Object.keys(files), outputDir)+ for (const name of Object.keys(files)) {+ expect(loadEmbeddableImage(name, outputDir, []), name).toMatchObject({+ format: 'png',+ width: 12,+ height: 8,+ })+ }+ })++ it('does not read as SVG a prolog that never reaches the <svg> root', async () => {+ const root =+ '<svg xmlns="http://www.w3.org/2000/svg" width="12" height="8"><rect width="12" height="8"/></svg>'+ const files: Record<string, string> = {+ 'unclosed.svg': `<!-- never closed ${root}`,+ 'html.svg': `<!-- a --><html>${root}</html>`,+ 'stray.svg': `<!-- a --> text --> ${root}`,+ }+ for (const [name, text] of Object.entries(files)) {+ fs.writeFileSync(path.join(outputDir, name), text)+ }+ await predecodeImages(Object.keys(files), outputDir)+ for (const name of Object.keys(files)) {+ const warnings: string[] = []+ expect(loadEmbeddableImage(name, outputDir, warnings), name).toBeUndefined()+ expect(warnings).toEqual([`'${name}' is not an image this tool can read and was left out.`])+ }+ })++ it('reads a prolog of back-to-back comments with the last one open in linear time', async () => {+ // Every "--><!--" could end one comment or sit inside a longer one, so a+ // pattern that let a comment run past its first "-->" tried each way.+ fs.writeFileSync(path.join(outputDir, 'comments.svg'), `<!--${'--><!--'.repeat(30)}`)+ const started = performance.now()+ await predecodeImages(['comments.svg'], outputDir)+ expect(performance.now() - started).toBeLessThan(2000)+ expect(loadEmbeddableImage('comments.svg', outputDir, [])).toBeUndefined()+ })+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.tsindex f5d593901..cafcb0872 100644--- a/mcp-host/src/workflow/embeddedImages.ts+++ b/mcp-host/src/workflow/embeddedImages.ts@@ -367,9 +367,23 @@ function boundedSvg(buf: Buffer): Buffer | undefined {)}+/**+ * Whether the bytes open an SVG document: an optional XML declaration, then+ * comments, an optional doctype and the <svg> root. A comment ends at its first+ * "-->", as in XML, so the prolog is read in one pass.+ */function isSvg(buf: Buffer): boolean {- const head = buf.toString('utf8', 0, Math.min(buf.length, 1024)).trimStart()- return /^(?:<\?xml[^>]*>\s*)?(?:<!--[\s\S]*?-->\s*)*(?:<!DOCTYPE[^>]*>\s*)?<svg\b/i.test(head)+ let head = buf+ .toString('utf8', 0, Math.min(buf.length, 1024))+ .trimStart()+ .replace(/^<\?xml[^>]*>/i, '')+ .trimStart()+ while (head.startsWith('<!--')) {+ const close = head.indexOf('-->', 4)+ if (close < 0) return false+ head = head.slice(close + 3).trimStart()+ }+ return /^(?:<!DOCTYPE[^>]*>\s*)?<svg\b/i.test(head)}/** Bytes safe to hand to the decoder, or undefined for a format or size this tool does not convert. */