You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Constructing a regular expression with unsanitized user input is dangerous as a malicious user may be able to modify the meaning of the expression. In particular, such a user may be able to provide a regular expression fragment that takes exponential time in the worst case, and use that to perform a Denial of Service attack.
/*- ReDos PoCNote: running this poc on system can cause hang or crash try on vmalso this is just for testing vulnerable the regular expression code*/const{ exec }=require('child_process');functionstart(){varre=process.argv[2] ? newRegExp(process.argv[2],'i') : /./;constbenchmarks=[{name: 'benchmark1'},{name: 'benchmark2'},{name: 'benchmark3'}];benchmarks.filter(function(b){returnre.test(b.name);}).forEach(function(b){console.log(b.name);});}// PoC for ReDoS attackif(require.main===module){constcomplexRegex='^(a+)+$';exec(`node ${process.argv[1]} "${complexRegex}"`,(error,stdout,stderr)=>{if(error){console.error(`exec error: ${error}`);return;}console.log(`stdout: ${stdout}`);console.error(`stderr: ${stderr}`);});}start();
More Example
The following example shows a HTTP request parameter that is used to construct a regular expression without sanitizing it first
varexpress=require('express');varapp=express();app.get('/findKey',function(req,res){varkey=req.param("key"),input=req.param("input");// BAD: Unsanitized user input is used to construct a regular expressionvarre=newRegExp("\\b"+key+"=(.*)\n");});
Instead, the request parameter should be sanitized first, for example using the function _.escapeRegExp from the lodash package. This ensures that the user cannot insert characters which have a special meaning in regular expressions.
varexpress=require('express');var_=require('lodash');varapp=express();app.get('/findKey',function(req,res){varkey=req.param("key"),input=req.param("input");// GOOD: User input is sanitized before constructing the regexvarsafeKey=_.escapeRegExp(key);varre=newRegExp("\\b"+safeKey+"=(.*)\n");});
Before embedding user input into a regular expression, use a sanitization function such as lodash's _.escapeRegExp to escape meta-characters that have special meaning.
Regular expression injection
Constructing a regular expression with unsanitized user input is dangerous as a malicious user may be able to modify the meaning of the expression. In particular, such a user may be able to provide a regular expression fragment that takes exponential time in the worst case, and use that to perform a Denial of Service attack.
PoC
More Example
The following example shows a HTTP request parameter that is used to construct a regular expression without sanitizing it first
Instead, the request parameter should be sanitized first, for example using the function _.escapeRegExp from the lodash package. This ensures that the user cannot insert characters which have a special meaning in regular expressions.
or you can use
safe-regex
to avoid this issueRecommendation
Before embedding user input into a regular expression, use a sanitization function such as lodash's _.escapeRegExp to escape meta-characters that have special meaning.
References
ReDos
The text was updated successfully, but these errors were encountered: