Commit 79ea8db85d4 for nodejs

commit 79ea8db85d49ef22df57b5dcd4491b7df82bfcf2
Author: Rafael Gonzaga <rafael.nunu@hotmail.com>
Date:   Mon Nov 23 22:41:49 2026 -0300

    src,permission: throw remaining fs errors async

    These APIs were missed in https://github.com/nodejs/node/pull/52730.

    Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com>
    PR-URL: https://github.com/nodejs/node/pull/67037
    Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com>

diff --git a/src/node_file.cc b/src/node_file.cc
index aee9882ccd6..5e33d40c776 100644
--- a/src/node_file.cc
+++ b/src/node_file.cc
@@ -1403,23 +1403,32 @@ static void Symlink(const FunctionCallbackInfo<Value>& args) {
   BufferValue target(isolate, args[0]);
   CHECK_NOT_NULL(*target);
   auto target_view = target.ToStringView();
-  // To avoid bypass the symlink target should be allowed to read and write
-  THROW_IF_INSUFFICIENT_PERMISSIONS(
-      env, permission::PermissionScope::kFileSystemRead, target_view);
-  THROW_IF_INSUFFICIENT_PERMISSIONS(
-      env, permission::PermissionScope::kFileSystemWrite, target_view);

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

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

   FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3);
   if (req_wrap_async != nullptr) {  // symlink(target, path, flags, req)
+    // To avoid bypass the symlink target should be allowed to read and write
+    ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS(
+        env,
+        req_wrap_async,
+        permission::PermissionScope::kFileSystemRead,
+        target_view);
+    ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS(
+        env,
+        req_wrap_async,
+        permission::PermissionScope::kFileSystemWrite,
+        target_view);
+    ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS(
+        env,
+        req_wrap_async,
+        permission::PermissionScope::kFileSystemWrite,
+        path.ToStringView());
     FS_ASYNC_TRACE_BEGIN2(UV_FS_SYMLINK,
                           req_wrap_async,
                           "target",
@@ -1439,6 +1448,15 @@ static void Symlink(const FunctionCallbackInfo<Value>& args) {
                   *path,
                   flags);
   } else {  // symlink(target, path, flags, undefined, ctx)
+    // To avoid bypass the symlink target should be allowed to read and write
+    THROW_IF_INSUFFICIENT_PERMISSIONS(
+        env, permission::PermissionScope::kFileSystemRead, target_view);
+    THROW_IF_INSUFFICIENT_PERMISSIONS(
+        env, permission::PermissionScope::kFileSystemWrite, target_view);
+    THROW_IF_INSUFFICIENT_PERMISSIONS(
+        env,
+        permission::PermissionScope::kFileSystemWrite,
+        path.ToStringView());
     FSReqWrapSync req_wrap_sync("symlink", *target, *path);
     FS_SYNC_TRACE_BEGIN(symlink);
     SyncCallAndThrowOnError(
@@ -1520,13 +1538,16 @@ static void ReadLink(const FunctionCallbackInfo<Value>& args) {
   BufferValue path(isolate, args[0]);
   CHECK_NOT_NULL(*path);
   ToNamespacedPath(env, &path);
-  THROW_IF_INSUFFICIENT_PERMISSIONS(
-      env, permission::PermissionScope::kFileSystemRead, path.ToStringView());

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

   FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 2);
   if (req_wrap_async != nullptr) {  // readlink(path, encoding, req)
+    ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS(
+        env,
+        req_wrap_async,
+        permission::PermissionScope::kFileSystemRead,
+        path.ToStringView());
     FS_ASYNC_TRACE_BEGIN1(
         UV_FS_READLINK, req_wrap_async, "path", TRACE_STR_COPY(*path))
     AsyncCall(env,
@@ -1538,6 +1559,8 @@ static void ReadLink(const FunctionCallbackInfo<Value>& args) {
               uv_fs_readlink,
               *path);
   } else {  // readlink(path, encoding)
+    THROW_IF_INSUFFICIENT_PERMISSIONS(
+        env, permission::PermissionScope::kFileSystemRead, path.ToStringView());
     FSReqWrapSync req_wrap_sync("readlink", *path);
     FS_SYNC_TRACE_BEGIN(readlink);
     int err =
@@ -1746,11 +1769,14 @@ static void RMDir(const FunctionCallbackInfo<Value>& args) {
   BufferValue path(env->isolate(), args[0]);
   CHECK_NOT_NULL(*path);
   ToNamespacedPath(env, &path);
-  THROW_IF_INSUFFICIENT_PERMISSIONS(
-      env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());

   FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 1);
   if (req_wrap_async != nullptr) {  // rmdir(path, req)
+    ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS(
+        env,
+        req_wrap_async,
+        permission::PermissionScope::kFileSystemWrite,
+        path.ToStringView());
     FS_ASYNC_TRACE_BEGIN1(
         UV_FS_RMDIR, req_wrap_async, "path", TRACE_STR_COPY(*path))
     AsyncCall(env,
@@ -1762,6 +1788,10 @@ static void RMDir(const FunctionCallbackInfo<Value>& args) {
               uv_fs_rmdir,
               *path);
   } else {  // rmdir(path)
+    THROW_IF_INSUFFICIENT_PERMISSIONS(
+        env,
+        permission::PermissionScope::kFileSystemWrite,
+        path.ToStringView());
     FSReqWrapSync req_wrap_sync("rmdir", *path);
     FS_SYNC_TRACE_BEGIN(rmdir);
     SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_rmdir, *path);
@@ -2123,9 +2153,6 @@ static void MKDir(const FunctionCallbackInfo<Value>& args) {
   CHECK_NOT_NULL(*path);
   ToNamespacedPath(env, &path);

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

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

   FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3);
   if (req_wrap_async != nullptr) {  // mkdir(path, mode, recursive, req)
+    ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS(
+        env,
+        req_wrap_async,
+        permission::PermissionScope::kFileSystemWrite,
+        path.ToStringView());
     FS_ASYNC_TRACE_BEGIN1(
         UV_FS_UNLINK, req_wrap_async, "path", TRACE_STR_COPY(*path))
     AsyncCall(env,
@@ -2146,6 +2178,10 @@ static void MKDir(const FunctionCallbackInfo<Value>& args) {
               *path,
               mode);
   } else {  // mkdir(path, mode, recursive)
+    THROW_IF_INSUFFICIENT_PERMISSIONS(
+        env,
+        permission::PermissionScope::kFileSystemWrite,
+        path.ToStringView());
     FSReqWrapSync req_wrap_sync("mkdir", *path);
     FS_SYNC_TRACE_BEGIN(mkdir);
     if (mkdirp) {
@@ -4064,14 +4100,17 @@ static void Chmod(const FunctionCallbackInfo<Value>& args) {
   BufferValue path(env->isolate(), args[0]);
   CHECK_NOT_NULL(*path);
   ToNamespacedPath(env, &path);
-  THROW_IF_INSUFFICIENT_PERMISSIONS(
-      env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());

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

   FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 2);
   if (req_wrap_async != nullptr) {  // chmod(path, mode, req)
+    ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS(
+        env,
+        req_wrap_async,
+        permission::PermissionScope::kFileSystemWrite,
+        path.ToStringView());
     FS_ASYNC_TRACE_BEGIN1(
         UV_FS_CHMOD, req_wrap_async, "path", TRACE_STR_COPY(*path))
     AsyncCall(env,
@@ -4084,6 +4123,10 @@ static void Chmod(const FunctionCallbackInfo<Value>& args) {
               *path,
               mode);
   } else {  // chmod(path, mode)
+    THROW_IF_INSUFFICIENT_PERMISSIONS(
+        env,
+        permission::PermissionScope::kFileSystemWrite,
+        path.ToStringView());
     FSReqWrapSync req_wrap_sync("chmod", *path);
     FS_SYNC_TRACE_BEGIN(chmod);
     SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_chmod, *path, mode);
@@ -4267,8 +4310,6 @@ static void UTimes(const FunctionCallbackInfo<Value>& args) {
   BufferValue path(env->isolate(), args[0]);
   CHECK_NOT_NULL(*path);
   ToNamespacedPath(env, &path);
-  THROW_IF_INSUFFICIENT_PERMISSIONS(
-      env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());

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

   FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3);
   if (req_wrap_async != nullptr) {  // utimes(path, atime, mtime, req)
+    ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS(
+        env,
+        req_wrap_async,
+        permission::PermissionScope::kFileSystemWrite,
+        path.ToStringView());
     FS_ASYNC_TRACE_BEGIN1(
         UV_FS_UTIME, req_wrap_async, "path", TRACE_STR_COPY(*path))
     AsyncCall(env,
@@ -4291,6 +4337,10 @@ static void UTimes(const FunctionCallbackInfo<Value>& args) {
               atime,
               mtime);
   } else {  // utimes(path, atime, mtime)
+    THROW_IF_INSUFFICIENT_PERMISSIONS(
+        env,
+        permission::PermissionScope::kFileSystemWrite,
+        path.ToStringView());
     FSReqWrapSync req_wrap_sync("utime", *path);
     FS_SYNC_TRACE_BEGIN(utimes);
     SyncCallAndThrowOnError(
@@ -4347,8 +4397,6 @@ static void LUTimes(const FunctionCallbackInfo<Value>& args) {
   BufferValue path(env->isolate(), args[0]);
   CHECK_NOT_NULL(*path);
   ToNamespacedPath(env, &path);
-  THROW_IF_INSUFFICIENT_PERMISSIONS(
-      env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());

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

   FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3);
   if (req_wrap_async != nullptr) {  // lutimes(path, atime, mtime, req)
+    ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS(
+        env,
+        req_wrap_async,
+        permission::PermissionScope::kFileSystemWrite,
+        path.ToStringView());
     FS_ASYNC_TRACE_BEGIN1(
         UV_FS_LUTIME, req_wrap_async, "path", TRACE_STR_COPY(*path))
     AsyncCall(env,
@@ -4371,6 +4424,10 @@ static void LUTimes(const FunctionCallbackInfo<Value>& args) {
               atime,
               mtime);
   } else {  // lutimes(path, atime, mtime)
+    THROW_IF_INSUFFICIENT_PERMISSIONS(
+        env,
+        permission::PermissionScope::kFileSystemWrite,
+        path.ToStringView());
     FSReqWrapSync req_wrap_sync("lutime", *path);
     FS_SYNC_TRACE_BEGIN(lutimes);
     SyncCallAndThrowOnError(
diff --git a/test/fixtures/permission/fs-read.js b/test/fixtures/permission/fs-read.js
index 2feb64d021b..1446dd7bc57 100644
--- a/test/fixtures/permission/fs-read.js
+++ b/test/fixtures/permission/fs-read.js
@@ -721,6 +721,34 @@ const regularFile = __filename;
   });
 }

+// fs.readlink
+{
+  fs.readlink(blockedFile, common.expectsError({
+    code: 'ERR_ACCESS_DENIED',
+    permission: 'FileSystemRead',
+    resource: path.toNamespacedPath(blockedFile),
+  }));
+  fs.readlink(bufferBlockedFile, common.expectsError({
+    code: 'ERR_ACCESS_DENIED',
+    permission: 'FileSystemRead',
+    resource: path.toNamespacedPath(blockedFile),
+  }));
+  assert.throws(() => {
+    fs.readlinkSync(blockedFile);
+  }, common.expectsError({
+    code: 'ERR_ACCESS_DENIED',
+    permission: 'FileSystemRead',
+    resource: path.toNamespacedPath(blockedFile),
+  }));
+  assert.rejects(async () => {
+    await fsPromises.readlink(blockedFile);
+  }, common.expectsError({
+    code: 'ERR_ACCESS_DENIED',
+    permission: 'FileSystemRead',
+    resource: path.toNamespacedPath(blockedFile),
+  })).then(common.mustCall());
+}
+
 // fs.realpath (JS implementation, walks the path with lstat)
 {
   assert.throws(() => {
diff --git a/test/fixtures/permission/fs-write.js b/test/fixtures/permission/fs-write.js
index 7a9963e17d9..4d226329e70 100644
--- a/test/fixtures/permission/fs-write.js
+++ b/test/fixtures/permission/fs-write.js
@@ -115,62 +115,59 @@ const relativeProtectedFolder = process.env.RELATIVEBLOCKEDFOLDER;

 // fs.utimes
 {
-  assert.throws(() => {
-    fs.utimes(blockedFile, new Date(), new Date(), () => {});
-  }, {
+  fs.utimes(blockedFile, new Date(), new Date(), common.expectsError({
     code: 'ERR_ACCESS_DENIED',
     permission: 'FileSystemWrite',
     resource: path.toNamespacedPath(blockedFile),
-  });
-  assert.throws(() => {
-    fs.utimes(bufferBlockedFile, new Date(), new Date(), () => {});
-  }, {
+  }));
+  fs.utimes(bufferBlockedFile, new Date(), new Date(), common.expectsError({
     code: 'ERR_ACCESS_DENIED',
     permission: 'FileSystemWrite',
     resource: path.toNamespacedPath(blockedFile),
-  });
-  assert.throws(() => {
-    fs.utimes(blockedFileURL, new Date(), new Date(), () => {});
-  }, {
+  }));
+  fs.utimes(blockedFileURL, new Date(), new Date(), common.expectsError({
     code: 'ERR_ACCESS_DENIED',
     permission: 'FileSystemWrite',
     resource: path.toNamespacedPath(blockedFile),
-  });
-  assert.throws(() => {
-    fs.utimes(relativeProtectedFile, new Date(), new Date(), () => {});
-  }, {
+  }));
+  fs.utimes(relativeProtectedFile, new Date(), new Date(), common.expectsError({
     code: 'ERR_ACCESS_DENIED',
     permission: 'FileSystemWrite',
     resource: path.toNamespacedPath(relativeProtectedFile),
-  });
-
+  }));
+  fs.utimes(path.join(blockedFolder, 'anyfile'), new Date(), new Date(), common.expectsError({
+    code: 'ERR_ACCESS_DENIED',
+    permission: 'FileSystemWrite',
+    resource: path.toNamespacedPath(path.join(blockedFolder, 'anyfile')),
+  }));
   assert.throws(() => {
-    fs.utimes(path.join(blockedFolder, 'anyfile'), new Date(), new Date(), () => {});
+    fs.utimesSync(blockedFile, new Date(), new Date());
   }, {
     code: 'ERR_ACCESS_DENIED',
     permission: 'FileSystemWrite',
-    resource: path.toNamespacedPath(path.join(blockedFolder, 'anyfile')),
+    resource: path.toNamespacedPath(blockedFile),
   });
 }

 // fs.lutimes
 {
-  assert.throws(() => {
-    fs.lutimes(blockedFile, new Date(), new Date(), () => {});
-  },{
+  fs.lutimes(blockedFile, new Date(), new Date(), common.expectsError({
     code: 'ERR_ACCESS_DENIED',
     permission: 'FileSystemWrite',
     resource: path.toNamespacedPath(blockedFile),
-  });
-  assert.throws(() => {
-    fs.lutimes(bufferBlockedFile, new Date(), new Date(), () => {});
-  },{
+  }));
+  fs.lutimes(bufferBlockedFile, new Date(), new Date(), common.expectsError({
     code: 'ERR_ACCESS_DENIED',
     permission: 'FileSystemWrite',
     resource: path.toNamespacedPath(blockedFile),
-  });
+  }));
+  fs.lutimes(blockedFileURL, new Date(), new Date(), common.expectsError({
+    code: 'ERR_ACCESS_DENIED',
+    permission: 'FileSystemWrite',
+    resource: path.toNamespacedPath(blockedFile),
+  }));
   assert.throws(() => {
-    fs.lutimes(blockedFileURL, new Date(), new Date(), () => {});
+    fs.lutimesSync(blockedFile, new Date(), new Date());
   }, {
     code: 'ERR_ACCESS_DENIED',
     permission: 'FileSystemWrite',
@@ -180,24 +177,46 @@ const relativeProtectedFolder = process.env.RELATIVEBLOCKEDFOLDER;

 // fs.mkdir
 {
+  fs.mkdir(path.join(blockedFolder, 'any-folder'), common.expectsError({
+    code: 'ERR_ACCESS_DENIED',
+    permission: 'FileSystemWrite',
+    resource: path.toNamespacedPath(path.join(blockedFolder, 'any-folder')),
+  }));
+  fs.mkdir(path.join(relativeProtectedFolder, 'any-folder'), common.expectsError({
+    code: 'ERR_ACCESS_DENIED',
+    permission: 'FileSystemWrite',
+    resource: path.toNamespacedPath(path.join(relativeProtectedFolder, 'any-folder')),
+  }));
   assert.throws(() => {
-    fs.mkdir(path.join(blockedFolder, 'any-folder'), (err) => {
-      assert.ifError(err);
-    });
-  },{
+    fs.mkdirSync(path.join(blockedFolder, 'any-folder'));
+  }, {
     code: 'ERR_ACCESS_DENIED',
     permission: 'FileSystemWrite',
     resource: path.toNamespacedPath(path.join(blockedFolder, 'any-folder')),
   });
+}
+
+// fs.rmdir
+{
+  fs.rmdir(blockedFolder, common.expectsError({
+    code: 'ERR_ACCESS_DENIED',
+    permission: 'FileSystemWrite',
+    resource: path.toNamespacedPath(blockedFolder),
+  }));
   assert.throws(() => {
-    fs.mkdir(path.join(relativeProtectedFolder, 'any-folder'), (err) => {
-      assert.ifError(err);
-    });
-  },{
+    fs.rmdirSync(blockedFolder);
+  }, {
     code: 'ERR_ACCESS_DENIED',
     permission: 'FileSystemWrite',
-    resource: path.toNamespacedPath(path.join(relativeProtectedFolder, 'any-folder')),
+    resource: path.toNamespacedPath(blockedFolder),
   });
+  assert.rejects(async () => {
+    await fsPromises.rmdir(blockedFolder);
+  }, {
+    code: 'ERR_ACCESS_DENIED',
+    permission: 'FileSystemWrite',
+    resource: path.toNamespacedPath(blockedFolder),
+  }).then(common.mustCall());
 }

 // fs.mkdtemp
@@ -401,20 +420,20 @@ const relativeProtectedFolder = process.env.RELATIVEBLOCKEDFOLDER;

 // fs.chmod
 {
-  assert.throws(() => {
-    fs.chmod(blockedFile, 0o755, common.mustNotCall());
-  }, {
+  fs.chmod(blockedFile, 0o755, common.expectsError({
     code: 'ERR_ACCESS_DENIED',
     permission: 'FileSystemWrite',
-  });
-  assert.throws(() => {
-    fs.chmod(bufferBlockedFile, 0o755, common.mustNotCall());
-  }, {
+  }));
+  fs.chmod(bufferBlockedFile, 0o755, common.expectsError({
     code: 'ERR_ACCESS_DENIED',
     permission: 'FileSystemWrite',
-  });
+  }));
+  fs.chmod(blockedFileURL, 0o755, common.expectsError({
+    code: 'ERR_ACCESS_DENIED',
+    permission: 'FileSystemWrite',
+  }));
   assert.throws(() => {
-    fs.chmod(blockedFileURL, 0o755, common.mustNotCall());
+    fs.chmodSync(blockedFile, 0o755);
   }, {
     code: 'ERR_ACCESS_DENIED',
     permission: 'FileSystemWrite',