Files
wekan/server/permissions/attachments.js
T
Lauri Ojansivu 51111e1958 StampBleed and UploadBleed: documents on one's own card stamped with another board, and unauthorized attachment uploads.
StampBleed (found reviewing the patch for #6813): checklists, checklist
items and text notes carry a denormalized boardId, by which the board
publication publishes them, and items a checklistId, by which a
checklist draws them. The insert allow rules check write access on the
document's card only, and the server's insert hooks kept a boardId the
client sent. So a member of any board could insert, on their own card,
a document stamped with a private board's id - published to that
board's members - and an item with that board's checklist id, drawn in
it. 4bd6a7f088 made the server take boardId from the card; now the
forged insert is refused outright and recorded, for those three and for
a direct attachment insert with a foreign meta.boardId. WeKan's own
client never sends a mismatch.

UploadBleed: Meteor-Files' upload pipeline (_FilesCollectionStart_
attachments / Write and its HTTP twin) inserts the stored attachment
SERVER-side, so the allow rules were never asked, and the attachments'
onBeforeUpload only cleaned the file name: any DDP client, signed in or
not, could put a file on any card of any board - published to its
members with an addAttachment activity and its notifications - and the
admin's uploads-blocked switch was skipped too. The upload is
authorized on every chunk now: signed in, allowed to edit the card (or
write the board, for a background or map image), uploads not blocked,
and a card attachment's board, list and swimlane are its card's.

Both are recorded in Admin Panel / Problems (authz.card-child-stamp,
authz.attachment-upload), once per upload. tests/stampBleed.test.cjs and
tests/uploadBleed.test.cjs run the real decisions, and fail on a card
child with no insert board check or a FilesCollection without a server
upload check; upload-stamp-bleed.e2e.js attacks the running server.

Thanks to xet7 !
2026-10-27 15:38:58 +02:00

141 lines
6.3 KiB
JavaScript

import Attachments, { normalizeRemovedFiles } from '/models/attachments';
import Boards from '/models/boards';
import AttachmentStorageSettings from '/models/attachmentStorageSettings';
import { allowIsBoardMemberWithWriteAccess } from '/server/lib/utils';
// GHSA-4mxf-m8pq-xc9p: these guards used to be written out here and NOT in
// server/permissions/avatars.js, which is how avatars ended up without them.
// They live in one place now, and both collections import that one.
import {
hasUnsafeClientVersionFields,
touchesVersionFields,
onlyTouchesAllowedFields,
} from '/models/lib/fileVersionFields';
import { tripCanary } from '/server/lib/canary';
import Cards from '/models/cards';
import { canEditCardOrLinkedCard } from '/server/lib/linkedCardPermission';
async function canEditAttachmentCard(userId, fileObj) {
const cardId = fileObj?.meta?.cardId;
if (cardId) {
const card = await Cards.findOneAsync(cardId);
if (card) return await canEditCardOrLinkedCard(userId, card);
}
return allowIsBoardMemberWithWriteAccess(
userId,
await Boards.findOneAsync(fileObj?.meta?.boardId),
);
}
// StampBleed: a direct insert on one's own card stamped with another board's
// meta.boardId is refused and recorded (server/lib/cardChildStamp.js). The
// upload pipeline is authorized in models/attachments.server.js (UploadBleed).
Attachments.deny({
async insert(userId, fileObj) {
const { denyForgedAttachmentInsert } = require('/server/lib/cardChildStamp');
return await denyForgedAttachmentInsert(fileObj, { source: 'ddp:attachment-insert' });
},
});
Attachments.allow({
async insert(userId, fileObj) {
// Block attempts to inject server-managed storage metadata.
if (hasUnsafeClientVersionFields(fileObj)) {
if (process.env.DEBUG === 'true') {
console.warn('Blocked attachment insert with client-supplied versions.path/storage');
}
return tripCanary('attachment.version-path', { userId });
}
// Admin-level hard stop for all non-API attachment uploads.
try {
const settings = await AttachmentStorageSettings.findOneAsync({});
if (settings?.limitSettings?.attachmentsUploadBlocked === true) {
return false;
}
} catch (error) {
if (process.env.DEBUG === 'true') {
console.warn('Could not read attachment upload block setting:', error);
}
}
// ReadOnly users cannot upload attachments
return await canEditAttachmentCard(userId, fileObj);
},
async update(userId, fileObj, fields) {
// SECURITY: The 'name' field is sanitized in onBeforeUpload and server-side methods,
// but we block direct client-side $set operations on 'versions.*.path' to prevent
// path traversal attacks via storage migration exploits.
// Block direct updates to server-managed version metadata.
if (touchesVersionFields(fields)) {
if (process.env.DEBUG === 'true') {
console.warn('Blocked attempt to update attachment versions metadata:', fields);
}
return tripCanary('attachment.version-path', { userId });
}
// Allow normal updates for file upload/management
// AvatarMimeBleed / attachment-move sibling (2026-10-02): `type` decided how a
// file is served and `meta` names its board and card, so a client changing
// either could serve its upload as HTML or move it onto another board's card.
// No client updates these documents (uploads and renames go through the
// server); only the name may change here.
const allowedFields = ['name'];
if (!onlyTouchesAllowedFields(fields, allowedFields)) {
if (process.env.DEBUG === 'true') {
console.warn('Blocked attempt to update restricted attachment fields:', fields);
}
return tripCanary('attachment.restricted-field', { userId });
}
// ReadOnly users cannot update attachments
return await canEditAttachmentCard(userId, fileObj);
},
// History.md §12.3: no per-attachment hard delete exists. "Delete" on a card
// is the 'attachments.softDelete' method; the client never removes a
// document. This rule is what the ordinary Mongo remove() consults, and the
// ostrio DDP remove method is refused in onBeforeRemove below.
remove() {
return false;
},
fetch: ['meta'],
});
// Advisory: "Unauthenticated DDP Methods _FilesCollectionRemove_attachments/
// _FilesCollectionRemove_avatars Allow Instance-Wide Deletion" - ostrio:files
// registers its OWN DDP method (_FilesCollectionRemove_attachments), gated
// only by `allowClientCode` (true above); its handler never goes through the
// `Attachments.allow({remove})` rule above - that only gates the ordinary
// Mongo `.remove()` call - and calls `self.removeAsync(selector)` directly.
// With no onBeforeRemove defined at all, any anonymous DDP connection could
// call it with selector `{}` and delete every attachment record and its
// physical file on the instance. `this.userId` here is the calling DDP
// method's own userId (ostrio:files' server.js binds it before invoking this
// hook), so this runs the same board-write-access check `remove` above does,
// against every document the selector actually matches - an empty or
// non-matching selector is refused rather than treated as "nothing to check".
// History.md §12.3 on top of that: the DDP remove is refused for EVERYONE, not
// only for callers without write access. There is no per-attachment hard delete
// in WeKan any more - Delete on a card soft-deletes through the
// 'attachments.softDelete' method, and the only removal of attachment records
// and files is the archived-board purge (server/models/boards.js boardRemover).
// A call that reaches here is a client bypassing the UI, so it is logged as an
// attempt under Admin Panel -> Problems and denied.
Attachments.onBeforeRemove = function (cursor) {
const userId = this.userId;
let count = 0;
try { count = normalizeRemovedFiles(cursor).length; } catch (e) { /* the count is only for the log */ }
try {
require('/server/lib/securityLog').record({
key: 'authz.file-remove',
action: 'blocked',
userId: userId || undefined,
source: '_FilesCollectionRemove_attachments',
detail: userId
? `refused a client attachment removal of ${count} document(s): attachments are soft-deleted, never removed`
: 'refused an unauthenticated attachment removal',
});
} catch (e) { /* logging must never break the guard */ }
return false;
};