mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -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.
111 lines
5.0 KiB
JavaScript
111 lines
5.0 KiB
JavaScript
'use strict';
|
||
|
||
// Upload-time filename hardening (models/lib/uploadFileName.js):
|
||
// - sanitizeUploadFileName: URL-decode + fold homoglyphs + strip invisible /
|
||
// exploit chars, give a type-based name when empty, correct the extension to
|
||
// the real MIME type, and cap the length for portability (Amiga OS FFS = 30);
|
||
// - filenameLooksLikeExploit: reject HTML/JS/XML/template/traversal/null-byte
|
||
// filenames outright.
|
||
//
|
||
// Run: node tests/uploadFileName.test.cjs
|
||
|
||
const assert = require('assert');
|
||
const path = require('path');
|
||
const Module = require('module');
|
||
|
||
// uploadFileName.js imports the shared display lib via the Meteor-absolute path
|
||
// '/imports/lib/fileNameDisplay'. Map it to the real file so this runs under plain
|
||
// Node (mime-types resolves normally from node_modules).
|
||
const origResolve = Module._resolveFilename;
|
||
Module._resolveFilename = function(request, ...rest) {
|
||
if (request === '/imports/lib/fileNameDisplay') {
|
||
return path.join(__dirname, '..', 'imports', 'lib', 'fileNameDisplay.js');
|
||
}
|
||
return origResolve.call(this, request, ...rest);
|
||
};
|
||
|
||
const {
|
||
MAX_FILENAME_CHARS,
|
||
truncateFilenameChars,
|
||
baseNameForMime,
|
||
extensionForMime,
|
||
filenameLooksLikeExploit,
|
||
sanitizeUploadFileName,
|
||
} = require('../models/lib/uploadFileName.js');
|
||
|
||
let passed = 0;
|
||
function check(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
|
||
|
||
const ZW = '';
|
||
|
||
console.log('uploadFileName:');
|
||
|
||
check('truncateFilenameChars caps length, preserves extension, never splits code points', () => {
|
||
assert.strictEqual(MAX_FILENAME_CHARS, 30);
|
||
assert.strictEqual(truncateFilenameChars('short.pdf'), 'short.pdf');
|
||
const long = truncateFilenameChars('this-is-a-very-long-presentation-name.pptx');
|
||
assert.ok(Array.from(long).length <= 30, 'within 30 code points');
|
||
assert.ok(long.endsWith('.pptx'), 'extension preserved');
|
||
// no extension, just too long -> clipped to 30
|
||
assert.strictEqual(Array.from(truncateFilenameChars('x'.repeat(50))).length, 30);
|
||
});
|
||
|
||
check('extensionForMime / baseNameForMime', () => {
|
||
assert.strictEqual(extensionForMime('application/pdf'), '.pdf');
|
||
assert.strictEqual(extensionForMime('image/png'), '.png');
|
||
assert.strictEqual(extensionForMime('nonsense/unknown'), '');
|
||
assert.strictEqual(baseNameForMime('image/png'), 'image');
|
||
assert.strictEqual(baseNameForMime('application/pdf'), 'document');
|
||
assert.strictEqual(baseNameForMime('application/vnd.ms-excel'), 'spreadsheet');
|
||
assert.strictEqual(baseNameForMime('application/zip'), 'archive');
|
||
});
|
||
|
||
check('sanitizeUploadFileName appends a missing extension', () => {
|
||
assert.strictEqual(sanitizeUploadFileName('report', 'application/pdf'), 'report.pdf');
|
||
});
|
||
|
||
check('sanitizeUploadFileName replaces a WRONG extension', () => {
|
||
assert.strictEqual(sanitizeUploadFileName('report.txt', 'application/pdf'), 'report.pdf');
|
||
});
|
||
|
||
check('sanitizeUploadFileName keeps a correct extension (jpg vs jpeg)', () => {
|
||
assert.strictEqual(sanitizeUploadFileName('photo.jpg', 'image/jpeg'), 'photo.jpg');
|
||
});
|
||
|
||
check('sanitizeUploadFileName generates a name from the type when empty', () => {
|
||
assert.strictEqual(sanitizeUploadFileName('', 'image/png'), 'image.png');
|
||
assert.strictEqual(sanitizeUploadFileName(' ' + ZW + ' ', 'text/plain'), 'document.txt');
|
||
});
|
||
|
||
check('sanitizeUploadFileName strips invisible chars and folds homoglyphs', () => {
|
||
assert.strictEqual(sanitizeUploadFileName('evil' + ZW, 'application/pdf'), 'evil.pdf');
|
||
assert.strictEqual(sanitizeUploadFileName('pаypal', 'application/pdf'), 'paypal.pdf'); // Cyrillic a
|
||
});
|
||
|
||
check('sanitizeUploadFileName caps the final length', () => {
|
||
const out = sanitizeUploadFileName('a'.repeat(60), 'application/pdf');
|
||
assert.ok(Array.from(out).length <= 30 && out.endsWith('.pdf'));
|
||
});
|
||
|
||
check('filenameLooksLikeExploit accepts normal names', () => {
|
||
assert.strictEqual(filenameLooksLikeExploit('report.pdf'), false);
|
||
assert.strictEqual(filenameLooksLikeExploit('Photo 2024.jpg'), false);
|
||
assert.strictEqual(filenameLooksLikeExploit('Отчёт.pdf'), false);
|
||
});
|
||
|
||
check('filenameLooksLikeExploit rejects exploit-looking names (negative tests)', () => {
|
||
assert.strictEqual(filenameLooksLikeExploit('<script>alert(1)</script>.png'), true);
|
||
assert.strictEqual(filenameLooksLikeExploit('<svg onload=alert(1)>.svg'), true);
|
||
assert.strictEqual(filenameLooksLikeExploit('<?xml version="1.0"?>.svg'), true);
|
||
assert.strictEqual(filenameLooksLikeExploit('x<!DOCTYPE y>.xml'), true);
|
||
assert.strictEqual(filenameLooksLikeExploit('x<!ENTITY y>.xml'), true);
|
||
assert.strictEqual(filenameLooksLikeExploit('report{{7*7}}.pdf'), true);
|
||
assert.strictEqual(filenameLooksLikeExploit('a${x}.pdf'), true);
|
||
assert.strictEqual(filenameLooksLikeExploit('javascript:alert(1).png'), true);
|
||
assert.strictEqual(filenameLooksLikeExploit('onerror=alert(1).png'), true);
|
||
assert.strictEqual(filenameLooksLikeExploit('shell.php\u0000.jpg'), true); // null byte
|
||
assert.strictEqual(filenameLooksLikeExploit('../../etc/passwd'), true); // path traversal
|
||
});
|
||
|
||
console.log(`\nuploadFileName: ${passed} checks passed`);
|