Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Implement crypto.createCipheriv #3277

Open
vicb opened this issue Dec 28, 2024 · 13 comments
Open

[Feature] Implement crypto.createCipheriv #3277

vicb opened this issue Dec 28, 2024 · 13 comments
Assignees
Labels
crypto feature request Request for Workers team to add a feature nodejs compat

Comments

@vicb
Copy link
Contributor

vicb commented Dec 28, 2024

crypto.createCipheriv is required by Next Auth.

See opennextjs/opennextjs-cloudflare#206

Can we prioritize the implementation of the API?

@jasnell

The crypto APIs are used by Next from crypto-utils.ts

What is used by Next is:

// * Ciphers
// * [x] crypto.createCipheriv(algorithm, key, iv[, options])
// * [x] crypto.createDecipheriv(algorithm, key, iv[, options])

with the aes-256-gcm algo

@jasnell
Copy link
Member

jasnell commented Dec 28, 2024

Was can likely prioritize more of the node:crypto APIs in 1Q

@jasnell jasnell added feature request Request for Workers team to add a feature nodejs compat labels Jan 2, 2025
@jasnell jasnell added the crypto label Jan 9, 2025
@jasnell jasnell self-assigned this Jan 9, 2025
@albertocavalcante
Copy link

Hi there! Do you know if there's any workaround in the meantime? Thanks!

@jasnell
Copy link
Member

jasnell commented Jan 10, 2025

The only workaround is to use web crypto.

@vicb
Copy link
Contributor Author

vicb commented Jan 10, 2025

The only workaround is to use web crypto.

The code is the Nextjs lib, not in user code so changing to use webcrypto is not trivial.
I would way your only chance is to check if you can setup Next to use a different algo.

@Macil
Copy link

Macil commented Jan 26, 2025

Small addition to the original post: in the issue opennextjs/opennextjs-cloudflare#206 using the package next-auth, the problem can (also?) happen from a call to crypto.createCipheriv with the "aes-256-cbc" algorithm (through next-auth/packages/core/src/jwt.ts which eventually calls into jose/src/runtime/node/encrypt.ts).

@vicb
Copy link
Contributor Author

vicb commented Jan 26, 2025

Small correction to the original post: in the issue opennextjs/opennextjs-cloudflare#206 using the package next-auth, the relevant call to crypto.createCipheriv actually happens with the "aes-256-cbc" algorithm (through next-auth/packages/core/src/jwt.ts which eventually calls into jose/src/runtime/node/encrypt.ts).

I'm pretty sure I tested it with Next 15, any chance you are using a different version than the one of the original bug report?

Anyway we should still implement the algo.

Thanks for your message.

@Macil
Copy link

Macil commented Jan 26, 2025

Oh sorry I was very zoned into thinking about my specific case while writing that out the first time, and I'm definitely not sure now that spot of code is the only relevant one, especially considering different versions and configurations. I've added a reproduction case on that original issue thread now.

@arnavgupta00
Copy link

arnavgupta00 commented Jan 27, 2025

Hi there! Do you know if there's any workaround in the meantime? Thanks!

I created this Repo, This approach addresses the crypto.createCipheriv limitation by substituting Node.js's crypto module with the Web Crypto API

Check it out, might be of help

@vicb
Copy link
Contributor Author

vicb commented Jan 27, 2025

Hi there! Do you know if there's any workaround in the meantime? Thanks!

I created this Repo, This approach addresses the crypto.createCipheriv limitation by substituting Node.js's crypto module with the Web Crypto API

Check it out, might be of help

Thanks @arnavgupta00!

If I understand correctly, the main point is to override the encode/decode methods here:

https://github.com/arnavgupta00/deployment-cf-workers-prisma-nextauth/blob/32aa1768edfef8ea112cd5065e2e9c07d5506866/src/lib/authConfig.ts#L43-L46

@arnavgupta00
Copy link

Hi there! Do you know if there's any workaround in the meantime? Thanks!

I created this Repo, This approach addresses the crypto.createCipheriv limitation by substituting Node.js's crypto module with the Web Crypto API
Check it out, might be of help

Thanks @arnavgupta00!

If I understand correctly, the main point is to override the encode/decode methods here:

https://github.com/arnavgupta00/deployment-cf-workers-prisma-nextauth/blob/32aa1768edfef8ea112cd5065e2e9c07d5506866/src/lib/authConfig.ts#L43-L46

Yes, that's correct, I've overridden the default encode and decode of next-auth . By implementing custom encode and decode functions, we can handle JWT operations securely within the Cloudflare environment.

@vicb
Copy link
Contributor Author

vicb commented Jan 27, 2025

Thanks @arnavgupta00, do you know if this would work the same with Next15? (I'm planning to add some docs until the workerd implementation is ready)

@arnavgupta00
Copy link

Thanks @arnavgupta00, do you know if this would work the same with Next15? (I'm planning to add some docs until the workerd implementation is ready)

Yes, based on the Next.js 15 release documentation, the integration should function similarly. Ensure your tsconfig.json includes:

{
  "compilerOptions": {
    "module": "esnext",
    "target": "es2017"
  }
}

This configuration enables top-level await as used in this File, but you can easily find out a way around that.

@vicb
Copy link
Contributor Author

vicb commented Jan 27, 2025

Yes, based on the Next.js 15

Thanks!

This line looks fishy as process.env is not always populated at top level - only guaranteed in the request context. Ping me on Discord to discuss more so that we don't add more OpenNext related messages to this thread, thanks (or on the OpenNext issue)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crypto feature request Request for Workers team to add a feature nodejs compat
Projects
None yet
Development

No branches or pull requests

5 participants