mirror of
https://github.com/wekan/wekan.git
synced 2026-11-26 20:45:07 -05:00
Resolve the site administrator role from the authenticated download token so the Files Report can open private-board attachments without requiring board membership. Keep ordinary private-board readers subject to membership checks. Wrap the shared attachment controls inside narrow table cells so neighboring cells cannot intercept the download button. Verify DOCX, image and PDF preview/download flows in Firefox and Chromium, plus negative authorization cases and eight focused Node suites. Thanks to xet7 !
45 lines
2.5 KiB
JavaScript
45 lines
2.5 KiB
JavaScript
'use strict';
|
||
const assert = require('node:assert/strict');
|
||
const fs = require('node:fs');
|
||
const vm = require('node:vm');
|
||
const { test } = require('node:test');
|
||
const source = fs.readFileSync('server/routes/universalFileServer.js', 'utf8');
|
||
const resolver = source.slice(source.indexOf(' async function getUserFromToken('), source.indexOf(' /**\n * Authorization helper'));
|
||
const guard = source.slice(source.indexOf(' async function isAuthorizedForBoard('), source.indexOf(' /**\n * Helper function to properly encode'));
|
||
const board = { isVisibleBy: user => user?._id === 'member' };
|
||
function policy(user) {
|
||
return vm.runInNewContext(`${resolver}\n${guard}\nisAuthorizedForBoard`, {
|
||
Meteor: { users: { async findOneAsync(query, options) {
|
||
assert.equal(query['services.resume.loginTokens.hashedToken'], 'hashed:valid-token-123');
|
||
const result = {};
|
||
if (!user) return null;
|
||
for (const key of Object.keys(options.fields)) result[key] = user[key];
|
||
return result;
|
||
} } },
|
||
Accounts: { _hashLoginToken: token => `hashed:${token}` },
|
||
extractLoginToken: req => req.token,
|
||
isSandstormRequest: () => false,
|
||
canReadBoard: (id, value) => !!value?.isVisibleBy(id ? { _id: id } : null),
|
||
process: { env: {} },
|
||
});
|
||
}
|
||
test('Files Report administrator can read another member’s private attachment', async () => {
|
||
assert.equal(await policy({ _id: 'admin', isAdmin: true })({ token: 'valid-token-123' }, board), true);
|
||
});
|
||
test('ordinary private-board access still requires membership', async () => {
|
||
for (const [user, expected] of [[{ _id: 'member' }, true], [{ _id: 'outsider' }, false], [null, false]]) {
|
||
assert.equal(await policy(user)({ token: 'valid-token-123' }, board), expected);
|
||
}
|
||
});
|
||
test('missing credentials, disabled admin, non-boolean admin and missing board do not grant access', async () => {
|
||
assert.equal(await policy({ _id: 'admin', isAdmin: true })({}, board), false);
|
||
assert.equal(await policy({ _id: 'admin', isAdmin: true })({ token: 'short' }, board), false);
|
||
for (const user of [{ _id: 'admin', isAdmin: true, loginDisabled: true }, { _id: 'outsider', isAdmin: 'true' }]) {
|
||
assert.equal(await policy(user)({ token: 'valid-token-123' }, board), false);
|
||
}
|
||
assert.equal(await policy({ _id: 'admin', isAdmin: true })({ token: 'valid-token-123' }, null), false);
|
||
});
|
||
test('public attachments remain readable without credentials', async () => {
|
||
assert.equal(await policy(null)({}, { isVisibleBy: () => true }), true);
|
||
});
|