Skip to content

Commit

Permalink
fs: improve fchmod{Sync} validation
Browse files Browse the repository at this point in the history
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: #20588
Fixes: #20498
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Weijia Wang <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>

Backport-PR-URL: #21172
  • Loading branch information
cjihrig authored and targos committed Jun 13, 2018
1 parent 36e5100 commit db0bb52
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ const {
isUint32,
validateAndMaskMode,
validateInteger,
validateInt32,
validateUint32
} = require('internal/validators');

Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand Down
15 changes: 12 additions & 3 deletions test/parallel/test-fs-fchmod.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down

0 comments on commit db0bb52

Please sign in to comment.