Skip to content

Commit 81f7a67

Browse files
himself65codebytere
authored andcommitted
fs: fix valid id range on chown, lchown, fchown
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]>
1 parent b00bd24 commit 81f7a67

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

lib/fs.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,10 @@ const {
102102
parseMode,
103103
validateBuffer,
104104
validateInteger,
105-
validateInt32,
106-
validateUint32
105+
validateInt32
107106
} = require('internal/validators');
107+
// 2 ** 32 - 1
108+
const kMaxUserId = 4294967295;
108109

109110
let truncateWarn = true;
110111
let fs;
@@ -1124,26 +1125,26 @@ function chmodSync(path, mode) {
11241125
function lchown(path, uid, gid, callback) {
11251126
callback = makeCallback(callback);
11261127
path = getValidatedPath(path);
1127-
validateUint32(uid, 'uid');
1128-
validateUint32(gid, 'gid');
1128+
validateInteger(uid, 'uid', -1, kMaxUserId);
1129+
validateInteger(gid, 'gid', -1, kMaxUserId);
11291130
const req = new FSReqCallback();
11301131
req.oncomplete = callback;
11311132
binding.lchown(pathModule.toNamespacedPath(path), uid, gid, req);
11321133
}
11331134

11341135
function lchownSync(path, uid, gid) {
11351136
path = getValidatedPath(path);
1136-
validateUint32(uid, 'uid');
1137-
validateUint32(gid, 'gid');
1137+
validateInteger(uid, 'uid', -1, kMaxUserId);
1138+
validateInteger(gid, 'gid', -1, kMaxUserId);
11381139
const ctx = { path };
11391140
binding.lchown(pathModule.toNamespacedPath(path), uid, gid, undefined, ctx);
11401141
handleErrorFromBinding(ctx);
11411142
}
11421143

11431144
function fchown(fd, uid, gid, callback) {
11441145
validateInt32(fd, 'fd', 0);
1145-
validateUint32(uid, 'uid');
1146-
validateUint32(gid, 'gid');
1146+
validateInteger(uid, 'uid', -1, kMaxUserId);
1147+
validateInteger(gid, 'gid', -1, kMaxUserId);
11471148

11481149
const req = new FSReqCallback();
11491150
req.oncomplete = makeCallback(callback);
@@ -1152,8 +1153,8 @@ function fchown(fd, uid, gid, callback) {
11521153

11531154
function fchownSync(fd, uid, gid) {
11541155
validateInt32(fd, 'fd', 0);
1155-
validateUint32(uid, 'uid');
1156-
validateUint32(gid, 'gid');
1156+
validateInteger(uid, 'uid', -1, kMaxUserId);
1157+
validateInteger(gid, 'gid', -1, kMaxUserId);
11571158

11581159
const ctx = {};
11591160
binding.fchown(fd, uid, gid, undefined, ctx);
@@ -1163,8 +1164,8 @@ function fchownSync(fd, uid, gid) {
11631164
function chown(path, uid, gid, callback) {
11641165
callback = makeCallback(callback);
11651166
path = getValidatedPath(path);
1166-
validateUint32(uid, 'uid');
1167-
validateUint32(gid, 'gid');
1167+
validateInteger(uid, 'uid', -1, kMaxUserId);
1168+
validateInteger(gid, 'gid', -1, kMaxUserId);
11681169

11691170
const req = new FSReqCallback();
11701171
req.oncomplete = callback;
@@ -1173,8 +1174,8 @@ function chown(path, uid, gid, callback) {
11731174

11741175
function chownSync(path, uid, gid) {
11751176
path = getValidatedPath(path);
1176-
validateUint32(uid, 'uid');
1177-
validateUint32(gid, 'gid');
1177+
validateInteger(uid, 'uid', -1, kMaxUserId);
1178+
validateInteger(gid, 'gid', -1, kMaxUserId);
11781179
const ctx = { path };
11791180
binding.chown(pathModule.toNamespacedPath(path), uid, gid, undefined, ctx);
11801181
handleErrorFromBinding(ctx);

test/parallel/test-fs-fchown.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@ function testGid(input, errObj) {
4444
testGid(input, errObj);
4545
});
4646

47-
[-1, 2 ** 32].forEach((input) => {
47+
[-2, 2 ** 32].forEach((input) => {
4848
const errObj = {
4949
code: 'ERR_OUT_OF_RANGE',
5050
name: 'RangeError',
5151
message: 'The value of "fd" is out of range. It must be ' +
5252
`>= 0 && <= 2147483647. Received ${input}`
5353
};
5454
testFd(input, errObj);
55-
errObj.message = 'The value of "uid" is out of range. It must be >= 0 && ' +
56-
`< 4294967296. Received ${input}`;
55+
errObj.message = 'The value of "uid" is out of range. It must be >= -1 && ' +
56+
`<= 4294967295. Received ${input}`;
5757
testUid(input, errObj);
5858
errObj.message = errObj.message.replace('uid', 'gid');
5959
testGid(input, errObj);

0 commit comments

Comments
 (0)