Skip to content

dbviewer: harden aggregation and query handling#7690

Closed
ar2rsawseen wants to merge 1 commit into
masterfrom
fix/dbviewer-hardening
Closed

dbviewer: harden aggregation and query handling#7690
ar2rsawseen wants to merge 1 commit into
masterfrom
fix/dbviewer-hardening

Conversation

@ar2rsawseen

Copy link
Copy Markdown
Member

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

  • 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.

Tests

test/unit-tests/plugins.dbviewer.aggregation-guard.js and plugins.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

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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 _id search, scoped single-document lookups, and expanded redaction for members.
  • 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) });
@ar2rsawseen

Copy link
Copy Markdown
Member Author

Superseded — keeping #7686 instead, which retains the full commit history of both PRs. Closing this squashed duplicate.

@ar2rsawseen ar2rsawseen closed this Jun 9, 2026
@ar2rsawseen ar2rsawseen deleted the fix/dbviewer-hardening branch June 9, 2026 07:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants