Files
wekan/tests/diskSpace.test.cjs
T
Lauri Ojansivu 81dbecfb87 Safe filename handling: display, upload, existing files, migrations
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.
2026-07-20 03:11:07 +03:00

48 lines
1.9 KiB
JavaScript

'use strict';
// Free-disk-space probe used before writing an uploaded file to filesystem storage
// (models/lib/diskSpace.js). Key behaviour: when free space is UNKNOWN the probe
// must return true (proceed with safe chunked streaming), and when it is known it
// must compare against need + margin.
//
// Run: node tests/diskSpace.test.cjs
const assert = require('assert');
const os = require('os');
const { getFreeDiskBytes, hasEnoughDiskSpace } = require('../models/lib/diskSpace.js');
let passed = 0;
function check(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
console.log('diskSpace:');
check('getFreeDiskBytes returns a non-negative number or null for a real dir', () => {
const v = getFreeDiskBytes(os.tmpdir());
assert.ok(v === null || (typeof v === 'number' && v >= 0), 'number>=0 or null');
});
check('getFreeDiskBytes returns null for a missing path (unknown, not a throw)', () => {
assert.strictEqual(getFreeDiskBytes('/no/such/path/xyz-' + process.pid), null);
});
check('hasEnoughDiskSpace: unknown free space -> true (proceed with safe streaming)', () => {
// A missing path is "unknown", so the caller proceeds and relies on error cleanup.
assert.strictEqual(hasEnoughDiskSpace('/no/such/path/xyz-' + process.pid, 10 * 1024 * 1024), true);
});
check('hasEnoughDiskSpace: tiny need on a real dir is enough', () => {
assert.strictEqual(hasEnoughDiskSpace(os.tmpdir(), 1), true);
});
check('hasEnoughDiskSpace: absurd need on a real dir is NOT enough (when knowable)', () => {
const free = getFreeDiskBytes(os.tmpdir());
if (free === null) {
// Platform cannot report free space; the function must then be permissive.
assert.strictEqual(hasEnoughDiskSpace(os.tmpdir(), Number.MAX_SAFE_INTEGER), true);
} else {
assert.strictEqual(hasEnoughDiskSpace(os.tmpdir(), Number.MAX_SAFE_INTEGER), false);
}
});
console.log(`\ndiskSpace: ${passed} checks passed`);