Files
wekan/tests/legacyAvatarPrefixFallback.test.cjs
T
Lauri Ojansivu 0afb1336db SVG is uploaded and shown as an image: an attachment, a cover, a board background, an avatar.
Since JVN#74210258 (stored XSS) an SVG upload was refused and any stored
SVG was served as a download, so an SVG could not be a card's picture.
Every uploaded SVG is already sanitized on the server before it is
stored - no script, event handler, javascript: URI, foreignObject or
DOCTYPE/ENTITY XML loop (models/lib/sanitizeUploadedFile.js) - so the
refusal goes, for attachments and avatars.

Serving it inline is made safe in one place, server/lib/svgResponse.js,
used by every route that sends file bytes (universalFileServer.js for
attachments, avatars and legacy files, avatarServer.js,
legacyAttachments.js and httpStream.js): the WHOLE file is sanitized
again before it is sent, whatever is stored, and sent as image/svg+xml
under a Content-Security-Policy with default-src 'none' and sandbox, no
script, X-Frame-Options DENY and nosniff, so even opened directly as a
page it runs nothing. A download is sanitized too; a range request gets
the whole file; an SVG over 8 MB stays a sandboxed download. A file whose
active markup is removed on the way out is logged to Admin Panel /
Problems like the stream sanitizer's, and one removed at upload trips
the existing sanitize.dangerous-content canary.

tests/svgServing.test.cjs runs the real sanitizer on a crafted SVG
(script, onload, javascript:, DOCTYPE/ENTITY, foreignObject/iframe) and
checks the body and headers, the download, the oversized case, and that
no file-serving route in server/routes or httpStream.js sends bytes
without the SVG path, and that nothing else sends image/svg+xml;
tests/playwright/specs/svg-attachment.e2e.js uploads one, checks what is
served, that it shows in the card without running anything, and makes
it the cover and the board background.

Thanks to xet7 !
2026-10-25 06:32:25 +02:00

46 lines
2.5 KiB
JavaScript

const assert = require('node:assert/strict');
const fs = require('node:fs');
const vm = require('node:vm');
const test = require('node:test');
const source = fs.readFileSync('server/lib/legacyAvatarRead.js', 'utf8');
function load({ user = 'member', metadata = { _id: 'avatar' }, stream = {} } = {}) {
const calls = [];
const context = {
getUserIdFromRequest: async () => { calls.push('auth'); return user; },
getOldAttachmentData: async (id, bucket) => { calls.push(['metadata', id, bucket]); return metadata; },
getOldAttachmentStream: async (id, bucket) => { calls.push(['stream', id, bucket]); return stream; },
};
vm.runInNewContext(source.replace(/^import .*;\n/gm, '').replace(/^export /gm, ''), context);
return { read: context.readAuthenticatedLegacyAvatar, calls };
}
test('authenticated legacy avatar streams from the avatars bucket', async () => {
const { read, calls } = load();
assert.equal((await read({}, 'avatar')).avatar._id, 'avatar');
assert.deepEqual(calls, ['auth', ['metadata', 'avatar', 'avatars'], ['stream', 'avatar', 'avatars']]);
});
test('anonymous public-board context cannot unlock legacy avatar metadata', async () => {
const { read, calls } = load({ user: null });
assert.equal(await read({ url: '/avatar?boardId=public' }, 'avatar'), null);
assert.deepEqual(calls, ['auth']);
});
test('missing records and missing binaries remain unavailable', async () => {
const missing = load({ metadata: null });
assert.equal(await missing.read({}, 'avatar'), null);
assert.equal(missing.calls.length, 2);
assert.equal(await load({ stream: null }).read({}, 'avatar'), null);
});
test('both avatar prefix routes perform authenticated legacy fallback before 404', () => {
const server = fs.readFileSync('server/routes/universalFileServer.js', 'utf8');
for (const prefix of ['/cdn/storage/avatars', '/cfs/files/avatars']) {
const start = server.indexOf(`WebApp.handlers.use('${prefix}'`);
const end = server.indexOf('\n });', start);
const route = server.slice(start, end);
assert.match(route, /readAuthenticatedLegacyAvatar\(req, (?:fileId|avatarId)\)/);
// serveFile, not streamFile: since SVG is shown as an image a legacy avatar
// can be one, and serveFile sends that sanitized (server/lib/svgResponse.js).
assert.match(route, /serveFile\(req, res, legacy.stream, legacy.avatar\)/);
assert.ok(route.indexOf('readAuthenticatedLegacyAvatar') < route.indexOf('res.writeHead(404)'));
assert.match(route, /legacy.stream.destroy\(\)/);
}
});