Skip to content

Commit

Permalink
fs: add validation for fd and path
Browse files Browse the repository at this point in the history
adds type validation to options fd & opts in createWriteStream and createReadStream.

Fixes: nodejs#35178
  • Loading branch information
dylanelliott27 committed Nov 10, 2020
1 parent da53a3c commit ab5dc94
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const { Buffer } = require('buffer');
const {
copyObject,
getOptions,
validatePath
} = require('internal/fs/utils');
const { Readable, Writable, finished } = require('stream');
const { toPathIfFileURL } = require('internal/url');
Expand Down Expand Up @@ -131,6 +132,13 @@ function ReadStream(path, options) {
this.pos = this.start;
}

// If fd has been set, validate, otherwise validate path.
if (this.fd !== null) {
validateInteger(this.fd, 'fd', 0);
} else if (this.fd === null) {
validatePath(this.path);
}

if (this.end === undefined) {
this.end = Infinity;
} else if (this.end !== Infinity) {
Expand Down Expand Up @@ -296,6 +304,13 @@ function WriteStream(path, options) {
this.closed = false;
this[kIsPerformingIO] = false;

// If fd has been set, validate, otherwise validate path.
if (this.fd !== null) {
validateInteger(this.fd, 'fd', 0);
} else if (this.fd === null) {
validatePath(this.path);
}

if (this.start !== undefined) {
validateInteger(this.start, 'start', 0);

Expand Down
49 changes: 49 additions & 0 deletions test/parallel/test-fs-stream-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';
require('../common');

const assert = require('assert');
const fs = require('fs');

{
const fd = 'k';

assert.throws(
() => {
fs.createReadStream(null, { fd: fd });
},
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
});

assert.throws(
() => {
fs.createWriteStream(null, { fd: fd });
},
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
});
}

{
const path = 46;

assert.throws(
() => {
fs.createReadStream(path);
},
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
});

assert.throws(
() => {
fs.createWriteStream(path);
},
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
});
}

0 comments on commit ab5dc94

Please sign in to comment.