Fixing a Prototype Override Protection Bypass Vulnerability in qs

Tim Kadlec's avatar Tim Kadlec

Last month, we added a high-severity Prototype Override Protection Bypass vulnerability in the qs package to our database. The fix was released in updated versions of the library about a week ago. This post explains the vulnerability and how to mitigate it.

qs is a popular npm package—just under 40 million downloads over the past month—used to parse querystring parameters into objects. For example:

// {a:'b', c: 'd'}

While that’s certainly helpful, where qs really shines is with the advanced functionality it enables. Using qs, you can even create nested objects within you query strings by using brackes ([ or ]).

// {
//   a: {
//     b: "c"
//   }
// }

The Vulnerability

The functionality to create nested objects within your querystring parameters brings some risk along with it. If not properly vetted, you could potentially overwrite properties in the object’s prototype. For example, you could attempt to overwrite the object’s hasOwnProperty method:


Thankfully, the above wouldn’t work. By default, qs ignores any parameters (such as hasOwnProperty) that would overwrite properties of the object prototype. It accomplishes this by checking for the presence of opening and closing brackets in the parameter, grabbing the chunk inside and then comparing to the object prototype to see if it’s a native property. There is an option to allow prototype overrides, but qs strongly advises against it.

Unfortunately, a gap in the validation meant that you could still override a property in the object prototype by prefixing the parameter with an unmatched [ or ] character.

For example, the following would override the object’s hasOwnProperty method even though we’ve explicitly told qs to disallow prototype overrides:

var paramData = qs.parse("]=hasOwnProperty", { allowPrototypes: false });
// {hasOwnProperty = true}

// Results in Type Error: paramData.hasOwnProperty is not a function

The most likely outcome of this would be breakage and unexpected fragility in your application, but depending on your application logic, it could be much more severe: even in some cases allowing attackers to change the execution flow of your application.

The fix

Our security research team discovered the issue on February 13th and reported it to package owner. Moving quickly, the package owner released a fix three days later in versions 6.0.3, 6.1.1, 6.2.2, 6.3.1.

The fix solved the issue if the parameter began with ]= but it wasn’t enough—it turns out qs was still vulnerable if the attacker used [=. As a result, the package owner updated their logic and released the more robust fix in versions 6.4.0, 6.3.2, 6.2.3, 6.1.2 and 6.0.4.

To address the issue, you’ll need to update to one of these versions of the package. If you’re using Snyk to monitor your project, you’ve probably already been prompted to perform the update either using an automatically generated pull-request or by running snyk wizard using the CLI.

Otherwise, you’ll need to check your application to see if you’re using the qs package—whether as a direct dependency listed in your package.json file or as an unlisted dependency that one of your direct dependency pulls in—and update to the latest version.

Building a Snyk Plugin for VS Code: An Interview with Peter Benjamin

March 16, 2017

Peter Benjamin recently built a fantastic VS Code plugin or Snyk. We asked him a few questions about the plugin and how and why he built it.

The Frequency of Known Vulnerabilities in JavaScript Libraries

March 09, 2017

An interesting whitepaper was released at the 2017 NDSS Symposium discussing a large-scale attempt at determining just how vulnerable client-side JavaScript libraries are. We wanted to share some of our thoughts on the report.

Subscribe to The Secure Developer Podcast

A podcast about security for developers, covering tools and best practices.

Find out more

Interested in web security?

Subscribe to our newsletter:

Get realtime updates and fixes for JavaScript, Ruby and Java vulnerabilities that affect your applications