jade (latest)
Published 06 Feb, 2018
Regular Expression Denial of Service (DoS)
Detailed paths
- Introduced through: jade@1.11.0 > transformers@2.1.0 > uglify-js@2.2.5
Overview
The parse()
function in the uglify-js
package prior to version 2.6.0 is vulnerable to regular expression denial of service (ReDoS) attacks when long inputs of certain patterns are processed.
Denial of Service (DoS) describes a family of attacks, all aimed at making a system inaccessible to its original and legitimate users. There are many types of DoS attacks, ranging from trying to clog the network pipes to the system by generating a large volume of traffic from many machines (a Distributed Denial of Service - DDoS - attack) to sending crafted requests that cause a system to crash or take a disproportional amount of time to process.
The Regular expression Denial of Service (ReDoS) is a type of Denial of Service attack. Regular expressions are incredibly powerful, but they aren't very intuitive and can ultimately end up making it easy for attackers to take your site down.
Let’s take the following regular expression as an example:
regex = /A(B|C+)+D/
This regular expression accomplishes the following:
A
The string must start with the letter 'A'(B|C+)+
The string must then follow the letter A with either the letter 'B' or some number of occurrences of the letter 'C' (the+
matches one or more times). The+
at the end of this section states that we can look for one or more matches of this section.D
Finally, we ensure this section of the string ends with a 'D'
The expression would match inputs such as ABBD
, ABCCCCD
, ABCBCCCD
and ACCCCCD
It most cases, it doesn't take very long for a regex engine to find a match:
$ time node -e '/A(B|C+)+D/.test("ACCCCCCCCCCCCCCCCCCCCCCCCCCCCD")'
0.04s user 0.01s system 95% cpu 0.052 total
$ time node -e '/A(B|C+)+D/.test("ACCCCCCCCCCCCCCCCCCCCCCCCCCCCX")'
1.79s user 0.02s system 99% cpu 1.812 total
The entire process of testing it against a 30 characters long string takes around ~52ms. But when given an invalid string, it takes nearly two seconds to complete the test, over ten times as long as it took to test a valid string. The dramatic difference is due to the way regular expressions get evaluated.
Most Regex engines will work very similarly (with minor differences). The engine will match the first possible way to accept the current character and proceed to the next one. If it then fails to match the next one, it will backtrack and see if there was another way to digest the previous character. If it goes too far down the rabbit hole only to find out the string doesn’t match in the end, and if many characters have multiple valid regex paths, the number of backtracking steps can become very large, resulting in what is known as catastrophic backtracking.
Let's look at how our expression runs into this problem, using a shorter string: "ACCCX". While it seems fairly straightforward, there are still four different ways that the engine could match those three C's:
- CCC
- CC+C
- C+CC
- C+C+C.
The engine has to try each of those combinations to see if any of them potentially match against the expression. When you combine that with the other steps the engine must take, we can use RegEx 101 debugger to see the engine has to take a total of 38 steps before it can determine the string doesn't match.
From there, the number of steps the engine must use to validate a string just continues to grow.
String | Number of C's | Number of steps |
---|---|---|
ACCCX | 3 | 38 |
ACCCCX | 4 | 71 |
ACCCCCX | 5 | 136 |
ACCCCCCCCCCCCCCX | 14 | 65,553 |
By the time the string includes 14 C's, the engine has to take over 65,000 steps just to see if the string is valid. These extreme situations can cause them to work very slowly (exponentially related to input size, as shown above), allowing an attacker to exploit this and can cause the service to excessively consume CPU, resulting in a Denial of Service.
Remediation
Upgrade to version 2.6.0
or greater.
If a direct dependency update is not possible, use snyk wizard
to patch this vulnerability.
References
Regular Expression Denial of Service (ReDoS)
Detailed paths
- Introduced through: jade@1.11.0 > clean-css@3.4.28
Overview
clean-css
is a fast and efficient CSS optimizer for Node.js platform and any modern browser.
Affected versions of this package are vulnerable to Regular Expression Denial of Service (ReDoS) attacks. This can cause an impact of about 10 seconds matching time for data 70k characters long.
Disclosure Timeline
- Feb 15th, 2018 - Initial Disclosure to package owner
- Feb 20th, 2018 - Initial Response from package owner
- Mar 6th, 2018 - Fix issued
- Mar 7th, 2018 - Vulnerability published
Details
Denial of Service (DoS) describes a family of attacks, all aimed at making a system inaccessible to its original and legitimate users. There are many types of DoS attacks, ranging from trying to clog the network pipes to the system by generating a large volume of traffic from many machines (a Distributed Denial of Service - DDoS - attack) to sending crafted requests that cause a system to crash or take a disproportional amount of time to process.
The Regular expression Denial of Service (ReDoS) is a type of Denial of Service attack. Regular expressions are incredibly powerful, but they aren't very intuitive and can ultimately end up making it easy for attackers to take your site down.
Let’s take the following regular expression as an example:
regex = /A(B|C+)+D/
This regular expression accomplishes the following:
A
The string must start with the letter 'A'(B|C+)+
The string must then follow the letter A with either the letter 'B' or some number of occurrences of the letter 'C' (the+
matches one or more times). The+
at the end of this section states that we can look for one or more matches of this section.D
Finally, we ensure this section of the string ends with a 'D'
The expression would match inputs such as ABBD
, ABCCCCD
, ABCBCCCD
and ACCCCCD
It most cases, it doesn't take very long for a regex engine to find a match:
$ time node -e '/A(B|C+)+D/.test("ACCCCCCCCCCCCCCCCCCCCCCCCCCCCD")'
0.04s user 0.01s system 95% cpu 0.052 total
$ time node -e '/A(B|C+)+D/.test("ACCCCCCCCCCCCCCCCCCCCCCCCCCCCX")'
1.79s user 0.02s system 99% cpu 1.812 total
The entire process of testing it against a 30 characters long string takes around ~52ms. But when given an invalid string, it takes nearly two seconds to complete the test, over ten times as long as it took to test a valid string. The dramatic difference is due to the way regular expressions get evaluated.
Most Regex engines will work very similarly (with minor differences). The engine will match the first possible way to accept the current character and proceed to the next one. If it then fails to match the next one, it will backtrack and see if there was another way to digest the previous character. If it goes too far down the rabbit hole only to find out the string doesn’t match in the end, and if many characters have multiple valid regex paths, the number of backtracking steps can become very large, resulting in what is known as catastrophic backtracking.
Let's look at how our expression runs into this problem, using a shorter string: "ACCCX". While it seems fairly straightforward, there are still four different ways that the engine could match those three C's:
- CCC
- CC+C
- C+CC
- C+C+C.
The engine has to try each of those combinations to see if any of them potentially match against the expression. When you combine that with the other steps the engine must take, we can use RegEx 101 debugger to see the engine has to take a total of 38 steps before it can determine the string doesn't match.
From there, the number of steps the engine must use to validate a string just continues to grow.
String | Number of C's | Number of steps |
---|---|---|
ACCCX | 3 | 38 |
ACCCCX | 4 | 71 |
ACCCCCX | 5 | 136 |
ACCCCCCCCCCCCCCX | 14 | 65,553 |
By the time the string includes 14 C's, the engine has to take over 65,000 steps just to see if the string is valid. These extreme situations can cause them to work very slowly (exponentially related to input size, as shown above), allowing an attacker to exploit this and can cause the service to excessively consume CPU, resulting in a Denial of Service.
Remediation
Update clean-css
to version 4.1.11 or higher.
References
Sandbox Bypass
Detailed paths
- Introduced through: jade@1.11.0 > constantinople@3.0.2
Overview
constantinople
determines whether a JavaScript expression evaluates to a constant (using acorn).
Affected versions of this package are vulnerable to a sandbox bypass which can lead to arbitrary code execution.
Remediation
Upgrade constantinople
to version 3.1.1 or higher.
References
Improper minification of non-boolean comparisons
Detailed paths
- Introduced through: jade@1.11.0 > transformers@2.1.0 > uglify-js@2.2.5
Overview
uglify-js
is a JavaScript parser, minifier, compressor and beautifier toolkit.
Tom MacWright discovered that UglifyJS versions 2.4.23 and earlier are affected by a vulnerability which allows a specially crafted Javascript file to have altered functionality after minification. This bug was demonstrated by Yan to allow potentially malicious code to be hidden within secure code, activated by minification.
Details
In Boolean algebra, DeMorgan's laws describe the relationships between conjunctions (&&
), disjunctions (||
) and negations (!
).
In Javascript form, they state that:
!(a && b) === (!a) || (!b)
!(a || b) === (!a) && (!b)
The law does not hold true when one of the values is not a boolean however.
Vulnerable versions of UglifyJS do not account for this restriction, and erroneously apply the laws to a statement if it can be reduced in length by it.
Consider this authentication function:
function isTokenValid(user) {
var timeLeft =
!!config && // config object exists
!!user.token && // user object has a token
!user.token.invalidated && // token is not explicitly invalidated
!config.uninitialized && // config is initialized
!config.ignoreTimestamps && // don't ignore timestamps
getTimeLeft(user.token.expiry); // > 0 if expiration is in the future
// The token must not be expired
return timeLeft > 0;
}
function getTimeLeft(expiry) {
return expiry - getSystemTime();
}
When minified with a vulnerable version of UglifyJS, it will produce the following insecure output, where a token will never expire:
( Formatted for readability )
function isTokenValid(user) {
var timeLeft = !( // negation
!config // config object does not exist
|| !user.token // user object does not have a token
|| user.token.invalidated // token is explicitly invalidated
|| config.uninitialized // config isn't initialized
|| config.ignoreTimestamps // ignore timestamps
|| !getTimeLeft(user.token.expiry) // > 0 if expiration is in the future
);
return timeLeft > 0
}
function getTimeLeft(expiry) {
return expiry - getSystemTime()
}
Remediation
Upgrade UglifyJS to version 2.4.24
or higher.
References
Vulnerable versions of jade
Fixed in 1.9.2
Prototype Pollution
Detailed paths
- Introduced through: hapi@1.9.1 > hoek@0.9.1
- Introduced through: hapi@1.9.1 > boom@0.4.2 > hoek@0.9.1
- Introduced through: hapi@1.9.1 > cryptiles@0.2.2 > boom@0.4.2 > hoek@0.9.1
- Introduced through: hapi@1.9.1 > iron@0.3.3 > cryptiles@0.2.2 > boom@0.4.2 > hoek@0.9.1
- Introduced through: hapi@1.9.1 > iron@0.3.3 > boom@0.4.2 > hoek@0.9.1
- Introduced through: hapi@1.9.1 > joi@0.4.0 > hoek@0.9.1
- Introduced through: hapi@1.9.1 > catbox@1.0.0 > hoek@0.9.1
- Introduced through: hapi@1.9.1 > iron@0.3.3 > hoek@0.9.1
- Introduced through: hapi@1.9.1 > hawk@1.1.2 > hoek@1.5.2
- Introduced through: hapi@1.9.1 > hawk@1.1.2 > boom@1.2.1 > hoek@1.5.2
- Introduced through: hapi@1.9.1 > hawk@1.1.2 > cryptiles@1.0.1 > boom@1.2.1 > hoek@1.5.2
- Introduced through: hapi@1.9.1 > hawk@1.1.2 > sntp@1.0.9 > hoek@2.16.3
- Introduced through: jade@1.9.1 > coveralls@2.13.3 > request@2.79.0 > hawk@3.1.3 > hoek@2.16.3
- Introduced through: jade@1.9.1 > coveralls@2.13.3 > request@2.79.0 > hawk@3.1.3 > boom@2.10.1 > hoek@2.16.3
- Introduced through: jade@1.9.1 > coveralls@2.13.3 > request@2.79.0 > hawk@3.1.3 > cryptiles@2.0.5 > boom@2.10.1 > hoek@2.16.3
- Introduced through: jade@1.9.1 > coveralls@2.13.3 > request@2.79.0 > hawk@3.1.3 > sntp@1.0.9 > hoek@2.16.3
Overview
hoek is a Utility methods for the hapi ecosystem.
Affected versions of this package are vulnerable to Prototype Pollution.
The utilities function allow modification of the Object
prototype. If an attacker can control part of the structure passed to this function, they could add or modify an existing property.
PoC by Olivier Arteau (HoLyVieR)
var Hoek = require('hoek');
var malicious_payload = '{"__proto__":{"oops":"It works !"}}';
var a = {};
console.log("Before : " + a.oops);
Hoek.merge({}, JSON.parse(malicious_payload));
console.log("After : " + a.oops);
Remediation
Upgrade hoek
to versions 4.2.1, 5.0.3 or higher.
References
Insecure Randomness
Detailed paths
- Introduced through: hapi@1.9.1 > cryptiles@0.2.2
- Introduced through: hapi@1.9.1 > iron@0.3.3 > cryptiles@0.2.2
- Introduced through: hapi@1.9.1 > hawk@1.1.2 > cryptiles@1.0.1
- Introduced through: jade@1.9.1 > coveralls@2.13.3 > request@2.79.0 > hawk@3.1.3 > cryptiles@2.0.5
Overview
cryptiles is a package for general crypto utilities.
Affected versions of this package are vulnerable to Insecure Randomness. The randomDigits()
method is supposed to return a cryptographically strong pseudo-random data string, but it was biased to certain digits. An attacker could be able to guess the created digits.
Remediation
Upgrade to version 4.1.2 and higher.
References
Uninitialized Memory Exposure
Detailed paths
- Introduced through: jade@1.9.1 > coveralls@2.13.3 > request@2.79.0 > tunnel-agent@0.4.3
Overview
tunnel-agent
is HTTP proxy tunneling agent. Affected versions of the package are vulnerable to Uninitialized Memory Exposure.
A possible memory disclosure vulnerability exists when a value of type number
is used to set the proxy.auth option of a request request
and results in a possible uninitialized memory exposures in the request body.
This is a result of unobstructed use of the Buffer
constructor, whose insecure default constructor increases the odds of memory leakage.
Details
Constructing a Buffer
class with integer N
creates a Buffer
of length N
with raw (not "zero-ed") memory.
In the following example, the first call would allocate 100 bytes of memory, while the second example will allocate the memory needed for the string "100":
// uninitialized Buffer of length 100
x = new Buffer(100);
// initialized Buffer with value of '100'
x = new Buffer('100');
tunnel-agent
's request
construction uses the default Buffer
constructor as-is, making it easy to append uninitialized memory to an existing list. If the value of the buffer list is exposed to users, it may expose raw server side memory, potentially holding secrets, private data and code. This is a similar vulnerability to the infamous Heartbleed
flaw in OpenSSL.
Proof of concept by ChALkeR
require('request')({
method: 'GET',
uri: 'http://www.example.com',
tunnel: true,
proxy:{
protocol: 'http:',
host:"127.0.0.1",
port:8080,
auth:80
}
});
You can read more about the insecure Buffer
behavior on our blog.
Similar vulnerabilities were discovered in request, mongoose, ws and sequelize.
Remediation
Upgrade tunnel-agent
to version 0.6.0 or higher.
Note This is vulnerable only for Node <=4