Fixing a Prototype Override Protection Bypass Vulnerability in qs
![Tim Kadlec](/_next/image/?url=https%3A%2F%2Fres.cloudinary.com%2Fsnyk%2Fimage%2Fupload%2Fv1530707820%2Fwordpress-sync%2Ftim-kadlec.jpg&w=48&q=75)
14 de março de 2017
0 minutos de leituraLast 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:
1qs.parse('a=b&c=d');
2// {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 ]
).
1qs.parse('a[b]=c');
2// {
3// a: {
4// b: "c"
5// }
6// }
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:
1qs.parse('a[hasOwnProperty]=b');
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:
1var paramData = qs.parse("]=hasOwnProperty", { allowPrototypes: false });
2// {hasOwnProperty = true}
3
4paramData.hasOwnProperty('toString');
5// 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.