Skip to content

Commit 0a539dd

Browse files
himself65addaleax
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 9dbe6ab commit 0a539dd

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
@@ -109,9 +109,10 @@ const {
109109
parseFileMode,
110110
validateBuffer,
111111
validateInteger,
112-
validateInt32,
113-
validateUint32
112+
validateInt32
114113
} = require('internal/validators');
114+
// 2 ** 32 - 1
115+
const kMaxUserId = 4294967295;
115116

116117
let truncateWarn = true;
117118
let fs;
@@ -1153,26 +1154,26 @@ function chmodSync(path, mode) {
11531154
function lchown(path, uid, gid, callback) {
11541155
callback = makeCallback(callback);
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 req = new FSReqCallback();
11591160
req.oncomplete = callback;
11601161
binding.lchown(pathModule.toNamespacedPath(path), uid, gid, req);
11611162
}
11621163

11631164
function lchownSync(path, uid, gid) {
11641165
path = getValidatedPath(path);
1165-
validateUint32(uid, 'uid');
1166-
validateUint32(gid, 'gid');
1166+
validateInteger(uid, 'uid', -1, kMaxUserId);
1167+
validateInteger(gid, 'gid', -1, kMaxUserId);
11671168
const ctx = { path };
11681169
binding.lchown(pathModule.toNamespacedPath(path), uid, gid, undefined, ctx);
11691170
handleErrorFromBinding(ctx);
11701171
}
11711172

11721173
function fchown(fd, uid, gid, callback) {
11731174
validateInt32(fd, 'fd', 0);
1174-
validateUint32(uid, 'uid');
1175-
validateUint32(gid, 'gid');
1175+
validateInteger(uid, 'uid', -1, kMaxUserId);
1176+
validateInteger(gid, 'gid', -1, kMaxUserId);
11761177

11771178
const req = new FSReqCallback();
11781179
req.oncomplete = makeCallback(callback);
@@ -1181,8 +1182,8 @@ function fchown(fd, uid, gid, callback) {
11811182

11821183
function fchownSync(fd, uid, gid) {
11831184
validateInt32(fd, 'fd', 0);
1184-
validateUint32(uid, 'uid');
1185-
validateUint32(gid, 'gid');
1185+
validateInteger(uid, 'uid', -1, kMaxUserId);
1186+
validateInteger(gid, 'gid', -1, kMaxUserId);
11861187

11871188
const ctx = {};
11881189
binding.fchown(fd, uid, gid, undefined, ctx);
@@ -1192,8 +1193,8 @@ function fchownSync(fd, uid, gid) {
11921193
function chown(path, uid, gid, callback) {
11931194
callback = makeCallback(callback);
11941195
path = getValidatedPath(path);
1195-
validateUint32(uid, 'uid');
1196-
validateUint32(gid, 'gid');
1196+
validateInteger(uid, 'uid', -1, kMaxUserId);
1197+
validateInteger(gid, 'gid', -1, kMaxUserId);
11971198

11981199
const req = new FSReqCallback();
11991200
req.oncomplete = callback;
@@ -1202,8 +1203,8 @@ function chown(path, uid, gid, callback) {
12021203

12031204
function chownSync(path, uid, gid) {
12041205
path = getValidatedPath(path);
1205-
validateUint32(uid, 'uid');
1206-
validateUint32(gid, 'gid');
1206+
validateInteger(uid, 'uid', -1, kMaxUserId);
1207+
validateInteger(gid, 'gid', -1, kMaxUserId);
12071208
const ctx = { path };
12081209
binding.chown(pathModule.toNamespacedPath(path), uid, gid, undefined, ctx);
12091210
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)