Skip to content

Commit

Permalink
fix invalid invocation on getRandomValues() (#2730)
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig authored Sep 18, 2024
1 parent 31b17c8 commit 20f3a42
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/node/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
//
import { ERR_METHOD_NOT_IMPLEMENTED } from 'node-internal:internal_errors';

// eslint-disable-next-line @typescript-eslint/unbound-method
export const getRandomValues = crypto.getRandomValues;
export const getRandomValues = crypto.getRandomValues.bind(crypto);
export const subtle = crypto.subtle;
export const webcrypto = crypto;

Expand Down
24 changes: 24 additions & 0 deletions src/workerd/api/node/tests/crypto_random-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,27 @@ export const timingSafeEqualTest = {
timingSafeEqual(new Uint8Array(1), new Uint8Array(1));
},
};

// Ref: https://github.com/cloudflare/workerd/issues/2716
export const getRandomValuesIllegalInvocation = {
async test() {
const crypto = await import('node:crypto');
{
// The following assertion doesn't fail as of Node.js v20.17.0
const getRandomValues = crypto.getRandomValues;
strictEqual(getRandomValues(new Uint8Array(6)).length, 6);
}
throws(
() => {
// This ensures that we are replicating Node.js behavior as of v20.17.0
const getRandomValues = crypto.webcrypto.getRandomValues;
strictEqual(getRandomValues(new Uint8Array(6)).length, 6);
},
{
name: 'TypeError',
message: /Illegal invocation/,
}
);
strictEqual(crypto.getRandomValues(new Uint8Array(6)).length, 6);
},
};

0 comments on commit 20f3a42

Please # to comment.