From db0bb5214a1c35404d9169caef0ce982ec6c32bd Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sat, 19 May 2018 21:09:45 -0400 Subject: [PATCH] fs: improve fchmod{Sync} validation This commit validates the fd parameters to fs.fchmod{Sync} as int32s instead of uint32s because they are ints in the binding layer. PR-URL: https://github.com/nodejs/node/pull/20588 Fixes: https://github.com/nodejs/node/issues/20498 Reviewed-By: Joyee Cheung Reviewed-By: Weijia Wang Reviewed-By: Luigi Pinca Reviewed-By: James M Snell Reviewed-By: Matteo Collina Backport-PR-URL: https://github.com/nodejs/node/pull/21172 --- lib/fs.js | 5 +++-- test/parallel/test-fs-fchmod.js | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 2e4d8499ef62e3..33d9967f54584a 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -86,6 +86,7 @@ const { isUint32, validateAndMaskMode, validateInteger, + validateInt32, validateUint32 } = require('internal/validators'); @@ -1054,7 +1055,7 @@ fs.unlinkSync = function(path) { }; fs.fchmod = function(fd, mode, callback) { - validateUint32(fd, 'fd'); + validateInt32(fd, 'fd', 0); mode = validateAndMaskMode(mode, 'mode'); callback = makeCallback(callback); @@ -1064,7 +1065,7 @@ fs.fchmod = function(fd, mode, callback) { }; fs.fchmodSync = function(fd, mode) { - validateUint32(fd, 'fd'); + validateInt32(fd, 'fd', 0); mode = validateAndMaskMode(mode, 'mode'); const ctx = {}; binding.fchmod(fd, mode, undefined, ctx); diff --git a/test/parallel/test-fs-fchmod.js b/test/parallel/test-fs-fchmod.js index df7748538a5cc4..4f6350e63c241a 100644 --- a/test/parallel/test-fs-fchmod.js +++ b/test/parallel/test-fs-fchmod.js @@ -35,12 +35,21 @@ const fs = require('fs'); const errObj = { code: 'ERR_OUT_OF_RANGE', name: 'RangeError [ERR_OUT_OF_RANGE]', - message: 'The value of "fd" is out of range. It must be >= 0 && < ' + - `${2 ** 32}. Received ${input}` + message: 'The value of "fd" is out of range. It must be >= 0 && <= ' + + `2147483647. Received ${input}` }; assert.throws(() => fs.fchmod(input), errObj); assert.throws(() => fs.fchmodSync(input), errObj); - errObj.message = errObj.message.replace('fd', 'mode'); +}); + +[-1, 2 ** 32].forEach((input) => { + const errObj = { + code: 'ERR_OUT_OF_RANGE', + name: 'RangeError [ERR_OUT_OF_RANGE]', + message: 'The value of "mode" is out of range. It must be >= 0 && < ' + + `4294967296. Received ${input}` + }; + assert.throws(() => fs.fchmod(1, input), errObj); assert.throws(() => fs.fchmodSync(1, input), errObj); });