Giter VIP home page Giter VIP logo

regex_extractor's Introduction

RegEx Extractor

Extract RegEx from Ruby or JavaScript source code.

Background

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.

Extracting

Ruby

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

JavaScript

I have tried several different libs. There are the Pros and Cons:

  1. recast: extract_regex.js

    • Pros: directly get all tokens, really fast
    • Cons: new syntax
  2. @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.
  3. 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

Verifying

  1. Fast check all RegEx with safe-regex

    According to this link, safe-regex reports a lot false positives. Update fast_check.js and run node fast_check.js

  2. 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 or test/js.json and run:

    docker run --rm -v ${pwd}/tests:/tests vuln-regex-detector bin/check-regex.pl /tests/js.json

    Check isVulnerable then.

Requirements

Tools

  • git: search files with git ls-files
  • ripgrep: faster grep to filter files
  • ruby: for Ruby
  • node, ts-node, yarn: for JS
  • vuln-regex-detector: RegEx vulnerability detection
  • docker to run vuln-regex-detector

Installation

  1. clone this repo and run yarn inside the folder
  2. Follow this link to install vuln-regex-detector

regex_extractor's People

Contributors

dependabot[bot] avatar jameschenjav avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.