mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
Preserve shared named palette classes, rounded badge styling and validated custom color contrast after sanitizing activity prose and rich label content. Global user CSS restrictions remain unchanged. Guard missing labels. Five positive/negative viewer checks passed; live browser rendering not verified. Thanks to xet7.
105 lines
6.4 KiB
JavaScript
105 lines
6.4 KiB
JavaScript
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const vm = require('node:vm');
|
|
const test = require('node:test');
|
|
const sanitizeHTML = require('sanitize-html');
|
|
const source = fs.readFileSync('client/components/activities/activities.js', 'utf8');
|
|
const jade = fs.readFileSync('client/components/activities/activities.jade', 'utf8');
|
|
|
|
test('both activity feeds render values through the shared HTML/Markdown/emoji viewer', () => {
|
|
assert.match(jade, /\+activity\(activity=activityData card=card mode=mode\)/);
|
|
assert.doesNotMatch(jade, /\(sanitize |\{\{\{_ /);
|
|
assert.match(jade, /activityMessage 'activity-changedTitle' \(activityValue activity.value\)/);
|
|
assert.match(source, /activityValue\(value\)[\s\S]*?return titleViewerHtml/);
|
|
assert.match(source, /stripLinks: !!\(setting && setting.renderLinksAsPlainText\)/);
|
|
assert.match(source, /\^https\?:/);
|
|
assert.match(jade, /div\.activity-desc/);
|
|
});
|
|
|
|
test('rich activity links preserve allowed HTML and avoid nested anchors', () => {
|
|
const html = {
|
|
Raw: value => value,
|
|
A: (attrs, text) => `<a href="${attrs.href}">${text}</a>`,
|
|
SPAN: (...values) => `<span>${values.join('')}</span>`,
|
|
};
|
|
const context = { HTML: html, Blaze: { toHTML: value => value },
|
|
titleViewerHtml: value => value,
|
|
sanitizeHTML,
|
|
};
|
|
const start = source.indexOf('function linkedActivityValue(');
|
|
const end = source.indexOf('\nfunction createBoardLink', start);
|
|
vm.runInNewContext(source.slice(start, end), context);
|
|
assert.equal(context.linkedActivityValue('<strong>👍</strong>', '', 'card'), '<strong>👍</strong>');
|
|
assert.equal(context.linkedActivityValue('<strong>👍</strong><script>bad()</script>', '/card', 'card'),
|
|
'<a href="/card"><strong>👍</strong></a>');
|
|
const linked = context.linkedActivityValue('<a href="https://example.com">Title</a>', '/card', 'card');
|
|
assert.equal(linked, '<span><a href="https://example.com">Title</a> <a href="/card">↗</a></span>');
|
|
});
|
|
|
|
// Browsers accept malformed closing tags; exercise the real parser rather
|
|
// than a regex substitute that could hide a sanitizer regression.
|
|
test('activity sanitizer handles malformed script end tags', () => {
|
|
for (const input of ['<script>bad()</script foo="bar">', '<SCRIPT>bad()</SCRIPT >']) {
|
|
assert.equal(sanitizeHTML('<strong>Title</strong>' + input), '<strong>Title</strong>');
|
|
}
|
|
assert.doesNotMatch(fs.readFileSync(__filename, 'utf8'), /sanitizeHTML:\s*value\s*=>\s*value\.replace/);
|
|
});
|
|
|
|
// Use the actual formatter so literal placeholders cannot pass a mocked API.
|
|
test('activity messages substitute all values and discard Spacebars options', async () => {
|
|
const i18next = require('i18next');
|
|
const sprintf = require('i18next-sprintf-postprocessor');
|
|
const i18n = i18next.createInstance().use(sprintf);
|
|
await i18n.init({ lng: 'en', resources: { en: { translation: {
|
|
added: "added label '%s' to %s", moved: 'moved %s from %s to %s',
|
|
comment: 'on %s', deleted: 'deleted an attachment from %s',
|
|
} } }, postProcess: ['sprintf'], interpolation: { escapeValue: false } });
|
|
let plainLinks = false;
|
|
const body = source.split(' activityMessage(key, ...values) {')[1].split('\n },')[0];
|
|
const helper = new Function('ReactiveCache', 'TAPi18n', 'sanitizeHTML', `return function(key, ...values) {${body}}`)(
|
|
{ getCurrentSetting: () => ({ renderLinksAsPlainText: plainLinks }) },
|
|
{ __: (key, options) => i18n.t(key, options) },
|
|
(value, options) => sanitizeHTML(value, { allowedTags: options.stripLinks ? ['strong', 'em'] : ['a', 'strong', 'em'], allowedAttributes: { a: ['href'] } }),
|
|
);
|
|
const link = '<a href="/card">Demo 👍</a>';
|
|
assert.equal(helper('added', '<em>Feature</em>', link, { hash: {} }), "added label '<em>Feature</em>' to " + link);
|
|
assert.equal(helper('moved', 'CARD', 'OLD', 'NEW', { hash: {} }), 'moved CARD from OLD to NEW');
|
|
assert.equal(helper('comment', link, { hash: {} }), 'on ' + link);
|
|
assert.equal(helper('deleted', 'CARD', { hash: {} }), 'deleted an attachment from CARD');
|
|
assert.equal(helper('comment', '100% safe %s', { hash: {} }), 'on 100% safe %s');
|
|
assert.doesNotMatch(helper('comment', '<script>bad()</script><strong>Good</strong>', { hash: {} }), /script|bad/);
|
|
plainLinks = true;
|
|
assert.equal(helper('comment', link, { hash: {} }), 'on Demo 👍');
|
|
});
|
|
|
|
test('activity label badges preserve trusted palette styling while sanitizing user content', () => {
|
|
let label = { name: '<b>Feature 👍</b><script>bad()</script>', color: 'green' };
|
|
let plainLinks = false;
|
|
const body = source.split(' labelActivityMessage(key, cardLink) {')[1].split('\n },')[0];
|
|
const helper = new Function('ReactiveCache', 'TAPi18n', 'sanitizeHTML', 'titleViewerHtml', 'isHexColor', 'contrastText', 'Blaze', 'HTML', `return function(key, cardLink) {${body}}`)(
|
|
{ getBoard: () => ({ getLabelById: () => label }), getCurrentSetting: () => ({ renderLinksAsPlainText: plainLinks }) },
|
|
{ __: (_, { sprintf }) => `added ${sprintf[0]} to ${sprintf[1]}` },
|
|
(value, opts) => sanitizeHTML(value, { allowedTags: opts.stripLinks ? ['b', 'span'] : ['b', 'span', 'a'], allowedAttributes: { a: ['href'] } }),
|
|
value => value, value => /^#[0-9a-f]{6}$/i.test(value), () => '#000000',
|
|
{ toHTML: value => value },
|
|
{ Raw: value => value, SPAN: (attrs, value) => `<span class="${attrs.class}"${attrs.style ? ` style="${attrs.style}"` : ''}>${value}</span>` },
|
|
);
|
|
const ctx = { activity: { boardId: 'b', labelId: 'l' } };
|
|
let result = helper.call(ctx, 'added', '<a href="/card">Card</a>');
|
|
assert.match(result, /class="card-label activity-label card-label-green"/);
|
|
assert.match(result, /<b>Feature 👍<\/b>/);
|
|
assert.doesNotMatch(result, /script|bad\(\)/);
|
|
label = { name: '<span style="color:red" class="evil">Label</span>', color: '#ffdab9' };
|
|
result = helper.call(ctx, 'added', 'Card');
|
|
assert.match(result, /style="background-color:#ffdab9;color:#000000;"/);
|
|
assert.doesNotMatch(result, /evil|color:red/);
|
|
label = { name: 'Label', color: 'green" onclick="bad()' };
|
|
assert.doesNotMatch(helper.call(ctx, 'added', 'Card'), /onclick|bad\(\)/);
|
|
label = null;
|
|
assert.doesNotThrow(() => helper.call(ctx, 'removed', 'Card'));
|
|
plainLinks = true;
|
|
assert.doesNotMatch(helper.call(ctx, 'added', '<a href="/card">Card</a>'), /<a /);
|
|
assert.match(jade, /labelActivityMessage 'activity-added-label' cardLink/);
|
|
assert.match(jade, /labelActivityMessage 'activity-removed-label' cardLink/);
|
|
});
|