Skip to content
DoS Advisory Intermediate CVE-2020-26302

GHSL-2020-295: ReDoS (Regular Expression Denial of service) in is.js - CVE-2020-26302

The is.js library contains a regular expression for URL validation that is vulnerable to catastrophic backtracking. An attacker who can supply input to this validation function could cause it to run for an extremely long time, leading to a denial of service.

ghsecuritylab Follow

Originally disclosed on GitHub Security Lab

The project contains one or more regular expressions that are vulnerable to ReDoS (Regular Expression Denial of Service)

ghsecuritylab, on GitHub Security Lab

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 4 min read.

Context

is.js is a JavaScript library that provides a set of type and format checks, such as validating that a value is a URL. The advisory concerns the URL validation path: is.js uses a regular expression copied from a gist to validate URLs. That regular expression is vulnerable to ReDoS (Regular Expression Denial of Service), meaning a malicious input string can make it loop "forever".

The issue is tracked as GHSL-2020-295 and CVE-2020-26302.

Setup

The advisory lists the latest commit at the time of reporting (30 November 2020) as the tested version. The proof of concept uses Node.js and the is_js npm package. No accounts, roles or special configuration are involved: the precondition is simply that an application passes attacker-controlled input to is.url().

Finding it

The vulnerability was found using a CodeQL query which identifies inefficient regular expressions. The reported regular expression is a copy of one published in a gist, and the advisory notes that trying to validate a malicious string can cause the regex to loop "forever".

Exploitation

Step 1: Install the library

Bash
npm install npm install is_js

This is the command exactly as given in the advisory. Note the repeated npm install in the argument list.

Step 2: Call the URL check with a crafted string

Put the following in a .js file and run it with node:

JavaScript
var is = require("is_js");
is.url("http://foobar.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.");

The input is a URL string ending in a long chain of dot-separated 00 segments. The advisory says validating a malicious string can cause the regex to loop "forever". The advisory does not give a measured runtime or a timeout value.

Root cause

ReDoS is a vulnerability affecting poorly constructed and potentially inefficient regular expressions, which can make them perform extremely badly given a creatively constructed input string. For the specific regular expression reported, it is possible to force it to work with an O(2^n) runtime performance when there is exponential backtracking.

ReDoS can be caused by ambiguity or overlap between some regex clauses. Such badly performing regular expressions become a security issue if a user can control the input. For example, if the project is an input validation library, then the project could be used by a server to validate untrusted user input. There is no one size fits all when it comes to fixing ReDoS; in general it is about removing ambiguity or overlap inside the regular expression.

The advisory gives three examples of ReDoS-vulnerable regular expressions and their fixes. The first is an HTML comment matcher:

JavaScript
var reg = /<!--(.|\s)*?-->/g;

The dot matches any character except newlines, and \s matches any whitespace, so both match a space. There are therefore many possible ways for the regular expression to match a sequence of spaces, and an input starting with <!-- followed by a long run of spaces makes the evaluator try every possible way of matching them. The fix removes the overlap:

JavaScript
var reg = /<!--(.|\r|\n)*?-->/g;

The second example matches a snake_case identifier ending in digits:

JavaScript
var reg = /(\w+_?)+_(\d+)/;

Here a repetition matching letters (\w+) sits inside another repetition that can match letters ((\w+_?)+), so for a string starting with lots of letters there are many ways to group them. The fix makes the inner repetition match groups that must end with an underscore:

JavaScript
var reg = /(\w+_)+(\d+)/;

The third example is a regular expression that used to be part of VS Code, with the vulnerable version on top and the fixed version below:

JavaScript
var linkPattern = /(\[((!\[[^\]]*?\]\(\s*)([^\s\(\)]+?)\s*\)\]|(?:\\\]|[^\]])*\])\(\s*)(([^\s\(\)]|\(\S*?\))+)\s*(".*?")?\)/g;
var linkPattern = /(\[((!\[[^\]]*?\]\(\s*)([^\s\(\)]+?)\s*\)\]|(?:\\\]|[^\]])*\])\(\s*)(([^\s\(\)]|\([^\s\(\)]*?\))+)\s*(".*?")?\)/g;

This is very similar to the first example: a section that was too general (\S) was changed to something more specific ([^\s\(\)]) to remove overlap with another part of the regular expression.

Impact

This issue may lead to a denial of service. The advisory frames the risk conditionally: the badly performing expression becomes a security issue if a user can control the input, for instance when a server uses the library to validate untrusted user input. No specific affected deployment, response time or availability outcome is given.

Fix

The advisory does not describe a patch to is.js itself. It explains the general remedy: remove the ambiguity or overlap inside the regular expression, as in the three examples above. The disclosure timeline records that a security contact was requested on 30 November 2020, the deadline expired on 2 March 2021, and the advisory was published on 13 July 2021 under the GitHub SecLab disclosure policy.

Variants to test

  • A positive result is a crafted host made of many dot-separated labels that makes the validation call hang or grow sharply in time as the input lengthens.
  • A positive result is a call whose runtime grows exponentially with input length.
  • A positive result is the same exponential growth on a crafted prefix.

Hunting notes

This kind of bug tends to hide in validation and parsing helpers that accept strings from untrusted sources, especially where a regular expression was copied from a public gist or another project rather than written for the codebase. The report includes a minimal Node.js snippet calling is.url() on a crafted string. The advisory does not record a bounty amount or a CVSS score.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 10 min read.

What happened, in plain words

A small JavaScript library called is.js offers helper functions that check what kind of value you have. One of those helpers, is.url(), answers the question "is this text a web address?". To answer it, the library uses a regular expression, which is a pattern-matching tool built into JavaScript. The pattern in question was copied from a public snippet (a gist) rather than written for the library.

That pattern has a flaw. If you feed it a specially crafted web address, it does not answer quickly. Instead it keeps trying different ways of matching the text, which can grow exponentially with input length. A short input is fine; a slightly longer one takes twice as long; a bit longer again takes four times as long, and so on. This is called a regular expression denial of service, or ReDoS.

The issue was found with a tool called CodeQL, which scans code for patterns known to be dangerous, and it was reported to the project. The report is tracked as GHSL-2020-295 and CVE-2020-26302. The report does not give a bounty amount or a severity score.

The product and the feature

is.js is a JavaScript library: a bundle of ready-made functions that a programmer can add to their own program instead of writing everything from scratch. It provides type and format checks, for example "is this value a number?", "is this a date?" or "is this a URL?".

The advisory concerns only the URL check. Internally, is.url() runs a regular expression over the text you give it and returns true or false. A regular expression (often shortened to regex) is a mini-language for describing text patterns: you write something like "a letter, then any number of digits, then a dot" and the engine tells you whether a given string fits. The engine works by trying possibilities, and if the pattern is written carelessly, the number of possibilities can explode.

The precondition for the bug is simple: some application must pass text that an attacker controls into is.url(). No accounts, roles or special configuration are involved.

How the problem was found

The vulnerability was found using a CodeQL query which identifies inefficient regular expressions. CodeQL is a code analysis tool: you give it a codebase and a query (a question written in its own language), and it reports places in the code that match. The query used here looks for regular expressions whose structure can lead to very slow matching. The report also notes that the URL regex in is.js is a copy of one published in a gist, which is a small public code snippet page.

Reproducing it

Step 1: Install the library

The proof of concept uses Node.js, which is a way of running JavaScript on your computer outside a browser, and the is_js package from npm, the standard package registry for JavaScript. The command given in the report is:

Bash
npm install npm install is_js

Line by line: npm is the package manager program. install tells it to fetch and set up a package. The rest of the line is the package name, but note that the words npm install appear twice. The report gives the command exactly in this form, with the repetition in the argument list.

Step 2: Call the URL check with a crafted string

Put the following in a .js file and run it with node:

JavaScript
var is = require("is_js");
is.url("http://foobar.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.");

Line by line: the first line loads the installed library and stores it in a variable called is, so that its functions can be called. The second line calls the URL check on a string. The string looks like a web address: http:// followed by a host name. The string is http:// followed by foobar and a long chain of dot-separated 00 segments, ending with a dot.

Why this input? The advisory says validating a malicious string can cause the regex to loop "forever". The report does not give a measured runtime or a timeout value; the point is that the call hangs rather than answering.

Why it happens

ReDoS is a vulnerability affecting poorly constructed and potentially inefficient regular expressions, which can make them perform extremely badly given a creatively constructed input string. For the specific regular expression reported, it is possible to force it to work with an O(2^n) runtime performance when there is exponential backtracking.

Two phrases there deserve unpacking. O(2^n) is a way of describing how work grows with input size: here, each extra unit of input roughly doubles the work, so a modest increase in length turns into an enormous increase in time. Backtracking is how a regex engine searches: it makes a choice, continues, and if the rest fails it goes back and tries a different choice. Exponential backtracking means the engine keeps undoing and retrying choices in combinations that multiply.

ReDoS can be caused by ambiguity or overlap between some regex clauses. Such badly performing regular expressions become a security issue if a user can control the input. For example, if the project is an input validation library, then the project could be used by a server to validate untrusted user input. There is no one size fits all when it comes to fixing ReDoS; in general it is about removing ambiguity or overlap inside the regular expression.

The report illustrates the pattern with three examples, which are worth walking through because they show the same mistake in different clothes.

Example 1: matching an HTML comment

JavaScript
var reg = /<!--(.|\s)*?-->/g;

Line by line: the pattern starts with the literal characters <!--, the opening of an HTML comment. Then comes a group, (.|\s), meaning "either any character (.) or any whitespace character (\s)". The *? after the group means "repeat that group any number of times, as few as possible". Finally --> is the closing of the comment. The g at the end is a flag meaning "find all matches in the string".

The problem: the dot matches any character except newlines, and \s matches any whitespace, so both match a space. There are therefore many possible ways for this regular expression to match a sequence of spaces. If the input is a string that starts with <!-- followed by a long sequence of spaces, the evaluator will try every possible way of matching the spaces. The report links to a debugging session on regex101 showing this in action.

The fix is to remove the ambiguity, so that the two alternatives no longer overlap:

JavaScript
var reg = /<!--(.|\r|\n)*?-->/g;

Here the group is (.|\r|\n): any character, or a carriage return, or a newline. Since the dot already excludes newlines, each character now has exactly one way to be matched, and the engine no longer has a pile of equivalent choices to try.

Example 2: matching a snake_case identifier

JavaScript
var reg = /(\w+_?)+_(\d+)/;

Line by line: \w+ means one or more word characters (letters, digits and underscore). _? means an optional underscore. That whole thing, (\w+_?), is wrapped in a group followed by +, so it repeats one or more times. Then comes a literal underscore, then (\d+), one or more digits. In words: a snake_case identifier (words joined by underscores) that ends with some digits.

The problem: for a string starting with lots of letters, there are many ways for the regular expression to match those letters, because the regex has a repetition matching letters (\w+) inside another repetition that can match letters ((\w+_?)+). The evaluator will try every possible grouping of letters into smaller groups of letters. The report again links to a debugging session showing the effect.

The fix is again to remove ambiguity, by changing the inner repetition to match groups of letters that must end with an underscore:

JavaScript
var reg = /(\w+_)+(\d+)/;

Now each repetition of the outer group must consume an underscore, so the boundaries between groups are fixed and there is only one sensible way to divide the text.

Example 3: a link pattern from VS Code

Often the regular expressions are not as simple as the examples above. The report gives a pattern that used to be part of VS Code, the code editor. The top line is the vulnerable version, the bottom line is the fixed one:

JavaScript
var linkPattern = /(\[((!\[[^\]]*?\]\(\s*)([^\s\(\)]+?)\s*\)\]|(?:\\\]|[^\]])*\])\(\s*)(([^\s\(\)]|\(\S*?\))+)\s*(".*?")?\)/g;
var linkPattern = /(\[((!\[[^\]]*?\]\(\s*)([^\s\(\)]+?)\s*\)\]|(?:\\\]|[^\]])*\])\(\s*)(([^\s\(\)]|\([^\s\(\)]*?\))+)\s*(".*?")?\)/g;

Line by line, at a high level: this pattern is meant to find Markdown-style links, including images and optional titles, inside a larger text. It has nested groups, optional parts and alternatives, which is why it looks intimidating. The important difference between the two lines is inside the middle: the vulnerable version has \(\S*?\) and the fixed version has \([^\s\(\)]*?\). In other words, a section that was too general (\S, any non-whitespace character) was changed to something a bit more specific ([^\s\(\)], any character that is neither whitespace nor a parenthesis) to remove overlap with another part of the regular expression. The report notes this is actually very similar to the first example: a broad character class overlapping with a neighbouring clause.

What the impact is

This issue may lead to a denial of service. The report frames the risk conditionally: the badly performing expression becomes a security issue if a user can control the input, for instance when a server uses the library to validate untrusted user input.

How to fix it

It explains the general remedy: remove the ambiguity or overlap inside the regular expression, as in the three examples above. In everyday terms, make sure that at each point in the pattern there is only one obvious way for the engine to proceed, so it never has to try a combinatorial pile of alternatives.

The disclosure timeline records the coordination: a security contact was requested on 30 November 2020, the deadline expired on 2 March 2021, and the advisory was published on 13 July 2021 under the GitHub SecLab disclosure policy. That policy is the set of rules the GitHub Security Lab follows for telling a project about a bug and then making the details public after a set period.

Where else to look

  • A positive result is a crafted host made of many dot-separated labels that makes the validation call hang or grow sharply in time as the input lengthens.
  • A positive result is a call whose runtime grows exponentially with input length.
  • A positive result is the same exponential growth on a crafted prefix.

Hunting notes

This kind of bug tends to hide in validation and parsing helpers that accept strings from untrusted sources, especially where a regular expression was copied from a public gist or another project rather than written for the codebase. The report includes a minimal Node.js snippet calling is.url() on a crafted string.

Glossary

  • Regular expression (regex): a pattern language for describing text, used by programs to test whether a string has a certain shape.
  • ReDoS (Regular Expression Denial of Service): a denial of service caused by a badly written regex that takes enormous time on a crafted input.
  • Denial of service: making a service unavailable to its legitimate users, for example by keeping it busy.
  • Backtracking: how a regex engine searches by making choices and undoing them when the rest of the match fails.
  • Exponential backtracking: backtracking where the number of combinations doubles with each extra unit of input.
  • O(2^n): notation for growth where each additional input unit roughly doubles the work.
  • Ambiguity / overlap: when two parts of a regex can match the same characters, giving the engine many equivalent ways to proceed.
  • Character class: a regex construct such as \s (whitespace) or [^\s\(\)] (anything that is not whitespace or a parenthesis) that matches one character from a set.
  • Quantifier: a regex symbol such as +, * or ? that says how many times the preceding element may repeat.
  • Nested quantifier: a repetition placed inside another repetition, a common source of ReDoS.
  • JavaScript: the programming language used by web pages and by Node.js.
  • Node.js: a program that runs JavaScript outside a browser.
  • npm: the package manager for JavaScript, used to download and install libraries.
  • Library / package: a bundle of reusable code that a programmer adds to a project.
  • is.js: a JavaScript library providing type and format checks, including a URL check.
  • is.url(): the is.js function that tests whether a string is a URL.
  • Gist: a small public code snippet page, often used to share short pieces of code.
  • CodeQL: a code analysis tool that runs queries over a codebase to find patterns such as inefficient regexes.
  • Query: a question written for a code analysis tool, describing what to look for.
  • CVE: a public identifier for a known security vulnerability.
  • GHSL: the identifier prefix used by the GitHub Security Lab for its advisories.
  • Advisory: a public write-up describing a vulnerability, its impact and how to address it.
  • Disclosure policy: the rules a security team follows for reporting a bug privately and then publishing details after a set period.
  • Proof of concept (PoC): a minimal demonstration that a vulnerability can actually be triggered.
  • VS Code: a code editor whose link-matching regex is used as an example in the report.
  • Markdown: a lightweight text formatting syntax, whose links the VS Code example pattern is designed to find.

More by ghsecuritylab

View all

132 more disclosed reports, best first.

More from GitHub Security Lab

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.