Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix - Alternate solution to CWE-1333 | Inefficient Regular Expression… #971

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
12 changes: 12 additions & 0 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ function setup(env) {
newDebug.log = this.log;
return newDebug;
}
/**
* Escapes special characters in a string for use in a regular expression.
* @param {string} str - The string to escape.
* @returns {string} The escaped string.
*/
function escapeRegexp(str) {
if (typeof str !== 'string') {
return str;
}
return str.replace(/[+?^${}()|[\]\\]/g, '\\$&');
}

/**
* Enables a debug mode by namespaces. This can include modes
Expand All @@ -160,6 +171,7 @@ function setup(env) {
* @api public
*/
function enable(namespaces) {
namespaces = escapeRegexp(namespaces);
createDebug.save(namespaces);
createDebug.namespaces = namespaces;

Expand Down