mirror of
https://github.com/wekan/wekan.git
synced 2026-11-25 10:48:06 -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.
223 lines
8.5 KiB
JavaScript
223 lines
8.5 KiB
JavaScript
import { ReactiveCache } from '/imports/reactiveCache';
|
|
import { Meteor } from 'meteor/meteor';
|
|
import { isFileValid } from './fileValidation';
|
|
import { createBucket } from './lib/grid/createBucket';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import FileStoreStrategyFactory, { FileStoreStrategyFilesystem, FileStoreStrategyGridFs, FileStoreStrategyCloud, STORAGE_NAME_FILESYSTEM, rename } from '/models/lib/fileStoreStrategy';
|
|
import { generateUniversalAvatarUrl } from '/models/lib/universalUrlGenerator';
|
|
import Avatars, { normalizeRemovedFiles, setAvatarsUploadSize } from './avatars';
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Server-only configuration
|
|
// ---------------------------------------------------------------------------
|
|
|
|
let avatarsUploadExternalProgram;
|
|
let avatarsUploadMimeTypes = [];
|
|
let avatarsUploadSize = 72000;
|
|
|
|
if (process.env.AVATARS_UPLOAD_MIME_TYPES) {
|
|
avatarsUploadMimeTypes = process.env.AVATARS_UPLOAD_MIME_TYPES.split(',');
|
|
avatarsUploadMimeTypes = avatarsUploadMimeTypes.map(value => value.trim());
|
|
}
|
|
|
|
if (process.env.AVATARS_UPLOAD_MAX_SIZE) {
|
|
const avatarsUploadSize_ = parseInt(process.env.AVATARS_UPLOAD_MAX_SIZE);
|
|
|
|
if (typeof avatarsUploadSize_ === 'number' && avatarsUploadSize_ > 0) {
|
|
avatarsUploadSize = avatarsUploadSize_;
|
|
// Sync the upload size to the shared module for onBeforeUpload validation
|
|
setAvatarsUploadSize(avatarsUploadSize);
|
|
}
|
|
}
|
|
|
|
if (process.env.AVATARS_UPLOAD_EXTERNAL_PROGRAM) {
|
|
avatarsUploadExternalProgram = process.env.AVATARS_UPLOAD_EXTERNAL_PROGRAM;
|
|
|
|
if (!avatarsUploadExternalProgram.includes("{file}")) {
|
|
avatarsUploadExternalProgram = undefined;
|
|
}
|
|
}
|
|
|
|
const avatarsBucket = createBucket('avatars');
|
|
|
|
// Compute storage path:
|
|
// - Docker (WRITABLE_PATH=/data): /data/files/avatars
|
|
// - Snap (WRITABLE_PATH=$SNAP_COMMON/files): $SNAP_COMMON/files/avatars
|
|
const basePath = process.env.WRITABLE_PATH || process.cwd();
|
|
const endsWithFiles = basePath.endsWith('/files') || basePath.endsWith('\\files');
|
|
const storagePath = endsWithFiles
|
|
? path.join(basePath, 'avatars')
|
|
: path.join(basePath, 'files', 'avatars');
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// File store strategy factory
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export const fileStoreStrategyFactory = new FileStoreStrategyFactory(
|
|
FileStoreStrategyFilesystem, storagePath,
|
|
FileStoreStrategyGridFs, avatarsBucket,
|
|
FileStoreStrategyCloud,
|
|
Avatars,
|
|
);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Assign server-only FilesCollection callbacks
|
|
// ---------------------------------------------------------------------------
|
|
|
|
Avatars.storagePath = function () {
|
|
return fileStoreStrategyFactory.storagePath;
|
|
};
|
|
|
|
Avatars.onAfterUpload = async function (fileObj) {
|
|
// current storage is the filesystem, update object and database
|
|
Object.keys(fileObj.versions).forEach(versionName => {
|
|
fileObj.versions[versionName].storage = STORAGE_NAME_FILESYSTEM;
|
|
});
|
|
|
|
// Detect and sanitize known exploits from the uploaded avatar IN PLACE, on the
|
|
// local filesystem staging area and BEFORE validation or any move to final
|
|
// storage: strip scripts, event handlers, javascript: URIs and XML
|
|
// DOCTYPE/ENTITY (XML loop) constructs from SVG avatars, so safe SVGs are
|
|
// accepted instead of rejected. Same general function used for attachments.
|
|
const { sanitizeUploadedFileExploits } = require('./lib/sanitizeUploadedFile');
|
|
if (sanitizeUploadedFileExploits(fileObj)) {
|
|
try {
|
|
await require('/server/lib/filenameSanitizeLog').logContentSanitized({
|
|
fileObj, source: 'avatarUpload',
|
|
});
|
|
} catch (e) { /* best effort */ }
|
|
}
|
|
|
|
await Avatars.updateAsync({ _id: fileObj._id }, { $set: { "versions": fileObj.versions } });
|
|
|
|
const isValid = await isFileValid(fileObj, avatarsUploadMimeTypes, avatarsUploadSize, avatarsUploadExternalProgram);
|
|
|
|
if (isValid) {
|
|
// Normalize + correct the stored avatar filename: URL-decode, fold confusable
|
|
// homoglyphs, remove invisible characters, strip exploit markup, and fix the
|
|
// extension to match the real detected file type — capped to a portable
|
|
// length. (isFileValid already rejected exploit-looking filenames above.)
|
|
try {
|
|
const { sanitizeUploadFileName, sanitizationReasons } = require('./lib/uploadFileName');
|
|
const { detectStoredFileMime } = require('./lib/fileTypeCorrection');
|
|
const detectedMime = await detectStoredFileMime(fileObj, fileStoreStrategyFactory);
|
|
const originalName = fileObj.name;
|
|
const correctedName = sanitizeUploadFileName(originalName, detectedMime || fileObj.type);
|
|
if (correctedName && correctedName !== originalName) {
|
|
rename(fileObj, correctedName, fileStoreStrategyFactory);
|
|
fileObj.name = correctedName;
|
|
try {
|
|
await require('/server/lib/filenameSanitizeLog').logFilenameSanitized({
|
|
fileObj, source: 'avatarUpload',
|
|
reasons: sanitizationReasons(originalName, detectedMime || fileObj.type, correctedName),
|
|
from: originalName, to: correctedName,
|
|
});
|
|
} catch (e) { /* best effort */ }
|
|
}
|
|
} catch (error) {
|
|
console.error('[Avatars.onAfterUpload] filename hardening failed:', error);
|
|
}
|
|
|
|
// Set avatar URL using universal URL generator (URL-agnostic)
|
|
const universalUrl = generateUniversalAvatarUrl(fileObj._id);
|
|
|
|
// Check if this is an admin uploading for another user
|
|
let targetUserId = fileObj.userId;
|
|
if (fileObj.meta && fileObj.meta.adminUploadForUserId) {
|
|
// Verify the uploader is an admin
|
|
const uploader = await Meteor.users.findOneAsync(fileObj.userId);
|
|
if (uploader && uploader.isAdmin) {
|
|
targetUserId = fileObj.meta.adminUploadForUserId;
|
|
// Update the file to belong to the target user
|
|
await Avatars.updateAsync({ _id: fileObj._id }, { $set: { userId: targetUserId } });
|
|
}
|
|
}
|
|
|
|
const user = await ReactiveCache.getUser(targetUserId);
|
|
user.setAvatarUrl(universalUrl);
|
|
} else {
|
|
await Avatars.removeAsync(fileObj._id);
|
|
}
|
|
};
|
|
|
|
Avatars.interceptDownload = function (http, fileObj, versionName) {
|
|
const ret = fileStoreStrategyFactory.getFileStrategy(fileObj, versionName).interceptDownload(http, this.cacheControl);
|
|
return ret;
|
|
};
|
|
|
|
Avatars.onBeforeRemove = async function (filesInput) {
|
|
let files;
|
|
try {
|
|
files = normalizeRemovedFiles(filesInput);
|
|
// If normalizeRemovedFiles returns a Promise (from async fetch), await it
|
|
if (files && typeof files.then === 'function') {
|
|
console.warn('normalizeRemovedFiles returned a Promise, awaiting it');
|
|
files = await files;
|
|
}
|
|
} catch (e) {
|
|
console.error('Error normalizing removed files:', e, 'filesInput:', filesInput);
|
|
files = [];
|
|
}
|
|
|
|
// Ensure files is an array
|
|
if (!Array.isArray(files)) {
|
|
console.error('normalizeRemovedFiles did not return an array, got:', typeof files);
|
|
files = [];
|
|
}
|
|
|
|
for (const fileObj of files) {
|
|
if (fileObj && fileObj.userId) {
|
|
const user = await ReactiveCache.getUser(fileObj.userId);
|
|
if (user) {
|
|
await user.setAvatarUrl('');
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
};
|
|
|
|
Avatars.onAfterRemove = async function (filesInput) {
|
|
let files;
|
|
try {
|
|
files = normalizeRemovedFiles(filesInput);
|
|
// If normalizeRemovedFiles returns a Promise (from async fetch), await it
|
|
if (files && typeof files.then === 'function') {
|
|
console.warn('normalizeRemovedFiles returned a Promise in onAfterRemove, awaiting it');
|
|
files = await files;
|
|
}
|
|
} catch (e) {
|
|
console.error('Error normalizing removed files in onAfterRemove:', e, 'filesInput:', filesInput);
|
|
files = [];
|
|
}
|
|
|
|
// Ensure files is an array
|
|
if (!Array.isArray(files)) {
|
|
console.error('normalizeRemovedFiles did not return an array in onAfterRemove, got:', typeof files);
|
|
files = [];
|
|
}
|
|
|
|
files.forEach(fileObj => {
|
|
if (!fileObj || !fileObj.versions) {
|
|
return;
|
|
}
|
|
|
|
Object.keys(fileObj.versions).forEach(versionName => {
|
|
fileStoreStrategyFactory.getFileStrategy(fileObj, versionName).onAfterRemove();
|
|
});
|
|
});
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Startup
|
|
// ---------------------------------------------------------------------------
|
|
|
|
Meteor.startup(() => {
|
|
const sp = fileStoreStrategyFactory.storagePath;
|
|
if (!fs.existsSync(sp)) {
|
|
console.log("create storagePath because it doesn't exist: " + sp);
|
|
fs.mkdirSync(sp, { recursive: true });
|
|
}
|
|
});
|