Skip to content

Commit

Permalink
Merge pull request #309 from petebacondarwin/fix-webcrypto-bindings-2
Browse files Browse the repository at this point in the history
fix: ensure Cloudflare webcrypto.getRandomBytes is bound to the correct this object
  • Loading branch information
petebacondarwin authored Sep 16, 2024
2 parents c0e1d9f + 1e87097 commit d8cf908
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/runtime/node/crypto/$cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ export const {
getDiffieHellman,
getFips,
getHashes,
getRandomValues,
hkdf,
hkdfSync,
pbkdf2,
Expand All @@ -106,18 +105,21 @@ export const {
timingSafeEqual,
} = workerdCrypto;

// Special case getRandomValues as it must be bound to the webcrypto object
export const getRandomValues = workerdCrypto.getRandomValues.bind(
workerdCrypto.webcrypto,
);

export const webcrypto = {
CryptoKey: unenvCryptoWebcrypto.CryptoKey,
getRandomValues: workerdCrypto.webcrypto.getRandomValues.bind(
workerdCrypto.webcrypto,
),
randomUUID: randomUUID.bind(workerdCrypto.webcrypto),
subtle: workerdCrypto.webcrypto.subtle,
getRandomValues,
randomUUID,
subtle,
} satisfies typeof nodeCrypto.webcrypto;

// Node.js exposes fips only via the default export 🤷🏼‍♂️
// so extract it separately from the other exports
const { fips } = workerdCrypto;
const fips = workerdCrypto.fips;

// Node.js exposes createCipher, createDecipher, pseudoRandomBytes only via the default export 🤷🏼‍♂️
// so extract it separately from the other exports
Expand Down

0 comments on commit d8cf908

Please sign in to comment.