ExportXMLWordPrintableJSON

    • Type: Bug
    • Resolution: Fixed
    • Priority: Minor - P4
    • 5.12.0
    • Affects Version/s: 5.9.0
    • None
    • 3
    • Not Needed
    • None
    • None
    • None
    • None
    • None
    • None

      Context

      The DatabaseQuery MCP tool shipped in mongodb/laravel-mongodb (src/Tools/DatabaseQuery.php) is annotated #[IsReadOnly] and enforces a command and aggregation-stage allow-list. Its input validation has two gaps that let server-side JavaScript execution operators reach mongod unexamined.

      Gaps

      Gap 1: find/count/distinct filters are never inspected.
      handleMql() at src/Tools/DatabaseQuery.php:117-120 only calls ensureNoNestedWriteInAggregation() for aggregate. For find (filter key "filter"), count and distinct (filter key "query"), a $where operator in the filter is forwarded to Database::command() at line 122 unchanged. $where executes arbitrary JavaScript per scanned document in mongod.

      Gap 2: aggregation stage bodies are never inspected.
      ensureNoNestedWriteInAggregation() at src/Tools/DatabaseQuery.php:194-199 validates only the stage name against an allow-list. For any allowed stage not in $supportsNestedWrites (facet, lookup, unionWith), it continues at line 199 without reading $stageBody. A $match whose body contains $expr.$function (arbitrary JS) passes through to command() as-is.

      Affected versions

      Introduced in commit 1bf7f53 (PHPLARA-251), first released in 5.9.0. Present in 5.9.0 through 5.11.0 (current latest). The file has had no other commit since introduction.

      Triage outcome

      Validated against the current source (both gaps reproduced with a mocked-connection capture of the command document that reaches Database::command()).

      Per the Threat Model: Drivers and the DBX Mythos Findings Triage Guide, this is assessed as valid bug, not a security bug:

      • User-composed MQL is explicitly allowed (Threat #1 excludes "user-composed MQL passed directly into the driver"); $where/$function/$accumulator are valid MQL operators.
      • Builders are not expected to validate input; NoSQL injection via user-fed input is a correctness concern, with input vetting being the user's responsibility (Assumption 1 / AR1).
      • Impact is a recoverable server-side DoS (CPU exhaustion in mongod), Low. The mongod JavaScript sandbox blocks write primitives from these operators, so there is no data confidentiality or integrity impact beyond the query access the caller a
        lready has.
      • The #[IsReadOnly] agentic auto-approve dimension is an AI-integration concern (ABX remit), out of scope for the driver/ODM threat model.

      No CVE. The SECBUG is closed as "Fix Not Needed, reason: valid bug, not a security bug." This ticket tracks the defense-in-depth hardening only.

      Suggested fix

      In src/Tools/DatabaseQuery.php:
      1. Add `private const JS_EXECUTION_OPERATORS = ['$where', '$function', '$accumulator'];`
      2. Add `ensureNoJsExecution(array $document, int $level = 1): void`, a recursive walk that throws InvalidArgumentException when any key is in JS_EXECUTION_OPERATORS, recursing into array values (with the existing MAX_NESTING_LEVEL guard).
      3. Gap 1: in handleMql(), for non-aggregate commands, call ensureNoJsExecution() on $command['filter'] ?? $command['query'] ?? [].
      4. Gap 2: in ensureNoNestedWriteInAggregation(), call ensureNoJsExecution($stageBody) for every allowed stage before the continue at line 199.
      5. Regression tests in tests/Tools/DatabaseQueryTest.php: reject $where in find/count/distinct filters; reject $function/$accumulator inside $match/$group/$project stage bodies.
      6. Update the DatabaseQuery docstring to state that the tool expects trusted, validated input (AR1 compensating control).

      Notes

      • Keep all work local until the fix is ready; nothing public until release.
      • SECBUG-5395 is rated Medium while SECBUG-5389 is rated Low; flagged to Product Security to reconcile.

              Assignee:
              Pauline Vos
              Reporter:
              Jérôme Tamarelle
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved: