The vulnerability is a regular expression denial of service (ReDoS) in node-forge's PEM decoding. The root cause is in the pem.decode() function in lib/pem.js, which used three inefficient regular expressions:
-
The message-splitting regex rMessage (/\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 body group are adjacent, lazy, and share characters, and the match must end with an END line whose label equals the BEGIN label (\1). When no matching END line follows, the engine tries every split of the text between the two groups for every label split and at every BEGIN line, causing polynomial (roughly cubic) time.
-
The header-trimming regex /\s+$/ applied to each header line — quadratic on whitespace followed by non-whitespace.
-
The unanchored header regex rHeader (/([\x21-\x7e]+):\s*([\x21-\x7e\s^:]+)/) — retries at every position of a long line without a colon, causing quadratic time.
The fix (commit 412345db583cd28a9d23636d0d93e9f751b935f3) replaces the message regex with a linear-time scanner (_pemMessageScanner), anchors the header regex, and replaces the /\s+$/ trim with a loop-based rtrim() function. Since all PEM entry points (pki.certificateFromPem, pki.privateKeyFromPem, pki.publicKeyFromPem, pki.certificationRequestFromPem, pki.encryptedPrivateKeyFromPem, pki.decryptRsaPrivateKey, pkcs7.messageFromPem) call pem.decode(), the single vulnerable function that would appear in a runtime profile during exploitation is pem.decode. The helper functions _pemMessageScanner and rtrim were ADDED by the fix and do not exist in the vulnerable version, so they are not vulnerable functions.