ExportXMLWordPrintableJSON

    • Type: Bug
    • Resolution: Fixed
    • Priority: Critical - P2
    • 9.1.1, 9.0.12, 8.1.13, 8.0.13
    • Affects Version/s: None
    • Component/s: Query
    • None
    • None
    • 2
    • Ruby Drivers
    • Not Needed
    • None
    • None
    • None
    • None
    • None
    • None

      Description

      Mongoid can be made to execute attacker-supplied JavaScript on the MongoDB server through two distinct paths into Criteria#where. Both were reported by an automated scan (SECBUG-4333 and SECBUG-4332) and both have been confirmed with working proofs of concept against a local cluster. They share a sink and should be fixed together.

      The allow_unsafe_query_operators allowlist added in MONGOID-5939 does not close either path in its current form: it is off by default, it does not inspect nested expressions, and it is not consulted at all on the string path.

      Background: how the two paths differ

      Selectable#where (lib/mongoid/criteria/queryable/selectable.rb:770) dispatches on the type of the criterion:

      • A Hash goes to expr_query (selectable.rb:813). This is where the operator allowlist lives.
      • A String goes to js_query (selectable.rb:845), which emits the string verbatim as $where. There is no config check on either branch of this method.

      Both eventually reach Storable#add_operator_expression (storable.rb:191), which stores the operator and payload into the selector unmodified, and the selector is sent to the server by Contextual::Mongo#view (mongo.rb:273).

      Confirmed issues

      1. Operator allowlist is disabled by default (SECBUG-4333)

      option :allow_unsafe_query_operators, default: true (config.rb:304) means the guard in expr_query short-circuits out of the box. Any hash whose keys the attacker controls can inject $where:

      SecUser.where('$where' => "this.password_hash.charAt(0) == 's'")
      # => selector: {"$where" => "this.password_hash.charAt(0) == 's'"}
      

      A byte-by-byte boolean oracle recovered a password_hash value the query never selected. find_by behaves identically.

      2. The allowlist does not recurse into nested expressions

      $expr is a member of ALLOWED_QUERY_OPERATORS (selectable.rb:806), and the guard only inspects top-level keys. With Mongoid.allow_unsafe_query_operators = false, this still executes attacker JavaScript:

      SecUser.where('$expr' => { '$function' => {
        'body' => 'function(h){ return h.charAt(0) == "h"; }',
        'args' => [ '$password_hash' ], 'lang' => 'js'
      } })
      # => matches; JavaScript is evaluated server-side
      

      3. The string path bypasses the allowlist entirely (SECBUG-4332)

      js_query is never gated. With strict mode explicitly enabled, a plain string still reaches $where:

      Mongoid.allow_unsafe_query_operators = false
      SecUser.where("this.password_hash.charAt(0) == 's'")
      # => selector: {"$where" => "this.password_hash.charAt(0) == 's'"}
      # full password_hash exfiltrated via boolean oracle
      

      This is the higher-risk of the two reports. It requires no attacker-controlled hash keys, only a String arriving where a Hash was expected, which is ordinary Rails parameter type confusion (params[:x] is a String by default). It also affects find_by/find_by! (findable.rb:220, 239), the find_or_create_by family (modifiable.rb:198), and association find_or (association/many.rb:216), all of which forward their argument to where unchanged. The negating branch wraps the same payload as {"$and" => [{"$nor" => [\{"$where" => ...\}]}]} and is equally exploitable.

      The only existing guard on this path, Criteria#where (criteria.rb:523), raises UnsupportedJavascript solely for embedded criteria. Top-level queries fall through to super.

      Scope of the fix

      1. Change allow_unsafe_query_operators to default to false.
      2. Make the guard apply to nested expressions, or remove $expr from the allowlist. Whichever approach we take, $function and $accumulator must be rejected anywhere in the selector when the allowlist is active.
      3. Gate the string path in js_query on the same setting, so strict mode actually means strict.
      4. Decide and document the deprecation story for the String form of where. Unlike the hash path, where("this.age == 50") is documented, intentional API surface with existing specs. Turning it off under strict mode is a behavior change for legitimate users and needs a release note; we may also want a dedicated escape hatch so that apps relying on it can opt in without re-enabling every unsafe operator.
      5. Audit the other front doors (find_by, find_by!, find_or_create_by family, association find_or) to confirm they inherit the new behavior rather than routing around it.

      Acceptance criteria

      • With default configuration, Model.where and find_by reject $where and $function supplied as either a top-level key or a nested one.
      • With default configuration, a String criterion does not reach the server as $where.
      • Both proofs of concept from SECBUG-4333 and SECBUG-4332 fail to exfiltrate data.
      • Existing specs that assert $where/$function pass-through are updated to reflect the new default, and the opt-in path is covered by tests.
      • Behavior change is documented in the release notes and upgrade guide.

      Notes on the original reports

      Two details in the scanner output are inaccurate and should not be carried forward:

      • Top-level $function is rejected by the server (unknown top level operator: $function). The working vector is $expr -> $function.
      • SECBUG-4332 names valid_for_count_documents? (mongo.rb:1019) as the sink. That helper only chooses between count_documents and estimated_document_count. The actual sink is collection.find at mongo.rb:273.

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

              Created:
              Updated:
              Resolved: