Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: fix valid id range on fs.chown, fs.chownSync, fs.lchown, fs.fchown #37997

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fs: fix valid id range on chown, lchown, fchown
  • Loading branch information
ycjcl868 committed Mar 31, 2021
commit 75ec1d987e7f74e7a12416e12043be74531702f3
27 changes: 13 additions & 14 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ const {
validateStringAfterArrayBufferView,
warnOnNonPortableTemplate
} = require('internal/fs/utils');
const { validateUint32 } = require('internal/validators');
const {
Dir,
opendir,
Expand All @@ -136,8 +137,6 @@ const {
validateFunction,
validateInteger,
} = require('internal/validators');
// 2 ** 32 - 1
const kMaxUserId = 4294967295;

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

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

function fchown(fd, uid, gid, callback) {
fd = getValidatedFd(fd);
validateInteger(uid, 'uid', -1, kMaxUserId);
validateInteger(gid, 'gid', -1, kMaxUserId);
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');
callback = makeCallback(callback);

const req = new FSReqCallback();
Expand All @@ -1377,8 +1376,8 @@ function fchown(fd, uid, gid, callback) {

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

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

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

function chownSync(path, uid, gid) {
path = getValidatedPath(path);
validateInteger(uid, 'uid', -1, kMaxUserId);
validateInteger(gid, 'gid', -1, kMaxUserId);
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');
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);
});

[-2, 2 ** 32].forEach((input) => {
[-1, 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 >= -1 && ' +
`<= 4294967295. Received ${input}`;
errObj.message = 'The value of "uid" is out of range. It must be >= 0 && ' +
`< 4294967295. Received ${input}`;
testUid(input, errObj);
errObj.message = errObj.message.replace('uid', 'gid');
testGid(input, errObj);
Expand Down