NewApp Monitoring is liveSign up free
AgentbeamBeta
Open-source package vulnerability · npm

CVE-2026-118779: Forge: Regular expression denial of service in PEM decoding (pem.decode)

highCVSS 3.1 7.5GHSA-7wch-p9px-956x

node-forge 1.4.0 and earlier parses PEM text with a regular expression that takes polynomial time when no matching END line follows. About 29 KB of crafted input blocks the Node.js event loop for roughly 24 seconds. Any service that passes untrusted PEM to node-forge is exposed. Upgrade to node-forge 1.4.1.

Published Oct 30, 2026 · Analysis Oct 31, 2026

What an attacker can do

pem.decode() in lib/pem.js finds messages with one regular expression. Its optional headers group and its body group are adjacent, lazy and share characters. If no END line with the same label as the BEGIN line follows, the engine tries every split between the two groups, for every label split and at every BEGIN line. Mismatched repeated BEGIN/END lines cost roughly cubic time. A BEGIN line followed by many newlines costs quadratic time. Header parsing adds two quadratic cases on long header lines.

Every PEM entry point calls pem.decode(): pki.certificateFromPem(), privateKeyFromPem(), publicKeyFromPem(), certificationRequestFromPem(), encryptedPrivateKeyFromPem(), decryptRsaPrivateKey() and pkcs7.messageFromPem().

The advisory's proof of concept, 29 KB built from repeated BEGIN/END fragments, blocked node-forge 1.4.0 for about 24 seconds on Node.js 24. A BEGIN line plus 32 KB of newlines took about 1.5 seconds.

An unauthenticated remote attacker needs only a path that parses their PEM text: certificate or CSR uploads, SAML metadata, or user-supplied configuration. The process then stops serving other requests, and repeated requests keep it down. There is no confidentiality or integrity impact.

Who is affected

node-forge 1.4.0 and earlier is affected. Applications are exposed when untrusted text reaches any of the PEM functions above.

npm ls node-forge

node-forge is often a transitive dependency, so check the full tree and lockfile, not just package.json.

node-forge

Ecosystem
npm
Affected
<= 1.4.0
Fixed in
1.4.1
Check
npm ls node-forge

How to fix it

Upgrade to node-forge 1.4.1.

npm install [email protected]

The fix replaces the message regular expression with a scanner that indexes END lines once and matches each BEGIN line in linear time. One behavior change: a label that contains five dashes now resolves to the shortest label. RFC 7468 labels cannot contain two consecutive dashes. On 1.4.1 the advisory's inputs are rejected in a few milliseconds, and 1 MB inputs take under 100 ms.

Workaround: reject untrusted PEM larger than a few kilobytes before parsing. This limits the delay but does not remove it. On 1.4.0, about 7 KB blocked for 0.4 s.

How to tell if you were hit

Look for event loop stalls and CPU spikes in Node.js services right after requests carrying certificates, keys or CSRs. Request logs with PEM-like bodies in the tens of kilobytes that repeat BEGIN lines, or contain one BEGIN line followed by long runs of newlines, are the signature. Error logs showing repeated Invalid PEM formatted message from forge after long delays fit the pattern.

Original advisory

From the GitHub Advisory Database (CC BY 4.0).

Summary

pem.decode(), which every node-forge API that parses PEM input uses, split messages with a regular expression that takes polynomial time on input without a matching END line, and parsed encapsulated headers with regular expressions that take quadratic time on long header lines. A remote attacker can block the Node.js event loop for seconds to minutes with a crafted input of a few tens of kilobytes. Fixed in node-forge 1.4.1.

Details

pem.decode() in lib/pem.js found PEM messages with this regular expression:

/\s*-----BEGIN ([A-Z0-9- ]+)-----\r?\n?([\x21-\x7e\s]+?(?:\r?\n\r?\n))?([:A-Za-z0-9+\/=\s]+?)-----END \1-----/g

The optional headers group and the body group are adjacent, lazy, and share characters (whitespace and the base64 alphabet), and the match must end with an END line whose label equals the BEGIN label (\1). When no such END line follows, the engine tries every split of the text between the two groups, for every label split and at every BEGIN line in the input. The work grows polynomially with the input length (roughly cubic for repeated BEGIN/END lines with mismatched labels, quadratic for a BEGIN line followed by many newlines).

The encapsulated header parsing in the same function had two smaller quadratic cases: trimming each header line with /\s+$/, and an unanchored header regex ([\x21-\x7e]+):... that retries at every position of a long line without a colon. These are reachable when a message does match.

All PEM entry points call pem.decode(), including pki.certificateFromPem(), pki.privateKeyFromPem(), pki.publicKeyFromPem(), pki.certificationRequestFromPem(), pki.encryptedPrivateKeyFromPem(), pki.decryptRsaPrivateKey() and pkcs7.messageFromPem().

The fix replaces the message regular expression with a scanner that indexes the END lines once and matches each BEGIN line in linear time, finding the same messages as before. The one difference: a label containing ----- (-----BEGIN A-----B-----) was matched as A-----B if possible; now only the shortest label, A, is used. RFC 7468 labels can't contain --. Header parsing now trims lines with a loop and only starts a header match at the beginning of a run of printable characters, which gives the same result.

Patches

Fixed in node-forge 1.4.1. PEM messages and encapsulated headers are now parsed in linear time.

Workarounds

Rejecting untrusted PEM input larger than a few kilobytes before passing it to node-forge limits the delay but doesn't remove it: on 1.4.0, about 7 KB of crafted input blocked for 0.4 s, and 29 KB for 24 s.

Credits

Reported by Ahmed Mokhtari (@AhmedMokhtari), @mwlik and @imenyoo2, and independently by @offset, @bibu123456 (coordinated by @Kayiz-PT), and @Mucahic.

Proof of concept

var forge = require('node-forge'); var pem = ' -----BEGIN I-----G-----END B'.repeat(1000) + '\x00'; // 29 KB try { forge.pki.certificateFromPem(pem); } catch(e) {}

On 1.4.0 (Node.js 24) this call blocks for about 24 seconds before throwing "Invalid PEM formatted message."; 14 KB takes about 3 seconds. A BEGIN line followed by 32 KB of newlines and no END line takes about 1.5 seconds, and the time grows quadratically from there. On 1.4.1 each of these inputs is rejected in a few milliseconds, and 1 MB inputs of any of these shapes take under 100 ms.

The PoC is in tests/pocs/ghsa-7wch-p9px-956x.js. Regression tests are in tests/unit/pem.js ("GHSA-7wch-p9px-956x ReDoS checks").

Impact

Any application that passes untrusted PEM text to node-forge is affected: for example, services that accept uploaded certificates, keys or certificate signing requests, or that parse certificates from configuration, SAML metadata or other user-supplied documents. A remote, unauthenticated attacker can send a small crafted input that blocks the Node.js event loop for seconds to minutes per request, so the process stops serving other requests. Repeated requests keep it unavailable. There is no confidentiality or integrity impact.

Credited to AhmedMokhtari, mwlik, imenyoo2, bibu123456, Kayiz-PT, Mucahic.

Questions

Does CVE-2026-118779 allow code execution or data theft?

No. The advisory states there is no confidentiality or integrity impact. The effect is a denial of service through CPU exhaustion.

Which node-forge version fixes it?

1.4.1. Versions 1.4.0 and earlier are affected.

Is limiting PEM input size enough?

It reduces the delay but does not remove the flaw. On 1.4.0, 7 KB of crafted input still blocked for 0.4 s and 29 KB for 24 s. Upgrade.

References

How this page is made: the data panel comes from the GitHub Advisory Database, CISA’s KEV catalog, the NVD and FIRST EPSS, and updates twice a day. The analysis is written by an AI model from the sources listed above and checked by automated rules before it is published. Check versions against the vendor's advisory before you act. Methodology

More npm vulnerabilities

All npm entries