215 lines
9.6 KiB
JavaScript
215 lines
9.6 KiB
JavaScript
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('<p>Code: $& $$ $`</p>')
|
|
assert(clean.includes('<p>Code: $& $$ $`</p>'))
|
|
})
|
|
|
|
test('Newsletter rendering preserves stylesheets, responsive rules, hidden preheaders, table geometry and source order', () => {
|
|
const clean = mailHtml(fixture('mail-newsletter.html'))
|
|
assert.match(clean, /<style>/)
|
|
assert.match(clean, /#mail-content \[data-mail-body\]/)
|
|
assert.match(clean, /\.card\{width:640px;max-width:640px;background:#fff;/)
|
|
assert.match(clean, /@media only screen and \(max-width:600px\)/)
|
|
assert.match(clean, /width:100%!important/)
|
|
assert.match(clean, /\.preheader\{display:none!important;/)
|
|
assert.match(clean, /align="center"/)
|
|
assert.match(clean, /valign="top" width="33%"/)
|
|
assert.match(clean, /cellpadding="0" cellspacing="0"/)
|
|
assert.match(clean, /bgcolor="#6750c9"/)
|
|
const cascade = mailHtml(
|
|
'<style>.x{color:red}</style><style>.x{color:blue}</style><p class="x">Text</p>',
|
|
)
|
|
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><\/span>/)
|
|
const signature = safeHtml(
|
|
'<table width="440"><tr><td align="left" valign="top" width="300"><p style="margin:0cm;font-size:10.5pt;line-height:14pt;padding:0 12px">Anna Beispiel</p></td></tr></table>',
|
|
)
|
|
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(
|
|
'<style>html,body,#brand,.a:is(.b,.c){color:red}@media screen{body p{margin:0}}</style><p id="brand" class="a darkreader syncova-image-unavailable">Text</p>',
|
|
)
|
|
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 =
|
|
'<html><head><base href="https://evil.invalid"><meta http-equiv="refresh" content="0;url=https://evil.invalid"><link rel="stylesheet" href="https://evil.invalid/a.css"></head><body onload="evil()"><script>evil()</script><iframe srcdoc="<script>evil()</script>"></iframe><object data="x"></object><svg onload="evil()"></svg><a href="javascript:evil()" onclick="evil()">Link</a><img src="data:image/svg+xml;base64,AAAA" onerror="evil()"><textarea><img src=x onerror=evil()></textarea><p style="background:url(file:///etc/passwd)">Text</p></body></html>'
|
|
const clean = mailHtml(input)
|
|
for (const forbidden of [
|
|
'<script',
|
|
'<iframe',
|
|
'<object',
|
|
'<svg',
|
|
'<base',
|
|
'<meta',
|
|
'<link',
|
|
'<textarea',
|
|
'onload',
|
|
'onclick',
|
|
'onerror',
|
|
'javascript:',
|
|
'file:///',
|
|
])
|
|
assert(!clean.includes(forbidden), forbidden)
|
|
assert.match(clean, />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(
|
|
'<style>.banner{background-image:url(//images.example.org/bg.png)}</style><table background="//images.example.org/table.png"><tr><td>Test</td></tr></table><img src="http://images.example.org/a.png" srcset="//images.example.org/b.png 2x, javascript:evil() 3x"><img src="/api/private"><p style="background:url(http://images.example.org/c.png)">Bild</p>',
|
|
)
|
|
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: '<style>.logo{background-image:url(cid:logo)}</style><img src="cid:logo"><img src="./logo.png"><img src="cid:%3Clogo%3E"><img srcset="cid:logo 2x"><img src="cid:foreign">',
|
|
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: '<img src="cid:large">'.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()
|
|
}
|
|
})
|