Files
wekan/tests/boardTextPublication.test.cjs
T
Lauri Ojansivu 479f530da0 Board filter matches re-check the board's access, not only its observer.
card-text-filter failed intermittently in the EVERYTHING runs: after a
member's assigned-only restriction was lifted, the text-filter matches
never widened again (still one match after 100 s). Instrumented on the
polling test server, the failing subscription's board observer delivered
its initial add and then nothing - neither the narrowing nor the widening
write - while the card observers of the same board kept firing; the
narrowing only applied because a card edit happened to trigger a rescan.
With nothing after the widening, nothing rescanned. The same stall would
also have kept a NARROWED member's old matches, which is an access leak.

publishBoardMatches (text matches, movement matches, table pages) now also
re-reads the board every 10 s and rescans when its access fields -
permission, members and the provider's own board fields - differ from
the ones the published result was computed from, unreadable boards
included. An unchanged board does not rescan.
tests/boardTextPublication.test.cjs widens, narrows and revokes with the
observer silent and only the re-check firing; both tests fail on the
previous code. card-text-filter passed 3 of 3 in Firefox afterwards.

Thanks to xet7 !
2026-10-07 23:46:07 +03:00

153 lines
9.4 KiB
JavaScript

'use strict';
const { test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const vm = require('node:vm');
const { assignedOnlyCardScope } = require('../models/lib/boardCardScope');
const tick = () => new Promise(resolve => setImmediate(resolve));
async function fixture(search, snapshot = false, prepare) {
const observers = [], rows = new Map(), intervals = [], cleared = [];
let handler, stop, ready = 0;
let board = { _id: 'board', permission: 'private', members: [{ userId: 'reader', isActive: true, isReadAssignedOnly: true }] };
const model = name => ({ rawCollection: () => name, find: () => ({ observeChangesAsync: async callbacks => {
const handle = { name, callbacks, stopped: false, stop() { this.stopped = true; } };
observers.push(handle); callbacks.added('initial', {}); return handle;
} }) });
const Boards = { ...model('boards'), findOneAsync: async () => structuredClone(board) };
const source = (fs.readFileSync('server/lib/publishBoardMatches.js', 'utf8').replace('export async function', 'async function') + '\n' +
fs.readFileSync('server/publications/boardTextMatches.js', 'utf8'))
.replace(/^import .*;\n/gm, '').replace(/^const \{ assignedOnlyCardScope \} = require\([^\n]+\);\n/m, '');
const snapshotSource = snapshot ? `
Meteor.publish('snapshot', function(boardId, key) {
return publishBoardMatches({ publication: this, boardId, key, identity: { key },
collectionName: 'pages', snapshot: true, cardFields: null, children: [], prepare: prepareFixture,
findMatches: boardTextSearch });
});` : '';
vm.runInNewContext(source + snapshotSource, { Meteor: { publish: (name, fn) => { handler = fn; }, Error,
setInterval: (fn, ms) => { intervals.push({ fn, ms }); return intervals.length; },
clearInterval: id => { cleared.push(id); } }, check() {},
Boards, Cards: model('cards'), CardComments: model('comments'), Checklists: model('checklists'), ChecklistItems: model('items'),
canReadBoard: (id, doc) => !!doc && (doc.permission === 'public' || doc.members.some(m => m.userId === id && m.isActive)),
assignedOnlyCardScope, boardTextSearch: search, prepareFixture: prepare,
});
const context = { userId: 'reader', onStop: fn => { stop = fn; }, ready: () => ready++,
added: (collection, id, fields) => rows.set(id, fields), removed: (collection, id) => rows.delete(id),
changed: (collection, id, fields) => rows.set(id, { ...rows.get(id), ...fields }),
error: error => { throw error; } };
return { observers, rows, context, start: term => handler.call(context, 'board', term),
setBoard(value) { board = value; }, stop: () => stop(), ready: () => ready, intervals, cleared };
}
test('publication scopes the join, retracts revoked results and cleans every observer', async () => {
const scopes = [];
const f = await fixture(async args => { scopes.push(args.scope); return ['allowed']; });
await f.start('needle');
assert.equal(f.rows.size, 1);
assert.deepEqual(Object.keys([...f.rows.values()][0]).sort(), ['boardId', 'cardId', 'term']);
assert.equal(scopes[0].assignees.$in[0], 'reader');
f.setBoard({ _id: 'board', members: [], permission: 'private' });
f.observers[0].callbacks.changed('board', {});
assert.equal(f.rows.size, 0, 'board policy changes retract old results immediately');
await tick(); assert.equal(f.rows.size, 0);
f.stop(); assert.ok(f.observers.every(handle => handle.stopped));
});
test('dynamic source watches invalidate pages, replace old handles and recheck prepared state', async () => {
const sources = []; let key = 'one', valid = true, count = 0;
const prepare = async ({ watch }) => {
await watch('source', key, { observeChangesAsync: async callbacks => {
const handle = { callbacks, stopped: false, stop() { this.stopped = true; } };
sources.push(handle); callbacks.added('source', {}); return handle;
} });
return { async isCurrent() { if (!valid) { valid = true; return false; } return true; } };
};
const f = await fixture(async () => ({ ids: ['card'], total: ++count, page: 1, cards: [{ _id: 'card', title: key }] }), true, prepare);
await f.start('page-key'); assert.equal(sources.length, 1);
sources[0].callbacks.changed('source', {});
assert.equal(f.rows.size, 0, 'a source change retracts old ordering immediately');
await tick(); assert.equal(sources.length, 1, 'unchanged source IDs reuse the observer');
key = 'two'; valid = false;
sources[0].callbacks.changed('source', {}); await tick(); await tick();
assert.equal(sources.length, 2); assert.equal(sources[0].stopped, true);
assert.equal(f.rows.get('card').title, 'two');
assert.ok(count >= 4, 'failed end-of-scan verification forces a fresh scan');
f.stop(); assert.ok(sources.every(handle => handle.stopped));
});
test('page snapshots replace cards, clear removed fields, retract on revocation and stop observers', async () => {
let cards = [{ _id: 'first', title: 'Before', description: 'Removed later' }];
const f = await fixture(async () => ({ ids: cards.map(card => card._id), total: 30, page: 1, cards }), true);
await f.start('page-key');
assert.equal(f.rows.size, 2); assert.equal(f.rows.get('first').description, 'Removed later');
cards = [{ _id: 'first', title: 'After' }];
f.observers[1].callbacks.changed('first', {}); await tick();
assert.equal(f.rows.get('first').title, 'After'); assert.equal(f.rows.get('first').description, undefined);
cards = [{ _id: 'second', title: 'Next' }];
f.observers[1].callbacks.changed('first', {}); await tick();
assert.equal(f.rows.has('first'), false); assert.equal(f.rows.get('second').title, 'Next');
f.setBoard({ _id: 'board', permission: 'private', members: [] });
f.observers[0].callbacks.changed('board', {});
assert.equal(f.rows.size, 0); await tick(); assert.equal(f.rows.size, 0);
f.stop(); assert.ok(f.observers.every(handle => handle.stopped));
});
test('authorization is rechecked after an in-flight search even before the observer event', async () => {
let finish, entered;
const started = new Promise(resolve => { entered = resolve; });
const f = await fixture(async () => { entered(); return new Promise(resolve => { finish = resolve; }); });
const running = f.start('needle'); await started;
f.setBoard({ _id: 'board', permission: 'private', members: [] });
finish(['formerly-allowed']); await running;
assert.equal(f.rows.size, 0); f.stop();
});
test('invalid or unauthorized requests never search and stopped scans never publish', async () => {
let calls = 0;
for (const term of ['', ' ', 'x'.repeat(513)]) {
const f = await fixture(async () => { calls++; return ['secret']; });
await f.start(term); assert.equal(f.observers.length, 0);
}
const denied = await fixture(async () => { calls++; return ['secret']; });
denied.setBoard({ _id: 'board', permission: 'private', members: [] });
await denied.start('needle'); denied.stop(); assert.equal(calls, 0);
let entered, finish;
const started = new Promise(resolve => { entered = resolve; });
const f = await fixture(async () => { entered(); return new Promise(resolve => { finish = resolve; }); });
const running = f.start('needle'); await started; f.stop(); finish(['secret']); await running;
assert.equal(f.rows.size, 0); assert.ok(f.observers.every(handle => handle.stopped));
});
// The board observer stalled for good on a busy test server (polling, no
// oplog): lifting a member's assigned-only restriction never widened the text
// matches. The periodic access re-check must catch a change the observer never
// reports - in BOTH directions, since a stalled narrowing would leak matches.
test('access changes reach the results even when the board observer never reports them', async () => {
const scopes = [];
const f = await fixture(async args => { scopes.push(args.scope); return args.scope.assignees ? ['assigned'] : ['assigned', 'other']; });
await f.start('needle');
assert.equal(f.intervals.length, 1, 'one access re-check per subscription');
assert.equal(f.intervals[0].ms, 10000);
assert.deepEqual([...f.rows.values()].map(row => row.cardId), ['assigned']);
// Widen: the restriction is lifted, and NO observer callback fires.
f.setBoard({ _id: 'board', permission: 'private', members: [{ userId: 'reader', isActive: true, isReadAssignedOnly: false }] });
await f.intervals[0].fn(); await tick(); await tick();
assert.deepEqual([...f.rows.values()].map(row => row.cardId).sort(), ['assigned', 'other']);
assert.equal(scopes.at(-1).assignees, undefined);
// Negative: narrowing again, also without an observer event, retracts.
f.setBoard({ _id: 'board', permission: 'private', members: [{ userId: 'reader', isActive: true, isReadAssignedOnly: true }] });
await f.intervals[0].fn(); await tick(); await tick();
assert.deepEqual([...f.rows.values()].map(row => row.cardId), ['assigned']);
// Negative: an unchanged board does not rescan on every tick.
const scans = scopes.length;
await f.intervals[0].fn(); await tick();
assert.equal(scopes.length, scans);
f.stop();
assert.deepEqual(f.cleared, [1], 'the re-check stops with the subscription');
});
test('revoked access is retracted by the re-check alone', async () => {
const f = await fixture(async () => ['assigned']);
await f.start('needle');
assert.equal(f.rows.size, 1);
f.setBoard({ _id: 'board', permission: 'private', members: [] });
await f.intervals[0].fn(); await tick(); await tick();
assert.equal(f.rows.size, 0);
f.stop();
});