|
| 1 | +// Issue #6795 — injectTitle() in both export paths passed the user-derived |
| 2 | +// `<title>` tag as the *string* replacement argument of String.replace(), so |
| 3 | +// ECMA-262 GetSubstitution expanded `$$`, `$&`, `` $` ``, `$'` sequences from |
| 4 | +// the artifact title: `Save $$$ This Quarter` lost a `$`, `$&`/`$'` spliced |
| 5 | +// the matched tag or the whole document tail into the title (leaking visible |
| 6 | +// text into the exported PDF/image). The sibling injectBaseHref/injectStyle |
| 7 | +// helpers already used function replacements and were unaffected. |
| 8 | +// |
| 9 | +// These tests pin the "replace an existing <title>" branch — the branch that |
| 10 | +// runs for virtually every generated artifact — by capturing the document each |
| 11 | +// exporter loads into its hidden render window and asserting the title landed |
| 12 | +// verbatim (HTML-escaped only) with no duplicated document content. |
| 13 | + |
| 14 | +import { mkdtemp, rm } from 'node:fs/promises'; |
| 15 | +import { tmpdir } from 'node:os'; |
| 16 | +import { dirname, join } from 'node:path'; |
| 17 | + |
| 18 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 19 | + |
| 20 | +const rendererState = vi.hoisted(() => ({ |
| 21 | + loadedUrls: [] as string[], |
| 22 | + savePath: '' as string, |
| 23 | +})); |
| 24 | + |
| 25 | +vi.mock('electron', () => { |
| 26 | + const image = { |
| 27 | + getSize: () => ({ height: 1, width: 1 }), |
| 28 | + toBitmap: () => Buffer.alloc(4), |
| 29 | + toJPEG: () => Buffer.from('jpeg'), |
| 30 | + toPNG: () => Buffer.from('png'), |
| 31 | + }; |
| 32 | + |
| 33 | + class BrowserWindow { |
| 34 | + readonly webContents = { |
| 35 | + capturePage: async () => image, |
| 36 | + executeJavaScript: async (source: string): Promise<unknown> => { |
| 37 | + if (source.includes('document.documentElement.scrollHeight')) return 1; |
| 38 | + return true; |
| 39 | + }, |
| 40 | + on: () => undefined, |
| 41 | + printToPDF: async () => Buffer.from('pdf'), |
| 42 | + setWindowOpenHandler: () => undefined, |
| 43 | + }; |
| 44 | + |
| 45 | + async loadURL(url: string): Promise<void> { |
| 46 | + rendererState.loadedUrls.push(url); |
| 47 | + } |
| 48 | + |
| 49 | + destroy(): void {} |
| 50 | + getContentSize(): [number, number] { return [1440, 900]; } |
| 51 | + isDestroyed(): boolean { return false; } |
| 52 | + setContentSize(): void {} |
| 53 | + } |
| 54 | + |
| 55 | + return { |
| 56 | + BrowserWindow, |
| 57 | + dialog: { |
| 58 | + showSaveDialog: async () => ({ canceled: false, filePath: rendererState.savePath }), |
| 59 | + }, |
| 60 | + }; |
| 61 | +}); |
| 62 | + |
| 63 | +import { exportArtifact } from '../../src/main/artifact-export.js'; |
| 64 | +import { exportPdfFromHtml } from '../../src/main/pdf-export.js'; |
| 65 | + |
| 66 | +const sourceHtml = |
| 67 | + '<!doctype html><html><head><title>Old</title></head><body><p>BODY MARKER</p></body></html>'; |
| 68 | + |
| 69 | +// One title per GetSubstitution pattern a string replacement would expand. |
| 70 | +// `expectedTitleTag` is the verbatim, HTML-escaped-only insertion the escape |
| 71 | +// helpers around injectTitle clearly intend. |
| 72 | +const titles = [ |
| 73 | + { expectedTitleTag: '<title>Save $$$ This Quarter</title>', title: 'Save $$$ This Quarter' }, |
| 74 | + { expectedTitleTag: '<title>Before $& After</title>', title: 'Before $& After' }, |
| 75 | + { expectedTitleTag: "<title>Rock $'n Roll Tour</title>", title: "Rock $'n Roll Tour" }, |
| 76 | +]; |
| 77 | + |
| 78 | +let workDir: string; |
| 79 | + |
| 80 | +beforeEach(async () => { |
| 81 | + workDir = await mkdtemp(join(tmpdir(), 'od-title-patterns-')); |
| 82 | + rendererState.savePath = join(workDir, 'out.pdf'); |
| 83 | +}); |
| 84 | + |
| 85 | +afterEach(async () => { |
| 86 | + rendererState.loadedUrls.length = 0; |
| 87 | + await rm(workDir, { force: true, recursive: true }); |
| 88 | +}); |
| 89 | + |
| 90 | +describe('export titles containing replacement patterns', () => { |
| 91 | + it.each(titles)( |
| 92 | + 'exportPdfFromHtml renders the title $title verbatim', |
| 93 | + async ({ expectedTitleTag, title }) => { |
| 94 | + const result = await exportPdfFromHtml({ |
| 95 | + deck: false, |
| 96 | + defaultFilename: 'artifact.pdf', |
| 97 | + html: sourceHtml, |
| 98 | + title, |
| 99 | + }); |
| 100 | + |
| 101 | + expect(result.ok).toBe(true); |
| 102 | + expect(loadedDocument()).toContain(expectedTitleTag); |
| 103 | + expect(countBodyMarkers(loadedDocument())).toBe(1); |
| 104 | + }, |
| 105 | + ); |
| 106 | + |
| 107 | + it.each(titles)( |
| 108 | + 'exportArtifact renders the title $title verbatim', |
| 109 | + async ({ expectedTitleTag, title }) => { |
| 110 | + const result = await exportArtifact({ |
| 111 | + deck: false, |
| 112 | + format: 'image', |
| 113 | + html: sourceHtml, |
| 114 | + imageFormat: 'png', |
| 115 | + title, |
| 116 | + }); |
| 117 | + |
| 118 | + try { |
| 119 | + expect(result.ok).toBe(true); |
| 120 | + expect(loadedDocument()).toContain(expectedTitleTag); |
| 121 | + expect(countBodyMarkers(loadedDocument())).toBe(1); |
| 122 | + } finally { |
| 123 | + if (result.path) await rm(dirname(result.path), { force: true, recursive: true }); |
| 124 | + } |
| 125 | + }, |
| 126 | + ); |
| 127 | +}); |
| 128 | + |
| 129 | +function loadedDocument(): string { |
| 130 | + expect(rendererState.loadedUrls).toHaveLength(1); |
| 131 | + const url = rendererState.loadedUrls[0]; |
| 132 | + if (!url) throw new Error('renderer did not load a document'); |
| 133 | + const prefix = 'data:text/html;charset=utf-8,'; |
| 134 | + expect(url.startsWith(prefix)).toBe(true); |
| 135 | + return decodeURIComponent(url.slice(prefix.length)); |
| 136 | +} |
| 137 | + |
| 138 | +// A `$'`/`$&` expansion splices document content into the <title>, so the |
| 139 | +// corrupted output carries the body text more than once. |
| 140 | +function countBodyMarkers(doc: string): number { |
| 141 | + return doc.split('BODY MARKER').length - 1; |
| 142 | +} |
0 commit comments