Files
wekan/tests/restCustomFieldUpdate.test.cjs
T
Lauri Ojansivu d4c0d3e882 Security: a REST write that changes nothing answers 404, and a card deletion naming another list keeps the card's children.
DELETE /api/boards/:boardId/lists/:listId/cards/:cardId found the card by
board alone, removed its checklists, checklist items, comments and
attachments, and then removed the card by board AND list. With the wrong
list id the card stayed, emptied, and the reply was 200. Only the named list
was asked, so a member a list refuses could strip that list's cards by naming
a list they may write (ListLockBleed follow-up, GHSA-4734-8vrg-8j3w). The card
now has to be in the named list, and its own list is asked too, which records
the attempt in Admin Panel / Problems as authz.list-locked.

The siblings of #6900, #6905 and #6906 answered 200 for a change that changed
nothing: the swimlane, list, card, dependency and board-domain deletions; the
custom-field update, dropdown-item add and rename; the dependency update; the
board-member add and remove; and the token deletion of an unknown user. Each
looks the record up first and answers a JSON 404, and removing a board's last
admin answers 409. Member removal deactivates the user's active entry, not
the first one.

tests/restWriteRoutes404.test.cjs runs each handler with the record missing
and present, and fails for any id-addressed REST write in server/ without a
not-found answer. tests/playwright/specs/rest-write-routes-404.e2e.js replays
them against a running server. Three guards that pinned the old code shape
were updated, keeping what they protect.

Thanks to xet7 !
2026-11-14 21:26:30 +02:00

115 lines
5.2 KiB
JavaScript

'use strict';
// Execute the registered REST handler; live schema/database coverage is in Playwright.
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const { test } = require('node:test');
const route = '/api/boards/:boardId/custom-fields/:customFieldId';
const plain = value => JSON.parse(JSON.stringify(value));
function fixture({ deny = false } = {}) {
const calls = [];
const document = { _id: 'field', boardIds: ['board'], name: 'Original', type: 'text', settings: {}, showOnCard: true };
let handler;
const context = {
WebApp: { handlers: Object.fromEntries(['get', 'post', 'put', 'delete'].map(verb => [verb, (url, fn) => {
if (verb === 'put' && url === route) handler = fn;
}])) },
Authentication: { async checkBoardWriteAccess(userId, boardId) {
calls.push(['access', userId, boardId]);
if (deny) throw new Error('Forbidden');
} },
// The handler looks the field up on the board first, so a field that is not
// there is 404 rather than a 200 for an update that matched nothing.
ReactiveCache: { async getCustomField(selector) {
calls.push(['find', plain(selector)]);
return selector._id === document._id && selector.boardIds.$in.some(id => document.boardIds.includes(id)) ? document : undefined;
} },
CustomFields: { direct: { async updateAsync(selector, modifier) {
calls.push(['update', plain(selector), plain(modifier)]);
const fields = plain(modifier.$set);
if ('settings' in fields && typeof fields.settings !== 'object') throw new Error('Invalid settings');
if ('type' in fields && !['text', 'number'].includes(fields.type)) throw new Error('Invalid type');
if (selector._id !== document._id || !selector.boardIds.$in.some(id => document.boardIds.includes(id))) return 0;
Object.assign(document, fields);
return 1;
} } },
sendJsonResult(res, result) { res.responses.push(plain(result)); },
};
const source = fs.readFileSync(path.join(__dirname, '../server/models/customFields.js'), 'utf8');
vm.runInNewContext(source.slice(source.indexOf('WebApp.handlers.get(')), context);
assert.equal(typeof handler, 'function');
return {
calls, document,
async put(body, params = {}) {
const res = { responses: [] };
await handler({ userId: 'member', params: { boardId: 'board', customFieldId: 'field', ...params }, body }, res);
assert.equal(res.responses.length, 1);
return res.responses[0];
},
};
}
for (const invalid of [{ settings: 'invalid' }, { type: 'invalid' }]) {
test(`a rejected ${Object.keys(invalid)[0]} update leaves accompanying fields unchanged`, async () => {
const f = fixture();
const before = plain(f.document);
await assert.rejects(f.put({ name: 'Changed', ...invalid }), /Invalid/);
assert.deepEqual(f.document, before);
});
}
test('accepted fields are saved together, including false values, with the existing response', async () => {
const f = fixture();
const body = {
name: 'Changed', type: 'number', settings: {}, showOnCard: false,
automaticallyOnCard: false, alwaysOnCard: false, showLabelOnMiniCard: false, showSumAtTopOfList: false,
};
assert.deepEqual(await f.put(body), { code: 200, data: { _id: 'field' } });
assert.deepEqual(f.document, { _id: 'field', boardIds: ['board'], ...body });
assert.deepEqual(f.calls, [
['access', 'member', 'board'],
['find', { _id: 'field', boardIds: { $in: ['board'] } }],
['update', { _id: 'field', boardIds: { $in: ['board'] } }, { $set: body }],
]);
});
test('empty or unsupported updates retain the successful response without a write', async () => {
for (const body of [{}, { boardIds: ['other'], adminOnly: true, readOnly: true, unexpected: 'value' }]) {
const f = fixture();
const before = plain(f.document);
assert.deepEqual(await f.put(body), { code: 200, data: { _id: 'field' } });
assert.deepEqual(f.document, before);
assert.deepEqual(f.calls, [['access', 'member', 'board'], ['find', { _id: 'field', boardIds: { $in: ['board'] } }]]);
}
});
test('only own properties are accepted', async () => {
const f = fixture();
const body = Object.assign(Object.create({ name: 'Inherited', type: 'number' }), { showOnCard: false });
await f.put(body);
assert.equal(f.document.name, 'Original');
assert.equal(f.document.type, 'text');
assert.equal(f.document.showOnCard, false);
});
test('the update stays scoped to its board and field', async () => {
for (const params of [{ boardId: 'other' }, { customFieldId: 'missing' }]) {
const f = fixture();
const before = plain(f.document);
assert.deepEqual(await f.put({ name: 'Changed' }, params), { code: 404, data: { error: 'Custom field not found' } });
assert.deepEqual(f.document, before);
assert.equal(f.calls.some(([op]) => op === 'update'), false, 'nothing is written');
}
});
test('authorization runs before any write', async () => {
const f = fixture({ deny: true });
await assert.rejects(f.put({ name: 'Changed', settings: 'invalid' }), /Forbidden/);
assert.deepEqual(f.calls, [['access', 'member', 'board']]);
assert.equal(f.document.name, 'Original');
});