Files
wekan/server/lib/selectorGuard.js
T
Lauri Ojansivu b4ebe48d7b WhereBleed: eight Admin Panel handlers ran the caller's selector unchecked.
GHSA-phm4-4v26-j2vq, CWE-943, CVSS 5.8. The Admin Panel's people, org, team and
translation publications and their companion count/page methods take a query
selector from the client and validate only its TYPE:

    check(query, Match.OneOf(Object, null));

which is not validation, because a MongoDB selector is executable data. `$where`
makes the database run the caller's JavaScript once per document scanned, so

    Meteor.subscribe('team', { $where: 'while(true){}' }, 25, 0)

pins a database worker for as long as the caller likes and is repeatable: denial
of service for every tenant on the instance, from one narrowly-scoped account.
The reporter demonstrated both halves on v10.81 against mongo:7 - `$where:
'sleep(2000) || true'` made the `people` subscription take 2.03s and return the
document, `$where: 'false'` returned nothing in 0.00s - which is the caller
deciding in JavaScript which documents come back.

It needs an authenticated admin session, so it is not reachable by a board member
or a visitor. It is still worth the fix at this severity because the people and
org surfaces are open to a PER-TENANT Global Admin, a role meant to be confined
to one Organization, and those two wrap the caller's selector as
`{ $and: [query, restriction] }` rather than stripping execution operators out of
it - so merging the tenant restriction never removed the `$where`, and a role
scoped to one tenant reached instance-wide impact.

WHAT MAKES THIS ONE PARTICULAR is that the defence was already here. classifySelector
(models/lib/injectionDetect.js) and hasWhere (models/lib/mongoSelectorSafety.js)
were written for exactly this class and were wired into the card-window
publication, with unit tests. Eight sibling handlers taking the identical shape of
client-supplied selector simply never called them. So this adds no new detection
logic: cardsWindow.js's own helper moves, unchanged, to
/server/lib/selectorGuard.js, and all nine call sites share the one copy. A second
copy of the check would be the same bug set up to happen again, which is why
cardsWindow.js now imports rather than keeps its own.

Each handler refuses with the "match nothing" selector that publication already
uses in production, `{ _id: { $in: [] } }` - frozen, so a caller cannot mutate the
shared object into one that matches - so a refused request returns an empty result
instead of throwing at an admin mid-page, and the canary still records who sent
what from where. Ordinary Admin Panel behaviour is untouched: none of the
searches, filters, regexes, $or/$and/$in/$elemMatch or date ranges those panes
send contains an execution operator, and the test pins fourteen real ones going
through unchanged.

FerretDB, WeKan's default database, rejects $where itself, so this degrades to a
rejected query there; the supported real-MongoDB path
(docker-compose-mongodb-v7.yml) is where it was reproduced. MongoDB 7 also happens
to reject $where inside the aggregation pipeline the four count methods use, but
that is an engine-level accident for one operator on one call path - $function and
$accumulator are legitimate aggregation operators - so the count methods are
guarded like the rest rather than left to it.

tests/selectorGuard.test.cjs pins the reported payloads (including nested inside
$and/$or, which is how the tenant merge would carry them), the other execution
operators, the fourteen ordinary queries, that all nine call sites route through
the guard, that none still passes the raw selector afterwards, and that exactly
one copy of the check exists. Three existing suites asserted the old shape -
canaryCoverage on where the canary trips, injectionDetect on the helper living in
cardsWindow.js, tenantWiring on the scope function receiving the raw query - and
each is updated to the new one, saying why.

Thanks to TungNGo02 and xet7 !
2026-08-11 15:50:31 +03:00

68 lines
3.3 KiB
JavaScript

// selectorGuard — refuse a client-supplied MongoDB selector that carries an
// EXECUTION operator, and record who sent it.
//
// GHSA-phm4-4v26-j2vq (WhereBleed). A publication or method that takes a query
// selector from the client and validates only its TYPE —
//
// check(query, Match.OneOf(Object, null));
//
// — has not validated it at all. A MongoDB selector is executable data: `$where`
// makes the database run the caller's JavaScript once per document scanned, so
// `{ $where: 'while(true){}' }` pins a database worker for as long as it likes,
// repeatably, and `{ $where: 'sleep(2000) || true' }` is a document-inclusion
// oracle whose truth value the caller controls. Reported against the Admin
// Panel's people, org, team and translation publications and their companion
// count/page methods — eight handlers, all taking the same shape of selector.
//
// The defence is NOT new here. classifySelector (models/lib/injectionDetect.js)
// and hasWhere (models/lib/mongoSelectorSafety.js) were written for exactly this
// and are wired into the card-window publication; the eight handlers simply never
// called them. This module is that publication's own helper, lifted out
// unchanged so there is ONE copy for every caller — the bug was eight places not
// doing what one place did, and a second copy of the check would be the same
// mistake waiting to happen again.
//
// Callers use the refusal cardsWindow.js already uses in production:
//
// const safe = selectorIsInjection(query, 'people') ? MATCH_NOTHING : query;
//
// MATCH_NOTHING is `{ _id: { $in: [] } }`: a selector that is valid, cheap and
// matches no document, so a refused request returns an empty result instead of
// throwing. Nothing an ordinary Admin Panel search, filter or page sends is
// affected — none of it contains an execution operator.
//
// See docs/Security/Remediation/WeKan.md §12.6 for the canary, and
// tests/selectorGuard.test.cjs for what counts as an injection.
import { tripCanary } from '/server/lib/canary';
const { hasWhere } = require('/models/lib/mongoSelectorSafety');
const { classifySelector, injectionDetail } = require('/models/lib/injectionDetect');
// The refusal: a well-formed selector that cannot match anything. Frozen so a
// caller cannot mutate the shared object into one that matches something.
export const MATCH_NOTHING = Object.freeze({ _id: Object.freeze({ $in: Object.freeze([]) }) });
// True when `selector` is an attempt to make the database run something rather
// than a query. `where` names the call site and only reaches the canary record,
// so an operator can see WHICH surface was probed.
export function selectorIsInjection(selector, where) {
const verdict = classifySelector(selector);
if (!verdict.injection && !hasWhere(selector)) return false;
tripCanary('injection.nosql-selector', {
detail: injectionDetail(
verdict.injection ? verdict : { kind: 'execution', operators: ['$where'] },
where,
),
});
return true;
}
// The whole pattern in one call, since every one of the nine call sites wants
// exactly this: hand back the selector, or a selector that matches nothing.
export function safeSelector(selector, where) {
return selectorIsInjection(selector, where) ? MATCH_NOTHING : selector;
}
export default { MATCH_NOTHING, selectorIsInjection, safeSelector };