mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
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 !
56 lines
2.5 KiB
JavaScript
56 lines
2.5 KiB
JavaScript
'use strict';
|
|
|
|
// General "sanitize known exploits from an uploaded file at its staging location,
|
|
// BEFORE it is promoted to the default storage (filesystem final location, S3,
|
|
// GridFS, ...)". Centralizes the exploit-removal step so every upload path
|
|
// (attachments, avatars) runs the same sanitization in the same place.
|
|
//
|
|
// Today this strips JavaScript (<script>, event handlers, javascript: URIs,
|
|
// embedded HTML) and XML-loop / billion-laughs DOCTYPE/ENTITY constructs from SVG
|
|
// uploads, in place, so the image still uploads but cannot execute script or
|
|
// expand entities. The file's on-disk bytes at version.path are rewritten while it
|
|
// is still on the local filesystem staging area; only after this returns should
|
|
// the caller move the file to its final storage backend.
|
|
|
|
const { isSvgFile, sanitizeSvgFile } = require('./sanitizeSvg');
|
|
|
|
// Sanitize known exploits from every version of fileObj in place. Resolves true
|
|
// if any version's bytes were modified (so the caller can persist the new
|
|
// sizes). Streaming, so an SVG of any size is sanitized whole.
|
|
async function sanitizeUploadedFileExploits(fileObj) {
|
|
if (!fileObj || !fileObj.versions) return false;
|
|
let changed = false;
|
|
|
|
// SVG: remove script / event handlers / javascript: URIs / XML DOCTYPE/ENTITY.
|
|
if (isSvgFile(fileObj.type, fileObj.name)) {
|
|
for (const versionName of Object.keys(fileObj.versions)) {
|
|
const version = fileObj.versions[versionName];
|
|
const newSize = await sanitizeSvgFile(version && version.path);
|
|
if (newSize !== null) {
|
|
version.size = newSize;
|
|
changed = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Active markup had to be REMOVED from an uploaded file - a script, an event
|
|
// handler, a javascript: URI inside an SVG. Nothing a drawing program produces
|
|
// needs any of it, so this is a stored-XSS attempt that was defused rather than
|
|
// a file that needed tidying, and it trips a canary with the uploader attached
|
|
// (docs/Security/Remediation/WeKan.md §12.6). The sanitized file is stored
|
|
// exactly as before; the uploader is told nothing.
|
|
if (changed) {
|
|
try {
|
|
const { tripCanary } = require('/server/lib/canary');
|
|
tripCanary('sanitize.dangerous-content', {
|
|
userId: (fileObj.userId || (fileObj.meta && fileObj.meta.userId)) || undefined,
|
|
detail: 'active markup removed from an uploaded ' + (fileObj.type || 'file'),
|
|
});
|
|
} catch (e) { /* a canary must never break an upload */ }
|
|
}
|
|
|
|
return changed;
|
|
}
|
|
|
|
module.exports = { sanitizeUploadedFileExploits };
|