Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 77 additions & 20 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1407,24 +1407,33 @@ 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();

if (argc > 3) { // symlink(target, path, flags, req)
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
CHECK_NOT_NULL(req_wrap_async);
// 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",
Expand All @@ -1444,6 +1453,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(
Expand Down Expand Up @@ -1534,14 +1552,17 @@ 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);

if (argc > 2) { // readlink(path, encoding, req)
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
CHECK_NOT_NULL(req_wrap_async);
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,
Expand All @@ -1553,6 +1574,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 =
Expand Down Expand Up @@ -1778,12 +1801,15 @@ 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());

if (argc > 1) {
FSReqBase* req_wrap_async = GetReqWrap(args, 1); // rmdir(path, req)
CHECK_NOT_NULL(req_wrap_async);
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,
Expand All @@ -1795,6 +1821,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);
Expand Down Expand Up @@ -2156,9 +2186,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();

Expand All @@ -2168,6 +2195,11 @@ static void MKDir(const FunctionCallbackInfo<Value>& args) {
if (argc > 3) { // mkdir(path, mode, recursive, req)
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
CHECK_NOT_NULL(req_wrap_async);
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,
Expand All @@ -2180,6 +2212,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) {
Expand Down Expand Up @@ -4117,15 +4153,18 @@ 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();

if (argc > 2) { // chmod(path, mode, req)
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
CHECK_NOT_NULL(req_wrap_async);
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,
Expand All @@ -4138,6 +4177,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);
Expand Down Expand Up @@ -4333,8 +4376,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();
Expand All @@ -4345,6 +4386,11 @@ static void UTimes(const FunctionCallbackInfo<Value>& args) {
if (argc > 3) { // utimes(path, atime, mtime, req)
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
CHECK_NOT_NULL(req_wrap_async);
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,
Expand All @@ -4358,6 +4404,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(
Expand Down Expand Up @@ -4415,8 +4465,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();
Expand All @@ -4427,6 +4475,11 @@ static void LUTimes(const FunctionCallbackInfo<Value>& args) {
if (argc > 3) { // lutimes(path, atime, mtime, req)
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
CHECK_NOT_NULL(req_wrap_async);
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,
Expand All @@ -4440,6 +4493,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(
Expand Down
28 changes: 28 additions & 0 deletions test/fixtures/permission/fs-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
Loading
Loading