adobe / adobe/Marinus

Useless regular-expression character escape

Open
#8 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
62
Forks
21
PR merge metrics
No merged PRs in 30d

Description

Using a regular expression with string literal (`new RegExp('[a-z]+')`) instead of regular expression slash syntax (`/[a-z]+/`) should be done with caution.
Usually, to escape `.` (dot) symbol within a regular expression, a developer would use backslash. But when it's done within a string literal (`new RegExp('\.')`) JavaScript will first process the string itself and only then call RegExp constructor.
It means `new RegExp('\.')` is equivalent to `/./`, which unintendedly allows to match any character instead of the intended dot only.
This may result in some security issues. For example, consider the hostname matching regular expression `new RegExp('www\.mysite\.com')`. In this case an input of `http://www-mysite.com/` will pass the validation.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.