mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
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 !
144 lines
5.7 KiB
JavaScript
144 lines
5.7 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const crypto = require('node:crypto');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const vm = require('node:vm');
|
|
const { test } = require('node:test');
|
|
const { publicErrorData } = require('../server/lib/apiResponseHelpers');
|
|
|
|
const source = fs.readFileSync(path.join(__dirname, '../server/models/users.js'), 'utf8');
|
|
const start = source.indexOf("WebApp.handlers.post('/api/deletetoken'");
|
|
const end = source.indexOf('const sanitizeUserForSearch', start);
|
|
assert.ok(start >= 0 && end > start, 'the token deletion route is present');
|
|
const hash = token => crypto.createHash('sha256').update(token).digest('base64');
|
|
const raw = () => crypto.randomBytes(24).toString('base64url');
|
|
const plain = value => JSON.parse(JSON.stringify(value));
|
|
|
|
function fixture({ denied, failure, waitForWrite } = {}) {
|
|
const issued = raw();
|
|
const other = raw();
|
|
const users = {
|
|
target: [{ hashedToken: hash(issued) }, { hashedToken: hash(other) }],
|
|
unrelated: [{ hashedToken: hash(issued) }],
|
|
};
|
|
const calls = [];
|
|
let handler;
|
|
vm.runInNewContext(source.slice(start, end), {
|
|
WebApp: { handlers: { post(route, callback) {
|
|
assert.equal(route, '/api/deletetoken');
|
|
handler = callback;
|
|
} } },
|
|
Authentication: { async checkUserId(userId) {
|
|
calls.push('authorize');
|
|
if (denied) throw Object.assign(new Error('Denied'), { statusCode: denied });
|
|
} },
|
|
check(value, type) {
|
|
assert.equal(type.name, 'String');
|
|
if (typeof value !== 'string') throw Object.assign(new Error('Invalid string'), { statusCode: 400 });
|
|
},
|
|
Accounts: {
|
|
// Meteor compares its argument directly to stored hashedToken/token fields.
|
|
async destroyToken(id, token) {
|
|
calls.push('destroy');
|
|
if (waitForWrite && users[id]?.some(entry => entry.hashedToken === token || entry.token === token)) {
|
|
await waitForWrite;
|
|
}
|
|
if (failure) throw failure;
|
|
if (users[id]) users[id] = users[id].filter(entry => entry.hashedToken !== token && entry.token !== token);
|
|
},
|
|
},
|
|
Users: { async findOneAsync(selector, options) {
|
|
calls.push('find');
|
|
assert.deepEqual(plain(options), { fields: { _id: 1 } });
|
|
return users[selector._id] ? { _id: selector._id } : undefined;
|
|
}, async updateAsync(selector, modifier) {
|
|
calls.push('clear');
|
|
assert.deepEqual(plain(modifier), { $set: { 'services.resume.loginTokens': [] } });
|
|
users[selector._id] = [];
|
|
} },
|
|
publicErrorData,
|
|
sendJsonResult(res, result) { calls.push('respond'); res.responses.push(plain(result)); },
|
|
});
|
|
return {
|
|
issued, other, users, calls,
|
|
async remove(body) {
|
|
const res = { responses: [] };
|
|
await handler({ userId: 'admin', body }, res);
|
|
assert.equal(res.responses.length, 1);
|
|
return res.responses[0];
|
|
},
|
|
};
|
|
}
|
|
|
|
test('deletes the supplied stored hash without clearing other sessions or users', async () => {
|
|
const f = fixture();
|
|
const unrelated = plain(f.users.unrelated);
|
|
assert.equal((await f.remove({ userId: 'target', token: hash(f.issued) })).code, 200);
|
|
assert.deepEqual(f.users.target, [{ hashedToken: hash(f.other) }]);
|
|
assert.deepEqual(f.users.unrelated, unrelated);
|
|
assert.deepEqual(f.calls, ['authorize', 'find', 'destroy', 'respond']);
|
|
});
|
|
|
|
test('does not acknowledge until token deletion has completed', async () => {
|
|
let release;
|
|
const waitForWrite = new Promise(resolve => { release = resolve; });
|
|
const f = fixture({ waitForWrite });
|
|
const pending = f.remove({ userId: 'target', token: hash(f.issued) });
|
|
await new Promise(resolve => setImmediate(resolve));
|
|
try {
|
|
assert.deepEqual(f.calls, ['authorize', 'find', 'destroy']);
|
|
} finally {
|
|
release();
|
|
}
|
|
assert.equal((await pending).code, 200);
|
|
assert.deepEqual(f.users.target, [{ hashedToken: hash(f.other) }]);
|
|
});
|
|
|
|
test('deletion failures return a sanitized error instead of success', async () => {
|
|
const f = fixture({ failure: new Error('private database diagnostic') });
|
|
const before = plain(f.users);
|
|
assert.deepEqual(await f.remove({ userId: 'target', token: hash(f.issued) }), {
|
|
code: 500, data: { error: 'Internal server error' },
|
|
});
|
|
assert.deepEqual(f.users, before);
|
|
});
|
|
|
|
for (const denied of [401, 403]) {
|
|
test(`authorization failure ${denied} cannot delete tokens`, async () => {
|
|
const f = fixture({ denied });
|
|
const before = plain(f.users);
|
|
assert.equal((await f.remove({ userId: 'target', token: hash(f.issued) })).code, denied);
|
|
assert.deepEqual(f.calls, ['authorize', 'respond']);
|
|
assert.deepEqual(f.users, before);
|
|
});
|
|
}
|
|
|
|
test('supplying an original token does not add implicit hashing', async () => {
|
|
const f = fixture();
|
|
const before = plain(f.users);
|
|
assert.equal((await f.remove({ userId: 'target', token: f.issued })).code, 200);
|
|
assert.deepEqual(f.users, before);
|
|
});
|
|
|
|
test('unknown tokens are harmless and omitting a token still clears all target sessions', async () => {
|
|
const f = fixture();
|
|
const before = plain(f.users);
|
|
assert.equal((await f.remove({ userId: 'target', token: raw() })).code, 200);
|
|
assert.deepEqual(f.users, before);
|
|
assert.equal((await f.remove({ userId: 'target' })).code, 200);
|
|
assert.deepEqual(f.users.target, []);
|
|
assert.deepEqual(f.users.unrelated, before.unrelated);
|
|
});
|
|
|
|
test('negative: a user that does not exist is 404, and nothing is deleted or cleared', async () => {
|
|
const f = fixture();
|
|
const before = plain(f.users);
|
|
for (const body of [{ userId: 'missing', token: hash(f.issued) }, { userId: 'missing' }]) {
|
|
assert.deepEqual(await f.remove(body), { code: 404, data: { error: 'User not found' } });
|
|
}
|
|
assert.deepEqual(f.users, before);
|
|
assert.equal(f.calls.includes('destroy') || f.calls.includes('clear'), false);
|
|
});
|