Files
wekan/server/lib/fileContext.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

72 lines
2.9 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';
// Resolve the full human-readable context of a stored file for the Admin Panel →
// Problems log: WHO originally uploaded it, WHEN, and WHERE (board › swimlane ›
// list › card), plus the board's organization(s) and team(s). Best-effort and
// server-only — any lookup that fails is simply omitted, never thrown.
const { ReactiveCache } = require('/imports/reactiveCache');
async function resolveFileContext(fileObj) {
const meta = (fileObj && fileObj.meta) || {};
const ctx = {
fileName: fileObj && fileObj.name,
uploaderUserId: fileObj && fileObj.userId,
uploadedAt: (fileObj && (fileObj.uploadedAtOstrio || fileObj.updatedAt || fileObj.uploadedAt)) || null,
boardId: meta.boardId,
cardId: meta.cardId,
};
try {
if (ctx.uploaderUserId) {
const u = await ReactiveCache.getUser(ctx.uploaderUserId);
ctx.uploaderName = (u && u.username) || ctx.uploaderUserId;
}
if (meta.boardId) {
const b = await ReactiveCache.getBoard(meta.boardId);
if (b) {
ctx.boardTitle = b.title;
ctx.orgNames = (b.orgs || []).map(o => o.orgDisplayName || o.orgId).filter(Boolean);
ctx.teamNames = (b.teams || []).map(t => t.teamDisplayName || t.teamId).filter(Boolean);
}
}
if (meta.swimlaneId) {
const s = await ReactiveCache.getSwimlane(meta.swimlaneId);
if (s) ctx.swimlaneTitle = s.title;
}
if (meta.listId) {
const l = await ReactiveCache.getList(meta.listId);
if (l) ctx.listTitle = l.title;
}
if (meta.cardId) {
const c = await ReactiveCache.getCard(meta.cardId);
if (c) ctx.cardTitle = c.title;
}
} catch (e) { /* best effort — omit whatever could not be resolved */ }
return ctx;
}
// Format the resolved context as a compact one-line string for the log detail:
// uploaded by alice on 2026-07-01 12:00:00 | board "Marketing" › swimlane "Q3" ›
// list "Todo" › card "Launch" | org "Acme" | team "Web"
function formatFileContext(ctx) {
if (!ctx) return '';
const parts = [];
if (ctx.uploaderName) parts.push(`uploaded by ${ctx.uploaderName}`);
if (ctx.uploadedAt) {
try {
parts.push(`on ${new Date(ctx.uploadedAt).toISOString().replace('T', ' ').slice(0, 19)}`);
} catch (e) { /* ignore bad date */ }
}
const loc = [];
if (ctx.boardTitle) loc.push(`board "${ctx.boardTitle}"`);
if (ctx.swimlaneTitle) loc.push(`swimlane "${ctx.swimlaneTitle}"`);
if (ctx.listTitle) loc.push(`list "${ctx.listTitle}"`);
if (ctx.cardTitle) loc.push(`card "${ctx.cardTitle}"`);
if (loc.length) parts.push(loc.join(' › '));
if (ctx.orgNames && ctx.orgNames.length) parts.push('org ' + ctx.orgNames.map(n => `"${n}"`).join(', '));
if (ctx.teamNames && ctx.teamNames.length) parts.push('team ' + ctx.teamNames.map(n => `"${n}"`).join(', '));
return parts.join(' | ');
}
module.exports = { resolveFileContext, formatFileContext };