Skip to content

Commit c106a85

Browse files
himself65MylesBorins
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 97965f5 commit c106a85

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

lib/fs.js

+15-14
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,10 @@ const {
106106
parseMode,
107107
validateBuffer,
108108
validateInteger,
109-
validateInt32,
110-
validateUint32
109+
validateInt32
111110
} = require('internal/validators');
111+
// 2 ** 32 - 1
112+
const kMaxUserId = 4294967295;
112113

113114
let truncateWarn = true;
114115
let fs;
@@ -1144,26 +1145,26 @@ function chmodSync(path, mode) {
11441145
function lchown(path, uid, gid, callback) {
11451146
callback = makeCallback(callback);
11461147
path = getValidatedPath(path);
1147-
validateUint32(uid, 'uid');
1148-
validateUint32(gid, 'gid');
1148+
validateInteger(uid, 'uid', -1, kMaxUserId);
1149+
validateInteger(gid, 'gid', -1, kMaxUserId);
11491150
const req = new FSReqCallback();
11501151
req.oncomplete = callback;
11511152
binding.lchown(pathModule.toNamespacedPath(path), uid, gid, req);
11521153
}
11531154

11541155
function lchownSync(path, uid, gid) {
11551156
path = getValidatedPath(path);
1156-
validateUint32(uid, 'uid');
1157-
validateUint32(gid, 'gid');
1157+
validateInteger(uid, 'uid', -1, kMaxUserId);
1158+
validateInteger(gid, 'gid', -1, kMaxUserId);
11581159
const ctx = { path };
11591160
binding.lchown(pathModule.toNamespacedPath(path), uid, gid, undefined, ctx);
11601161
handleErrorFromBinding(ctx);
11611162
}
11621163

11631164
function fchown(fd, uid, gid, callback) {
11641165
validateInt32(fd, 'fd', 0);
1165-
validateUint32(uid, 'uid');
1166-
validateUint32(gid, 'gid');
1166+
validateInteger(uid, 'uid', -1, kMaxUserId);
1167+
validateInteger(gid, 'gid', -1, kMaxUserId);
11671168

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

11731174
function fchownSync(fd, uid, gid) {
11741175
validateInt32(fd, 'fd', 0);
1175-
validateUint32(uid, 'uid');
1176-
validateUint32(gid, 'gid');
1176+
validateInteger(uid, 'uid', -1, kMaxUserId);
1177+
validateInteger(gid, 'gid', -1, kMaxUserId);
11771178

11781179
const ctx = {};
11791180
binding.fchown(fd, uid, gid, undefined, ctx);
@@ -1183,8 +1184,8 @@ function fchownSync(fd, uid, gid) {
11831184
function chown(path, uid, gid, callback) {
11841185
callback = makeCallback(callback);
11851186
path = getValidatedPath(path);
1186-
validateUint32(uid, 'uid');
1187-
validateUint32(gid, 'gid');
1187+
validateInteger(uid, 'uid', -1, kMaxUserId);
1188+
validateInteger(gid, 'gid', -1, kMaxUserId);
11881189

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

11941195
function chownSync(path, uid, gid) {
11951196
path = getValidatedPath(path);
1196-
validateUint32(uid, 'uid');
1197-
validateUint32(gid, 'gid');
1197+
validateInteger(uid, 'uid', -1, kMaxUserId);
1198+
validateInteger(gid, 'gid', -1, kMaxUserId);
11981199
const ctx = { path };
11991200
binding.chown(pathModule.toNamespacedPath(path), uid, gid, undefined, ctx);
12001201
handleErrorFromBinding(ctx);

test/parallel/test-fs-fchown.js

+3-3
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)