Files
wekan/server/routes/avatarServer.js
T
Lauri Ojansivu e748559838 SVG of any size is sanitized whole, streaming: on upload, on validation and on every serve.
A streaming tokenizer (models/lib/svgSanitizer.js) replaces the regular
expressions that read an SVG whole: it removes script, foreignObject,
iframe/object/embed, SMIL set/animate, event handlers, javascript:/vbscript:
URIs after decoding character references, dangerous CSS, DOCTYPE/ENTITY XML
loops and processing instructions, wherever they are and however the file is
split into chunks, with bounded memory. UTF-16 is decoded and checked.

Upload sanitizing streams to a temporary file and renames it; validation
checks the whole SVG instead of rejecting any over its 1 MB scan; serving
pipes through the sanitizer with no 8 MB fallback to an unsanitized
download.

Thanks to xet7 !
2026-10-25 07:23:31 +02:00

292 lines
11 KiB
JavaScript

/**
* Avatar File Server
* Handles serving avatar files from the /cdn/storage/avatars/ path
*/
import { Meteor } from 'meteor/meteor';
import { WebApp } from 'meteor/webapp';
import { getUserIdFromRequest, parseQuery } from '/server/lib/requestUser';
import { ReactiveCache } from '/imports/reactiveCache';
import Avatars from '/models/avatars';
import { fileStoreStrategyFactory } from '/models/avatars.server';
import { getOldAttachmentData, getOldAttachmentStream } from '/models/lib/attachmentBackwardCompatibility';
const { fileResponsePolicy } = require('/models/lib/fileResponseSafety');
// An SVG avatar is streamed, sanitized, as an image (server/lib/svgResponse.js).
const svgResponse = require('/server/lib/svgResponse');
function sendSvgAvatar(res, stream, fileObj) {
return svgResponse.sendInlineSvg(res, stream, {
fileName: fileObj.name,
onSanitized: text => {
try {
const { classifyExploitKinds } = require('/imports/lib/fileNameDisplay');
require('/server/lib/filenameSanitizeLog').logContentSanitized({
fileObj, source: 'fileView', kinds: classifyExploitKinds(text),
});
} catch (e) { /* logging must never break serving */ }
},
}).catch(() => { if (!res.headersSent) { res.writeHead(500); res.end(); } });
}
// CacheBleed (GHSA-w3qg-pf27-g68r): an avatar is served after an access check.
const { setPrivateFileCacheHeaders } = require('/models/lib/fileCacheHeaders');
function setAvatarResponseHeaders(res, type) {
const policy = fileResponsePolicy(type || 'image/jpeg');
res.setHeader('Content-Type', policy.contentType);
for (const [name, value] of Object.entries(policy.headers)) {
res.setHeader(name, value);
}
if (policy.forceDownload) {
res.setHeader('Content-Disposition', 'attachment');
}
}
// Serve a legacy CollectionFS avatar (cfs.avatars.filerecord + cfs_gridfs.avatars
// bucket) in place, without migrating it. Returns true when it handled the
// response, false when there is no such legacy avatar.
async function serveLegacyAvatar(fileId, req, res) {
const legacy = await getOldAttachmentData(fileId, 'avatars');
if (!legacy) {
return false;
}
const stream = await getOldAttachmentStream(fileId, 'avatars');
if (!stream) {
return false;
}
setAvatarResponseHeaders(res, legacy.type);
if (legacy.size) res.setHeader('Content-Length', legacy.size);
setPrivateFileCacheHeaders(res);
res.setHeader('ETag', `"${legacy._id}"`);
const ifNoneMatch = req.headers['if-none-match'];
if (ifNoneMatch && ifNoneMatch === `"${legacy._id}"`) {
res.writeHead(304);
res.end();
return true;
}
if (svgResponse.isSvg(legacy.type, legacy.name)) {
sendSvgAvatar(res, stream, legacy);
return true;
}
res.writeHead(200);
stream.pipe(res);
stream.on('error', (error) => {
console.error('Legacy avatar stream error:', error);
if (!res.headersSent) {
res.writeHead(500);
res.end('Error reading avatar file');
}
});
return true;
}
// Advisory: "serveLegacyAvatar Serves Legacy CollectionFS Avatars Without Any
// Authentication" - both call sites below streamed a legacy avatar to any
// anonymous caller who knew (or guessed) its old cfs.avatars.filerecord _id,
// entirely ahead of - or, on /cfs/files/avatars, with no equivalent at all
// to - the getUserIdFromRequest/avatarIsOnAPublicBoard check current avatars
// already get. Legacy records carry no owner/board link we can safely check
// (unlike current avatars, whose avatarIsOnAPublicBoard exemption needs
// avatar.userId), so this requires an authenticated caller with no
// anonymous/public-board bypass - a deliberately narrower rule than current
// avatars, not the same one, because it is the only one that can be applied
// without trusting an unverifiable claim about who a legacy file belonged to.
async function isLegacyAvatarAuthorized(req) {
const userId = await getUserIdFromRequest(req);
return !!userId;
}
// May a caller who is NOT signed in see this avatar?
//
// Only on a public board, and only for somebody who is on it. The client appends
// ?boardId= to every avatar URL it renders for exactly this case - see the
// avatarUrl helper in client/components/users/userAvatar.js, whose own comment
// says "so public viewers can access avatars on public boards" - and this route
// ignored the parameter, so a public board showed the missing-picture icon to
// every visitor who was not logged in.
//
// The board has to be named AND public AND actually have the avatar's owner on
// it. Without that last part, naming any public board would unlock any avatar on
// the instance, which is not what a public board publishes.
async function avatarIsOnAPublicBoard(req, avatar) {
const boardId = parseQuery(req).boardId;
if (!boardId || !avatar || !avatar.userId) {
return false;
}
const board = await ReactiveCache.getBoard(boardId);
return !!(board && board.isPublic() && board.hasMember(avatar.userId));
}
// Handle avatar file downloads
WebApp.handlers.use('/cdn/storage/avatars/:fileName', async (req, res, next) => {
if (req.method !== 'GET') {
return next();
}
try {
const fileName = req.params.fileName;
if (!fileName) {
res.writeHead(400);
res.end('Invalid avatar file name');
return;
}
// Extract file ID from filename (format: fileId-original-filename)
const fileId = fileName.split('-original-')[0];
if (!fileId) {
res.writeHead(400);
res.end('Invalid avatar file format');
return;
}
// Get avatar file from database
const avatar = await ReactiveCache.getAvatar(fileId);
if (!avatar) {
// Fall back to a legacy CollectionFS avatar (read in place) - only for
// an authenticated caller (isLegacyAvatarAuthorized above), so an
// anonymous request never learns whether the id exists at all.
if (!(await isLegacyAvatarAuthorized(req))) {
// An attempt: no current WeKan avatar exists for this id, so an
// anonymous request here is either probing for a legacy id or
// guessing one - what an attempt against Advisory "serveLegacyAvatar
// Serves Legacy CollectionFS Avatars Without Any Authentication"
// looks like now that the check exists.
try {
require('/server/lib/securityLog').record({
key: 'authz.legacy-avatar',
action: 'blocked',
source: '/cdn/storage/avatars/:fileName',
detail: 'refused an unauthenticated legacy-avatar fallback request',
});
} catch (e) { /* logging must never break the guard */ }
res.writeHead(401);
res.end('Authentication required');
return;
}
if (await serveLegacyAvatar(fileId, req, res)) {
return;
}
res.writeHead(404);
res.end('Avatar not found');
return;
}
// Check if user has permission to view this avatar
// For avatars, we allow viewing by any logged-in user.
//
// Resolved FROM THE REQUEST. This was `Meteor.userId()`, which reads the
// current DDP invocation's environment - it exists in a method or a
// publication and not in a WebApp handler, where it THROWS
// ("Meteor.userId can only be invoked in method calls or publications").
// The catch at the bottom of this handler turned that into a 500, so no
// avatar served through this route ever reached anybody, and it looked like
// a broken image rather than broken authentication. That is what a 6.x
// upgrade shows as lost profile pictures: `/cfs/files/avatars/<id>` is
// redirected here, and here is where it died - the migrated file itself was
// fine.
const userId = await getUserIdFromRequest(req);
if (!userId && !(await avatarIsOnAPublicBoard(req, avatar))) {
res.writeHead(401);
res.end('Authentication required');
return;
}
// Get file strategy
const strategy = fileStoreStrategyFactory.getFileStrategy(avatar, 'original');
const readStream = strategy.getReadStream();
if (!readStream) {
res.writeHead(404);
res.end('Avatar file not found in storage');
return;
}
// Set appropriate headers
setAvatarResponseHeaders(res, avatar.type);
res.setHeader('Content-Length', avatar.size || 0);
setPrivateFileCacheHeaders(res);
res.setHeader('ETag', `"${avatar._id}"`);
// Handle conditional requests
const ifNoneMatch = req.headers['if-none-match'];
if (ifNoneMatch && ifNoneMatch === `"${avatar._id}"`) {
res.writeHead(304);
res.end();
return;
}
// Stream the file - an SVG sanitized, as an image.
if (svgResponse.isSvg(avatar.type, avatar.name)) {
sendSvgAvatar(res, readStream, avatar);
return;
}
res.writeHead(200);
readStream.pipe(res);
readStream.on('error', (error) => {
console.error('Avatar stream error:', error);
if (!res.headersSent) {
res.writeHead(500);
res.end('Error reading avatar file');
}
});
} catch (error) {
console.error('Avatar server error:', error);
if (!res.headersSent) {
res.writeHead(500);
res.end('Internal server error');
}
}
});
// Handle legacy avatar URLs (from CollectionFS). user.profile.avatarUrl for
// migrated-from-6.x installs is '/cfs/files/avatars/<filerecordId>', so serve
// the CollectionFS binary in place. If it isn't a legacy avatar, fall back to
// redirecting to the new URL format (e.g. for already-migrated avatars).
WebApp.handlers.use('/cfs/files/avatars/:fileName', async (req, res, next) => {
if (req.method !== 'GET') {
return next();
}
try {
const fileName = req.params.fileName;
// The legacy URL carries the filerecord _id directly; tolerate the
// '<id>-original-<name>' form too.
const fileId = fileName.split('-original-')[0] || fileName;
// Only an authenticated caller may read the legacy store directly; an
// anonymous one falls through to the redirect below, which is itself
// authorized (current avatars get the getUserIdFromRequest/
// avatarIsOnAPublicBoard check on /cdn/storage/avatars) - so a public
// board's already-migrated avatars still work for anonymous viewers,
// and only the un-migrated legacy path requires a login.
if (await isLegacyAvatarAuthorized(req)) {
if (await serveLegacyAvatar(fileId, req, res)) {
return;
}
}
// Not a legacy avatar — redirect to the new avatar URL format, KEEPING the
// query string. A redirect that drops it drops ?boardId=, which is how a
// visitor who is not signed in is allowed to see an avatar on a public board
// at all - and this is the redirect every migrated 6.x avatar URL goes
// through, so losing it there would 401 exactly the installs this fixes.
const query = (req.url || '').split('?')[1];
const newUrl = `/cdn/storage/avatars/${fileName}${query ? `?${query}` : ''}`;
res.writeHead(301, { 'Location': newUrl });
res.end();
} catch (error) {
console.error('Legacy avatar serve error:', error);
if (!res.headersSent) {
res.writeHead(500);
res.end('Internal server error');
}
}
});
console.log('Avatar server routes initialized');