Files
wekan/tests/notificationEmailHtmlSafety.test.cjs
T
Lauri Ojansivu ab785acd1e Choose what notifications contain, and group and schedule them, for e-mail, the tray and webhooks.
One delivery model (models/lib/notificationDelivery.js) for every channel -
e-mail, the notification tray and outgoing webhooks - set in the
Notifications popup at Admin Panel, board and Member Settings scope, and in
each one-way webhook's form:

- Content. Webhooks: the text on or off, and which property groups are sent
  (standard, ids, names, links, actor, people, details), with checklist,
  list, swimlane and board names, before/after values, the card description
  and links added as opt-in groups. E-mail and tray: the classic sentence or
  a clear layout - the board as a link first, the actor and card in bold -
  and which parts are shown.
- Grouping: one message per event, per card, per board, or all together.
- Scheduling: immediately, every 5 minutes to 24 hours, or once a day, with
  optional quiet hours, in the time zone of whoever set it.

The most specific explicit setting wins: member, then board, then Admin
Panel for e-mail and tray; the webhook, then its board, then Admin Panel for
webhooks. A member can only leave their own name out of outgoing webhooks.
The built-in defaults keep today's behaviour. Grouped and scheduled webhooks
and e-mail go through durable queues that survive restarts; queued webhooks
use the SSRF guard and log a blocked address in Admin Panel - Problems.
Values of admin-only custom fields now reach only board admins in e-mail and
tray, and never a webhook.

The new strings are in English in every locale, pending Transifex.

Thanks to HaKePlan, Meeques and xet7 !

Fixes #3695,
Fixes #5171.
2026-10-10 15:28:26 +03:00

90 lines
4.0 KiB
JavaScript

'use strict';
// Regression coverage for MailTitleBleed.
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const root = path.resolve(__dirname, '..');
const safetyModulePath = path.join(root, 'models/lib/emailNotificationSafety');
const { escapeEmailHtml, safeEmailSubject } = require(safetyModulePath);
const safetySource = fs.readFileSync(`${safetyModulePath}.js`, 'utf8');
const email = fs.readFileSync(
path.join(root, 'server/notifications/email.js'),
'utf8',
);
assert.equal(
escapeEmailHtml(`<img src=x onerror="alert('x')"> & card`),
'&lt;img src=x onerror=&quot;alert(&#39;x&#39;)&quot;&gt; &amp; card',
'HTML-active card, list and board titles must render only as text',
);
assert.equal(
escapeEmailHtml('ordinary Roadmap title'),
'ordinary Roadmap title',
'ordinary notification text must remain readable',
);
assert.equal(
safeEmailSubject('Roadmap\r\nBcc: attacker@example.test'),
'Roadmap Bcc: attacker@example.test',
'titles must not inject additional mail headers',
);
assert.match(
email,
// Not `bodyTemplate ? text : ...`: that sent the template's substituted
// member-written values as HTML (MailTitleBleed regression, 2026-10-02).
// #5171: the clearly arranged layout comes first when chosen; it is built
// by models/lib/notificationDelivery.js renderNotificationItem, which
// escapes every value (tests/notificationDelivery.test.cjs).
/const html = clear \? clear\.html : bodyTemplate\s*\?[^\n]*\$\{substituteVars\(bodyTemplate, htmlVars\)\}`\s*:\s*buildHtmlNotificationLine\(\{/,
'the HTML notification body must be built by the shared, escaping-aware helper',
);
assert.match(
safetySource,
/escapeEmailHtml\(actorName \|\| ''\)/,
'the actor name must be escaped before it is placed into the HTML message',
);
assert.match(
safetySource,
/escapeEmailHtml\(\s*descriptionText \|\| '',?\s*\)/,
'the localized description must be escaped before it is placed into the HTML message',
);
assert.match(
safetySource,
/escapeEmailHtml\(safeUrl\)/,
'#3118: the card\\/board URL must be escaped before it is placed into the <a href> link',
);
assert.match(
safetySource,
/<a href="\$\{escapeEmailHtml\(safeUrl\)\}">\$\{escapeEmailHtml\(safeUrl\)\}<\/a>/,
'#3118: an HTML-formatted notification email must wrap the card\\/board URL in a real <a href> link, not plain text',
);
assert.match(
email,
// `let`, not `const`: the admin-template override (#2022) below reassigns
// it through the same safeEmailSubject() guard, so it must stay mutable.
/let subject = safeEmailSubject\(formatActivityNotificationTitle\(/,
'notification subjects must use the shared header-safe formatter',
);
// MailTitleBleed regression (2026-10-02): with an admin-defined activity
// body template, the HTML body was the substituted template, and the values
// substituted into it - card, board and list titles, usernames, comments - are
// written by members. Markup in a card title reached other members' mail.
{
const { substituteVars } = require('../models/lib/ruleVarsSubstitute');
const { escapeEmailHtml } = require('../models/lib/emailNotificationSafety');
const vars = { card: '<img src=x onerror=alert(1)>', board: 'B&B "x"', username: 'u' };
const htmlVars = Object.fromEntries(Object.entries(vars).map(([k, v]) => [k, escapeEmailHtml(v)]));
const out = substituteVars('<p>{username} changed <b>{card}</b> on {board}</p>', htmlVars);
assert.equal(out, '<p>u changed <b>&lt;img src=x onerror=alert(1)&gt;</b> on B&amp;B &quot;x&quot;</p>',
'the admin template keeps its HTML; the member-written values are escaped');
assert.doesNotMatch(email, /const html = bodyTemplate\s*\?\s*text\b/,
'the HTML body is never the raw substituted text');
assert.match(email, /const htmlVars = Object\.fromEntries\(Object\.entries\(templateVars\)\.map\(\(\[key, value\]\) => \[key, escapeEmailHtml\(value\)\]\)\);/);
assert.match(email, /\$\{substituteVars\(bodyTemplate, htmlVars\)\}/);
}
console.log('notificationEmailHtmlSafety: 12 assertions passed');