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.
Recommendation
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.
POC
The following shows a HTTP request parameter that is used to construct a regular expression without sanitizing it first:
var express = require('express');
var app = express();
app.get('/findKey', function(req, res) {
var key = req.param("key"), input = req.param("input");
// BAD: Unsanitized user input is used to construct a regular expression
var re = new RegExp("\\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");});
This agent has been deprecated and we don't maintain this repository anymore nor provide builds. See https://github.com/logdna/logdna-agent-v2 for the currently maintained codebase.
logdna-agent/index.js
Line 279 in 2313b15
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.
Recommendation
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.POC
The following 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.References
OWASP: Regular expression Denial of Service - ReDoS
Wikipedia: ReDoS
npm: lodash
Common Weakness Enumeration: CWE-730
Common Weakness Enumeration: CWE-400
The text was updated successfully, but these errors were encountered: