Files
wekan/tests/apiGateCase.test.cjs
T
Lauri Ojansivu bb4c892254 Gate the REST API as Express routes it, and keep impersonation status private.
With WITH_API off, the API gate refused req.url.startsWith('/api') - but
Express matches routes case-insensitively and on the decoded path, so /API/...
and /%61pi/... reached every API handler anyway. The gate now decides the
same way. The test covers the spellings Express routes and those it must not.

The isImpersonated method answered for any user id, to anyone, signed in or
not; it now answers only about the caller, or to a site admin.

Thanks to xet7 !
2026-10-02 11:22:54 +03:00

35 lines
1.4 KiB
JavaScript

'use strict';
// Guard (2026-10-02): with WITH_API off, the REST API is refused by a gate
// that tested req.url.startsWith('/api') - but Express matches routes
// case-insensitively and on the decoded path, so /API/... and /%61pi/...
// reached every API handler anyway.
// Run: node tests/apiGateCase.test.cjs
const { test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const src = fs.readFileSync(path.join(__dirname, '..', 'server/apiMiddleware.js'), 'utf8');
const fnSrc = src.slice(src.indexOf('function isApiPath(url) {'), src.indexOf('\n}\n', src.indexOf('function isApiPath(url) {')) + 2);
// eslint-disable-next-line no-new-func
const isApiPath = new Function(`${fnSrc}\nreturn isApiPath;`)();
test('every spelling Express routes to the API is gated', () => {
for (const url of ['/api/boards', '/API/boards', '/Api/user', '/%61pi/boards', '/%41PI/users', '/api', '/api?x=1']) {
assert.equal(isApiPath(url), true, url);
}
});
test('nothing else is (negative)', () => {
for (const url of ['/', '/b/x/board', '/apiary', '/cdn/storage/attachments/x', '/sign-in', '/%zz']) {
assert.equal(isApiPath(url), false, url);
}
});
test('the gate uses it', () => {
assert.match(src, /const api = isApiPath\(req\.url\);/);
assert.doesNotMatch(src, /req\.url\.startsWith\('\/api'\)/);
});