Skip to content

Commit 29953a0

Browse files
LiviaMedeirosaduh95
authored andcommitted
fs: harden filehandle.read(params) typecheck
Make sure that first argument is a nullable object Co-authored-by: Antoine du Hamel <[email protected]> PR-URL: #42772 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 53633c0 commit 29953a0

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/internal/fs/promises.js

+4
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ const {
7777
validateBuffer,
7878
validateEncoding,
7979
validateInteger,
80+
validateObject,
8081
validateString,
8182
} = require('internal/validators');
8283
const pathModule = require('path');
@@ -517,6 +518,9 @@ async function read(handle, bufferOrParams, offset, length, position) {
517518
let buffer = bufferOrParams;
518519
if (!isArrayBufferView(buffer)) {
519520
// This is fh.read(params)
521+
if (bufferOrParams !== undefined) {
522+
validateObject(bufferOrParams, 'options', { nullable: true });
523+
}
520524
({
521525
buffer = Buffer.alloc(16384),
522526
offset = 0,

test/parallel/test-fs-promises.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,24 @@ async function executeOnHandle(dest, func) {
153153
});
154154
}
155155

156-
// Use fallback buffer allocation when input not buffer
156+
// Use fallback buffer allocation when first argument is null
157157
{
158158
await executeOnHandle(dest, async (handle) => {
159-
const ret = await handle.read(0, 0, 0, 0);
159+
const ret = await handle.read(null, 0, 0, 0);
160160
assert.strictEqual(ret.buffer.length, 16384);
161161
});
162162
}
163163

164+
// TypeError if buffer is not ArrayBufferView or nullable object
165+
{
166+
await executeOnHandle(dest, async (handle) => {
167+
await assert.rejects(
168+
async () => handle.read(0, 0, 0, 0),
169+
{ code: 'ERR_INVALID_ARG_TYPE' }
170+
);
171+
});
172+
}
173+
164174
// Bytes written to file match buffer
165175
{
166176
await executeOnHandle(dest, async (handle) => {

0 commit comments

Comments
 (0)