Skip to content
Prototype pollution Write-up Intermediate

Hunting for Prototype Pollution and it’s vulnerable code on JS libraries

The article describes how to find client-side prototype pollution in JavaScript libraries by using a tool to detect pollution and then debugging to locate the vulnerable code. It shows that a URL parameter can pollute Object.prototype, and the root cause is unsafe parsing of query strings that allows setting arbitrary properties on objects.

infosecwriteups.com

Published on infosecwriteups.com

Bug Bounty

kleiton0x7e, on infosecwriteups.com

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.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 6 min read.

Context

This write-up is about client-side prototype pollution in JavaScript, and specifically about the harder half of the problem: once a scanner tells you a page is polluted, how do you find the exact line of code that does the polluting. The target used throughout is a demo site, https://grey-acoustics.surge.sh, and the tooling is the author's own scanner, ppmap. The article does not introduce the vulnerability itself; it assumes you already know what prototype pollution is and focuses on mass scanning of JavaScript packages (npm packages included) and on manual debugging of more complex JavaScript to reach the root cause.

The feature under examination is ordinary client-side URL parsing: the page reads its query string and builds objects from it. That is exactly where the pollution comes from.

Setup

The only precondition stated is a browser with Developer Tools. Chrome or Chromium is used, because the author considers their DevTools the most compatible for this kind of debugging. No accounts, roles or versions are mentioned.

Finding it

The starting point is a yes/no question: is the site vulnerable to client-side prototype pollution at all? ppmap answers it by automatically trying different payloads that attempt to pollute variables in the global context. After downloading the tool, it is run against the target with a single command:

Bash
echo 'https://grey-acoustics.surge.sh' | ./ppmap

The URL is piped into the tool, which tries different payloads and reported the site as vulnerable. In this case ppmap reported the site as vulnerable.

Prototype Pollution found by ppmap From the original on infosecwriteups.com

The screenshot shows ppmap's output marking the target as vulnerable.

Exploitation

Step 1: confirm the pollution in the browser

Open the site with the payload that ppmap flagged, which is carried in the query string:

HTTP
https://grey-acoustics.surge.sh/?constructor%5Bprototype%5D%5Bppmap%5D=reserved

The parameter name is the URL-encoded form of constructor[prototype][ppmap], so the value reserved is meant to end up on Object.prototype.ppmap. Opening the Console in Developer Tools confirms whether the pollution actually happened.

From the original on infosecwriteups.com

The screenshot shows the page loaded with that query string and the Console used to verify the polluted property.

Step 2: break on the first script

To find the vulnerable code, go to Sources and set a breakpoint on the first script of the page. The reason given is that you do not want the whole JavaScript to execute, because then you would not know when the ppmap gadget gets polluted. After setting the breakpoint, click Resume Script Execution, then refresh the site; it should come up as Paused on debugger.

Set a breakpoint and then resume script execution From the original on infosecwriteups.com

In this case line 7 is the first executed JavaScript, so the breakpoint goes there. Typing ppmap in the Console at this point returns undefined, because the site is stuck on the breakpoint and the property has not been polluted yet.

Website paused on debugger From the original on infosecwriteups.com

The screenshot shows the site paused on the debugger with the breakpoint in place.

Step 3: watch for the pollution with a Snippet

A Snippet is used to detect the moment a property is polluted. The script is taken from this gist and sets a breakpoint as soon as a property (here ppmap) is polluted. To install it, go to Source, click Snippet and create a new one with that code.

Executing the Snippet From the original on infosecwriteups.com

Run the Snippet; the only output on the Console is undefined, which means the Snippet ran successfully.

Step 4: arm the breakpoint from the Console

Back in the Console tab, execute the following:

JavaScript
debugAccess(Object.prototype, 'ppmap')

This sets a breakpoint automatically once a pollution happens to the ppmap property, so execution will be redirected to the vulnerable code where the pollution occurs. There is no output, which the author notes is completely fine.

command executed on console From the original on infosecwriteups.com

The screenshot shows the command executed on the Console.

Step 5: read the call stack

Return to Sources and click Resume script execution. The whole JavaScript now runs and ppmap gets polluted again as expected, and the Snippet lets you see exactly where. Clicking through the Call Stack shows several stacks where the pollution happened.

Which one to pick? Most of the time prototype pollution happens inside JavaScript libraries, so aim for the stack attached to a .js library file; the right-hand side of the panel shows which endpoint each stack belongs to. Here there are two stacks, on line 4 and line 6. The 4th line is chosen because it is the first time the pollution happens, which means that line is the reason for the vulnerability. Clicking the stack redirects to the vulnerable code.

From the original on infosecwriteups.com

The screenshot shows the stack list with the two candidate frames.

With the help of a Beautifier, the vulnerable code of the JS library reads:

JavaScript
params.replace(/\+/g, ' ').split('&').forEach(function(v) {            var param = v.split('='),                key = decodeURIComponent(param[0]),                val, cur = obj,                i = 0,                keys = key.split(']['),                keys_last = keys.length - 1;            if (/\[/.test(keys[0]) && /\]$/.test(keys[keys_last])) {                keys[keys_last] = keys[keys_last].replace(/\]$/, '');                keys = keys.shift().split('[').concat(keys);                keys_last = keys.length - 1;            } else {                keys_last = 0;            }

The excerpt splits the query string on &, splits each pair on =, decodes the key, and splits the key on ][ and [ to recover a nested path such as constructor[prototype][ppmap].

Step 6: the second source of pollution

The library is not the only reason the site is vulnerable. Looking closely at the stacks, there is one more endpoint where ppmap is being polluted, and it is located in the index HTML page.

One more stack to the HTML code From the original on infosecwriteups.com

The screenshot shows the additional stack pointing at the HTML page.

The vulnerable code of the index HTML page is:

JavaScript
var query = deparam(location.search.slice(1));

Anyone with some JavaScript experience will spot the issue directly: the exploitation starts with a payload injected into an input, here URL parsing, that is used to build the client-side logic or rendering of the application. The most common source of input is the URL and its different properties, such as location.search.

Root cause

The page takes location.search, strips the leading ? with slice(1) and hands the rest to deparam. The page hands the query string to deparam, and the report identifies this call as one of the two sources of the pollution. Because the polluted property is then visible on every object in the page, any gadget that reads it inherits the attacker's value. The report does not describe any defence that was in place.

Impact

The report does not spell out a concrete impact beyond the pollution itself. What it demonstrates is that an attacker who can get a crafted query string in front of a victim can set arbitrary properties on Object.prototype in that victim's browser, and that the pollution originates both in a third-party JavaScript library and in the page's own inline code. The consequences depend on which gadgets the application exposes; the author does not enumerate them here.

Fix

The report does not state that anything was changed, and gives no remediation.

Variants to test

  1. Run ppmap against other targets and confirm any hit in the browser with the same constructor[prototype][...] query string. A positive result is the property being readable on the Console after load.
  2. Set a breakpoint on the first script, arm debugAccess(Object.prototype, '<property>') and resume. A positive result is execution pausing inside a .js library file, which identifies the parser to review.
  3. Inspect the call stack for more than one frame. A positive result is a second stack pointing at an HTML page, meaning the page's own code also feeds user input into the parser, as happened here with deparam(location.search.slice(1)).
  4. Beautify the library code and look for query-string parsers that split keys on [ and ][. A positive result is a parser that walks nested keys and assigns without rejecting constructor, prototype or __proto__.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 14 min read.

What happened, in plain words

A security researcher wrote a small tool called ppmap that checks whether a website can be tricked into a bug called prototype pollution. He pointed it at a demo site, https://grey-acoustics.surge.sh, and the tool said the site was vulnerable. The interesting part of the article is what he did next: instead of stopping at "yes, it is vulnerable", he used the browser's built-in developer tools to walk through the site's JavaScript step by step and find the exact lines of code that cause the problem.

He found two causes. One is inside a third-party JavaScript library (a reusable chunk of code the site loads). The other is in the site's own HTML page. Both are involved in taking a value from the address bar and building an object out of it.

You do not need any special software to follow along. You need a computer and a browser with developer tools, which Chrome and Chromium have. The author chose Chrome/Chromium because he considers their developer tools the most compatible for this kind of debugging. No accounts, user roles or software versions are mentioned in the report.

Before going further, two ideas need explaining, because everything else depends on them.

What is an object in JavaScript? Think of an object as a labelled box with compartments. Each compartment has a name (a property) and holds a value. {name: "Sam", age: 30} is an object with two properties. JavaScript also has a shared template that every object is built from, called the prototype. If you look for a property in an object and it is not there, JavaScript checks the prototype instead. This is normally helpful: it is how shared behaviour is reused. It is also the weakness behind prototype pollution.

What is prototype pollution? If an attacker can add a property to the shared prototype, then every object in the page suddenly has that property. It is like slipping an extra page into the master copy of a form that every office in a company uses: now every office's copy has that page, whether they wanted it or not. The site's own code may then read that property and behave in a way the attacker chose.

What is a query string? When you open https://example.com/page?colour=blue, the part after the ? is the query string. The site's JavaScript can read it and use it. That is the feature under examination here: the page reads its query string and builds objects from it. That is exactly where the pollution comes from.

Finding out whether the site is vulnerable

The first question is a simple yes or no: can this site be polluted at all? ppmap answers it by automatically trying different payloads (crafted input values) that attempt to add properties to the shared global objects in the page. After downloading the tool to your own machine, you run it against the target with one command:

Bash
echo 'https://grey-acoustics.surge.sh' | ./ppmap

Line by line:

  • echo 'https://grey-acoustics.surge.sh' prints the site's address to the screen. On its own that does nothing useful.
  • The | symbol is a pipe. It takes whatever the left command printed and hands it to the right command as input, instead of showing it on screen.
  • ./ppmap runs the ppmap program that sits in the current folder (the ./ means "in this folder").

So the whole line means: "here is a website address, ppmap, please test it". The tool tries different payloads and reported the site as vulnerable. In this case it reported the site as vulnerable.

Prototype Pollution found by ppmap From the original on infosecwriteups.com

The screenshot shows ppmap's output marking the target as vulnerable.

Confirming the pollution in the browser

A scanner's word is not proof. The next step is to reproduce the result by hand, so you can see the effect yourself. Open the site with the payload that ppmap flagged. The payload travels in the query string:

HTTP
https://grey-acoustics.surge.sh/?constructor%5Bprototype%5D%5Bppmap%5D=reserved

That address looks strange, so let us unpack it. The part after the ? is one parameter with the name constructor%5Bprototype%5D%5Bppmap%5D and the value reserved. The %5B and %5D sequences are how a URL writes the characters [ and ]; browsers and servers encode them this way because those characters have special meanings in addresses. Decoded, the parameter name is:

Square brackets in a query string are a common convention for saying "nested": read this as "inside constructor, inside prototype, set ppmap". The intent is that the value reserved ends up on Object.prototype.ppmap, the shared prototype of every object. constructor is a property every object has, and it points back to the function that built the object; from there, prototype reaches the shared template.

To check whether it actually happened, open the Console in Developer Tools. The Console is a panel where you can type JavaScript and see its result immediately, and it is the quickest way to inspect what the page currently holds in memory.

From the original on infosecwriteups.com

The screenshot shows the page loaded with that query string and the Console used to verify the polluted property.

Breaking on the first script

Now the real work begins: finding the vulnerable code. The technique is to pause the page's JavaScript at a chosen moment and step through it, which is what a breakpoint does. A breakpoint is a marker you place on a line of code; when the browser reaches that line, it freezes execution and waits for you to tell it to continue. It is like pausing a film at the exact frame you want to examine.

Go to the Sources panel (the part of Developer Tools that lists the files the page has loaded) and set a breakpoint on the first script of the page. Why the first script? Because you do not want the whole JavaScript to execute straight away. If it all ran, the pollution would already have happened and you would have no way to see when the ppmap property got its value. Pausing early keeps the crime scene untouched.

After setting the breakpoint, click Resume Script Execution (the button that lets a paused page continue), then refresh the site. It should come up as Paused on debugger, meaning the browser stopped at your breakpoint.

Set a breakpoint and then resume script execution From the original on infosecwriteups.com

In this case line 7 is the first JavaScript that runs, so the breakpoint goes there. If you now type ppmap in the Console, the answer is undefined, which in JavaScript means "no value". That is expected: the site is stuck on the breakpoint, the rest of the code has not run, and the property has not been polluted yet. This is your baseline, the "before" picture.

Website paused on debugger From the original on infosecwriteups.com

The screenshot shows the site paused on the debugger with the breakpoint in place.

Watching for the pollution with a Snippet

You could click through the code line by line, but that is slow and you might miss the moment. Instead, use a Snippet: a small piece of JavaScript you save inside Developer Tools and run on the current page, separate from the site's own files. Think of it as a sticky note you can stick onto someone else's document without editing the document.

The snippet used here comes from this gist. A gist is simply a small file shared on a code-hosting site. This particular script sets a breakpoint automatically as soon as a property is polluted, in this case the ppmap property. To install it, go to Source, click Snippet, and create a new one containing that code.

Executing the Snippet From the original on infosecwriteups.com

Run the snippet. The only thing you see on the Console is undefined, which here means the snippet ran successfully and returned nothing to show. That is fine; its job is to wait, not to print.

Arming the breakpoint from the Console

Back in the Console tab, run this line:

JavaScript
debugAccess(Object.prototype, 'ppmap')

Line by line:

  • debugAccess is a debugging helper the snippet provides. It watches a chosen object and a chosen property name.
  • Object.prototype is the shared prototype that every object in the page inherits from. This is the target: if anything writes to it, we want to know.
  • 'ppmap' is the property name to watch, written in quotes because it is text.

Put together, the line says: "pause execution the moment something writes a ppmap property onto the shared prototype". That way, when the pollution happens, the browser stops right at the code responsible and shows it to you. There is no output after you run it, which the author notes is completely fine: a silent command that sets a trap is working as intended.

command executed on console From the original on infosecwriteups.com

The screenshot shows the command executed on the Console.

Reading the call stack

Return to Sources and click Resume script execution. The whole JavaScript now runs, ppmap gets polluted again as expected, and the snippet's trap fires. What you see is the call stack: a list of the functions that were running at the moment of the pause, with the most recent at the top. It is like a stack of letters showing who asked whom to do what, so you can trace the request back to its origin.

Clicking through the call stack shows several stacks where the pollution happened. Which one should you pick? Most of the time prototype pollution happens inside JavaScript libraries, so aim for the stack attached to a .js library file. The right-hand side of the panel shows which file (endpoint) each stack belongs to, so you can tell the library frames from the page's own code. Here there are two stacks, on line 4 and line 6. The 4th line is chosen because it is the first time the pollution happens, which means that line is the reason for the vulnerability. Clicking the stack takes you straight to the vulnerable code.

From the original on infosecwriteups.com

The screenshot shows the stack list with the two candidate frames.

With the help of a Beautifier (a tool that reformats compressed, hard-to-read JavaScript into neatly indented lines), the vulnerable code of the library reads:

JavaScript
params.replace(/\+/g, ' ').split('&').forEach(function(v) {            var param = v.split('='),                key = decodeURIComponent(param[0]),                val, cur = obj,                i = 0,                keys = key.split(']['),                keys_last = keys.length - 1;            if (/\[/.test(keys[0]) && /\]$/.test(keys[keys_last])) {                keys[keys_last] = keys[keys_last].replace(/\]$/, '');                keys = keys.shift().split('[').concat(keys);                keys_last = keys.length - 1;            } else {                keys_last = 0;            }

This is a query-string parser, the kind of code that turns ?a=1&b=2 into an object. In plain words, line by line:

  • params.replace(/\+/g, ' ') turns every + into a space, because in URLs a + often stands for a space.
  • .split('&') cuts the string at every &, giving one piece per parameter.
  • .forEach(function(v) { runs the following code once for each piece, calling the current piece v.
  • var param = v.split('=') cuts the piece at =, so param[0] is the name and param[1] would be the value.
  • key = decodeURIComponent(param[0]) decodes the name, turning %5B back into [ and so on.
  • val, cur = obj, i = 0 sets up variables: val will hold the value, cur starts at the object being built, i is a counter.
  • keys = key.split('][') splits the name at ][, and keys_last = keys.length - 1 records the position of the last piece.
  • The if checks whether the name starts with [ and ends with ], the bracket notation for nesting.
  • Inside the if, keys[keys_last].replace(/\]$/, '') removes the trailing ], then keys.shift().split('[').concat(keys) splits the first piece at [ and joins everything back into a list of path segments. For constructor[prototype][ppmap] that list is constructor, prototype, ppmap.
  • The else sets keys_last = 0, meaning a plain name with no brackets is treated as a single segment.

The excerpt stops here; the report does not show the rest of the function.

The second source of pollution

The library is not the only reason the site is vulnerable. Looking closely at the stacks, there is one more place where ppmap is being polluted, and it is located in the index HTML page, the page's own code rather than a library.

One more stack to the HTML code From the original on infosecwriteups.com

The screenshot shows the additional stack pointing at the HTML page.

The vulnerable code of the index HTML page is:

JavaScript
var query = deparam(location.search.slice(1));

Line by line:

  • location.search is the browser's built-in way of reading the query string of the current address, including the leading ?.
  • .slice(1) removes the first character, that ?, leaving just the parameters.
  • deparam(...) is the parser function, the same kind of code examined above, which turns that text into an object.
  • var query = ... stores the result in a variable called query, which the rest of the page then uses.

Anyone with some JavaScript experience will spot the issue directly: the exploitation starts with a payload injected into an input, here URL parsing, that is used to build the client-side logic or rendering of the application. The most common source of input is the URL and its different properties, such as location.search. In other words, the page trusts text that anyone can put in the address bar, and feeds it into a parser that does not filter dangerous names.

Why it works

The page takes location.search, strips the leading ? with slice(1) and hands the rest to deparam. The page hands the query string to deparam, and the report identifies this call as one of the two sources of the pollution. Because the polluted property is then visible on every object in the page, any code that reads it inherits the attacker's value. The report does not describe any defence that was in place.

What it means in practice

The report does not spell out a concrete impact beyond the pollution itself. What it demonstrates is that an attacker who can get a crafted query string in front of a victim can set arbitrary properties on Object.prototype in that victim's browser, and that the pollution originates both in a third-party JavaScript library and in the page's own inline code. The consequences depend on which parts of the application read the polluted property; the author does not enumerate them here.

Was it fixed?

Things to try on other sites

  1. Run ppmap against other targets and confirm any hit in the browser with the same constructor[prototype][...] query string. A positive result is the property being readable on the Console after the page loads.
  2. Set a breakpoint on the first script, arm debugAccess(Object.prototype, '<property>') and resume. A positive result is execution pausing inside a .js library file, which identifies the parser to review.
  3. Inspect the call stack for more than one frame. A positive result is a second stack pointing at an HTML page, meaning the page's own code also feeds user input into the parser, as happened here with deparam(location.search.slice(1)).
  4. Beautify the library code and look for query-string parsers that split keys on [ and ][. A positive result is a parser that walks nested keys and assigns without rejecting constructor, prototype or __proto__.

Glossary

  • Prototype: the shared template that every JavaScript object inherits properties from when it does not have its own.
  • Prototype pollution: a bug where an attacker adds a property to that shared template, so every object in the page gains it.
  • Object: a JavaScript container of named properties and their values, like a labelled box with compartments.
  • Property: one named slot inside an object, holding a value.
  • Payload: a crafted input value designed to trigger a bug.
  • Query string: the part of a URL after the ?, carrying parameters such as ?colour=blue.
  • Parameter: one name-and-value pair inside a query string.
  • URL encoding: writing special characters in a URL as % codes, for example %5B for [ and %5D for ].
  • Scanner: a tool that automatically sends many test inputs to a target and reports which ones worked.
  • ppmap: the author's scanner that tests sites for client-side prototype pollution.
  • Client-side: code that runs in the visitor's browser rather than on the server.
  • Developer Tools: the debugging panel built into browsers, opened with a keyboard shortcut or the browser menu.
  • Console: the Developer Tools panel where you can type JavaScript and see results immediately.
  • Sources panel: the Developer Tools panel that lists the files a page has loaded and lets you set breakpoints.
  • Breakpoint: a marker on a line of code that freezes execution when the browser reaches it.
  • Resume Script Execution: the button that lets a paused page continue running.
  • Snippet: a small piece of JavaScript saved inside Developer Tools and run on the current page, separate from the site's own files.
  • Gist: a small file shared on a code-hosting site.
  • Call stack: the list of functions that were running at the moment execution paused, showing who called whom.
  • Stack frame: one entry in the call stack, pointing at a specific line in a specific file.
  • Endpoint: here, the file or address a stack frame belongs to.
  • Library: reusable code written by someone else that a site loads and uses.
  • Beautifier: a tool that reformats compressed JavaScript into readable, indented lines.
  • Parser: code that reads text in a structured format and turns it into usable data, such as a query string into an object.
  • deparam: the parser function in this report that turns a query string into an object.
  • location.search: the browser property that holds the current URL's query string, including the leading ?.
  • undefined: JavaScript's value meaning "nothing here, no value set".
  • constructor: a property every object has, pointing to the function that created it; it can be used to reach the prototype.
  • __proto__: another name for the link to an object's prototype, often abused in pollution attacks.
  • Gadget: a piece of the application's own code that reads a polluted property and does something with it, turning the pollution into a visible effect.
  • npm: the main public registry of JavaScript packages.
  • Package: a bundle of reusable JavaScript code distributed for others to install.

Similar reports

View all

Same bug class and techniques, clearest first.

Prototype pollution

The lesson behind this report.

Input changes JavaScript's shared object prototype, altering behaviour across the application, sometimes to XSS or code execution.