Files
wekan/server/lib/filenameSanitizeLog.js
T
Lauri Ojansivu c7c541ccc5 FileNameBleed: a percent-encoded name is not an exploit, and opening a file is not an upload.
Admin Panel / Problems / Security showed FileNameBleed, "uploaded a filename
carrying an exploit pattern; URL-encoded name", for a user who had only
opened a board of 2019 photos with Cyrillic names. Reading each file
repaired its stored percent-encoded name (the FileBleed "fileRead" rows,
correct and recorded once per file). The repair's reasons were then passed to
the upload canary, where "URL-encoded name" counted as dangerous. Browsers and
older WeKan percent-encode every non-ASCII name, so every such name tripped it.

- "URL-encoded name" is no longer a dangerous reason.
- What an encoding could hide is named from the decoded name instead:
  sanitizationReasons() now reports "path traversal" and "null byte". Before,
  "URL-encoded name" was the only thing an encoded "../../etc/passwd" tripped;
  it now trips the path-traversal canary under its own name, which
  DANGEROUS_REASON already listed but no reason ever produced.
- A name repaired while a file is read (source fileRead) never trips the upload
  canary: the repair is still recorded, but it is not an attempt.

tests/filenameCanaryReasons.test.cjs loads the real module, with its server
dependencies stubbed:
- the Cyrillic name trips nothing;
- negatives: an encoded traversal and an encoded null byte still trip, and
  markup in a name uploaded now still trips;
- a read-time repair is recorded without a canary.

Thanks to xet7 !
2026-10-20 10:23:16 +03:00

98 lines
4.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict';
// Log filename / file-content SANITIZATION events (not just blocks) to the Admin
// Panel → Problems → Security stream, so an admin can see EVERY time a filename or
// file required sanitization, FOR WHAT REASON (which exploit kind, wrong file type,
// typosquatting, too long, …), WHO originally uploaded it, WHEN, and WHERE it was
// uploaded (board › swimlane › list › card, organization and team).
//
// These are the `action:'sanitized'` companions to the `action:'blocked'` events
// already recorded by models/fileValidation.js. The uploader is stored as `userId`
// (shown, clickable, in the report) and the full context is appended to `detail`.
// Best-effort and server-only: never throws into the upload/migration/view path.
const { resolveFileContext, formatFileContext } = require('/server/lib/fileContext');
function record(evt) {
try {
require('/server/lib/securityLog').record(evt);
} catch (e) { /* logging must never break the caller */ }
}
// Sanitizing a filename is routine - names get trimmed, unicode gets normalised,
// an extension gets corrected - and routine is not worth an admin's attention.
// What IS worth it is a name that carried an EXPLOIT: markup, a shell
// metacharacter, a path trying to leave its directory. Those reasons, and only
// those, also trip a canary, so the Security report separates "somebody uploaded
// a file with an awkward name" from "somebody is probing the upload path"
// (docs/Security/Remediation/WeKan.md §12.6).
//
// "URL-encoded name" is not one of them. Browsers and older WeKan percent-encode
// any non-ASCII name, so every Cyrillic, Chinese or Finnish filename had it, and
// opening a board of 2019 photos tripped "uploaded a filename carrying an exploit
// pattern". What an encoding could hide is found anyway: the exploit, path
// traversal, null-byte and invisible-character reasons are worked out from the
// DECODED name (models/lib/uploadFileName.js sanitizationReasons).
const DANGEROUS_REASON = /^exploit: |traversal|null byte|invisible characters/i;
// The canary is about somebody probing the UPLOAD path now. A name repaired
// while a file is read (source 'fileRead') was uploaded years ago - the
// repair is recorded, but it is not an attempt, and the reader did nothing.
const NOT_AN_UPLOAD = new Set(['fileRead']);
function canaryForReasons(reasons, fileObj, source) {
try {
if (NOT_AN_UPLOAD.has(source)) return;
const list = Array.isArray(reasons) ? reasons : [];
const dangerous = list.filter(r => typeof r === 'string' && DANGEROUS_REASON.test(r));
if (!dangerous.length) return;
const traversal = dangerous.some(r => /traversal/i.test(r));
const { tripCanary } = require('/server/lib/canary');
tripCanary(traversal ? 'sanitize.path-traversal' : 'sanitize.dangerous-filename', {
userId: (fileObj && (fileObj.userId || (fileObj.meta && fileObj.meta.userId))) || undefined,
// The REASONS, which are a short fixed vocabulary - never the filename,
// which is the attacker's own text.
detail: dangerous.slice(0, 4).join(', '),
});
} catch (e) { /* a canary must never break an upload */ }
}
// Record that a stored filename was sanitized. `reasons` is the array from
// uploadFileName.sanitizationReasons(); `fileObj` supplies the uploader + location.
async function logFilenameSanitized({ fileObj, source, reasons, from, to }) {
try {
canaryForReasons(reasons, fileObj, source);
const why = Array.isArray(reasons) && reasons.length ? reasons.join('; ') : 'normalized';
let context = '';
try { context = formatFileContext(await resolveFileContext(fileObj)); } catch (e) { /* omit */ }
record({
key: 'file.sanitize',
action: 'sanitized',
source: source || 'fileUpload',
userId: (fileObj && fileObj.userId) || undefined,
detail: `${why}: "${from}" → "${to}"${context ? ' | ' + context : ''}`,
});
} catch (e) { /* never throw */ }
}
// Record that exploits were removed from a file's CONTENT (e.g. JavaScript / XML
// loop stripped from an SVG). `kinds` is the array from classifyExploitKinds().
async function logContentSanitized({ fileObj, source, kinds }) {
try {
const what = Array.isArray(kinds) && kinds.length ? kinds.join(', ') : 'active content (JavaScript / XML)';
const name = (fileObj && fileObj.name) || '';
let context = '';
try { context = formatFileContext(await resolveFileContext(fileObj)); } catch (e) { /* omit */ }
record({
key: 'file.content',
action: 'sanitized',
source: source || 'fileUpload',
userId: (fileObj && fileObj.userId) || undefined,
detail: `removed ${what} from file content "${name}"${context ? ' | ' + context : ''}`,
});
} catch (e) { /* never throw */ }
}
module.exports = { canaryForReasons, DANGEROUS_REASON, logFilenameSanitized, logContentSanitized };