Mongo BSON Injection: Ruby Regexps Strike Again
A regular expression in the BSON-ruby library used to validate MongoDB ObjectIds incorrectly allowed newline characters, so an attacker could supply a crafted ObjectId containing binary data. This could cause a denial of service or, with newer versions, inject arbitrary BSON data into the database socket, potentially bypassing authentication.
sakurity.com
Published on sakurity.com
TL;DR There’s a subtle bug in BSON-ruby implementation, leading in best case scenario to low-severity DoS, but most likely to critical BSON Injection (similar to SQL injection) - depends on gem versions you use.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
The regular expression used to validate ObjectIds used ^ and $ anchors, which in Ruby match at line boundaries, so a trailing newline was accepted and the ObjectId was not strictly validated.
Impact
An attacker could cause a denial of service by forcing repeated requests, or with newer versions inject arbitrary BSON data into the database socket, potentially bypassing authentication and executing arbitrary database commands.
What the attacker needed
Application uses vulnerable BSON-ruby version; attacker can control the _id parameter
How it was found
- Send an ObjectId consisting of 24 'a' characters followed by a newline character.
- The legal? method returns true because the regex allows the newline.
- MongoDB receives the corrupted ObjectId and responds with an assertion error.
- Moped interprets the error as MongoDB being down and retries 39 more times, keeping the worker busy for 5 seconds.
- With newer BSON-ruby using ^$, send an ObjectId containing arbitrary binary data and newlines to inject BSON commands.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
MongoDB uses a format called BSON to store data, and ObjectIds are supposed to be 24-character hex strings. A validation check in a Ruby library used a regular expression that mistakenly allowed a newline character at the end, so a malicious ObjectId could sneak in extra data. This let an attacker either crash the application or inject their own database commands.
What is SQL injection?
Input reaches a database query as code, letting an attacker read or change data the application never meant to expose.