Skip to content

fix(app): replace node-forge with native crypto for private key validation#3111

Open
kyungseopk1m wants to merge 1 commit intofirebase:mainfrom
kyungseopk1m:fix/replace-node-forge-with-native-crypto
Open

fix(app): replace node-forge with native crypto for private key validation#3111
kyungseopk1m wants to merge 1 commit intofirebase:mainfrom
kyungseopk1m:fix/replace-node-forge-with-native-crypto

Conversation

@kyungseopk1m
Copy link
Copy Markdown

Fixes #3075

Problem

initializeApp() with a cert() credential calls require('node-forge') synchronously during ServiceAccount construction. Loading node-forge initializes a PRNG that uses Math.random(), which causes errors in environments that restrict non-deterministic APIs during static rendering — specifically Next.js App Router with Partial Pre-Rendering (PPR) / SSG:

Error: Route used Math.random() before accessing either uncached data or Request data...

Solution

Replace forge.pki.privateKeyFromPem() with Node.js built-in crypto.createPrivateKey(). This provides equivalent PEM validation while removing the node-forge dependency entirely.

Testing

All 6231 existing unit tests pass. The existing test should throw given an object with a malformed "private_key" property in credential-internal.spec.ts already covers the error path for invalid private keys.

Notes

…ation

Replace the node-forge dependency with Node.js built-in crypto module
for validating PEM-formatted private keys in ServiceAccount. This
eliminates Math.random() calls during initializeApp(), which caused
errors in environments that restrict non-deterministic APIs such as
Next.js React Server Components with PPR/SSG.

Fixes firebase#3075
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the package version to 13.8.0 and removes the node-forge dependency. The logic for validating service account private keys in src/app/credential-internal.ts has been refactored to use the native Node.js crypto.createPrivateKey method instead of node-forge. I have no feedback to provide as there were no review comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Admin SDK uses Math.random during initialization, making it unsuitable to use in a RSC

1 participant