ExportXMLWordPrintableJSON

    • Type: Bug
    • Resolution: Fixed
    • Priority: Critical - P2
    • 9.1.1, 9.0.12, 8.1.13, 8.0.13, 7.6.2
    • Affects Version/s: 9.1.0, 8.1.12, 8.0.12, 7.6.1, 9.0.11
    • Component/s: Query
    • None
    • None
    • 2
    • Minor Change
    • Ruby Drivers
    • Not Needed
    • None
    • None
    • None
    • None
    • None
    • None

      Description

      The allow_unsafe_query_operators allowlist added in MONGOID-5939 does not cover the string form of where. Close the gap so that Mongoid.allow_unsafe_query_operators = false actually blocks $where on every path into the query builder.

      Selectable#where type-dispatches: a Hash criterion goes to expr_query, which
      enforces the allowlist, while a String criterion goes to js_query and is emitted
      verbatim as $where with no check at all. The identical clause is therefore
      rejected as a hash and accepted as a string:

      Mongoid.allow_unsafe_query_operators = false
      
      Account.where('$where' => "this.username == 'alice'")  # raises InvalidQuery
      Account.where("this.username == 'alice'")              # executes on the server
      

      The string-to-$where behavior is intentional, documented, long-standing API. The
      defect is the incomplete mitigation: MONGOID-5939's stated purpose is to reject
      $where when user input reaches the query builder, and it covers only one of the
      two paths, so anyone who opts into strict mode gets false assurance.

      Verified against SECBUG-4332. See that ticket for the full reproduction, including
      oracle-based extraction of unqueried fields and per-document CPU exhaustion.

      Affected code

      • lib/mongoid/criteria/queryable/selectable.rb:778-782 - where dispatches any String to js_query, bypassing expr_query
      • lib/mongoid/criteria/queryable/selectable.rb:845-855 - js_query emits $where with no config check on either the plain or $nor branch
      • lib/mongoid/config.rb:298-304 - the allow_unsafe_query_operators documentation promises rejection of $where, which the string path does not honor

      Proposed approach

      Make js_query honor the same flag as expr_query: when
      allow_unsafe_query_operators is false, raise Errors::InvalidQuery rather
      than building the $where clause. This is the minimal change that makes the flag
      mean what it says, and it does not alter default behavior, since the flag still
      defaults to true.

      Both branches of js_query need the guard, the plain $where and the $nor
      form used by not.where.

      The error message should match the existing one from expr_query closely enough
      that operators recognize it as the same control, while making clear the trigger was
      a string criterion rather than an explicit operator.

      Open for discussion: whether to also deprecate the string form of where outright.
      That is a larger behavior change for existing applications and probably belongs in
      its own ticket, but it is worth deciding now whether this fix is the endpoint or a
      first step.

      Acceptance criteria

      • With allow_unsafe_query_operators = false, Model.where(<string>) raises Errors::InvalidQuery.
      • Same for Model.find_by, find_by!, the find_or_ and first_or_ families, and association find_or, all of which forward their argument to where.
      • Same for Model.not.where(<string>), which takes the $nor branch.
      • With allow_unsafe_query_operators = true (the default), all of the above behave exactly as they do today. No change to default behavior.
      • Embedded criteria continue to raise UnsupportedJavascript as they do now (criteria.rb:522); that path is already covered and should not regress.
      • allow_unsafe_query_operators documentation in config.rb updated to state that it governs the string form as well.
      • Specs added to spec/mongoid/criteria/queryable/selectable_where_spec.rb alongside the existing allow_unsafe_query_operators contexts, covering both config values and both js_query branches.

      Notes

      • SECBUG-4332 cites lib/mongoid/contextual/mongo.rb:1019 (valid_for_count_documents?) as the sink. That is incidental; the helper only selects between count APIs. The actual sink is the driver view filter on any read operation. No change needed there.
      • Verification was done against a local replica set (MongoDB 8.2.12, Mongoid 9.1.0, driver 2.24.1) with server-side JavaScript at its default. Atlas and sharded deployments were not tested; $where availability may differ there.
      • Decide whether this needs backporting to the 9.x stable branch, given that MONGOID-5939 shipped the incomplete allowlist.

            Assignee:
            Jamis Buck
            Reporter:
            Jamis Buck
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: