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

feat(remix-node): use Web Crypto API in createFileSessionStorage #7203

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/node-web-crypto.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/node": patch
---

Switch from `crypto.randomBytes` to `crypto.webcrypto.getRandomValues` for file session storage ID generation
2 changes: 1 addition & 1 deletion packages/remix-node/sessions/fileStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function createFileSessionStorage<Data = SessionData, FlashData = Data>({
while (true) {
// TODO: Once Node v19 is supported we should use the globally provided
// Web Crypto API's crypto.getRandomValues() function here instead.
brophdawg11 marked this conversation as resolved.
Show resolved Hide resolved
let randomBytes = crypto.randomBytes(8);
let randomBytes = crypto.webcrypto.getRandomValues(new Uint8Array(8));
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand why Node put Web Crypto API under crypto.webCrypto instead of globally like in any other runtime

Copy link
Member Author

Choose a reason for hiding this comment

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

It will be under the global crypto, but only in Node v19
Since the new minimum version is v18, we need to do it like this unfortunately 😢

Copy link
Member

Choose a reason for hiding this comment

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

That's nice to hear, so far I couldn't use WebCrypto in code that's not dependent of the runtime (some remix-auth strategies) because how to access the API depends if it's Node or basically anything else where it's global.

Copy link
Member Author

Choose a reason for hiding this comment

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

I updated the comments in our code to point to Node v19 in #7204, so we at least know that we can use the global crypto once we drop support for Node 18

// This storage manages an id space of 2^64 ids, which is far greater
// than the maximum number of files allowed on an NTFS or ext4 volume
// (2^32). However, the larger id space should help to avoid collisions
Expand Down
Loading