npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vuln-regex-detector

v1.3.0

Published

Detect vulnerable regexes by querying a service hosted at Virginia Tech.

Downloads

4,656

Readme

Summary

In JavaScript, regular expressions (regexes) can be "vulnerable": susceptible to catastrophic backtracking. If your application is used on the client side, this can be a performance issue. On the server side, this can expose you to Regular Expression Denial of Service (REDOS).

This module lets you check a regex for vulnerability.

Example

const vulnRegexDetector = require('vuln-regex-detector');

const regex = /(a+)+$/; // RegExp
const pattern = regex.source; // String

const cacheConfig = {
	type: vulnRegexDetector.cacheTypes.persistent
};
const config = {
	cache: cacheConfig
};

/* This runs synchronously so it's expensive.
 * It uses a persistent cache, so subsequent queries in this process or another one
 * can be resolved locally. */
const result = vulnRegexDetector.testSync(regex, config);
console.log(`I got ${result}`);

vulnRegexDetector.test(pattern, config)
	.then((result) => {
		if (result === vulnRegexDetector.responses.vulnerable) {
			console.log('Regex is vulnerable');
		} else if (result === vulnRegexDetector.responses.safe) {
			console.log('Regex is safe');
		} else {
			console.log('Not sure if regex is safe or not');
		}
	});

API

The module exports:

  • functions test and testSync for making queries
  • macro cacheTypes for use specifying config.cache
  • a set of responses responses for interpreting results

test

/**
 * @param regex: RegExp or string (e.g. /re/ or 're')
 * @param [config]: provide a config object like this:
 *  {
 *    server: {
 *      hostname: 'toybox.cs.vt.edu',
 *      port: 8000
 *    },
 *    cache: {
 *      type: cacheTypes.persistent,
 *      [persistentDir]: '/tmp/vuln-regex-detector-client-persistentCache'
 *    }
 *  }
 *
 * Config defaults if not provided:
 *   server: indicated in the example. This is a research server at Virginia Tech.
 *   cache: 'persistent' with persistentDir in a subdir of os.tmpdir().
 *
 * @returns Promise fulfilled with responses.X or rejected with responses.invalid.
 */
vulnRegexDetector.test (regex, config)

testSync

/**
 * @param regex: see checkRegex API
 * @param [config]: see checkRegex API
 *
 * @returns synchronous result: responses.X
 *
 * Since this makes a synchronous HTTP query it will be slow.
 */
vulnRegexDetector.testSync (regex, config)

NB: This API makes synchronous HTTP queries, which can be slow. You should not use it in server software. On an AWS micro instance this API can be called about 200 times per minute.

This API is intended for use in CI contexts where performance is less critical. For use in CI, see this module. If your application defines many regexes dynamically you might want to write your own CI stage.

responses

If fulfilled, the returned Promise gets one of the following values:

  • responses.vulnerable
  • responses.safe
  • responses.unknown

If rejected, the returned Promise gets the value:

  • responses.invalid

Implementation details

This module queries a server hosted at Virginia Tech. When you use it, your regex will be shipped (via HTTPS) to the server and tested there.

If the regex has not been seen before, the server will respond "unknown" and test it in the background. The server cannot test synchronously because testing is expensive (potentially minutes) and there might be a long line.

If the server has not seen the regex before, it should have an answer if you query it again in a few minutes.

If you cannot connect to the server or your query is malformed, you'll get the answer "invalid".

Optimizations

This module maintains a persistent local cache stored in os.tmpdir() to reduce the number of HTTP queries.

Privacy

By using this module you are consenting to send us your regexes. If your code is not open-source then feel free to host your own service. See here for details, and specify your service's hostname and port in config when you call the API.

We may:

  • store them
  • analyze properties of your queries
  • release the regexes as a public dataset for future researchers

The IP address of any client querying our server will be anonymized.

Related projects

  1. https://github.com/olivo/redos-detector
  2. https://github.com/substack/safe-regex
  3. https://github.com/google/re2

How is this module different from safe-regex?

  1. This module guarantees no false positives. If it reports a vulnerable regex, then there is an attack string that produces catastrophic backtracking in JavaScript (Node.js). If you're curious, you can obtain this attack string by using the check-regex.pl tool in this repo.
  2. This module guarantees far fewer false negatives. safe-regex uses a heuristic called star height which will miss a lot of regexes that are actually dangerous. safe-regex misses about 90% of vulnerabilities by my estimate.

Contributing

Issues and PRs welcome.

License

MIT