mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
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.
86 lines
3.5 KiB
JavaScript
86 lines
3.5 KiB
JavaScript
import { Meteor } from 'meteor/meteor';
|
|
import { check, Match } from 'meteor/check';
|
|
import { ReactiveCache } from '/imports/reactiveCache';
|
|
import Attachments from '/models/attachments';
|
|
import { fileStoreStrategyFactory as attachmentsFactory } from '/models/attachments.server';
|
|
import { fileStoreStrategyFactory as avatarsFactory } from '/models/avatars.server';
|
|
import { rename } from '/models/lib/fileStoreStrategy';
|
|
import { correctedNameForStoredFile } from '/models/lib/fileTypeCorrection';
|
|
|
|
// Correct the extensions of ALREADY-uploaded files.
|
|
//
|
|
// New uploads are corrected in the onAfterUpload hooks; this method fixes files
|
|
// that were stored BEFORE that hook existed (the field problem: a file saved with
|
|
// the wrong/missing extension opens the wrong app on double-click). It reuses the
|
|
// general, storage-agnostic detector (fileTypeCorrection.js) which streams only a
|
|
// small header of each file to WRITABLE_PATH/files/temp, detects the type with the
|
|
// `file` command, then deletes the temp file — so it works for filesystem, GridFS
|
|
// and cloud storage without loading whole files into RAM.
|
|
//
|
|
// Admin-only and BOUNDED (processes at most `limit` files per call) so it never
|
|
// stalls a large instance; call repeatedly until `remaining` is 0.
|
|
|
|
async function correctBatch(collection, factory, limit) {
|
|
const result = { checked: 0, corrected: 0, errors: 0, examples: [] };
|
|
// Read directly from the collection so this works for both Attachments and Avatars.
|
|
const cursor = collection.find({}, { limit });
|
|
const docs = await cursor.fetchAsync();
|
|
|
|
for (const fileObj of docs) {
|
|
result.checked++;
|
|
try {
|
|
const { name, changed, detectedMime } = await correctedNameForStoredFile(fileObj, factory);
|
|
if (changed && name) {
|
|
const from = fileObj.name;
|
|
rename(fileObj, name, factory);
|
|
result.corrected++;
|
|
if (result.examples.length < 20) {
|
|
result.examples.push({ from, to: name });
|
|
}
|
|
// Log to Admin Panel / Problems: who uploaded, why, when, where.
|
|
try {
|
|
const { sanitizationReasons } = require('/models/lib/uploadFileName');
|
|
await require('/server/lib/filenameSanitizeLog').logFilenameSanitized({
|
|
fileObj, source: 'existingFileCorrection',
|
|
reasons: sanitizationReasons(from, detectedMime || fileObj.type, name),
|
|
from, to: name,
|
|
});
|
|
} catch (e) { /* best effort */ }
|
|
}
|
|
} catch (error) {
|
|
result.errors++;
|
|
console.error('[correctFileExtensions] failed for', fileObj && fileObj._id, error);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
Meteor.methods({
|
|
// Correct up to `limit` attachment (and optionally avatar) extensions. Returns
|
|
// per-collection counts. Admin only.
|
|
async 'correctFileExtensions.run'(limit = 200, includeAvatars = true) {
|
|
check(limit, Match.Optional(Number));
|
|
check(includeAvatars, Match.Optional(Boolean));
|
|
|
|
const user = await ReactiveCache.getCurrentUser();
|
|
if (!(user && user.isAdmin)) {
|
|
throw new Meteor.Error('not-authorized', 'Only admins can correct file extensions');
|
|
}
|
|
|
|
const cap = Math.max(1, Math.min(1000, Number(limit) || 200));
|
|
const attachments = await correctBatch(Attachments, attachmentsFactory, cap);
|
|
|
|
let avatars = null;
|
|
if (includeAvatars) {
|
|
try {
|
|
const Avatars = require('/models/avatars').default;
|
|
avatars = await correctBatch(Avatars, avatarsFactory, cap);
|
|
} catch (e) {
|
|
console.error('[correctFileExtensions] avatars pass failed:', e);
|
|
}
|
|
}
|
|
|
|
return { attachments, avatars };
|
|
},
|
|
});
|