Relevant source files
- .github/security.md
Security
Introduction
This page provides information on how to safely use escape-string-regexp. It is essential to understand the potential risks and best practices when using this library to prevent security vulnerabilities.
Using escape-string-regexp safely
When using escape-string-regexp, it is crucial to ensure that the input string is properly sanitized to prevent regular expression denial of service (ReDoS) attacks. This can be achieved by:
- Validating user input to prevent malicious data from being passed to the
escape-string-regexpfunction. - Using the
escape-string-regexpfunction with a well-crafted regular expression that is designed to prevent ReDoS attacks.
Preventing ReDoS attacks
To prevent ReDoS attacks, it is essential to use a regular expression that is designed to be efficient and not vulnerable to catastrophic backtracking. This can be achieved by:
- Using a regular expression with a fixed length or a limited number of possible matches.
- Avoiding the use of nested quantifiers or complex patterns that can lead to catastrophic backtracking.
Example use case
const escape = require('escape-string-regexp');
const regex = new RegExp(escape('Hello, world!'), 'g');
console.log(regex); // /Hello,\s*world!/g
Best practices
- Always validate user input to prevent malicious data from being passed to the
escape-string-regexpfunction. - Use the
escape-string-regexpfunction with a well-crafted regular expression that is designed to prevent ReDoS attacks. - Regularly review and update your regular expressions to ensure they are secure and efficient.
Sources: .github/security.md