Files
wekan/tests/adminAttachmentRead.test.cjs
T
Lauri Ojansivu f03463e7de Fix administrator attachment previews and downloads in Files Report
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 !
2026-09-25 02:13:25 +03:00

45 lines
2.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'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);
});