One lazy quantifier, seven seconds of blocked CPU: PEM ReDoS in @peculiar/utils
A lazy quantifier hunting for a PEM END marker turns many unmatched BEGIN headers into quadratic work: about seven seconds of blocked CPU from a 560 KB input.
- Advisory
- GHSA-r5p3-rj5x-c7cc
- Affected
- @peculiar/utils (npm) >= 2.0.1, < 4.1.1, patched in 4.1.1
- Weakness
- CWE-1333 · Inefficient Regular Expression Complexity
- Reading
- ~6 min · 21 Nov 2026
Credit: I found and reported this issue; the maintainers credit me as finder on the advisory.
Parsing a certificate sounds like one of the duller things a server does. Read some text, find the -----BEGIN----- and -----END----- lines, base64-decode what is between them. It is also the kind of code that ends up on the request path: upload a certificate, paste a public key, submit a CSR.
In @peculiar/utils 4.1.0, a package with tens of millions of weekly downloads that sits underneath @peculiar/x509, that “find the markers” step was a single regular expression. With the right input, one call took about seven seconds of CPU, and on Node.js those are seven seconds in which the process serves nobody else.
A little theory: why one regex can take down a server
Regular expressions look like simple string matching, but most engines, JavaScript’s included, implement them with backtracking. When an attempt fails, the engine rewinds and tries another way to split the input. For most patterns that costs almost nothing. For some, the number of attempts grows polynomially or exponentially with the input length, and an input built to maximise the rewinding pushes matching from microseconds to seconds. That is ReDoS, regular-expression denial of service. I covered the wider family in DoS is more than DDoS.
The best-known ReDoS patterns are nested quantifiers like (a+)+, which blow up exponentially. The shape in this post is quieter and more common: a lazy quantifier scanning for a terminator, such as [\s\S]*? reaching for a closing marker. If the closer exists, the scan is cheap. If an attacker sends many openers and no closer, the engine starts a fresh scan of the rest of the buffer at every opener. With n openers, each scan covers on the order of n characters, so the total work is O(n²). Quadratic is not exponential, but it is enough: a few hundred kilobytes of input buys seconds of CPU.
The concrete finding
The PEM module lives in src/pem/pem.ts. Every block was located with one global regex, run in a loop by the internal function collectBlocks():
const PEM_BLOCK_REGEX = /-----BEGIN ([^-]+)-----([\s\S]*?)-----END \1-----/g;
PEM_BLOCK_REGEX.lastIndex = 0;
while ((match = PEM_BLOCK_REGEX.exec(text))) {
const label = match[1].trim();
// ...
const parsed = parseBody(match[2]);Read it the way the engine does. It finds a -----BEGIN LABEL----- header, then ([\s\S]*?) lazily takes one character at a time, checking after each one whether -----END LABEL----- follows. If it reaches the end of the input without finding one, the match fails at that header and the engine moves on to try the next position. The next header starts the same walk to the end of the buffer again.
So the attack input is many BEGIN headers and no END:
const { pem } = require("@peculiar/utils");
for (const count of [10000, 20000]) {
const payload = "-----BEGIN CERTIFICATE-----\n".repeat(count);
console.time(`pem.decode (${count} BEGIN markers)`);
pem.decode(payload);
console.timeEnd(`pem.decode (${count} BEGIN markers)`);
}| BEGIN markers | Input size | Time in pem.decode() |
|---|---|---|
| 10,000 | ~280 KB | ~1.7 s |
| 20,000 | ~560 KB | ~7.1 s |
Doubling the input made it roughly four times slower, which is what O(n²) looks like. The input contains no valid PEM block at all, so the call returns an empty result. All that CPU is spent establishing that there is nothing to parse.
Because every public entry point goes through collectBlocks(), the whole decoding API was affected: pem.decode(), pem.decodeFirst(), pem.find(), pem.findAll(), pem.parse(), and pemConverter.decode() / pemConverter.parse().
How to find this in your own code
- Grep for lazy or nested quantifiers on untrusted input. Look for
*?,+?, and(x+)+-style nesting, especially in patterns that scan for a delimiter:BEGIN…END, comment closers, tag closers, quoted strings. - Ask what happens when the closer never arrives. If a pattern can start matching at many positions and each attempt scans to the end of the input, you have a quadratic loop. Backreferences such as
\1in the closer make this more likely, because the engine cannot skip ahead to “any END”. - Measure the scaling instead of eyeballing it. Time the function on inputs of size
n,2nand4n, built from repeated openers. If doubling the input more than doubles the time, the behaviour is super-linear. A timing run is more reliable than reading the regex. - Use a checker. Static analysers for backtracking complexity, such as
recheckorredos-detector, flag many of these patterns and fit well in CI. - Check your dependencies too. Here the regex lived in a utility package, not in application code. If your service parses certificates, keys or other structured text from users, the parser’s complexity is your availability problem.
The fix
Version 4.1.1 removes PEM_BLOCK_REGEX and replaces it with a linear scan. The new collectBlocks() makes one pass over the text to record the offset of every -----END LABEL marker, grouped by label. A second pass walks the -----BEGIN LABEL markers and pairs each one with the next unused END of the same label:
// Index END markers once instead of searching the remaining body for every BEGIN.
const endPattern = /-----END ([^-]+)(?=-----)/g;
// Each END offset is visited at most once as BEGIN positions advance.A BEGIN with no matching END is now skipped with a map lookup instead of a full scan of the remaining input, and each END offset is consumed at most once. The total work grows linearly with the input. The patch also adds tests that pin down existing behaviour: multiple blocks, optional headers, CRLF line endings, exact label matching and single-line PEM. When you replace a regex with hand-written parsing, those tests matter as much as the speed-up.
The bug class is not unique to this library. node-forge fixed a PEM-decoding ReDoS of its own, including the search for a matching END line, as CVE-2026-118779 in 1.4.1. The lesson carries over: stop asking a backtracking engine to find delimiters, and find them yourself in one pass.
If you depend on @peculiar/utils directly, upgrade to 4.1.1. @peculiar/x509 declares ^4.1.0, so refreshing your lockfile pulls in the fixed version. Independently of the patch, cap the size of PEM input you accept. A certificate is a few kilobytes, not half a megabyte.
Impact
Denial of service (CWE-1333). The advisory rates it High, CVSS 3.1 7.5 (AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H). The parsing is synchronous, so a single crafted input of around 0.5 MB blocks a Node.js event loop for seconds, and a handful of concurrent requests is effectively an outage for that process.
The precondition matters. The library does not expose a network service. An application is exploitable only if it passes attacker-controlled text to one of the PEM decoding functions without a reasonable size limit, for example a certificate upload or a “paste your public key” field. Downstream packages are affected only if they actually call the PEM parser on untrusted input. There is no confidentiality or integrity impact.
Disclosure timeline
| Date | Event |
|---|---|
| 2026-11-04 | Reported by email to the maintainer (the repository had no private reporting channel) |
| 2026-11-16 | Fix commit ffe3c15 merged to master |
| 2026-11-16 | @peculiar/utils 4.1.1 published to npm |
| 2026-11-19 | Advisory GHSA-r5p3-rj5x-c7cc published, crediting me as finder |
| 2026-11-21 | This write-up |
References
- Vendor advisory: GHSA-r5p3-rj5x-c7cc, PeculiarVentures, published 2026-11-19
- Fix: PeculiarVentures/pvtsutils@ffe3c15, “fix(pem): prevent quadratic-time PEM parsing”, released in 4.1.1
- Same class in node-forge: CVE-2026-118779, fixed in node-forge 1.4.1
- CWE-1333: Inefficient Regular Expression Complexity
- Background: DoS is more than DDoS: algorithmic-complexity attacks
Takeaway
A lazy quantifier hunting for a closing marker is a quadratic loop in disguise.
Time your parsers on doubled inputs built from repeated openers, and find delimiters in one linear pass rather than with backtracking.