cloudflare / cloudflare/workerd

🐛 `crypto.randomBytes` errors for values above 65536

Open
#6,749 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
8.7k
Forks
739
Avg merge
2d 20h
Merged PRs (30d)
174

Description

With the following worker and `nodejs_compat`, you'll receive an error:

```ts
import crypto from 'node:crypto';

export default {
async fetch(request, env, ctx): Promise {
const random = crypto.randomBytes(65537).toString('hex');
return new Response(random);
},
} satisfies ExportedHandler;
```

errors with ` QuotaExceededError: getRandomValues() only accepts buffers of size <= 64K but provided 65537 bytes.`.

VS in Node.js where this works fine:
```ts
import crypto from 'node:crypto';

const random = crypto.randomBytes(65537).toString('hex');
console.log(random);
```

I discovered this entirely by accident when passing the wrong variable to `randomBytes`, but figured it was worth reporting for Node.js compat conformance.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.