mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
One unified filename subsystem, used everywhere a name is shown, uploaded,
served, or migrated. Design: docs/Features/Filename/Filename.md.
Display (imports/lib/fileNameDisplay.js, general cleanFileName + global
{{cleanFilename}}/{{downloadFilename}} helpers): URL-decode, normalize to
generally-used characters (NFKC + confusable-homoglyph folding so typosquatting
"payp<Cyrillic-a>l.exe" shows as "paypal.exe", only in a Latin-majority base so
genuine non-Latin names are preserved), remove invisible/control/bidi chars,
strip HTML/JS/XML/template markup. Removed the earlier invisible-char filter
button, warning triangle, inline descriptions, legend and server invisibleOnly
filter.
Upload (attachments + avatars, all backends): reject exploit-looking filenames
and EICAR virus test files; strip invisible chars; correct the extension to the
real detected type (double-click opens the right app); type-based name when
empty; cap length to a portable max (30, Amiga OS FFS) preserving the extension;
sanitize SVG JS / XML-loop content at the staging location before promoting to
final storage and delete the temp file after; enough-disk-space check with
small-RAM streaming and stop-and-remove-partial on any write error.
Existing files: general storage-agnostic detector streams a small header to
WRITABLE_PATH/files/temp, runs `file`, deletes the temp file; admin-only bounded
batch extension corrector; serve-time on-the-fly exploit sanitizer wired into
httpStreamOutput (sniffs the start, rewrites only dangerous markup, streams the
rest unchanged).
Migrations (moveToStorage/copyFile): sanitize content, finalize the name with
the same general function (detected type, corrected extension, folded
homoglyphs, capped length), save it at the destination, and number
same-name/different-content collisions (document.pdf, document-1.pdf, ...).
Tests: rewrote fileNameDisplay.test.cjs; added uploadFileName, diskSpace,
fileTypeCorrection and fileHardeningGuards unit tests (positive + negative);
updated Playwright spec 41 to the clean-name model. CHANGELOG + design doc.
64 lines
2.7 KiB
JavaScript
64 lines
2.7 KiB
JavaScript
export const httpStreamOutput = function(readStream, name, http, downloadFlag, cacheControl) {
|
|
// Sanitize known exploits from EXISTING files on the fly, straight from the
|
|
// storage backend, without buffering whole files: the sanitizer sniffs the
|
|
// start of the stream and only rewrites documents that begin like dangerous
|
|
// markup (SVG/XML with <script>, an <!DOCTYPE/<!ENTITY XML loop, inline event
|
|
// handlers, ...); everything else streams through unchanged. See
|
|
// models/lib/serveFileSanitizer.js.
|
|
let outStream = readStream;
|
|
try {
|
|
const { createServeSanitizer } = require('./serveFileSanitizer');
|
|
const sanitizer = createServeSanitizer(name);
|
|
readStream.on('error', err => sanitizer.destroy(err));
|
|
outStream = readStream.pipe(sanitizer);
|
|
} catch (e) {
|
|
console.error('serveFileSanitizer unavailable, streaming file as-is:', e);
|
|
outStream = readStream;
|
|
}
|
|
|
|
outStream.on('data', data => {
|
|
http.response.write(data);
|
|
});
|
|
|
|
outStream.on('end', () => {
|
|
// don't pass parameters to end() or it will be attached to the file's binary stream
|
|
http.response.end();
|
|
});
|
|
|
|
outStream.on('error', (err) => {
|
|
console.error(`Download stream error for file '${name}':`, err);
|
|
http.response.statusCode = 404;
|
|
http.response.end('not found');
|
|
});
|
|
|
|
if (cacheControl) {
|
|
http.response.setHeader('Cache-Control', cacheControl);
|
|
}
|
|
|
|
// Set Content-Disposition header
|
|
http.response.setHeader('Content-Disposition', getContentDisposition(name, http?.params?.query?.download));
|
|
|
|
// Add security headers to prevent XSS attacks
|
|
const isSvgFile = name && name.toLowerCase().endsWith('.svg');
|
|
if (isSvgFile) {
|
|
// For SVG files, add strict CSP to prevent script execution
|
|
http.response.setHeader('Content-Security-Policy', "default-src 'none'; script-src 'none'; object-src 'none';");
|
|
http.response.setHeader('X-Content-Type-Options', 'nosniff');
|
|
http.response.setHeader('X-Frame-Options', 'DENY');
|
|
}
|
|
};
|
|
|
|
/** will initiate download, if links are called with ?download="true" queryparam */
|
|
const getContentDisposition = (name, downloadFlag) => {
|
|
// Force attachment disposition for SVG files to prevent XSS attacks
|
|
const isSvgFile = name && name.toLowerCase().endsWith('.svg');
|
|
const forceAttachment = isSvgFile || downloadFlag === 'true';
|
|
const dispositionType = forceAttachment ? 'attachment;' : 'inline;';
|
|
|
|
const encodedName = encodeURIComponent(name);
|
|
const dispositionName = `filename="${encodedName}"; filename=*UTF-8"${encodedName}";`;
|
|
const dispositionEncoding = 'charset=utf-8';
|
|
|
|
return `${dispositionType} ${dispositionName} ${dispositionEncoding}`;
|
|
};
|