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('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 |
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 = '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: '