Skip to content

Commit 8598745

Browse files
RaisinTentargos
authored andcommitted
fs: fix chown abort
This syncs the type assertion introduced in the referenced PR in the C++ side. Since chown, lchown, and fchown can accept -1 as a value for uid and gid, we should also accept signed integers from the JS side. Fixes: #37995 Refs: #31694 PR-URL: #38004 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 21bc5d4 commit 8598745

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

lib/internal/fs/promises.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ const kIoMaxLength = 2 ** 31 - 1;
99
const kReadFileMaxChunkSize = 2 ** 14;
1010
const kWriteFileMaxChunkSize = 2 ** 14;
1111

12+
// 2 ** 32 - 1
13+
const kMaxUserId = 4294967295;
14+
1215
const {
1316
Error,
1417
MathMax,
@@ -66,7 +69,6 @@ const {
6669
validateAbortSignal,
6770
validateBuffer,
6871
validateInteger,
69-
validateUint32
7072
} = require('internal/validators');
7173
const pathModule = require('path');
7274
const { promisify } = require('internal/util');
@@ -580,22 +582,22 @@ async function lchmod(path, mode) {
580582

581583
async function lchown(path, uid, gid) {
582584
path = getValidatedPath(path);
583-
validateUint32(uid, 'uid');
584-
validateUint32(gid, 'gid');
585+
validateInteger(uid, 'uid', -1, kMaxUserId);
586+
validateInteger(gid, 'gid', -1, kMaxUserId);
585587
return binding.lchown(pathModule.toNamespacedPath(path),
586588
uid, gid, kUsePromises);
587589
}
588590

589591
async function fchown(handle, uid, gid) {
590-
validateUint32(uid, 'uid');
591-
validateUint32(gid, 'gid');
592+
validateInteger(uid, 'uid', -1, kMaxUserId);
593+
validateInteger(gid, 'gid', -1, kMaxUserId);
592594
return binding.fchown(handle.fd, uid, gid, kUsePromises);
593595
}
594596

595597
async function chown(path, uid, gid) {
596598
path = getValidatedPath(path);
597-
validateUint32(uid, 'uid');
598-
validateUint32(gid, 'gid');
599+
validateInteger(uid, 'uid', -1, kMaxUserId);
600+
validateInteger(gid, 'gid', -1, kMaxUserId);
599601
return binding.chown(pathModule.toNamespacedPath(path),
600602
uid, gid, kUsePromises);
601603
}

src/node_file.cc

+12-13
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ using v8::ObjectTemplate;
6969
using v8::Promise;
7070
using v8::String;
7171
using v8::Symbol;
72-
using v8::Uint32;
7372
using v8::Undefined;
7473
using v8::Value;
7574

@@ -2178,11 +2177,11 @@ static void Chown(const FunctionCallbackInfo<Value>& args) {
21782177
BufferValue path(env->isolate(), args[0]);
21792178
CHECK_NOT_NULL(*path);
21802179

2181-
CHECK(args[1]->IsUint32());
2182-
const uv_uid_t uid = static_cast<uv_uid_t>(args[1].As<Uint32>()->Value());
2180+
CHECK(IsSafeJsInt(args[1]));
2181+
const uv_uid_t uid = static_cast<uv_uid_t>(args[1].As<Integer>()->Value());
21832182

2184-
CHECK(args[2]->IsUint32());
2185-
const uv_gid_t gid = static_cast<uv_gid_t>(args[2].As<Uint32>()->Value());
2183+
CHECK(IsSafeJsInt(args[2]));
2184+
const uv_gid_t gid = static_cast<uv_gid_t>(args[2].As<Integer>()->Value());
21862185

21872186
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
21882187
if (req_wrap_async != nullptr) { // chown(path, uid, gid, req)
@@ -2211,11 +2210,11 @@ static void FChown(const FunctionCallbackInfo<Value>& args) {
22112210
CHECK(args[0]->IsInt32());
22122211
const int fd = args[0].As<Int32>()->Value();
22132212

2214-
CHECK(args[1]->IsUint32());
2215-
const uv_uid_t uid = static_cast<uv_uid_t>(args[1].As<Uint32>()->Value());
2213+
CHECK(IsSafeJsInt(args[1]));
2214+
const uv_uid_t uid = static_cast<uv_uid_t>(args[1].As<Integer>()->Value());
22162215

2217-
CHECK(args[2]->IsUint32());
2218-
const uv_gid_t gid = static_cast<uv_gid_t>(args[2].As<Uint32>()->Value());
2216+
CHECK(IsSafeJsInt(args[2]));
2217+
const uv_gid_t gid = static_cast<uv_gid_t>(args[2].As<Integer>()->Value());
22192218

22202219
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
22212220
if (req_wrap_async != nullptr) { // fchown(fd, uid, gid, req)
@@ -2241,11 +2240,11 @@ static void LChown(const FunctionCallbackInfo<Value>& args) {
22412240
BufferValue path(env->isolate(), args[0]);
22422241
CHECK_NOT_NULL(*path);
22432242

2244-
CHECK(args[1]->IsUint32());
2245-
const uv_uid_t uid = static_cast<uv_uid_t>(args[1].As<Uint32>()->Value());
2243+
CHECK(IsSafeJsInt(args[1]));
2244+
const uv_uid_t uid = static_cast<uv_uid_t>(args[1].As<Integer>()->Value());
22462245

2247-
CHECK(args[2]->IsUint32());
2248-
const uv_gid_t gid = static_cast<uv_gid_t>(args[2].As<Uint32>()->Value());
2246+
CHECK(IsSafeJsInt(args[2]));
2247+
const uv_gid_t gid = static_cast<uv_gid_t>(args[2].As<Integer>()->Value());
22492248

22502249
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
22512250
if (req_wrap_async != nullptr) { // lchown(path, uid, gid, req)

test/parallel/test-fs-promises.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -183,24 +183,24 @@ async function getHandle(dest) {
183183

184184
assert.rejects(
185185
async () => {
186-
await chown(dest, 1, -1);
186+
await chown(dest, 1, -2);
187187
},
188188
{
189189
code: 'ERR_OUT_OF_RANGE',
190190
name: 'RangeError',
191191
message: 'The value of "gid" is out of range. ' +
192-
'It must be >= 0 && < 4294967296. Received -1'
192+
'It must be >= -1 && <= 4294967295. Received -2'
193193
});
194194

195195
assert.rejects(
196196
async () => {
197-
await handle.chown(1, -1);
197+
await handle.chown(1, -2);
198198
},
199199
{
200200
code: 'ERR_OUT_OF_RANGE',
201201
name: 'RangeError',
202202
message: 'The value of "gid" is out of range. ' +
203-
'It must be >= 0 && < 4294967296. Received -1'
203+
'It must be >= -1 && <= 4294967295. Received -2'
204204
});
205205

206206
await handle.close();

0 commit comments

Comments
 (0)