dbviewer: harden aggregation and query handling#7690
Closed
ar2rsawseen wants to merge 1 commit into
Closed
Conversation
Consolidated DB Viewer hardening. Adds two small, unit-tested guard modules
(plugins/dbviewer/api/parts/aggregation_guard.js, query_guard.js) and applies
them on both the global-admin and non-admin code paths:
Aggregation:
- validate stages at every depth, including $facet sub-pipelines (recursive
allow-list); require an explicit allow-list match so inherited Object
prototype keys (constructor/__proto__) are never treated as allowed
- block joins/unions into the redacted collections (members/auth_tokens),
block write stages ($out/$merge), and block server-side-JS operators
($function/$accumulator/$where) — all detected by a full deep walk so any
(incl. future) nested stage shape is covered
- place the members/auth_tokens redaction as the first pipeline stage so no
user stage can read the raw fields first
Find / document:
- restrict projections to strict include/exclude (0/1/booleans), dropping
expression / field-path alias values; normalize a non-object projection to {}
- treat the _id search term (sSearch) as a literal (regex-escaped, no ReDoS)
- scope single-document lookups to the caller's apps (same base filter as the
listing path)
- redact members.two_factor_auth alongside password/api_key
- cap result size (limit / iDisplayLength) and return generic 500 messages
instead of raw MongoDB errors
Adds unit tests covering the aggregation guard and query guard.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This was referenced Jun 9, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens the DB Viewer plugin’s MongoDB query and aggregation handling by extracting guard logic into unit-tested modules and applying additional protections consistently across both global-admin and non-admin paths.
Changes:
- Added a recursive aggregation-stage sanitizer plus deep-walk detectors for protected-collection joins, write stages, and server-side-JS operators.
- Hardened find/document handling: strict projection sanitization, regex-escaped
_idsearch, scoped single-document lookups, and expanded redaction formembers. - Added unit tests covering projection sanitization, regex escaping, recursive aggregation sanitization, and deep-walk detection of blocked constructs.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
plugins/dbviewer/api/api.js |
Integrates the new guard modules, adds stricter query/aggregation bounds and redaction, and avoids returning raw Mongo errors. |
plugins/dbviewer/api/parts/aggregation_guard.js |
New aggregation guard module (recursive stage allow-list + deep-walk detectors for joins/write stages/server-side JS). |
plugins/dbviewer/api/parts/query_guard.js |
New query guard module (projection sanitization + regex escaping helper). |
test/unit-tests/plugins.dbviewer.aggregation-guard.js |
Unit tests for recursive stage sanitization and protected-collection join detection. |
test/unit-tests/plugins.dbviewer.query-guard.js |
Unit tests for projection sanitization, regex escaping, and deep-walk detection of write stages / server-side JS. |
Comment on lines
405
to
+409
| function aggregate(collection, aggregation, changes) { | ||
| if (params.qstring.iDisplayLength) { | ||
| aggregation.push({ "$limit": parseInt(params.qstring.iDisplayLength) }); | ||
| var iDisplayLength = parseInt(params.qstring.iDisplayLength, 10); | ||
| if (!isNaN(iDisplayLength) && iDisplayLength > 0) { | ||
| aggregation.push({ "$limit": Math.min(iDisplayLength, MAX_DBVIEWER_LIMIT) }); |
Member
Author
|
Superseded — keeping #7686 instead, which retains the full commit history of both PRs. Closing this squashed duplicate. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Consolidated DB Viewer hardening (supersedes #7685 and #7686, combined into one reviewable PR). Adds two small, unit-tested guard modules and applies them on both the global-admin and non-admin code paths.
Aggregation
$facetsub-pipelines (recursive allow-list); require an explicit allow-list match so inheritedObject.prototypekeys (constructor/__proto__) are never treated as allowed.members/auth_tokens), block write stages ($out/$merge), and block server-side-JS operators ($function/$accumulator/$where) — all detected by a full deep walk, so any (incl. future) nested stage shape is covered.members/auth_tokensredaction as the first pipeline stage so no user stage can read the raw fields first.Find / document
0/1/booleans), dropping expression / field-path alias values; normalize a non-object projection to{}._idsearch term (sSearch) as a literal (regex-escaped, no ReDoS).members.two_factor_authalongsidepassword/api_key.limit/iDisplayLength) and return generic 500 messages instead of raw MongoDB errors.Tests
test/unit-tests/plugins.dbviewer.aggregation-guard.jsandplugins.dbviewer.query-guard.js— 33 cases covering recursive sanitization, prototype-key handling, deep-walk detection of protected-collection joins / write stages / server-side-JS, projection sanitization, regex escaping. All passing.🤖 Generated with Claude Code