Files
wekan/server/lib/tests/permissions.security.tests.js
T
Lauri OjansivuandClaude Opus 4.8 b9f9390d04 Server-side Mocha suite: fixed a startup crash and the 44 latent failures it had been hiding
- imports/i18n/i18n.test.js crashed the whole run at load: chai 6.x plugins (sinon-chai,
  chai-as-promised) are ESM-only, so use(require('sinon-chai')) handed chai.use() a module
  namespace instead of the plugin function ("fn is not a function"). Now imports the default
  export.
- That unmasked a second load crash - PositionHistory.helpers is not a function: the
  meteor test entry (server/lib/tests/index.js) never ran the .helpers / .attachSchema shim
  that server/main.js bootstraps, so the first model to call Collection.helpers({...}) threw.
  The shim is now imported first in the test entry.
- With the suite finally running, 44 server tests failed because meteor test only loads what
  the specs import (not the app's /server/imports). The specs now import the files that
  register the methods / globals under test (cards.vote / cards.pokerVote, api.attachment.*,
  cloneBoard, getBackgroundImageURL, applyListWidth, updateListSort, moveChecklist,
  userPositionHistory.*, archiveBoard, sendSMTPTestEmail, and the Attachments global). Also
  fixed genuine test bugs: the cards.vote / cards.pokerVote specs were written synchronously
  against async methods; the header-login trust specs restored env vars with
  process.env.X = undefined (which stores the string "undefined" and shadowed the trusted-IP
  allowlist, making every trusted source read as untrusted); the DnsBleed decimal-loopback
  matcher assumed the integer host survived URL normalisation; and the dependencies-OpenAPI
  spec could not locate its source file from the built bundle. The two cards.archive /
  cards.move specs tested Meteor methods that do not exist (archive / move are Minimongo
  document helpers secured by Cards.allow / Cards.deny, already covered by the cards security
  tests) and were removed. Server-side Mocha is now 409 passing, 0 failing.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-05 16:48:58 +03:00

108 lines
4.6 KiB
JavaScript

/* eslint-env mocha */
import { expect } from 'chai';
import sinon from 'sinon';
import { Meteor } from 'meteor/meteor';
import { ReactiveCache } from '/imports/reactiveCache';
import { allowIsAnyBoardMemberWithWriteAccess } from '/server/lib/utils';
// Register the Meteor methods under test (meteor-test loads only what tests
// import; the app registers these via server/main.js → /server/imports).
import '/server/models/boards'; // archiveBoard
import '/server/models/settings'; // sendSMTPTestEmail
// Tests that client-side permission gates are also enforced server-side.
// See the "client-side permissions verified server-side" audit.
describe('server-side permission enforcement', function() {
afterEach(function() {
sinon.restore();
});
// Helper: build a board whose single member has the given role flags.
const boardWithMember = (userId, flags = {}) => ({
members: [Object.assign(
{ userId, isActive: true, isNoComments: false, isCommentOnly: false, isWorker: false, isReadOnly: false, isReadAssignedOnly: false },
flags,
)],
});
describe('allowIsAnyBoardMemberWithWriteAccess (CustomFields / multi-board write)', function() {
it('denies a read-only member on every board', function() {
const userId = 'ro';
const boards = [boardWithMember(userId, { isReadOnly: true }), boardWithMember(userId, { isReadOnly: true })];
expect(allowIsAnyBoardMemberWithWriteAccess(userId, boards)).to.equal(false);
});
it('denies comment-only and worker members', function() {
const userId = 'co';
expect(allowIsAnyBoardMemberWithWriteAccess(userId, [boardWithMember(userId, { isCommentOnly: true })])).to.equal(false);
expect(allowIsAnyBoardMemberWithWriteAccess(userId, [boardWithMember(userId, { isWorker: true })])).to.equal(false);
});
it('denies a non-member', function() {
expect(allowIsAnyBoardMemberWithWriteAccess('outsider', [boardWithMember('someone-else')])).to.equal(false);
});
it('allows a member with write access on at least one board', function() {
const userId = 'writer';
const boards = [boardWithMember(userId, { isReadOnly: true }), boardWithMember(userId)];
expect(allowIsAnyBoardMemberWithWriteAccess(userId, boards)).to.equal(true);
});
});
describe('archiveBoard (server/models/boards.js) — board-admin only', function() {
const handler = () => Meteor.server.method_handlers['archiveBoard'];
it('denies a non-admin board member', async function() {
const archiveStub = sinon.stub().resolves();
sinon.stub(ReactiveCache, 'getBoard').resolves({ hasAdmin: () => false, archive: archiveStub });
sinon.stub(ReactiveCache, 'getUser').resolves({ isAdmin: false });
let thrown;
try {
await handler().apply({ userId: 'member' }, ['board-1']);
} catch (error) {
thrown = error;
}
expect(thrown).to.exist;
expect(thrown.error).to.equal('error-board-notAdmin');
expect(archiveStub.called).to.equal(false);
});
it('allows a board admin', async function() {
const archiveStub = sinon.stub().resolves();
sinon.stub(ReactiveCache, 'getBoard').resolves({ hasAdmin: (id) => id === 'admin', archive: archiveStub });
sinon.stub(ReactiveCache, 'getUser').resolves({ isAdmin: false });
const result = await handler().apply({ userId: 'admin' }, ['board-1']);
expect(result).to.equal(true);
expect(archiveStub.calledOnce).to.equal(true);
});
it('allows a global admin who is not a board member', async function() {
const archiveStub = sinon.stub().resolves();
sinon.stub(ReactiveCache, 'getBoard').resolves({ hasAdmin: () => false, archive: archiveStub });
sinon.stub(ReactiveCache, 'getUser').resolves({ isAdmin: true });
const result = await handler().apply({ userId: 'siteadmin' }, ['board-1']);
expect(result).to.equal(true);
expect(archiveStub.calledOnce).to.equal(true);
});
});
describe('sendSMTPTestEmail (server/models/settings.js) — global-admin only', function() {
const handler = () => Meteor.server.method_handlers['sendSMTPTestEmail'];
it('denies a non-admin authenticated user', async function() {
sinon.stub(ReactiveCache, 'getCurrentUser').resolves({
isAdmin: false,
emails: [{ address: 'user@example.com' }],
getLanguage: () => 'en',
});
let thrown;
try {
await handler().apply({ userId: 'user-1', unblock: () => {} }, []);
} catch (error) {
thrown = error;
}
expect(thrown).to.exist;
expect(thrown.error).to.equal('error-notAuthorized');
});
});
});