📧✨ Beautiful email. For everyone.
fix: bound HTML preview processing
Paul Koeckfd4e8d52f36c
diff --git a/packages/imap/src/convert.ts b/packages/imap/src/convert.tsindex 6652fa2..a9adabc 100644--- a/packages/imap/src/convert.ts+++ b/packages/imap/src/convert.ts@@ -108,11 +108,34 @@ export function parseBlobId(value: string): { messageId: string; part: string }return { messageId: value.slice(0, slash), part: value.slice(slash + 1) }}+const previewInputLimit = 4096++/** Skip non-visible blocks in a forward scan, including an unterminated final block. */+function withoutHiddenBlocks(value: string): string {+ const opening = /<(style|script|head)(?=[\s/>])/gi+ const visible: string[] = []+ let start = 0+ let match: RegExpExecArray | null+ while ((match = opening.exec(value))) {+ visible.push(value.slice(start, match.index), ' ')+ const closing = new RegExp('</' + match[1] + '>', 'gi')+ closing.lastIndex = opening.lastIndex+ // Do not retry the suffix for every unmatched opener, or show truncated CSS/JS.+ if (!closing.exec(value)) return visible.join('')+ start = closing.lastIndex+ opening.lastIndex = start+ }+ visible.push(value.slice(start))+ return visible.join('')+}+export function previewText(value: string, html = false): string {+ // Opening a message supplies the full body. Bound work here for every caller,+ // before any HTML processing, entity decoding, or line splitting.+ value = value.slice(0, previewInputLimit)const text = html- ? value- .replace(/<(style|script|head)[\s\S]*?<\/\1>/gi, ' ')- .replace(/<[^>]+>/g, ' ')+ ? withoutHiddenBlocks(value)+ .replace(/<[^<>]*>/g, ' ').replace(/ /gi, ' ').replace(/&/gi, '&').replace(/</gi, '<')diff --git a/tests/imap-convert.test.ts b/tests/imap-convert.test.tsnew file mode 100644index 0000000..fa75f22--- /dev/null+++ b/tests/imap-convert.test.ts@@ -0,0 +1,53 @@+import { describe, expect, it } from 'vitest'+import { previewText } from '../packages/imap/src/convert'++describe('IMAP previews', () => {+ it('normalizes whitespace and leaves quoted reply lines out', () => {+ expect(previewText(' Hello\tthere\r\n > Old reply\n\nNew text ')).toBe('Hello there New text')+ expect(previewText('x'.repeat(300))).toHaveLength(256)+ })++ it('removes HTML tags and hidden blocks and decodes supported entities', () => {+ expect(+ previewText(+ '<HEAD><title>Hidden</title><style>CSS</style></HEAD>' ++ '<p>Hello <b>there</b> & <world> ' ' "</p>' ++ '<script type="text/javascript">hidden()</script><style>more CSS</style><p>Bye</p>',+ true,+ ),+ ).toBe("Hello there & <world> ' ' \" Bye")+ })++ it.each(['style', 'script', 'head'])('omits an unterminated %s block', (tag) => {+ expect(previewText(`<p>Visible</p><${tag}>Hidden<${tag}>Still hidden`, true)).toBe('Visible')+ })++ it('keeps visible text between separate hidden blocks', () => {+ expect(previewText('Before<style>CSS</style>Between<script>JS</script>After', true)).toBe(+ 'Before Between After',+ )+ })++ it('preserves Unicode around mixed-case hidden tags', () => {+ expect(previewText('İstanbul<StYlE>CSS</sTyLe>世界', true)).toBe('İstanbul 世界')+ })++ it('requires an actual hidden tag name', () => {+ expect(previewText('<header>Heading</header><stylesheet>Text</stylesheet>', true)).toBe(+ 'Heading Text',+ )+ })++ it.each([false, true])('bounds the input before processing (HTML: %s)', (html) => {+ expect(previewText(' '.repeat(4096) + 'Outside the preview sample', html)).toBe('')+ expect(previewText(' '.repeat(4095) + 'AB', html)).toBe('A')+ })++ it('omits a hidden block whose closing tag falls outside the sample', () => {+ expect(previewText('Before<style>' + ' '.repeat(4096) + '</style>After', true)).toBe('Before')+ })++ it('strips complete tags after malformed angle brackets', () => {+ expect(previewText('Hello <<b>world</b>', true)).toBe('Hello < world')+ })+})diff --git a/tests/imap-provider.test.ts b/tests/imap-provider.test.tsindex ae54483..b9a75fb 100644--- a/tests/imap-provider.test.ts+++ b/tests/imap-provider.test.ts@@ -143,6 +143,32 @@ describe('IMAP TLS policy', () => {})describe('IMAP indexing and reading', () => {+ it('bounds an empty preview on opening while preserving the full HTML body', async () => {+ const html = ' '.repeat(4096) + '<p>Full message content</p>'+ server.deliver('INBOX', { messageId: 'html@x', subject: 'HTML', date: at(1), html })+ const p = await connected()+ // Exercise the large-message, per-part path without allocating a large attachment.+ const port = server.commandPorts[0]+ const fetch = port.fetch.bind(port)+ vi.spyOn(port, 'fetch').mockImplementation(async (...args) =>+ (await fetch(...args)).map((m) => ({+ ...m,+ ...(args[1].size ? { size: 10_000_001 } : {}),+ })),+ )+ await sync(p)+ const [thread] = (await p.query(account, { view: 'inbox' })).items+ expect(thread.messages[0].preview).toBe('')++ const [message] = await p.conversation(account, thread.id)+ expect(message.text).toBe('')+ expect(message.html).toBe(html)+ expect(message.preview).toBe('')++ const [reopened] = await p.messages(account, [message.id], true)+ expect(reopened.html).toBe(message.html)+ expect(reopened.preview).toBe('')+ })it('indexes recent Inbox mail first and reports incomplete results until history is indexed', async () => {for (let i = 0; i < 12; i++)server.deliver('INBOX', {