Skip to content

Commit 70f4f08

Browse files
ZYSzyslpinca
authored andcommitted
fs: harden validation of start option in createWriteStream
PR-URL: #25579 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent e154176 commit 70f4f08

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

doc/api/fs.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -1470,9 +1470,11 @@ returned by this method has a default `highWaterMark` of 64 kb.
14701470

14711471
`options` can include `start` and `end` values to read a range of bytes from
14721472
the file instead of the entire file. Both `start` and `end` are inclusive and
1473-
start counting at 0. If `fd` is specified and `start` is omitted or `undefined`,
1474-
`fs.createReadStream()` reads sequentially from the current file position.
1475-
The `encoding` can be any one of those accepted by [`Buffer`][].
1473+
start counting at 0, allowed values are in the
1474+
[0, [`Number.MAX_SAFE_INTEGER`][]] range. If `fd` is specified and `start` is
1475+
omitted or `undefined`, `fs.createReadStream()` reads sequentially from the
1476+
current file position. The `encoding` can be any one of those accepted by
1477+
[`Buffer`][].
14761478

14771479
If `fd` is specified, `ReadStream` will ignore the `path` argument and will use
14781480
the specified file descriptor. This means that no `'open'` event will be
@@ -1548,7 +1550,8 @@ changes:
15481550
* Returns: {fs.WriteStream} See [Writable Stream][].
15491551

15501552
`options` may also include a `start` option to allow writing data at
1551-
some position past the beginning of the file. Modifying a file rather
1553+
some position past the beginning of the file, allowed values are in the
1554+
[0, [`Number.MAX_SAFE_INTEGER`][]] range. Modifying a file rather
15521555
than replacing it may require a `flags` mode of `r+` rather than the
15531556
default mode `w`. The `encoding` can be any one of those accepted by
15541557
[`Buffer`][].
@@ -4953,3 +4956,4 @@ the file contents.
49534956
[chcp]: https://ss64.com/nt/chcp.html
49544957
[inode]: https://en.wikipedia.org/wiki/Inode
49554958
[support of file system `flags`]: #fs_file_system_flags
4959+
[`Number.MAX_SAFE_INTEGER`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER

lib/internal/fs/streams.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,7 @@ function WriteStream(path, options) {
243243
this.closed = false;
244244

245245
if (this.start !== undefined) {
246-
if (typeof this.start !== 'number') {
247-
throw new ERR_INVALID_ARG_TYPE('start', 'number', this.start);
248-
}
249-
if (this.start < 0) {
250-
const errVal = `{start: ${this.start}}`;
251-
throw new ERR_OUT_OF_RANGE('start', '>= 0', errVal);
252-
}
246+
checkPosition(this.start, 'start');
253247

254248
this.pos = this.start;
255249
}

test/parallel/test-file-write-stream3.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ function run_test_3() {
161161
assert.strictEqual(fileData, fileDataExpected_3);
162162

163163
run_test_4();
164+
run_test_5();
164165
});
165166

166167
file.on('error', function(err) {
@@ -184,7 +185,22 @@ const run_test_4 = common.mustCall(function() {
184185
const err = {
185186
code: 'ERR_OUT_OF_RANGE',
186187
message: 'The value of "start" is out of range. ' +
187-
'It must be >= 0. Received {start: -5}',
188+
'It must be >= 0 and <= 2 ** 53 - 1. Received -5',
189+
type: RangeError
190+
};
191+
common.expectsError(fn, err);
192+
});
193+
194+
195+
const run_test_5 = common.mustCall(function() {
196+
// Error: start must be <= 2 ** 53 - 1
197+
const fn = () => {
198+
fs.createWriteStream(filepath, { start: 2 ** 53, flags: 'r+' });
199+
};
200+
const err = {
201+
code: 'ERR_OUT_OF_RANGE',
202+
message: 'The value of "start" is out of range. ' +
203+
'It must be >= 0 and <= 2 ** 53 - 1. Received 9007199254740992',
188204
type: RangeError
189205
};
190206
common.expectsError(fn, err);

0 commit comments

Comments
 (0)