Skip to content

Commit

Permalink
fs: fix valid id range on chown, lchown, fchown
Browse files Browse the repository at this point in the history
PR-URL: #31694
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Yongsheng Zhang <[email protected]>
  • Loading branch information
himself65 authored and MylesBorins committed Mar 9, 2020
1 parent 97965f5 commit c106a85
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
29 changes: 15 additions & 14 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ const {
parseMode,
validateBuffer,
validateInteger,
validateInt32,
validateUint32
validateInt32
} = require('internal/validators');
// 2 ** 32 - 1
const kMaxUserId = 4294967295;

let truncateWarn = true;
let fs;
Expand Down Expand Up @@ -1144,26 +1145,26 @@ function chmodSync(path, mode) {
function lchown(path, uid, gid, callback) {
callback = makeCallback(callback);
path = getValidatedPath(path);
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');
validateInteger(uid, 'uid', -1, kMaxUserId);
validateInteger(gid, 'gid', -1, kMaxUserId);
const req = new FSReqCallback();
req.oncomplete = callback;
binding.lchown(pathModule.toNamespacedPath(path), uid, gid, req);
}

function lchownSync(path, uid, gid) {
path = getValidatedPath(path);
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');
validateInteger(uid, 'uid', -1, kMaxUserId);
validateInteger(gid, 'gid', -1, kMaxUserId);
const ctx = { path };
binding.lchown(pathModule.toNamespacedPath(path), uid, gid, undefined, ctx);
handleErrorFromBinding(ctx);
}

function fchown(fd, uid, gid, callback) {
validateInt32(fd, 'fd', 0);
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');
validateInteger(uid, 'uid', -1, kMaxUserId);
validateInteger(gid, 'gid', -1, kMaxUserId);

const req = new FSReqCallback();
req.oncomplete = makeCallback(callback);
Expand All @@ -1172,8 +1173,8 @@ function fchown(fd, uid, gid, callback) {

function fchownSync(fd, uid, gid) {
validateInt32(fd, 'fd', 0);
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');
validateInteger(uid, 'uid', -1, kMaxUserId);
validateInteger(gid, 'gid', -1, kMaxUserId);

const ctx = {};
binding.fchown(fd, uid, gid, undefined, ctx);
Expand All @@ -1183,8 +1184,8 @@ function fchownSync(fd, uid, gid) {
function chown(path, uid, gid, callback) {
callback = makeCallback(callback);
path = getValidatedPath(path);
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');
validateInteger(uid, 'uid', -1, kMaxUserId);
validateInteger(gid, 'gid', -1, kMaxUserId);

const req = new FSReqCallback();
req.oncomplete = callback;
Expand All @@ -1193,8 +1194,8 @@ function chown(path, uid, gid, callback) {

function chownSync(path, uid, gid) {
path = getValidatedPath(path);
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');
validateInteger(uid, 'uid', -1, kMaxUserId);
validateInteger(gid, 'gid', -1, kMaxUserId);
const ctx = { path };
binding.chown(pathModule.toNamespacedPath(path), uid, gid, undefined, ctx);
handleErrorFromBinding(ctx);
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-fs-fchown.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ function testGid(input, errObj) {
testGid(input, errObj);
});

[-1, 2 ** 32].forEach((input) => {
[-2, 2 ** 32].forEach((input) => {
const errObj = {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "fd" is out of range. It must be ' +
`>= 0 && <= 2147483647. Received ${input}`
};
testFd(input, errObj);
errObj.message = 'The value of "uid" is out of range. It must be >= 0 && ' +
`< 4294967296. Received ${input}`;
errObj.message = 'The value of "uid" is out of range. It must be >= -1 && ' +
`<= 4294967295. Received ${input}`;
testUid(input, errObj);
errObj.message = errObj.message.replace('uid', 'gid');
testGid(input, errObj);
Expand Down

0 comments on commit c106a85

Please sign in to comment.