import { test } from 'node:test' import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import { mailHtml } from '../server/mail-html.js' import { stylesheetMailCss, inlineMailCss } from '../server/mail-css.js' import { safeHtml } from '../server/content.js' import { openDb, seed, uid } from '../server/db.js' import { Service } from '../server/service.js' import { createApp } from '../server/app.js' import { htmlPreviewDocument } from '../server/html-preview.js' import { darkReaderPath } from '../server/preview-assets.js' const fixture = (name) => readFileSync(new URL('./fixtures/' + name, import.meta.url), 'utf8') const png = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+a3ioAAAAASUVORK5CYII=' test('Mail text containing literal dollar replacement sequences is preserved', () => { const clean = mailHtml('

Code: $& $$ $`

') assert(clean.includes('

Code: $& $$ $`

')) }) test('Newsletter rendering preserves stylesheets, responsive rules, hidden preheaders, table geometry and source order', () => { const clean = mailHtml(fixture('mail-newsletter.html')) assert.match(clean, /

Text

', ) assert(cascade.indexOf('color:red') < cascade.indexOf('color:blue')) }) test('Outlook paragraph spacing, decimal point sizes, Word tags and inline signature styles survive filtering', () => { const clean = mailHtml(fixture('mail-outlook.html')) assert.match(clean, /margin:0cm;margin-bottom:.0001pt;font-size:11.0pt/) assert.match(clean, /line-height:normal/) assert.match(clean, /class="MsoNormal"/) assert.match(clean, /<\/span>/) const signature = safeHtml( '

Anna Beispiel

', ) assert.match(signature, /margin:0cm;font-size:10.5pt;line-height:14pt;padding:0 12px;/) assert.match(signature, /align="left" valign="top" width="300"/) }) test('Embedded class/ID selectors are scoped to the email, including body selectors inside media queries', () => { const clean = mailHtml( '

Text

', ) assert.match( clean, /#mail-content \[data-mail-html\],#mail-content \[data-mail-body\],#mail-content #email-brand/, ) assert.match(clean, /id="email-brand" class="a"/) assert.match(clean, /@media screen\{#mail-content \[data-mail-body\] p/) assert(!clean.includes('class="a darkreader')) }) test('Stylesheet and inline CSS reject executable rules, imports, fonts, unsafe URL schemes and escaped URL tricks', () => { const payload = '@import "https://evil.invalid/css";@font-face{font-family:x;src:url(https://evil.invalid/font)}body{color:#123;margin:0;background:url(\\6a avascript:evil());behavior:url(https://evil.invalid/x);-moz-binding:url(https://evil.invalid/x);position:fixed;width:expression(alert(1))}.ok{background-image:u\\72l(\\6a avascript:evil())}' const clean = stylesheetMailCss(payload) assert.match(clean, /color:#123;margin:0/) for (const forbidden of [ '@import', '@font-face', 'javascript', 'expression', 'behavior', 'binding', 'position:fixed', 'evil.invalid', ]) assert(!clean.includes(forbidden), forbidden) assert.equal( inlineMailCss('color:red;background-image:u\\72l(javascript:alert(1));-moz-binding:url(x);'), 'color:red;', ) assert.equal(stylesheetMailCss('.broken{'), '') assert.equal(inlineMailCss('width:10px; invalid }{'), '') }) test('HTML active content, tracking styles outside the mail and malicious raw-text reparse payloads are removed', () => { const input = 'Link

Text

' const clean = mailHtml(input) for (const forbidden of [ 'Text<\/p>/) assert(!clean.includes('src="data:')) }) test('Image URL normalization covers protocol-relative, HTTP, srcset and CSS backgrounds without introducing relative requests', () => { const clean = mailHtml( '
Test

Bild

', ) for (const asset of ['bg.png', 'table.png', 'a.png', 'b.png', 'c.png']) assert(clean.includes('https://images.example.org/' + asset)) assert(!clean.includes('javascript:')) assert(!clean.includes('/api/private')) assert.match(clean, /referrerpolicy="no-referrer"/) }) test('CID images, filename images, srcset and CSS backgrounds resolve only within the owning message', () => { const db = openDb(':memory:') seed(db) const s = new Service(db), admin = s.user('admin') try { const ticket = s.ingest({ messageId: `<${uid()}@example.org>`, from: 'anna@example.org', to: ['support@syncova.local'], body: 'HTML', html: '', attachments: [{ name: 'logo.png', mime: 'image/png', content: png, contentId: 'logo' }], }).ticket_id const m = s.detail(admin, ticket).messages[0], view = s.viewMessage(admin, m.id) assert.equal(view.blockedInline, 1) assert(view.html.includes('background-image:url("data:image/png;base64,')) assert.match(view.html, /srcset="data:image\/png;base64,[^"]+ 2x"/) assert(!view.html.includes('cid:')) assert(!view.html.includes('src="logo.png"')) assert(view.html.includes('Bild nicht verfügbar')) } finally { db.close() } }) test('Preview uses nonce-scoped Dark Reader without blanket color overrides or same-origin sandbox access', () => { const dark = htmlPreviewDocument({ dark: true, remoteImages: false }) assert(dark.html.includes(darkReaderPath)) assert(dark.html.includes('ignoreImageAnalysis')) assert(dark.html.includes('Preview fetch disabled')) assert(!dark.html.includes('background-color:transparent!important')) assert(!dark.csp.includes('allow-same-origin')) assert(!dark.csp.includes("script-src 'unsafe-inline'")) assert(!dark.csp.includes('https:')) assert(!htmlPreviewDocument({ dark: false }).html.includes(`src="${darkReaderPath}"`)) }) test('Repeated CID references cannot expand an email beyond the inline image budget', () => { const db = openDb(':memory:') seed(db) const s = new Service(db), admin = s.user('admin') try { const image = Buffer.alloc(2 * 1024 * 1024) Buffer.from(png, 'base64').copy(image) const id = s.ingest({ messageId: `<${uid()}@example.org>`, from: 'anna@example.org', to: ['support@syncova.local'], body: 'Bilder', html: ''.repeat(12), attachments: [ { name: 'large.png', mime: 'image/png', content: image.toString('base64'), contentId: 'large', }, ], }).ticket_id const view = s.viewMessage(admin, s.detail(admin, id).messages[0].id) assert.equal(view.blockedInline, 2) assert.equal((view.html.match(/src="data:image/g) || []).length, 10) assert(Buffer.byteLength(view.html) < 29 * 1024 * 1024) } finally { db.close() } }) test('Dark Reader is a public immutable vendor asset while the actual preview document remains authenticated', async () => { const db = openDb(':memory:') seed(db) const { app } = createApp(db), server = app.listen(0, '127.0.0.1') await new Promise((resolve) => server.once('listening', resolve)) const base = `http://127.0.0.1:${server.address().port}` try { const asset = await fetch(base + darkReaderPath) assert.equal(asset.status, 200) assert.match(asset.headers.get('content-type'), /javascript/) assert.match(asset.headers.get('cache-control'), /immutable/) assert((await asset.text()).includes('DarkReader')) assert.equal((await fetch(base + '/api/html-preview')).status, 401) } finally { server.closeAllConnections() await new Promise((resolve) => server.close(resolve)) db.close() } })