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

crypto.randomFill : error with TypedArray #38137

Closed
arkerone opened this issue Apr 7, 2021 · 6 comments
Closed

crypto.randomFill : error with TypedArray #38137

arkerone opened this issue Apr 7, 2021 · 6 comments
Labels
confirmed-bug Issues with confirmed bugs. crypto Issues and PRs related to the crypto subsystem.

Comments

@arkerone
Copy link
Contributor

arkerone commented Apr 7, 2021

  • Version: v15.14.0
  • Platform: Linux dev-laptop 5.8.10-050810-generic #202009171232 SMP Thu Sep 17 12:34:50 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
  • Subsystem: crypto.randomFill

I've opened a PR about a typo bug : #38131
But I think that I found a bug.

What steps will reproduce the bug?

Run this code :

const crypto = require('crypto')

const buf = new Uint16Array(10)

crypto.randomFill(buf, 0, (err, res) => {
if (err) throw err;
   console.log(buf.toString('hex'));
});

How often does it reproduce? Is there a required condition?

Always with the code above or TypedArray with size element > 1 Byte

What is the expected behavior?

a buffer fill with random bytes

What do you see instead?

I got this error :
RangeError [ERR_OUT_OF_RANGE]: The value of "size + offset" is out of range. It must be <= 20. Received 40

Additional information

I think there a problem with the assertSize function because the first param is the number of element but we pass the number of bytes (ie: buf.byteLength). The assertSize function must be called only when the size parameter is passed to the randomFill function.

I can open a PR if the issue is confirmed : arkerone@77bbef8

@Ayase-252 Ayase-252 added the crypto Issues and PRs related to the crypto subsystem. label Apr 7, 2021
arkerone added a commit to arkerone/node that referenced this issue Apr 7, 2021
@jasnell
Copy link
Member

jasnell commented Apr 7, 2021

Yep, you're right. I've got a fix just about ready.

@arkerone
Copy link
Contributor Author

arkerone commented Apr 7, 2021

Yep, you're right. I've got a fix just about ready.

I've a solution that seems work : arkerone@77bbef8

@Ayase-252
Copy link
Member

@arkerone Thank you for bug report.

Your PR #38131 may reveal an intricate bug that in calling crypo.randomFill(buf, callback). The size of buffer is not checked properly.

if (size === undefined) {

Since bytesLength does not exist in any supported buffer type <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView>, it prevents assertSize from happening.

Your solution arkerone@77bbef8 seems making assertSize is also skipped in crypo.randomFill(buf, offset, callback) situation. I'm afraid that it may not be a valid fix.

@arkerone
Copy link
Contributor Author

arkerone commented Apr 7, 2021

@arkerone Thank you for bug report.

Your PR #38131 may reveal an intricate bug that in calling crypo.randomFill(buf, callback). The size of buffer is not checked properly.

if (size === undefined) {

Since bytesLength does not exist in any supported buffer type <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView>, it prevents assertSize from happening.

Your solution arkerone@77bbef8 seems making assertSize is also skipped in crypo.randomFill(buf, offset, callback) situation. I'm afraid that it may not be a valid fix.

I thought that assertSize should only be called when the size parameter was passed to the randomFill function. It's the case for the randomFillSync function.

@Ayase-252
Copy link
Member

@arkerone Thank you for bug report.

Your PR #38131 may reveal an intricate bug that in calling crypo.randomFill(buf, callback). The size of buffer is not checked properly.

if (size === undefined) {

Since bytesLength does not exist in any supported buffer type <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView>, it prevents assertSize from happening.

Your solution arkerone@77bbef8 seems making assertSize is also skipped in crypo.randomFill(buf, offset, callback) situation. I'm afraid that it may not be a valid fix.

I thought that assertSize should only be called when the size parameter was passed to the randomFill function. It's the case for the randomFillSync function.

Yes, it seems so. Thank you for kind correction.

@jasnell
Copy link
Member

jasnell commented Apr 7, 2021

PR Here: #38138

@marsonya marsonya added the confirmed-bug Issues with confirmed bugs. label Apr 7, 2021
tniessen added a commit to tniessen/node that referenced this issue Apr 8, 2021
@jasnell jasnell closed this as completed in d2f116c Apr 9, 2021
jasnell pushed a commit that referenced this issue Apr 12, 2021
Refs: #38137

PR-URL: #38150
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: James M Snell <[email protected]>
BethGriggs pushed a commit that referenced this issue Apr 15, 2021
Refs: #38137

PR-URL: #38150
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. crypto Issues and PRs related to the crypto subsystem.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants