Files
wekan/tests/restCommentDeleteAcl.test.cjs
T
Lauri Ojansivu d8d3446cc9 Update five node-suite guards for intentional changes made earlier today.
- headerBars.test.cjs: the starred-boards count moved into the user
  model's own starredCount() (now covering swimlanes/lists/cards too,
  #1172), so the test now checks the header delegates to it instead of
  pinning the old inline boards+pages arithmetic.
- notificationEmailHtmlSafety.test.cjs: the notification subject is `let`,
  not `const` - the #2022 admin-template override reassigns it through
  the same safeEmailSubject() guard - so the regex now matches `let`. The
  guard itself was already intact at every call site.
- restCommentDeleteAcl.test.cjs / restSecurityAdvisories.test.cjs: a
  comment-edit PUT route was added (with the correct
  Authentication.checkBoardAccess + assertCanMutateComment(req.userId, ...)
  pair, same as DELETE). restCommentDeleteAcl's negative test previously
  banned any PUT outright; it now asserts the PUT carries the same ACL
  instead. restSecurityAdvisories' commentCreate route slice ended at the
  DELETE handler string, so it now silently swallowed the new PUT's
  checkBoardAccess call and failed a doesNotMatch aimed at POST; the slice
  now stops at the PUT handler, with its own assertions pinning the PUT's
  membership + object-ACL checks.
- rulesDateFieldTrigger.test.cjs: the "text contains" trigger landed
  between r-when-a-card-matches-advanced-filter and the four date
  triggers in every locale file (en.i18n.json included), so the ordered
  key list now expects it there too.

Thanks to xet7 !
2026-09-10 22:44:38 +03:00

141 lines
6.4 KiB
JavaScript

'use strict';
// CommentBleed — GHSA-pqr4-rxgp-hv2m, "REST comment DELETE allows any board
// member to delete other users' comments (BOLA)" (Moderate, CWE-639 / CWE-863),
// reported by Alpastx. https://wekan.fi/hall-of-fame/commentbleed/
//
// Over DDP, deleting a comment is author-or-board-admin, and a board that sets
// restrictCommentEditing takes even the admin's ability away (#5906). That rule
// was enforced in a CardComments collection hook keyed off the Meteor userId,
// with an early `if (!userId) return` for genuine server-internal work (board
// copy, cleanup, migrations).
//
// An HTTP request carries no Meteor userId into the invocation context. So
// `DELETE /api/boards/:b/cards/:c/comments/:id` — which checked only board
// membership — reached the collection, the hook took its "server-internal"
// path, and any normal member could delete anybody's comment, restricted board
// or not.
//
// The fix is not to trust the hook: the handler loads the comment and applies
// the same rule itself, with req.userId, before removing anything.
//
// Run: node tests/restCommentDeleteAcl.test.cjs
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const repoRoot = path.resolve(__dirname, '..');
const read = rel => fs.readFileSync(path.join(repoRoot, rel), 'utf8');
const restComments = read('server/models/cardComments.js');
const modelComments = read('models/cardComments.js');
let passed = 0;
function test(name, fn) {
fn();
passed += 1;
console.log(' ok -', name);
}
const deleteHandler = restComments.match(
/WebApp\.handlers\.delete\([\s\S]*?\n\);/,
);
// ------------------------------------------------- the rule itself (pure)
// canEditComment is the pure decision the whole thing rests on. It has its own
// mocha suite (server/lib/tests/comments.permissions.tests.js); re-pinned here
// because this advisory is about it being SKIPPED, not about it being wrong.
test('the pure rule still says author-or-admin, and restricted means author only', () => {
const source = modelComments.match(/export function canEditComment\([\s\S]*?\n\}/)[0];
assert.ok(/if \(isAuthor\) \{\s*\n\s*return true;/.test(source));
assert.ok(/if \(restrictCommentEditing\) \{\s*\n\s*return false;/.test(source));
assert.ok(/return !!isBoardAdmin;/.test(source));
});
// ------------------------------------------------------ the REST handler
test('the DELETE handler loads the comment before removing it', () => {
assert.ok(deleteHandler, 'the DELETE handler is found');
const body = deleteHandler[0];
assert.ok(/const comment = await ReactiveCache\.getCardComment\(/.test(body));
});
test('a comment that does not exist is a 404, not a silent success', () => {
const body = deleteHandler[0];
assert.ok(/if \(!comment\) \{[\s\S]*?code: 404[\s\S]*?return;/.test(body));
});
test('the object ACL is applied, with the REST caller identity', () => {
const body = deleteHandler[0];
assert.ok(/await assertCanMutateComment\(req\.userId, comment\);/.test(body),
'the same rule DDP applies, called with req.userId');
});
test('the ACL runs BEFORE the removal, not after it', () => {
const body = deleteHandler[0];
const aclAt = body.indexOf('assertCanMutateComment');
const removeAt = body.indexOf('CardComments.removeAsync');
assert.ok(aclAt > -1 && removeAt > -1, 'both are present');
assert.ok(aclAt < removeAt, 'the check must precede the delete');
});
test('negative: board membership alone is no longer the only check', () => {
const body = deleteHandler[0];
assert.ok(/checkBoardAccess\(req\.userId, paramBoardId\)/.test(body),
'membership is still required...');
assert.ok(/assertCanMutateComment/.test(body),
'...but it is no longer sufficient, which is the whole advisory');
});
// --------------------------------------------------- the shared rule module
test('the rule is exported, so REST and DDP cannot enforce different things', () => {
assert.ok(/export async function assertCanMutateComment\(userId, doc\)/.test(modelComments));
assert.ok(
/import CardComments, \{ assertCanMutateComment \} from '\/models\/cardComments'/.test(restComments),
'the REST file imports that exact function',
);
});
test('the hooks still call the same function, so DDP is unchanged', () => {
assert.ok(/CardComments\.before\.update\(async \(userId, doc\) => \{\s*\n\s*await assertCanMutateComment\(userId, doc\);/.test(modelComments));
assert.ok(/CardComments\.before\.remove\(async \(userId, doc\) => \{\s*\n\s*await assertCanMutateComment\(userId, doc\);/.test(modelComments));
});
test('the no-userId trusted path is documented as internal-only, and kept', () => {
const fn = modelComments.match(/export async function assertCanMutateComment[\s\S]*?\n\}/)[0];
// Board copy, cleanup and migrations legitimately run with no user; removing
// this would break them. What changed is that REST no longer arrives here
// anonymously.
assert.ok(/if \(!userId\) \{\s*\n\s*return;/.test(fn));
assert.ok(/GHSA-pqr4-rxgp-hv2m/.test(fn), 'and why, so it is not "simplified" back');
});
test('the refusal is a 403 for REST callers, not a 500', () => {
const fn = modelComments.match(/export async function assertCanMutateComment[\s\S]*?\n\}/)[0];
assert.ok(/error\.statusCode = 403;/.test(fn));
assert.ok(/error-comment-edit-not-allowed/.test(fn), 'and keeps the DDP error id');
});
test('negative: a REST edit route exists now, and it carries the same rule', () => {
// A PUT (comment edit) route was added. It must not be a way to bypass the
// rule DELETE enforces: it has to require board membership AND run
// assertCanMutateComment with the REST caller's own identity, exactly like
// the DELETE handler above. tests/restSecurityAdvisories.test.cjs pins the
// same route from the other side (membership present, no bare
// Authentication.checkBoardAccess-only path for comment creation).
const putHandler = restComments.match(
/WebApp\.handlers\.put\(\s*\n\s*'\/api\/boards\/:boardId\/cards\/:cardId\/comments\/:commentId'[\s\S]*?\n\);/,
);
assert.ok(putHandler, 'the comment edit route is found');
const body = putHandler[0];
assert.ok(/Authentication\.checkBoardAccess\(req\.userId, paramBoardId\)/.test(body),
'board membership is required');
assert.ok(/assertCanMutateComment\(req\.userId, comment\)/.test(body),
'and the same object-level rule DELETE applies, called with req.userId');
});
console.log(`\n${passed} tests passed`);