Files
wekan/tests/mongoSelectorSafety.test.cjs
T
xet7 d2ec3ee400 Unit + negative tests for newest features (lazy loading, streaming export, backup)
Extract the pure, security- and correctness-critical logic of the recent features
into Meteor-free models/lib/* modules (so production and tests share one
implementation) and add plain-Node tests with positive AND negative cases, wired
into test:unit:node:

- models/lib/mongoSelectorSafety.js hasWhere() — the windowed card publication
  refuses a client selector carrying $where (server-side JS). Tests: safe
  selectors pass; $where at top level / in $and / deep in arrays / in $elemMatch
  is rejected. (was inline in server/publications/cardsWindow.js)
- models/lib/cardsLoading.js resolveCardsLoadingMode / parseCardsLoadingEnv /
  windowCountId — CARDS_LOADING maps to 'lazy' only for exactly "lazy" (garbage
  never enables the experimental mode); a falsy swimlane id collapses to one
  count-doc id (listBody uses undefined, listHeader uses ''). (used by
  server/cards-loading.js, server/models/settings.js, client/lib/lazyCards.js)
- models/lib/base64Chunk.js encodeAligned/encodeFinal — streaming an attachment
  into the JSON export as base64 in any chunk sizes reconstructs exactly the
  whole-file base64; aligned pieces never carry padding. (used by
  Exporter.buildStream)
- models/lib/backupPaths.js filesRootFrom/scheduleText — WRITABLE_PATH -> files
  root (no double /files suffix, Windows-safe) and the synced-cron schedule text
  (daily/weekly/monthly + defaults, unknown frequency falls back to daily). (used
  by server/methods/backup.js)

41 new assertions across 4 files, all passing; whole test:unit:node suite green.
2026-07-11 00:14:49 +03:00

78 lines
2.6 KiB
JavaScript

'use strict';
// Plain-Node unit test (no Meteor) for the windowed-card publication's selector
// safety check. Run: node tests/mongoSelectorSafety.test.cjs
//
// The lazy card-loading feature (CARDS_LOADING=lazy) lets the client pass the
// exact selector it renders with to the `boardCardsWindow` publication, which
// runs it against the database. hasWhere() must refuse any selector carrying a
// `$where` clause (server-side JS execution) at ANY depth, while never flagging
// an ordinary field/operator selector.
const assert = require('assert');
const { hasWhere } = require('../models/lib/mongoSelectorSafety');
let passed = 0;
function test(name, fn) {
fn();
passed += 1;
console.log(' ok -', name);
}
// --- NEGATIVE (safe selectors → hasWhere returns false) ---------------------
test('empty selector is safe', () => {
assert.strictEqual(hasWhere({}), false);
});
test('plain field selector is safe', () => {
assert.strictEqual(hasWhere({ listId: 'l1', archived: false }), false);
});
test('operators and $in arrays are safe', () => {
assert.strictEqual(
hasWhere({ swimlaneId: { $in: ['s1', null, ''] }, boardId: 'b1' }),
false,
);
});
test('nested $and / $or are safe', () => {
assert.strictEqual(
hasWhere({ $and: [{ listId: 'l1' }, { $or: [{ a: 1 }, { b: 2 }] }] }),
false,
);
});
test('a field literally named where (no $) is safe', () => {
assert.strictEqual(hasWhere({ where: 'somewhere', location: { where: 'x' } }), false);
});
test('null / non-object inputs are safe', () => {
assert.strictEqual(hasWhere(null), false);
assert.strictEqual(hasWhere(undefined), false);
assert.strictEqual(hasWhere('string'), false);
assert.strictEqual(hasWhere(42), false);
});
// --- POSITIVE (malicious selectors → hasWhere returns true) -----------------
test('top-level $where is rejected', () => {
assert.strictEqual(hasWhere({ $where: 'this.x == 1' }), true);
});
test('$where as a function value is rejected', () => {
assert.strictEqual(hasWhere({ $where: function () { return true; } }), true);
});
test('$where nested inside $and is rejected', () => {
assert.strictEqual(
hasWhere({ $and: [{ listId: 'l1' }, { $where: 'sleep(9999)' }] }),
true,
);
});
test('$where deep inside an array element is rejected', () => {
assert.strictEqual(
hasWhere({ $or: [{ a: 1 }, { b: { $in: [{ $where: 'x' }] } }] }),
true,
);
});
test('$where inside $elemMatch is rejected', () => {
assert.strictEqual(
hasWhere({ members: { $elemMatch: { $where: 'evil' } } }),
true,
);
});
console.log(`\nmongoSelectorSafety: ${passed} tests passed`);