Files
wekan/tests/searchBoardScope.test.cjs
T
Lauri Ojansivu f122a8287c Add semi-open boards, visible to every signed-in user.
A board's visibility can now be "Signed-in users" (permission 'instance'):
every signed-in user of this WeKan reads it, nobody signed out does, and
only its members edit it, as on a public board. One pure rule,
models/lib/boardPermission.js, feeds isVisibleBy and the shared selector
builder, so the board and card publications, All Boards, search, exports,
attachments, backgrounds, watching, filter presets and rule destinations
follow it. Anonymous paths - link previews, public avatars, the no-token
export branch and archived lists - stay public-only, and a source suite
lists every remaining public-only check so a new one is reviewed. The
Public page and GET /api/boards list instance boards for a signed-in caller
only; "private boards only" refuses them like public ones; a WeKan export
keeps the setting on import. New strings are English, pending Transifex.

Fixes #3249,

Thanks to relikd and xet7 !
2026-09-29 16:28:44 +03:00

164 lines
7.9 KiB
JavaScript

'use strict';
// "Search All Boards" searches all of YOUR boards, not every board on the server.
// Run: node tests/searchBoardScope.test.cjs
//
// The board scope came from `Boards.userBoards`, whose `$or` lists the ways a user
// reaches a board: member, organization, team, e-mail domain — and
// `{ permission: 'public' }`. That last one is the odd one out. It is not a
// relationship to the user at all, it is "anybody may open this", and it belongs
// in the boards LIST, where a public board is meant to be discoverable.
//
// In a search it meant every public board on the instance was searched: on a
// public server a common word answered with strangers' cards, and following a hit
// dropped the user into a board they have no part in. Someone who wants to look
// inside a public board can still open it and search there.
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const ROOT = path.join(__dirname, '..');
const read = rel => fs.readFileSync(path.join(ROOT, rel), 'utf8');
const boards = read('models/boards.js');
const cards = read('server/publications/cards.js');
let passed = 0;
function test(name, fn) {
try {
fn();
passed += 1;
console.log(' ok -', name);
} catch (err) {
console.error(` FAIL - ${name}\n ${err.message}`);
process.exitCode = 1;
}
}
console.log('searchBoardScope:');
test('the public clause is optional, and only that clause', () => {
const at = boards.indexOf('Boards.userBoards = (');
const body = boards.slice(at, boards.indexOf('Boards.userBoardIds', at));
assert.ok(/includePublic = options\.includePublic !== false/.test(body),
'it must default to INCLUDING public boards, so every existing caller is unchanged');
// The `$or` used to be written out here as an array literal, and this test
// matched that literal. GHSA-gwc4-fw7p-gw58 moved it into ONE builder shared
// with the `board` publication - the publication had its own copy that
// ignored isActive, so a revoked org/team/domain share still published the
// whole private board. The BEHAVIOUR this test protects is unchanged, so it
// now asks the builder instead of the literal.
assert.ok(/selector\.\$or = boardVisibilitySelectors\(\{/.test(body),
'the scope comes from the shared builder');
assert.ok(/includePublic,/.test(body), 'and the option is passed to it');
const { boardVisibilitySelectors } = require('../models/lib/boardVisibilitySelectors');
const args = {
userId: 'u', orgIds: ['o'], teamIds: ['t'], emailDomains: ['example.com'],
};
const withPublic = boardVisibilitySelectors(args);
const withoutPublic = boardVisibilitySelectors({ ...args, includePublic: false });
assert.ok(withPublic.some(c => c.permission === 'public'),
'included by default');
assert.ok(!withoutPublic.some(c => c.permission === 'public'),
'the public clause is the one that drops out');
// The four ways a user actually reaches a board must never be optional.
for (const clause of ['members', 'orgs', 'teams', 'domains']) {
assert.ok(withoutPublic.some(c => Object.prototype.hasOwnProperty.call(c, clause)),
`${clause} is a real relationship and must always be in the scope`);
}
// #3249: for a signed-in user the "readable without membership" clauses are
// two - public and 'instance' - and includePublic: false drops both.
assert.ok(withPublic.some(c => c.permission === 'instance'), 'instance boards for a signed-in user');
assert.ok(!withoutPublic.some(c => c.permission === 'instance'), 'and they drop out with public');
assert.ok(!boardVisibilitySelectors({ includePublic: true }).some(c => c.permission === 'instance'),
'never for an anonymous caller');
assert.strictEqual(withPublic.length - withoutPublic.length, 2,
'dropping the public clauses drops nothing else');
});
test('the option reaches userBoardIds, which is what the search calls', () => {
const at = boards.indexOf('Boards.userBoardIds = async (');
const body = boards.slice(at, boards.indexOf('};', at));
assert.ok(/options = \{\}/.test(body), 'it takes the options');
assert.ok(/\}, options\)/.test(body), 'and passes them through');
});
test('every board lookup in the search excludes public boards', () => {
assert.ok(/const SEARCH_BOARD_SCOPE = \{ includePublic: false, membersOnly: true \};/.test(cards),
'the search names its scope once');
// One scope, used by every lookup - if one is missed, that branch of the search
// still reaches the whole instance and nothing looks wrong.
const code = cards.replace(/\/\*[\s\S]*?\*\//g, '').replace(/^\s*\/\/.*$/gm, '');
const lookups = [...code.matchAll(/Boards\.user(?:BoardIds|Search)\([^)]*\)[^;]*/g)]
.map(m => m[0]);
assert.ok(lookups.length >= 5, `expected the search's board lookups, found ${lookups.length}`);
for (const lookup of lookups) {
assert.ok(lookup.includes('SEARCH_BOARD_SCOPE'),
`this lookup still reaches every public board: ${lookup.slice(0, 90)}`);
}
});
test('the board: filter scopes the same way as the search around it', () => {
// It resolves a board NAME to ids. Left unscoped it would happily name a public
// board the search itself can return nothing from - a filter that silently
// empties the results.
const at = cards.indexOf('OPERATOR_BOARD)) {');
const body = cards.slice(at, at + 600);
assert.ok(/Boards\.userSearch\([\s\S]{0,160}SEARCH_BOARD_SCOPE\)/.test(body),
'the board: operator must use the search scope');
});
test('userSearch keeps its old behaviour for anyone who does not ask', () => {
const at = boards.indexOf('Boards.userSearch = (');
const body = boards.slice(at, boards.indexOf('};', at));
// #3249: the "readable without membership" clauses come from
// models/lib/boardPermission.js - public boards always, 'instance' boards for
// a signed-in caller.
assert.ok(/options\.includePublic === false \? \[\] : withoutMembershipSelectors\(!!userId\)/.test(body),
'public boards are still included unless excluded explicitly');
// An anonymous caller with public boards excluded can reach NOTHING. An empty
// `$or` means "match nothing" in Mongo but is an error in some backends, so it
// must be answered directly rather than sent to the database.
assert.ok(/selector\.\$or\.length === 0/.test(body),
'the empty-scope case must be answered without querying');
});
test('generic board helpers retain public access for callers that need it', () => {
// Direct/public discovery still defaults to including public boards. The
// relationship-only All Boards publication opts out at its own call site.
for (const helper of ['Boards.userBoards = (', 'Boards.userBoardIds = async (']) {
assert.ok(boards.includes(helper), `${helper} must exist`);
}
// On the CODE: the comments there explain the option by name, and a guard that
// reads its own explanation fails on it.
const boardsCode = boards.replace(/\/\*[\s\S]*?\*\//g, '').replace(/^\s*\/\/.*$/gm, '');
assert.ok(!/includePublic: false/.test(boardsCode),
'models/boards.js must not switch public boards off for everybody');
for (const rel of ['models/lists.js', 'models/cardComments.js', 'server/models/lists.js']) {
const src = read(rel);
for (const m of src.matchAll(/Boards\.userBoardIds\([^)]*\)/g)) {
assert.ok(!/includePublic/.test(m[0]),
`${rel}: ${m[0]} - these are not the search and are deliberately unchanged`);
}
}
});
console.log(`\nsearchBoardScope: ${passed} tests passed`);
const { boardVisibilitySelectors } = require('../models/lib/boardVisibilitySelectors');
for (const includePublic of [true, false]) {
assert.deepStrictEqual(boardVisibilitySelectors({
userId: 'member', orgIds: ['org'], teamIds: ['team'],
emailDomains: ['example.com'], includePublic, membersOnly: true,
}), [{ members: { $elemMatch: { userId: 'member', isActive: true } } }]);
assert.deepStrictEqual(boardVisibilitySelectors({ membersOnly: true, includePublic }), []);
}
assert.match(boards, /membersOnly: options\.membersOnly === true/);