Files
wekan/server/lib/filenameSanitizeLog.js
T
Lauri Ojansivu fdb683892d Log every filename/content sanitization to Admin Panel / Problems
Whenever a filename or file requires sanitization — on upload, migration, the
existing-file corrector, or viewing/serving — record it to the Security event
stream (action:'sanitized'), the companion to the existing action:'blocked'
upload rejections. Each row carries:

- WHEN: the event timestamp.
- WHO: the original uploader (userId), shown as a new clickable username column
  in the Problems event report.
- FOR WHAT REASON: sanitizationReasons() — URL-encoded name, invisible
  characters, typosquatting (look-alike characters), the specific exploit KIND
  (JavaScript code, XML code, XML loop (billion laughs), server-side PHP/ASP,
  template injection, HTML code), wrong file type (.txt -> .png), filename too
  long, or empty-name-generated.
- THE FILENAME: "from" -> "to".
- WHERE: board > swimlane > list > card, plus the board's organization(s) and
  team(s) (server/lib/fileContext.js).

New: classifyExploitKinds() + sanitizationReasons() (isomorphic), the
server-only fileContext resolver, filenameSanitizeLog helpers, catalog keys
file.sanitize / file.content / file.malware, and the uploader column in the
event report. Wired at all fix points (attachments/avatars onAfterUpload,
moveToStorage, correctFileExtensions, httpStreamOutput). EICAR now uses the
file.malware key. Logging is best-effort and never breaks the upload/view path.

Tests: classifyExploitKinds + sanitizationReasons unit tests (positive +
negative) and logging-wiring source guards. CHANGELOG + design doc updated.
2026-07-20 03:11:07 +03:00

58 lines
2.6 KiB
JavaScript
Raw Permalink 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 */ }
}
// 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 {
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 = { logFilenameSanitized, logContentSanitized };