Skip to content

GHSA-7wch-p9px-956x CVE-2026-118779

Forge: Regular expression denial of service in PEM decoding (pem.decode)

High npm
Severity
7.5 / 10
High · CVSS 3.1
Exploit chance (EPSS)
0.56%
Chance of exploitation in the next 30 days. Higher than 46.41% of scored vulnerabilities.
Known exploited
No
Not in CISA's catalog
Weakness
CWE-400 Uncontrolled Resource Consumption
CWE-1333 Inefficient Regular Expression Complexity
Fix
1.4.1
Upgrade node-forge to this version or later

Description

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.

Affected versions

npm

PackageVulnerableFixed in
node-forge
<= 1.4.0
No fix yet
node-forge
< 1.4.1
1.4.1

How to fix

Move to the fixed version or later with your package manager, update the lockfile, and run your tests. If the package only arrives through another dependency, upgrade that dependency first. If none of its releases pulls a fixed version yet, force the version with your package manager's override.

node-forge npm → 1.4.1

npm
npm install node-forge@1.4.1
yarn
yarn add node-forge@1.4.1
pnpm
pnpm add node-forge@1.4.1

Severity breakdown

CVSS 3.1 7.5

CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H

Attack vector
Network
Attack complexity
Low
Privileges required
None
User interaction
None
Scope
Unchanged
Confidentiality
None
Integrity
None
Availability
High

Weakness

CWE-400: Uncontrolled Resource Consumption

The product does not properly control the allocation and maintenance of a limited resource.

How to prevent it
  • Architecture and Design: Design throttling mechanisms into the system architecture. The best protection is to limit the amount of resources that an unauthorized user can cause to be expended. A strong authentication and access control model will help prevent such attacks from occurring in the first place. The login application should be protected against DoS attacks as much as possible. Limiting the database access, perhaps by caching result sets, can help minimize the resources expended. To further limit the potential for a DoS attack, consider tracking the rate of requests received from users and blocking requests that exceed a defined rate threshold.
  • Architecture and Design: Mitigation of resource exhaustion attacks requires that the target system either: recognizes the attack and denies that user further access for a given amount of time, or uniformly throttles all requests in order to make it more difficult to consume resources more quickly than they can again be freed. The first of these solutions is an issue in itself though, since it may allow attackers to prevent the use of the system by a particular valid user. If the attacker impersonates the valid user, they may be able to prevent the user from accessing the server in question. The second solution is simply difficult to effectively institute -- and even when properly done, it does not provide a full solution. It simply makes the attack require more resources on the part of the attacker.
  • Architecture and Design: Ensure that protocols have specific limits of scale placed on them.
  • Implementation: Ensure that all failures in resource allocation place the system into a safe posture.

CWE-1333: Inefficient Regular Expression Complexity

The product uses a regular expression with a worst-case computational complexity that is inefficient and possibly exponential.

How to prevent it
  • Architecture and Design: Use regular expressions that do not support backtracking, e.g. by removing nested quantifiers.
  • System Configuration: Set backtracking limits in the configuration of the regular expression implementation, such as PHP's pcre.backtrack_limit. Also consider limits on execution time for the process.
  • Implementation: Do not use regular expressions with untrusted input. If regular expressions must be used, avoid using backtracking in the expression.
  • Implementation: Limit the length of the input that the regular expression will process.

References

Timeline

  1. Published
  2. Last updated by the source
  3. Latest EPSS score
AdvisorySeverityWhy it's related
GHSA-4642-5mcj-2rqw
Forge: ASN.1 OID first arc truncation in asn1.oidToDer
Low Also affects node-forge
GHSA-6wvx-3r32-xmcg
Forge: Prototype Pollution in forge.http cookie handling via a malicious Set-Cookie
Medium Also affects node-forge
GHSA-525v-9gp9-w22h
Forge: TLS client crash when the server sends no certificate
Medium Also affects node-forge
GHSA-h6cr-wjv9-3cpp
Forge: Denial of Service via infinite loop in BigInteger.modInverse() with negative input (incomplete fix of CVE-2026-33891)
Low Also affects node-forge
GHSA-4487-j8r4-4g2f
Forge: verifyCertificateChain() removes trusted CA certificates with the same subject from the CA store
Low Also affects node-forge
GHSA-5cwg-6vw7-rj6p
Forge: AES-GCM authentication bypass via unvalidated tagLength option
Medium Also affects node-forge
GHSA-jc66-7q78-782g
Forge: PKCS#12 MAC forgery and constant-key PBE via zero iteration count
High Also affects node-forge
GHSA-8hrf-8g6f-7xwp
Forge: Denial of service in TLS record processing via a record interleaved with a fragmented handshake message
High Also affects node-forge

Credits: AhmedMokhtari, Kayiz-PT, Mucahic, bibu123456, imenyoo2, mwlik

Is this in your code?

MergeHarbor checks every repository's dependencies against this database, ranks what matters by exploitability and reachability, routes it to the owning team and opens the fix pull request.

Check your repos with MergeHarbor