Skip to content

Commit 79ea8db

Browse files
authored
src,permission: throw remaining fs errors async
These APIs were missed in #52730. Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com> PR-URL: #67037 Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com>
1 parent 85eb0c7 commit 79ea8db

3 files changed

Lines changed: 170 additions & 66 deletions

File tree

‎src/node_file.cc‎

Lines changed: 77 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,23 +1403,32 @@ static void Symlink(const FunctionCallbackInfo<Value>& args) {
14031403
BufferValue target(isolate, args[0]);
14041404
CHECK_NOT_NULL(*target);
14051405
auto target_view = target.ToStringView();
1406-
// To avoid bypass the symlink target should be allowed to read and write
1407-
THROW_IF_INSUFFICIENT_PERMISSIONS(
1408-
env, permission::PermissionScope::kFileSystemRead, target_view);
1409-
THROW_IF_INSUFFICIENT_PERMISSIONS(
1410-
env, permission::PermissionScope::kFileSystemWrite, target_view);
14111406

14121407
BufferValue path(isolate, args[1]);
14131408
CHECK_NOT_NULL(*path);
14141409
ToNamespacedPath(env, &path);
1415-
THROW_IF_INSUFFICIENT_PERMISSIONS(
1416-
env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());
14171410

14181411
CHECK(args[2]->IsInt32());
14191412
int flags = args[2].As<Int32>()->Value();
14201413

14211414
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3);
14221415
if (req_wrap_async != nullptr) { // symlink(target, path, flags, req)
1416+
// To avoid bypass the symlink target should be allowed to read and write
1417+
ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS(
1418+
env,
1419+
req_wrap_async,
1420+
permission::PermissionScope::kFileSystemRead,
1421+
target_view);
1422+
ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS(
1423+
env,
1424+
req_wrap_async,
1425+
permission::PermissionScope::kFileSystemWrite,
1426+
target_view);
1427+
ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS(
1428+
env,
1429+
req_wrap_async,
1430+
permission::PermissionScope::kFileSystemWrite,
1431+
path.ToStringView());
14231432
FS_ASYNC_TRACE_BEGIN2(UV_FS_SYMLINK,
14241433
req_wrap_async,
14251434
"target",
@@ -1439,6 +1448,15 @@ static void Symlink(const FunctionCallbackInfo<Value>& args) {
14391448
*path,
14401449
flags);
14411450
} else { // symlink(target, path, flags, undefined, ctx)
1451+
// To avoid bypass the symlink target should be allowed to read and write
1452+
THROW_IF_INSUFFICIENT_PERMISSIONS(
1453+
env, permission::PermissionScope::kFileSystemRead, target_view);
1454+
THROW_IF_INSUFFICIENT_PERMISSIONS(
1455+
env, permission::PermissionScope::kFileSystemWrite, target_view);
1456+
THROW_IF_INSUFFICIENT_PERMISSIONS(
1457+
env,
1458+
permission::PermissionScope::kFileSystemWrite,
1459+
path.ToStringView());
14421460
FSReqWrapSync req_wrap_sync("symlink", *target, *path);
14431461
FS_SYNC_TRACE_BEGIN(symlink);
14441462
SyncCallAndThrowOnError(
@@ -1520,13 +1538,16 @@ static void ReadLink(const FunctionCallbackInfo<Value>& args) {
15201538
BufferValue path(isolate, args[0]);
15211539
CHECK_NOT_NULL(*path);
15221540
ToNamespacedPath(env, &path);
1523-
THROW_IF_INSUFFICIENT_PERMISSIONS(
1524-
env, permission::PermissionScope::kFileSystemRead, path.ToStringView());
15251541

15261542
const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8);
15271543

15281544
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 2);
15291545
if (req_wrap_async != nullptr) { // readlink(path, encoding, req)
1546+
ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS(
1547+
env,
1548+
req_wrap_async,
1549+
permission::PermissionScope::kFileSystemRead,
1550+
path.ToStringView());
15301551
FS_ASYNC_TRACE_BEGIN1(
15311552
UV_FS_READLINK, req_wrap_async, "path", TRACE_STR_COPY(*path))
15321553
AsyncCall(env,
@@ -1538,6 +1559,8 @@ static void ReadLink(const FunctionCallbackInfo<Value>& args) {
15381559
uv_fs_readlink,
15391560
*path);
15401561
} else { // readlink(path, encoding)
1562+
THROW_IF_INSUFFICIENT_PERMISSIONS(
1563+
env, permission::PermissionScope::kFileSystemRead, path.ToStringView());
15411564
FSReqWrapSync req_wrap_sync("readlink", *path);
15421565
FS_SYNC_TRACE_BEGIN(readlink);
15431566
int err =
@@ -1746,11 +1769,14 @@ static void RMDir(const FunctionCallbackInfo<Value>& args) {
17461769
BufferValue path(env->isolate(), args[0]);
17471770
CHECK_NOT_NULL(*path);
17481771
ToNamespacedPath(env, &path);
1749-
THROW_IF_INSUFFICIENT_PERMISSIONS(
1750-
env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());
17511772

17521773
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 1);
17531774
if (req_wrap_async != nullptr) { // rmdir(path, req)
1775+
ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS(
1776+
env,
1777+
req_wrap_async,
1778+
permission::PermissionScope::kFileSystemWrite,
1779+
path.ToStringView());
17541780
FS_ASYNC_TRACE_BEGIN1(
17551781
UV_FS_RMDIR, req_wrap_async, "path", TRACE_STR_COPY(*path))
17561782
AsyncCall(env,
@@ -1762,6 +1788,10 @@ static void RMDir(const FunctionCallbackInfo<Value>& args) {
17621788
uv_fs_rmdir,
17631789
*path);
17641790
} else { // rmdir(path)
1791+
THROW_IF_INSUFFICIENT_PERMISSIONS(
1792+
env,
1793+
permission::PermissionScope::kFileSystemWrite,
1794+
path.ToStringView());
17651795
FSReqWrapSync req_wrap_sync("rmdir", *path);
17661796
FS_SYNC_TRACE_BEGIN(rmdir);
17671797
SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_rmdir, *path);
@@ -2123,9 +2153,6 @@ static void MKDir(const FunctionCallbackInfo<Value>& args) {
21232153
CHECK_NOT_NULL(*path);
21242154
ToNamespacedPath(env, &path);
21252155

2126-
THROW_IF_INSUFFICIENT_PERMISSIONS(
2127-
env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());
2128-
21292156
CHECK(args[1]->IsInt32());
21302157
const int mode = args[1].As<Int32>()->Value();
21312158

@@ -2134,6 +2161,11 @@ static void MKDir(const FunctionCallbackInfo<Value>& args) {
21342161

21352162
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3);
21362163
if (req_wrap_async != nullptr) { // mkdir(path, mode, recursive, req)
2164+
ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS(
2165+
env,
2166+
req_wrap_async,
2167+
permission::PermissionScope::kFileSystemWrite,
2168+
path.ToStringView());
21372169
FS_ASYNC_TRACE_BEGIN1(
21382170
UV_FS_UNLINK, req_wrap_async, "path", TRACE_STR_COPY(*path))
21392171
AsyncCall(env,
@@ -2146,6 +2178,10 @@ static void MKDir(const FunctionCallbackInfo<Value>& args) {
21462178
*path,
21472179
mode);
21482180
} else { // mkdir(path, mode, recursive)
2181+
THROW_IF_INSUFFICIENT_PERMISSIONS(
2182+
env,
2183+
permission::PermissionScope::kFileSystemWrite,
2184+
path.ToStringView());
21492185
FSReqWrapSync req_wrap_sync("mkdir", *path);
21502186
FS_SYNC_TRACE_BEGIN(mkdir);
21512187
if (mkdirp) {
@@ -4064,14 +4100,17 @@ static void Chmod(const FunctionCallbackInfo<Value>& args) {
40644100
BufferValue path(env->isolate(), args[0]);
40654101
CHECK_NOT_NULL(*path);
40664102
ToNamespacedPath(env, &path);
4067-
THROW_IF_INSUFFICIENT_PERMISSIONS(
4068-
env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());
40694103

40704104
CHECK(args[1]->IsInt32());
40714105
int mode = args[1].As<Int32>()->Value();
40724106

40734107
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 2);
40744108
if (req_wrap_async != nullptr) { // chmod(path, mode, req)
4109+
ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS(
4110+
env,
4111+
req_wrap_async,
4112+
permission::PermissionScope::kFileSystemWrite,
4113+
path.ToStringView());
40754114
FS_ASYNC_TRACE_BEGIN1(
40764115
UV_FS_CHMOD, req_wrap_async, "path", TRACE_STR_COPY(*path))
40774116
AsyncCall(env,
@@ -4084,6 +4123,10 @@ static void Chmod(const FunctionCallbackInfo<Value>& args) {
40844123
*path,
40854124
mode);
40864125
} else { // chmod(path, mode)
4126+
THROW_IF_INSUFFICIENT_PERMISSIONS(
4127+
env,
4128+
permission::PermissionScope::kFileSystemWrite,
4129+
path.ToStringView());
40874130
FSReqWrapSync req_wrap_sync("chmod", *path);
40884131
FS_SYNC_TRACE_BEGIN(chmod);
40894132
SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_chmod, *path, mode);
@@ -4267,8 +4310,6 @@ static void UTimes(const FunctionCallbackInfo<Value>& args) {
42674310
BufferValue path(env->isolate(), args[0]);
42684311
CHECK_NOT_NULL(*path);
42694312
ToNamespacedPath(env, &path);
4270-
THROW_IF_INSUFFICIENT_PERMISSIONS(
4271-
env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());
42724313

42734314
CHECK(args[1]->IsNumber());
42744315
const double atime = args[1].As<Number>()->Value();
@@ -4278,6 +4319,11 @@ static void UTimes(const FunctionCallbackInfo<Value>& args) {
42784319

42794320
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3);
42804321
if (req_wrap_async != nullptr) { // utimes(path, atime, mtime, req)
4322+
ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS(
4323+
env,
4324+
req_wrap_async,
4325+
permission::PermissionScope::kFileSystemWrite,
4326+
path.ToStringView());
42814327
FS_ASYNC_TRACE_BEGIN1(
42824328
UV_FS_UTIME, req_wrap_async, "path", TRACE_STR_COPY(*path))
42834329
AsyncCall(env,
@@ -4291,6 +4337,10 @@ static void UTimes(const FunctionCallbackInfo<Value>& args) {
42914337
atime,
42924338
mtime);
42934339
} else { // utimes(path, atime, mtime)
4340+
THROW_IF_INSUFFICIENT_PERMISSIONS(
4341+
env,
4342+
permission::PermissionScope::kFileSystemWrite,
4343+
path.ToStringView());
42944344
FSReqWrapSync req_wrap_sync("utime", *path);
42954345
FS_SYNC_TRACE_BEGIN(utimes);
42964346
SyncCallAndThrowOnError(
@@ -4347,8 +4397,6 @@ static void LUTimes(const FunctionCallbackInfo<Value>& args) {
43474397
BufferValue path(env->isolate(), args[0]);
43484398
CHECK_NOT_NULL(*path);
43494399
ToNamespacedPath(env, &path);
4350-
THROW_IF_INSUFFICIENT_PERMISSIONS(
4351-
env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());
43524400

43534401
CHECK(args[1]->IsNumber());
43544402
const double atime = args[1].As<Number>()->Value();
@@ -4358,6 +4406,11 @@ static void LUTimes(const FunctionCallbackInfo<Value>& args) {
43584406

43594407
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3);
43604408
if (req_wrap_async != nullptr) { // lutimes(path, atime, mtime, req)
4409+
ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS(
4410+
env,
4411+
req_wrap_async,
4412+
permission::PermissionScope::kFileSystemWrite,
4413+
path.ToStringView());
43614414
FS_ASYNC_TRACE_BEGIN1(
43624415
UV_FS_LUTIME, req_wrap_async, "path", TRACE_STR_COPY(*path))
43634416
AsyncCall(env,
@@ -4371,6 +4424,10 @@ static void LUTimes(const FunctionCallbackInfo<Value>& args) {
43714424
atime,
43724425
mtime);
43734426
} else { // lutimes(path, atime, mtime)
4427+
THROW_IF_INSUFFICIENT_PERMISSIONS(
4428+
env,
4429+
permission::PermissionScope::kFileSystemWrite,
4430+
path.ToStringView());
43744431
FSReqWrapSync req_wrap_sync("lutime", *path);
43754432
FS_SYNC_TRACE_BEGIN(lutimes);
43764433
SyncCallAndThrowOnError(

‎test/fixtures/permission/fs-read.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,34 @@ const regularFile = __filename;
721721
});
722722
}
723723

724+
// fs.readlink
725+
{
726+
fs.readlink(blockedFile, common.expectsError({
727+
code: 'ERR_ACCESS_DENIED',
728+
permission: 'FileSystemRead',
729+
resource: path.toNamespacedPath(blockedFile),
730+
}));
731+
fs.readlink(bufferBlockedFile, common.expectsError({
732+
code: 'ERR_ACCESS_DENIED',
733+
permission: 'FileSystemRead',
734+
resource: path.toNamespacedPath(blockedFile),
735+
}));
736+
assert.throws(() => {
737+
fs.readlinkSync(blockedFile);
738+
}, common.expectsError({
739+
code: 'ERR_ACCESS_DENIED',
740+
permission: 'FileSystemRead',
741+
resource: path.toNamespacedPath(blockedFile),
742+
}));
743+
assert.rejects(async () => {
744+
await fsPromises.readlink(blockedFile);
745+
}, common.expectsError({
746+
code: 'ERR_ACCESS_DENIED',
747+
permission: 'FileSystemRead',
748+
resource: path.toNamespacedPath(blockedFile),
749+
})).then(common.mustCall());
750+
}
751+
724752
// fs.realpath (JS implementation, walks the path with lstat)
725753
{
726754
assert.throws(() => {

0 commit comments

Comments
 (0)