Skip to content
DoS Advisory Intermediate CVE-2022-30126CVE-2022-33879

GHSL-2022-021: Regular Expression Denial of Service (ReDoS) in Apache Tika - CVE-2022-30126, CVE-2022-33879

Apache Tika's StandardsText class used a regular expression with nested repetition that could cause catastrophic backtracking when parsing a text file containing a long sequence of digits, leading to exponential time consumption. An attacker could exploit this to cause a denial of service by resource consumption.

ghsecuritylab Follow

Originally disclosed on GitHub Security Lab

Apache Tika up to version 1.28.1 is vulnerable to Regular Expression Denial of Service (ReDoS) in the way it handles standard references in text files. Specially crafted files may cause catastrophic backtracking, taking exponential time to complete.

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

Apache Tika is a content analysis toolkit that extracts text and metadata from many file formats. One of its components, StandardsText, extracts standard references from text files: things like document numbers followed by uppercase standard headers. To find those headers it matches a regular expression against the input.

The report covers a Regular Expression Denial of Service (ReDoS) in that matching logic. Apache Tika up to version 1.28.1 is affected: specially crafted files can cause catastrophic backtracking, taking exponential time to complete. Two CVEs were assigned, CVE-2022-30126 for the original issue and CVE-2022-33879 for a bypass of the first fix.

Setup

The issue was tested against Apache Tika 1.28.1.

The vulnerable code path is reached when a text file is parsed with the StandardsExtractingContentHandler class. That handler calls StandardsText.extractStandardReferences, which calls StandardsText.findHeaders, which applies the regular expression.

To reproduce, the report uses the StandardsExtractionExample shipped with Tika. One important precondition: JDK 9 introduced important mitigations for this problem, so the example must be run with JDK =< 8 to observe the issue.

Finding it

The researcher looked at the regular expression used to match uppercase standard headers in StandardsText.java:

Java
private static final String REGEX_HEADER =
        "(\\d+\\.(\\d+\\.?)*)\\p{Blank}+([A-Z]+(\\s[A-Z]+)*){5,}";

The problem is the nested repetition at (\\d+\\.?)*. When an input contains a long sequence of numbers and there is no full match after it, the regex engine has to work out which part of the expression matched which characters: either \\d+, or the * after the parentheses, since \\. is optional. To distinguish between those possibilities it needs to backtrack exponentially.

Exploitation

Step 1: Create a payload file

The payload is a text file containing a number followed by a long run of 9s:

$ cat /tmp/test/test.txt
2.9999999999999999999999999999999

The digits after the dot are the sequence that drives the backtracking. The time needed to parse the file grows exponentially with the length of that sequence of 9s.

Step 2: Run the example against the file

From the tika-examples directory, the StandardsExtractionExample is run with the directory containing the payload as its argument:

$ cd tika-examples

$ mvn compile exec:java -Dexec.mainClass="org.apache.tika.example.StandardsExtractionExample" -Dexec.args="/tmp/test"

Because the file is parsed through StandardsExtractingContentHandler, the header regex is evaluated against the crafted content and the engine enters catastrophic backtracking. Lengthening the run of 9s makes the parse take exponentially longer, which is the denial of service.

Root cause

The regex (\\d+\\.(\\d+\\.?)*)\\p{Blank}+([A-Z]+(\\s[A-Z]+)*){5,} contains a nested quantifier: a group ending in * whose inner element \\d+ is itself a repetition, and whose separator \\. is optional. When the overall match fails, the engine tries every way of splitting the digit run between the inner \\d+ and the outer *, and the number of such splits grows exponentially with the input length.

The report points to the OWASP ReDoS page and to Google's RE2/J as resources. JDK 9's regex mitigations are why the reproduction requires JDK =< 8; on newer JDKs the same input does not exhibit the same behaviour.

Impact

This issue may lead to a denial of service by resource consumption. The condition is that a text file is parsed using the StandardsExtractingContentHandler and contains the specific payload.

Fix

Apache released 1.28.2 and 2.4.0 with a fix on 2022-05-02. A bypass of that fix was then sent to the Tika team on 2022-05-31, and further releases followed: 1.28.4 on 2022-06-16 and 2.4.1 on 2022-06-17. CVE-2022-30126 covers the original issue and CVE-2022-33879 the bypass.

Variants to test

  • Other Tika components that apply user-influenced regular expressions to extracted text. A positive result is parse time that grows sharply as the input length increases.
  • The same nested-quantifier shape, a repeated group whose inner element is itself a repetition with an optional separator, in any header or reference matching code. A positive result is exponential slowdown on a long digit run with no trailing match.
  • Re-testing the fixed versions with the original payload and with variations of it, since the first fix here was bypassed. A positive result is that a modified payload still causes the slowdown on a patched release.

Hunting notes

This kind of bug hides in parsing and extraction code that runs regular expressions over file content supplied by users, especially where the regex is applied automatically by a content handler. During recon, look for regexes with nested quantifiers such as (\\d+\\.?)* and for optional separators inside repeated groups. To show impact, the researcher demonstrated the slowdown with a minimal text file and the shipped example; the report also notes the JDK version dependency, which matters when reproducing on modern runtimes.

Coordinated disclosure timeline

  • 2022-04-07: Report sent to [redacted]
  • 2022-04-12: Issue is acknowledged
  • 2022-05-02: 1.28.2 and 2.4.0 with a fix were released
  • 2022-05-03: An email was sent to Security Lab about the release which was missed
  • 2022-05-16: CVE-2022-30126 was assigned
  • 2022-05-31: Bypass to the fix was sent to the Tika team
  • 2022-06-16: 1.28.4 with a fix was released
  • 2022-06-17: 2.4.1 with a fix was released
  • 2022-06-27: CVE-2022-33879 was assigned

Credit

The issue was discovered and reported by CodeQL team members @atorralba (Tony Torralba) and @joefarebrother (Joseph Farebrother). The incomplete fix was discovered and reported by @atorralba (Tony Torralba) and @jarlob (Jaroslav Lobačevski) from GitHub Security Lab.

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. 13 min read.

What happened, in plain words

Apache Tika is a free toolkit that other programs use to read files. You give it a document, a spreadsheet, a PDF or a plain text file, and it pulls out the text and the information about the file (its metadata) so that the program using it can search or index the content. Think of it as a very patient reader that can open almost any kind of file and tell you what is inside.

One small part of Tika, called StandardsText, has a job of its own: it looks through text files for references to standards, meaning document numbers followed by uppercase headings, the kind of thing you see in technical documents. To spot those headings it uses a regular expression, which is a pattern written in a special mini-language for matching text.

The problem is that this particular pattern can be made to work enormously hard by a carefully crafted file. A file containing a long run of digits makes the pattern-matching engine try an exploding number of possible ways to read the text, and the time needed grows exponentially: each extra digit roughly doubles the work. A small file can therefore take an exponentially long time to parse. This kind of attack is called a Regular Expression Denial of Service, or ReDoS: denial of service means making a service unavailable to the people who need it, and here it is done by consuming its resources rather than by breaking in.

Two security identifiers, called CVEs, were assigned. CVE-2022-30126 is the original bug. CVE-2022-33879 is a second one, because the first fix could be worked around (bypassed) and needed fixing again.

Context

Apache Tika is a content analysis toolkit: a library that extracts text and metadata from many file formats. Metadata is the information about a file rather than its contents, such as its title or the program that created it.

One of its components, StandardsText, extracts standard references from text files. A standard reference here is something like a document number followed by uppercase standard headers. To find those headers, the component matches a regular expression against the input.

A regular expression (often shortened to regex) is a pattern that describes what a piece of text should look like. For example, a pattern can say "one or more digits, then a dot, then uppercase letters". The program then asks the regex engine, the part of the software that understands these patterns, whether a given piece of text fits.

The report covers a ReDoS in that matching logic. Apache Tika up to version 1.28.1 is affected: specially crafted files can cause catastrophic backtracking, taking exponential time to complete. Backtracking is what the engine does when a match attempt fails partway: it goes back and tries a different way of dividing up the text. Catastrophic backtracking means there are so many alternative ways to try that the number of attempts explodes.

Two CVEs were assigned: CVE-2022-30126 for the original issue and CVE-2022-33879 for a bypass of the first fix. A CVE is a public catalogue number for a known security flaw, so that everyone can refer to the same problem unambiguously.

Setup

The issue was tested against Apache Tika 1.28.1. A version number like this identifies one particular release of the software, so that others can reproduce the same conditions.

The vulnerable code path is reached when a text file is parsed with the StandardsExtractingContentHandler class. Parsing means reading a file and turning it into a structured form the program can work with. A class is a named block of code in Java, the programming language Tika is written in; a content handler is a class whose job is to receive the content as it is being read and do something with it. This particular handler's job is to pull out standard references.

The chain of calls is: the handler calls StandardsText.extractStandardReferences, which calls StandardsText.findHeaders, which applies the regular expression. In plain terms, the handler asks for the references, that method asks for the headers, and finding the headers is where the pattern is run.

To reproduce, the report uses the StandardsExtractionExample shipped with Tika. An example is a small ready-made program included with the library to show how it is used. One important precondition: JDK 9 introduced important mitigations for this problem, so the example must be run with JDK =< 8 to observe the issue. The JDK is the Java Development Kit, the software you install to compile and run Java programs; version 9 and later include protections that change how the regex engine behaves, which is why an older version is needed to see the slowdown.

Finding it

The researcher looked at the regular expression used to match uppercase standard headers in StandardsText.java:

Java
private static final String REGEX_HEADER =
        "(\\d+\\.(\\d+\\.?)*)\\p{Blank}+([A-Z]+(\\s[A-Z]+)*){5,}";

Line by line, in plain words:

  • private static final String REGEX_HEADER = declares a constant named REGEX_HEADER that holds a piece of text (a string). private means only this class can use it, static means it belongs to the class rather than to an individual object, and final means it cannot be changed later.
  • The string itself is the pattern. In Java source, a backslash has to be written twice, so \\d in the source means the regex sees \d, which stands for "any digit".
  • (\\d+\\.(\\d+\\.?)*) is the first group: one or more digits, then a literal dot, then a repeated part. The repeated part is (\\d+\\.?)*: one or more digits, optionally followed by a dot, and the whole thing can repeat any number of times. The ? makes the dot optional, and the * means zero or more repetitions.
  • \\p{Blank}+ means one or more blank characters, such as spaces or tabs.
  • ([A-Z]+(\\s[A-Z]+)*){5,} is the second group: uppercase letters, then possibly more words made of uppercase letters separated by whitespace, and the whole group must appear at least five times.

The problem is the nested repetition at (\\d+\\.?)*. A repetition inside a repetition is like a set of nested loops in a program: the number of combinations multiplies. When an input contains a long sequence of numbers and there is no full match after it, the regex engine has to work out which part of the expression matched which characters: either \\d+, or the * after the parentheses, since \\. is optional. To distinguish between those possibilities it needs to backtrack exponentially. In everyday terms, imagine being asked to split a long row of coins into piles, where each pile may or may not be followed by a marker, and you must try every possible split before concluding that none of them works. The number of ways to split grows very fast with the number of coins.

Exploitation

Exploitation here means putting the flaw to use. The steps are simple and need no special tools beyond Java and the example that ships with Tika.

Step 1: Create a payload file

The payload is a text file containing a number followed by a long run of 9s:

$ cat /tmp/test/test.txt
2.9999999999999999999999999999999

Line by line:

  • $ cat /tmp/test/test.txt runs the cat command, which prints the contents of a file to the screen. The $ is the shell prompt, the sign that you are typing a command in a terminal. The path /tmp/test/test.txt is where the file lives: a directory called test inside /tmp, which is a temporary folder on Linux and similar systems.
  • 2.9999999999999999999999999999999 is the content of the file: the digit 2, a dot, and a long run of 9s. This looks like the beginning of a numbered heading, which is exactly what the pattern is designed to look for.

The digits after the dot are the sequence that drives the backtracking. The time needed to parse the file grows exponentially with the length of that sequence of 9s. In other words, adding a few more 9s does not add a little time, it multiplies it.

Step 2: Run the example against the file

From the tika-examples directory, the StandardsExtractionExample is run with the directory containing the payload as its argument:

$ cd tika-examples

$ mvn compile exec:java -Dexec.mainClass="org.apache.tika.example.StandardsExtractionExample" -Dexec.args="/tmp/test"

Line by line:

  • $ cd tika-examples changes the current directory to tika-examples, the folder in the Tika source tree that holds the example programs. cd stands for change directory.
  • $ mvn compile exec:java ... runs Maven, a tool that builds Java projects and can also launch a program. compile builds the code first, and exec:java runs a Java class.
  • -Dexec.mainClass="org.apache.tika.example.StandardsExtractionExample" tells Maven which class to run. The long dotted name is the class's full name: the example called StandardsExtractionExample inside the example package of tika.
  • -Dexec.args="/tmp/test" passes an argument to that program: the directory /tmp/test, which contains the payload file. The example will read the files in that directory.

Because the file is parsed through StandardsExtractingContentHandler, the header regex is evaluated against the crafted content and the engine enters catastrophic backtracking. Lengthening the run of 9s makes the parse take exponentially longer, which is the denial of service.

Root cause

The regex (\\d+\\.(\\d+\\.?)*)\\p{Blank}+([A-Z]+(\\s[A-Z]+)*){5,} contains a nested quantifier: a group ending in * whose inner element \\d+ is itself a repetition, and whose separator \\. is optional. A quantifier is the part of a pattern that says how many times something may repeat, such as + for one or more or * for zero or more.

When the overall match fails, the engine tries every way of splitting the digit run between the inner \\d+ and the outer *, and the number of such splits grows exponentially with the input length.

The report points to the OWASP ReDoS page and to Google's RE2/J as resources. OWASP is a well-known organisation that publishes free guidance on web security, and its page explains ReDoS in general. RE2/J is an alternative regex library designed so that this kind of blow-up cannot happen, which is why it is listed as a resource.

JDK 9's regex mitigations are why the reproduction requires JDK =< 8; on newer JDKs the same input does not exhibit the same behaviour. A mitigation is a change that makes a problem harder to trigger without necessarily removing its cause.

Impact

This issue may lead to a denial of service by resource consumption. Resource consumption means the program uses up something it needs to keep working, here processor time, so that it cannot serve other requests. The condition is that a text file is parsed using the StandardsExtractingContentHandler and contains the specific payload. In other words, the attack only works where an application uses this particular handler on files that an attacker can supply.

Fix

Apache released 1.28.2 and 2.4.0 with a fix on 2022-05-02. A bypass of that fix was then sent to the Tika team on 2022-05-31, and further releases followed: 1.28.4 on 2022-06-16 and 2.4.1 on 2022-06-17. A bypass is a way of getting around a fix, so the original problem still occurs in a slightly different form. CVE-2022-30126 covers the original issue and CVE-2022-33879 the bypass.

Variants to test

These are ideas for where else the same kind of bug might be found. Each comes with what a positive result, meaning a sign that the bug is present, would look like.

  • Other Tika components that apply user-influenced regular expressions to extracted text. A positive result is parse time that grows sharply as the input length increases.
  • The same nested-quantifier shape, a repeated group whose inner element is itself a repetition with an optional separator, in any header or reference matching code. A positive result is exponential slowdown on a long digit run with no trailing match.
  • Re-testing the fixed versions with the original payload and with variations of it, since the first fix here was bypassed. A positive result is that a modified payload still causes the slowdown on a patched release.

Hunting notes

This kind of bug hides in parsing and extraction code that runs regular expressions over file content supplied by users, especially where the regex is applied automatically by a content handler. Hunting notes are the practical lessons for someone looking for similar flaws.

During recon, short for reconnaissance, the phase where you look around a target before testing, look for regexes with nested quantifiers such as (\\d+\\.?)* and for optional separators inside repeated groups. Those two shapes are the warning signs.

To show impact, the researcher demonstrated the slowdown with a minimal text file and the shipped example. The report also notes the JDK version dependency, which matters when reproducing on modern runtimes: if you test on a newer Java version and see nothing, that does not mean the bug is absent.

Coordinated disclosure timeline

Coordinated disclosure is the practice of telling the maker of the software about a flaw privately, giving them time to fix it, and only then making the details public.

  • 2022-04-07: Report sent to [redacted]
  • 2022-04-12: Issue is acknowledged
  • 2022-05-02: 1.28.2 and 2.4.0 with a fix were released
  • 2022-05-03: An email was sent to Security Lab about the release which was missed
  • 2022-05-16: CVE-2022-30126 was assigned
  • 2022-05-31: Bypass to the fix was sent to the Tika team
  • 2022-06-16: 1.28.4 with a fix was released
  • 2022-06-17: 2.4.1 with a fix was released
  • 2022-06-27: CVE-2022-33879 was assigned

Credit

The issue was discovered and reported by CodeQL team members @atorralba (Tony Torralba) and @joefarebrother (Joseph Farebrother). CodeQL is a tool that analyses source code to find bugs automatically. The incomplete fix was discovered and reported by @atorralba (Tony Torralba) and @jarlob (Jaroslav Lobačevski) from GitHub Security Lab.

Glossary

  • Apache Tika: a free toolkit that extracts text and metadata from many file formats.
  • Metadata: information about a file rather than its contents, such as its title or the program that created it.
  • Regular expression (regex): a pattern written in a special mini-language that describes what a piece of text should look like.
  • Regex engine: the part of the software that understands regular expressions and checks text against them.
  • Backtracking: when a match attempt fails partway, the engine goes back and tries a different way of dividing up the text.
  • Catastrophic backtracking: so many alternative ways to divide the text that the number of attempts explodes.
  • ReDoS (Regular Expression Denial of Service): an attack that makes a service unavailable by feeding it text that makes a regular expression work for an extremely long time.
  • Denial of service: making a service unavailable to the people who need it.
  • Resource consumption: using up something a program needs to keep working, such as processor time, so it cannot serve other requests.
  • Exponential: growing by multiplication rather than addition, so each extra unit of input multiplies the work.
  • CVE: a public catalogue number for a known security flaw, so everyone can refer to the same problem unambiguously.
  • Bypass: a way of getting around a fix, so the original problem still occurs in a slightly different form.
  • Mitigation: a change that makes a problem harder to trigger without necessarily removing its cause.
  • Parsing: reading a file and turning it into a structured form the program can work with.
  • Class: a named block of code in Java, the programming language Tika is written in.
  • Content handler: a class whose job is to receive content as it is being read and do something with it.
  • JDK (Java Development Kit): the software you install to compile and run Java programs.
  • String: a piece of text in a program.
  • Constant: a named value in a program that is not meant to change.
  • Quantifier: the part of a pattern that says how many times something may repeat, such as + for one or more or * for zero or more.
  • Nested quantifier: a repetition inside a repetition, which multiplies the number of possible combinations.
  • Payload: the crafted input used to trigger the flaw.
  • Shell prompt: the sign, often $, that you are typing a command in a terminal.
  • Terminal: a text window where you type commands for the computer to run.
  • cat: a command that prints the contents of a file to the screen.
  • cd: a command that changes the current directory.
  • Maven: a tool that builds Java projects and can also launch a program.
  • Argument: a value passed to a program when it is started, telling it what to work on.
  • OWASP: a well-known organisation that publishes free guidance on web security.
  • RE2/J: an alternative regex library designed so that this kind of blow-up cannot happen.
  • Recon (reconnaissance): the phase where you look around a target before testing.
  • Coordinated disclosure: telling the maker of the software about a flaw privately, giving them time to fix it, and only then making the details public.
  • CodeQL: a tool that analyses source code to find bugs automatically.

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.