Extract RegEx from Ruby or JavaScript source code.
To prevent ReDoS we need to find out all the Regular Expressions in our codebase. This project is to help find all literal RegEx in Ruby or JavaScript source code.
Ruby provides Parser::CurrentRuby
to parse Ruby code. It's very straightforward to use.
Usage:
ROOT_PATH=<Your git root> ./find_rb_regexp.rb > out/rb_re.json
I have tried several different libs. There are the Pros and Cons:
-
recast
: extract_regex.js- Pros: directly get all tokens, really fast
- Cons: new syntax
-
@babel/parser
: ext_regex.js- Pros: pretty fast
- Cons: need to install
@babel/core
and plugins individually. the instruction is not very clear when it goes wrong. hard to configure.
-
typescript
: ext_re.ts- Pros: all-in-one, no need other packages.
- Cons: really slow. a little bit confusing how to use it.
Usage: run
env ROOT_PATH=<Your git root> ./find_js_regexp.rb > out/js_re.json
-
Fast check all RegEx with safe-regex
According to this link,
safe-regex
reports a lot false positives. Updatefast_check.js
and runnode fast_check.js
-
Verify possible vulnerable RegEx with vuln-regex-detector
This is really slow but accurate. Run it against all RegEx if possible.
Follow the instruction to install docker. Then edit
tests/rb.json
ortest/js.json
and run:docker run --rm -v ${pwd}/tests:/tests vuln-regex-detector bin/check-regex.pl /tests/js.json
Check
isVulnerable
then.
git
: search files withgit ls-files
ripgrep
: fastergrep
to filter filesruby
: for Rubynode
,ts-node
,yarn
: for JS- vuln-regex-detector: RegEx vulnerability detection
docker
to runvuln-regex-detector
- clone this repo and run
yarn
inside the folder - Follow this link to install
vuln-regex-detector