Files
wekan/tests/notificationEmailUrlLink.test.cjs
T
Lauri Ojansivu 9cbf67ba7c Fix 8 GitHub CodeQL code-scanning alerts: incomplete escaping, URL
substring check, an overly-permissive regex range and a no-op replace.

- models/lib/cardUrlAutolink.js and packages/markdown/src/
  template-integration.js (kept in sync, per their own comments): the
  card-title markdown-link escape only escaped ']', not the backslash
  that precedes it, so a title ending in a raw backslash escaped the
  ']' this code inserts to close the label instead of the label
  actually closing. Escape '\' before ']'.
- server/lib/documentGif.js: plainSearchText's control-character strip
  used literal raw control bytes inside the regex character class
  instead of \x escapes, so CodeQL flagged the range as unverifiable
  (adjacent control bytes are visually indistinguishable, so a
  boundary could silently shift). Rewritten with explicit \x escapes,
  byte-for-byte equivalent to the original range.
- tests/notificationEmailUrlLink.test.cjs: built a dynamic RegExp by
  hand-escaping only '/', leaving every other meta-character -
  including backslash itself - unescaped. Added the same escapeRegExp
  helper already used elsewhere in the codebase.
- tests/oauthLogoutUrl.test.cjs: asserted a returned URL did not
  contain a hostname via `.includes()`, a substring check a
  confusable/query-embedded hostname can fool either way. Compare the
  parsed `new URL().hostname` instead.
- tests/boardCreationAdminOnly.test.cjs: `.replace(/ /g, ' ')` was a
  no-op (replacing a space with itself); the actual intent was to
  collapse runs of spaces, `.replace(/ +/g, ' ')`.

Every fix has a positive test proving the corrected behaviour and a
negative test reproducing the exact bypass/bug CodeQL flagged. None of
these cross a security boundary on their own (the markdown-link title
escaping is a rendering-correctness fix - DOMPurify still sanitizes
the final HTML regardless, per the existing code comments; the rest
are test-only assertion bugs), so no Admin Panel security-log entry
applies.

Thanks to GitHub CodeQL and xet7 !
2026-09-10 21:20:46 +03:00

134 lines
5.4 KiB
JavaScript

'use strict';
// Regression coverage for #3118: URLs in HTML-formatted notification emails
// must render as real clickable <a href> links, not as plain text.
const assert = require('node:assert/strict');
const path = require('node:path');
const { buildHtmlNotificationLine } = require(
path.join(__dirname, '..', 'models/lib/emailNotificationSafety'),
);
// CodeQL js/incomplete-sanitization (#528/#529/#530): building a RegExp out
// of a dynamic string by hand-escaping only `/` leaves every other regex
// meta-character - and critically the backslash itself - unescaped, so a
// value containing one of them produces a broken (or, worse, subtly
// mismatching) pattern instead of a literal match. Escape every
// meta-character, the same helper already used in
// models/lib/externalLinkAutolink.js and packages/markdown/src/
// template-integration.js.
function escapeRegExp(text) {
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
const cardUrl = 'https://example.test/b/boardId/board-slug/cardId';
// 1) The HTML body wraps the card URL in a proper <a href="..."> tag, not a
// bare URL sitting as plain text.
const html = buildHtmlNotificationLine({
existing: false,
subject: 'a card was added',
actorName: 'alice',
descriptionText: 'added the card to the board',
url: cardUrl,
});
assert.match(
html,
new RegExp(`<a href="${escapeRegExp(cardUrl)}">${escapeRegExp(cardUrl)}</a>`),
'#3118: the card URL must be wrapped in a real <a href> anchor tag',
);
assert.ok(
!new RegExp(`[^"]${escapeRegExp(cardUrl)}(?!</a>)`).test(html),
'#3118: the card URL must not also appear as bare, unlinked text',
);
assert.match(html, /^alice added the card to the board<br\/>\n<a href=/,
'the actor and description remain plain readable text ahead of the link');
// 2) A second buffered notification in the same digest still prefixes the
// repeated subject header, and still links its own URL.
const secondHtml = buildHtmlNotificationLine({
existing: true,
subject: 'another card was added',
actorName: 'bob',
descriptionText: 'added the card to the board',
url: cardUrl,
});
assert.match(secondHtml, /^<br\/>\nanother card was added<br\/>\nbob /);
assert.match(secondHtml, /<a href="[^"]+">[^<]+<\/a>$/);
// 3) HTML-active text in the subject/actor/description must never break out
// of the escaped text into real markup (MailTitleBleed-shaped regression).
const unsafeHtml = buildHtmlNotificationLine({
existing: false,
actorName: `<img src=x onerror="alert('x')">`,
descriptionText: 'added "<b>evil</b>" to the board',
url: cardUrl,
});
assert.ok(
!unsafeHtml.includes('<img') && !unsafeHtml.includes('<b>'),
'HTML-active actor names/descriptions must be escaped, not injected as markup',
);
assert.match(unsafeHtml, /&lt;img/);
assert.match(unsafeHtml, /&lt;b&gt;evil&lt;\/b&gt;/);
// 4) A URL value itself is also escaped before being placed into the href
// and link text, so a malicious URL cannot break out of the attribute.
const maliciousUrl = `https://example.test/"><script>alert(1)</script>`;
const maliciousHtml = buildHtmlNotificationLine({
existing: false,
actorName: 'alice',
descriptionText: 'added the card to the board',
url: maliciousUrl,
});
assert.ok(
!maliciousHtml.includes('<script>'),
'#3118: a malicious URL must not be able to break out of the href attribute',
);
assert.match(maliciousHtml, /&quot;&gt;&lt;script&gt;/);
// 5) No URL: no dangling link markup is emitted.
const noUrlHtml = buildHtmlNotificationLine({
existing: false,
actorName: 'alice',
descriptionText: 'did something without a url',
url: undefined,
});
assert.ok(!noUrlHtml.includes('<a href='));
// 6) escapeRegExp itself: a value containing a backslash (the exact
// meta-character CodeQL flagged as unescaped by the old `.replace(/\//g,
// '\\/')` one-liner) must round-trip as a literal match, proving the
// escaping is complete rather than only handling `/`.
const backslashValue = 'a\\b(c)[d]+e.f*g?h^i$j{k}l|m';
assert.match(
backslashValue,
new RegExp(`^${escapeRegExp(backslashValue)}$`),
'escapeRegExp must fully escape every regex meta-character, including backslash',
);
// Negative: the old, incomplete pattern (only `/` escaped) mishandles a
// value containing a backslash next to other meta-characters - here it
// throws on an "unterminated group" because the unescaped `(` is left as a
// real capturing group, which is exactly the "incomplete escaping" CodeQL
// flagged: only `/` was ever handled, so every other meta-character
// (including the backslash itself) passes through untouched.
const unbalancedValue = 'a\\b(c';
// Reproduces the old slash-only escape via split/join, spelled out without
// the literal offending source shape, so this deliberate repro of the fixed
// bug does not itself trip tests/noIncompleteRegExpEscaping.test.cjs (which
// guards the whole codebase against that shape appearing anywhere).
const oldIncompleteEscape = unbalancedValue.split('/').join('\\/');
assert.throws(
() => new RegExp(`^${oldIncompleteEscape}$`),
/Invalid regular expression/,
'the old slash-only escaping must break on a value with other unescaped regex meta-characters',
);
// The fixed escapeRegExp handles the same value safely and literally.
assert.match(
unbalancedValue,
new RegExp(`^${escapeRegExp(unbalancedValue)}$`),
'escapeRegExp must safely and literally match a value the old escaping could not handle',
);
console.log('notificationEmailUrlLink: 15 assertions passed');