fs: harden options typecheck in reading methods#42772
Closed
LiviaMedeiros wants to merge 3 commits into
Closed
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Can we have this onsemver-patchlevel?This PR adds validation in fs.read() and filehandle.read() to make sure that first argument is a buffer or a params object (that is, not arbitrary-type value). In case of
nullorundefined, default values are used.It also adds the same validation in fs.readSync(), which has a different signature:
bufferis not a part ofoptionsobject, but a mandatory first argument.Currently, said methods are destructuring any non-ArrayBufferView value as
{ buffer, offset, position, length }object.For example, this happens without any warning if we accidentaly pass string or array (both have
.lengthproperty):$ cat /tmp/tst ABCDEFGHI $ node test.mjs /tmp/tst result: { bytesRead: 4, buffer: <Buffer 41 42 43 44 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... 16334 more bytes> } string: ABCD string length: 16384With the patch:
$ node test.mjs /tmp/tst node:internal/errors:475 ErrorCaptureStackTrace(err); ^ TypeError [ERR_INVALID_ARG_TYPE]: The "options" argument must be of type object. Received type string ('gwak') at read (node:internal/fs/promises:516:7) at fsCall (node:internal/fs/promises:367:18) at FileHandle.read (node:internal/fs/promises:168:12) at file://test.mjs:3:22 { code: 'ERR_INVALID_ARG_TYPE' } Node.js v18.0.0-pre