Skip to main content

Runtime-Attack Verification

Describes the runtime-attack verification, which checks an attack string that recheck finds by running it on the real regex engine of the dialect.

Overview​

An attack string that recheck reports may not work on the engine that actually runs the pattern. recheck models a naive backtracking engine, but most engines have optimizations that make some vulnerable patterns fast (see Approximations). For example, recheck reports ^(a|a)*$ as exponential in every dialect, while Ruby 4.0 matches its attack string in less than 1 ms by its memoization.

To avoid such a wrong report, recheck can run each attack string on the real engine of the dialect before reporting it, which is called the runtime attack. When the matching does not finish within the timeout, the runtime attack confirms the attack, and recheck reports it. When the matching finishes, the runtime attack refutes the attack, and recheck looks for another attack string instead (see Refuted Attacks).

Usage​

The runtime-attack verification runs when runtimeAttackTimeout is positive.

$ recheck --dialect=ruby --runtime-attack-timeout=1s '/^(a|a)*$/'
info

The runtime-attack verification is off by default, so enable it explicitly as above.

On Ruby 4.0, the runtime attack refutes the attack of ^(a|a)*$, so the result is safe, and the CLI shows the refuted attack after the result.

$ recheck --dialect=ruby --runtime-attack-timeout=1s '/^(a|a)*$/'
Input : /^(a|a)*$/
Dialect : ruby (encoding: UTF-8)
Status : safe
Complexity: safe (fuzz)
Checker : fuzz

Upper bound:
- Complexity: exponential
Hotspot : /^(a|a)*$/

Refuted attacks:
- Complexity : exponential
Attack string : "a" + "a" * 31 + "あ\n\n"
Runtime attack: refuted (finished in 0 ms on ruby 4.0.10, timeout: 1000 ms)

The result records the runtime attack as runtimeAttack of vulnerable diagnostics, and the refuted attacks in refutedAttacks (see Diagnostics).

info

The runtime-attack verification runs only where recheck can run commands. It does not run in the browser or with the Scala.js build (the worker and pure backends of the JavaScript library).

Timeout​

The timeout decides which attacks are confirmed, so choose it for the runtime and the machine that run the checks. The attack string that the runtime attack runs is sized by runtimeAttackLimit, and the time that the matching of such a string takes differs between runtimes and machines. With a timeout that is too long, an effective attack finishes within it and is refuted. With one that is too short, an attack that the engine handles fast is confirmed.

The CLI's calibrate-runtime-attack subcommand measures the runtime of the dialect, and recommends a timeout.

$ recheck calibrate-runtime-attack
Dialect: javascript
Runtime: node v26.11.1
References:
- Pattern: /^a*a*!\?/
String : 'a'.repeat(10000) + '!!?'
Time : 185 ms (the median of 16 runs)
- Pattern: /^[ab]*[ac]*!\?/
String : 'a'.repeat(10000) + '!!?'
Time : 205 ms (the median of 16 runs)
- Pattern: /^(?:a|b)*a(?:a|b)*!\?/
String : 'a'.repeat(10000) + '!!?'
Time : 246 ms (the median of 16 runs)
- Pattern: /^([^!]*),([^!]*)!\?/
String : ','.repeat(10000) + '!!?'
Time : 185 ms (the median of 16 runs)
- Pattern: /[ab]+!\?/
String : 'a'.repeat(10000) + '!!?'
Time : 185 ms (the median of 16 runs)
- Pattern: /^(a?)[ab]*[ac]*\1!\?/
String : 'a'.repeat(10000) + '!!?'
Time : 592 ms (the median of 15 runs)
Attack time : 5550 ms (estimated for 54773 repeats from the median of the references, 185 ms)
Runtime attack timeout: 111 ms (0.02 of the attack time)

Run the check with these options to enable the runtime-attack verification:

recheck --runtime-attack-timeout=111ms <pattern>

It runs the matchings of the references, which take quadratic time on a backtracking engine, on the runtime in turn for 30 seconds. From the median of their times, it estimates the time of the matching of a string sized by runtimeAttackLimit, and recommends 0.02 of this time, or 100 ms if it is shorter. A reference that the engine optimizes or stops is left out, as Ruby 4.0 matches most of them in 1 ms by its memoization. The subcommand fails when no reference is left.

The subcommand takes the options of the checking (e.g. --dialect and --runtime-attack-limit) and the following ones.

OptionDescriptionDefault
--timeThe time of the measurement.30s
--ratioThe ratio of the recommended timeout to the estimated time of an attack.0.02

The last line of the output is the command line of the checking with the recommended timeout. With the JavaScript library or the Scala library, give the timeout as runtimeAttackTimeout (e.g. runtimeAttackTimeout: 111 in JavaScript). Run the subcommand again when the runtime, the machine or runtimeAttackLimit changes.

Refuted Attacks​

A refuted attack shows that the pattern is vulnerable on a naive backtracking engine, though the dialect's engine is not affected by the attack. When no attack is confirmed, the result is safe, with the refuted attacks in refutedAttacks. However, when maxRefutedAttacks attacks are refuted, the checking stops there, and the result is unknown with the runtime-attacks-refuted error.

With the auto checker, when every attack of the automaton checker is refuted, the fuzz checker looks for another attack, starting from the refuted ones. If it finds none, the result is safe by the fuzz checker, with the complexity of the refuted attacks as the upper bound. If it does not finish within the timeout, or the refuted attacks reach maxRefutedAttacks while it runs, the result is safe by the automaton checker instead of unknown.

Engine Limits and Errors​

Some engines stop a long matching before the timeout, and the runtime attack tells the two kinds of stops apart.

An engine limit is a limit on the number of the matching steps. When the engine stops on it, the attack is considered effective, so the runtime-attack verification confirms the attack as with a timeout. The result records it as engine-limit.

An engine error is an error on the stack depth or the memory. The engines may raise it on a long string even if the matching takes linear time, so it does not show that the attack is effective, and the runtime-attack verification refutes the attack. The result records it as engine-error with the message (see Diagnostics).

When the command cannot run (e.g. the runtime is not installed), the result is unknown with an unexpected error.

Runtime Command​

By default, the runtime-attack verification runs the runtime of the dialect found by its name in PATH (e.g. ruby for the ruby dialect). To use another runtime, set the environment variable of the dialect (e.g. RECHECK_RUBY) to the command line to run instead. Each dialect's page (see Dialects) lists its command and its environment variable. recheck adds the dialect's arguments after the command line (e.g. --disable-gems - for ruby), as it does to the default command. The command runs in a directory of recheck, not in the current directory, so a path in the command line needs to be absolute.

For example, the following uses Ruby installed in /opt/ruby-4.0 with --yjit.

$ RECHECK_RUBY='/opt/ruby-4.0/bin/ruby --yjit' recheck --dialect=ruby --runtime-attack-timeout=1s '/^(a|a)*$/'

The environment variables work with the JavaScript library and the ESLint plugin too, since they pass their environment to recheck.

Some dialects compile the program before running it (e.g. java and dotnet). recheck compiles it once for each command line and version of the runtime, and keeps the compiled program for the next runs, even of another process. The compilation does not count toward the timeout of the check, and runtimeAttackCompileTimeout limits it instead.

Runtime-Attack Script​

The CLI's generate-runtime-attack-script subcommand writes the program that the runtime-attack verification runs as a standalone script, so that you can run the attack on the engine yourself. It takes a file of vulnerable diagnostics in JSON (- reads the standard input), which is the output of the CLI with --json, or the result of the JavaScript library.

$ recheck --json --dialect=python '/^(a|a)*$/' > diagnostics.json
$ recheck generate-runtime-attack-script diagnostics.json > runtime-attack.py
$ python3 -I runtime-attack.py

The script prints the lines that the runtime-attack verification reads: recheck:runtime with the runtime, recheck:start before the matching, and recheck:time with the matching time in milliseconds (or recheck:engine-limit or recheck:engine-error with the message). It has no timeout, so an effective attack does not end soon.