Skip to content

Commit

Permalink
fix: ensure Cloudflare webcrypto bindings are bound to the correct th…
Browse files Browse the repository at this point in the history
…is object
  • Loading branch information
petebacondarwin committed Sep 16, 2024
1 parent c0e1d9f commit db2a475
Showing 1 changed file with 56 additions and 48 deletions.
104 changes: 56 additions & 48 deletions src/runtime/node/crypto/$cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,61 +63,69 @@ const workerdCrypto = process.getBuiltinModule("node:crypto");

// TODO: Ideally this list is not hardcoded but instead is generated when the preset is being generated in the `env()` call
// This generation should use information from https://github.com/cloudflare/workerd/issues/2097
export const {
DiffieHellman,
DiffieHellmanGroup,
Hash,
Hmac,
KeyObject,
checkPrime,
checkPrimeSync,
createDiffieHellman,
createDiffieHellmanGroup,
createHash,
createHmac,
createPrivateKey,
createPublicKey,
createSecretKey,
generateKey,
generateKeyPair,
generateKeyPairSync,
generateKeySync,
generatePrime,
generatePrimeSync,
getCiphers,
getCurves,
getDiffieHellman,
getFips,
getHashes,
getRandomValues,
hkdf,
hkdfSync,
pbkdf2,
pbkdf2Sync,
randomBytes,
randomFill,
randomFillSync,
randomInt,
randomUUID,
secureHeapUsed,
setEngine,
setFips,
subtle,
timingSafeEqual,
} = workerdCrypto;
export const DiffieHellman = workerdCrypto.DiffieHellman;
export const DiffieHellmanGroup = workerdCrypto.DiffieHellmanGroup;
export const Hash = workerdCrypto.Hash;
export const Hmac = workerdCrypto.Hmac;
export const KeyObject = workerdCrypto.KeyObject;
export const checkPrime = workerdCrypto.checkPrime.bind(workerdCrypto);
export const checkPrimeSync = workerdCrypto.checkPrimeSync.bind(workerdCrypto);
export const createDiffieHellman =
workerdCrypto.createDiffieHellman.bind(workerdCrypto);
export const createDiffieHellmanGroup =
workerdCrypto.createDiffieHellmanGroup.bind(workerdCrypto);
export const createHash = workerdCrypto.createHash.bind(workerdCrypto);
export const createHmac = workerdCrypto.createHmac.bind(workerdCrypto);
export const createPrivateKey =
workerdCrypto.createPrivateKey.bind(workerdCrypto);
export const createPublicKey =
workerdCrypto.createPublicKey.bind(workerdCrypto);
export const createSecretKey =
workerdCrypto.createSecretKey.bind(workerdCrypto);
export const generateKey = workerdCrypto.generateKey.bind(workerdCrypto);
export const generateKeyPair =
workerdCrypto.generateKeyPair.bind(workerdCrypto);
export const generateKeyPairSync =
workerdCrypto.generateKeyPairSync.bind(workerdCrypto);
export const generateKeySync =
workerdCrypto.generateKeySync.bind(workerdCrypto);
export const generatePrime = workerdCrypto.generatePrime.bind(workerdCrypto);
export const generatePrimeSync =
workerdCrypto.generatePrimeSync.bind(workerdCrypto);
export const getCiphers = workerdCrypto.getCiphers.bind(workerdCrypto);
export const getCurves = workerdCrypto.getCurves.bind(workerdCrypto);
export const getDiffieHellman =
workerdCrypto.getDiffieHellman.bind(workerdCrypto);
export const getFips = workerdCrypto.getFips.bind(workerdCrypto);
export const getHashes = workerdCrypto.getHashes.bind(workerdCrypto);
export const getRandomValues =
workerdCrypto.getRandomValues.bind(workerdCrypto);
export const hkdf = workerdCrypto.hkdf.bind(workerdCrypto);
export const hkdfSync = workerdCrypto.hkdfSync.bind(workerdCrypto);
export const pbkdf2 = workerdCrypto.pbkdf2.bind(workerdCrypto);
export const pbkdf2Sync = workerdCrypto.pbkdf2Sync.bind(workerdCrypto);
export const randomBytes = workerdCrypto.randomBytes.bind(workerdCrypto);
export const randomFill = workerdCrypto.randomFill.bind(workerdCrypto);
export const randomFillSync = workerdCrypto.randomFillSync.bind(workerdCrypto);
export const randomInt = workerdCrypto.randomInt.bind(workerdCrypto);
export const randomUUID = workerdCrypto.randomUUID.bind(workerdCrypto);
export const secureHeapUsed = workerdCrypto.secureHeapUsed.bind(workerdCrypto);
export const setEngine = workerdCrypto.setEngine.bind(workerdCrypto);
export const setFips = workerdCrypto.setFips.bind(workerdCrypto);
export const subtle = workerdCrypto.subtle;
export const timingSafeEqual =
workerdCrypto.timingSafeEqual.bind(workerdCrypto);

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 db2a475

Please sign in to comment.