Skip to content

Commit 2417f0e

Browse files
committed
permission: clamp Worker grants to parent for explicit execArgv
SEMVER-MAJOR: when the parent has the Permission Model enabled, a Worker with explicit execArgv (including []) cannot obtain wider permission-related grants than the parent. Only runs when execArgv is an explicit array. Clamps EnvironmentOptions and rebuilds exec_argv_out so CreateEnvironment enables Permission with the clamped allow lists. Signed-off-by: yunshingng <yunshingng25@gmail.com>
1 parent 76bb3f7 commit 2417f0e

4 files changed

Lines changed: 441 additions & 0 deletions

File tree

‎doc/api/permissions.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ changes:
3838
description: This feature is no longer experimental.
3939
-->
4040

41+
<!-- worker-execargv-permission-ceiling -->
42+
When the Permission Model is enabled in the parent process, creating a
43+
`worker_threads.Worker` with an explicit `execArgv` option (including an empty
44+
array) no longer allows the worker to obtain a wider permission-related grant
45+
set than the parent. Non-permission `execArgv` flags are unaffected. This is a
46+
breaking change relative to earlier releases where `execArgv: []` could drop
47+
the parent's Permission Model grants.
48+
49+
4150
> Stability: 2 - Stable
4251
4352
The Node.js Permission Model is a mechanism for restricting access to specific

‎doc/api/worker_threads.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,6 +1605,13 @@ changes:
16051605
description: The `resourceLimits` option was introduced.
16061606
-->
16071607
1608+
<!-- worker-execargv-permission-ceiling -->
1609+
**Permission Model (breaking):** If the parent process runs with the
1610+
Permission Model enabled, an explicit `execArgv` (including `[]`) does not
1611+
disable or exceed the parent's permission-related grants. See the
1612+
[Permission Model](permissions.md#permission-model) documentation.
1613+
1614+
16081615
* `filename` {string|URL} The path to the Worker's main script or module. Must
16091616
be either an absolute path or a relative path (i.e. relative to the
16101617
current working directory) starting with `./` or `../`, or a WHATWG `URL`

‎src/node_worker.cc‎

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "node_profiling.h"
1212
#include "node_snapshot_builder.h"
1313
#include "permission/permission.h"
14+
#include "path.h"
1415
#include "util-inl.h"
1516
#include "v8-cppgc.h"
1617
#include "v8-profiler.h"
@@ -504,6 +505,253 @@ Worker::~Worker() {
504505
Debug(this, "Worker %llu destroyed", thread_id_.id);
505506
}
506507

508+
509+
// SEMVER-MAJOR: Permission ceiling for Worker explicit execArgv.
510+
static bool WorkerConfiguredPermission(const EnvironmentOptions* w) {
511+
if (w->permission || w->permission_audit) {
512+
return true;
513+
}
514+
if (!w->allow_fs_read.empty() || !w->allow_fs_write.empty()) {
515+
return true;
516+
}
517+
return w->allow_addons || w->allow_inspector || w->allow_child_process ||
518+
w->allow_net || w->allow_wasi || w->allow_ffi ||
519+
w->allow_openssl_store || w->allow_worker_threads;
520+
}
521+
522+
static void ApplyParentPermissionCeiling(EnvironmentOptions* w,
523+
const EnvironmentOptions* parent) {
524+
w->permission = true;
525+
w->permission_audit = parent->permission_audit;
526+
w->allow_addons = parent->allow_addons;
527+
w->allow_inspector = parent->allow_inspector;
528+
w->allow_child_process = parent->allow_child_process;
529+
w->allow_net = parent->allow_net;
530+
w->allow_wasi = parent->allow_wasi;
531+
w->allow_ffi = parent->allow_ffi;
532+
w->allow_openssl_store = parent->allow_openssl_store;
533+
w->allow_worker_threads = parent->allow_worker_threads;
534+
w->allow_fs_read = parent->allow_fs_read;
535+
w->allow_fs_write = parent->allow_fs_write;
536+
}
537+
538+
static void NormalizePathForCompare(std::string* s) {
539+
while (s->size() > 1 && (s->back() == '/' || s->back() == '\\')) {
540+
s->pop_back();
541+
}
542+
#ifdef _WIN32
543+
for (char& c : *s) {
544+
if (c >= 'A' && c <= 'Z') {
545+
c = static_cast<char>(c - 'A' + 'a');
546+
}
547+
if (c == '/') {
548+
c = '\\';
549+
}
550+
}
551+
#endif
552+
}
553+
554+
static std::string ResolveForCompare(Environment* env, const std::string& in) {
555+
if (in.empty() || in == "*") {
556+
return in;
557+
}
558+
std::string resolved =
559+
PathResolve(env, std::vector<std::string_view>{std::string_view(in)});
560+
if (resolved.empty()) {
561+
resolved = in;
562+
}
563+
NormalizePathForCompare(&resolved);
564+
return resolved;
565+
}
566+
567+
static bool PathCoveredByParentEntry(Environment* env,
568+
const std::string& parent_raw,
569+
const std::string& requested_raw) {
570+
if (parent_raw == "*") {
571+
return true;
572+
}
573+
const std::string parent = ResolveForCompare(env, parent_raw);
574+
const std::string requested = ResolveForCompare(env, requested_raw);
575+
if (parent.empty()) {
576+
return false;
577+
}
578+
if (requested == parent) {
579+
return true;
580+
}
581+
if (requested.size() <= parent.size()) {
582+
return false;
583+
}
584+
if (requested.compare(0, parent.size(), parent) != 0) {
585+
return false;
586+
}
587+
const char next = requested[parent.size()];
588+
return next == '/' || next == '\\';
589+
}
590+
591+
static bool ParentListHasWildcard(const std::vector<std::string>& parent) {
592+
for (const std::string& entry : parent) {
593+
if (entry == "*") {
594+
return true;
595+
}
596+
}
597+
return false;
598+
}
599+
600+
static void FilterPathListToParentSubset(
601+
Environment* env,
602+
EnvironmentOptions* w,
603+
std::vector<std::string>* worker,
604+
const std::vector<std::string>& parent) {
605+
if (worker == nullptr) {
606+
return;
607+
}
608+
if (worker->empty()) {
609+
if (w->permission || w->permission_audit) {
610+
return;
611+
}
612+
*worker = parent;
613+
return;
614+
}
615+
if (ParentListHasWildcard(parent)) {
616+
return;
617+
}
618+
std::vector<std::string> out;
619+
out.reserve(worker->size());
620+
for (const std::string& wpath : *worker) {
621+
if (wpath == "*") {
622+
continue;
623+
}
624+
for (const std::string& entry : parent) {
625+
if (PathCoveredByParentEntry(env, entry, wpath)) {
626+
out.push_back(wpath);
627+
break;
628+
}
629+
}
630+
}
631+
*worker = std::move(out);
632+
}
633+
634+
static void IntersectPermissionGrants(Environment* env,
635+
EnvironmentOptions* w,
636+
const EnvironmentOptions* parent) {
637+
w->permission = true;
638+
w->permission_audit = w->permission_audit || parent->permission_audit;
639+
w->allow_addons = w->allow_addons && parent->allow_addons;
640+
w->allow_inspector = w->allow_inspector && parent->allow_inspector;
641+
w->allow_child_process =
642+
w->allow_child_process && parent->allow_child_process;
643+
w->allow_net = w->allow_net && parent->allow_net;
644+
w->allow_wasi = w->allow_wasi && parent->allow_wasi;
645+
w->allow_ffi = w->allow_ffi && parent->allow_ffi;
646+
w->allow_openssl_store =
647+
w->allow_openssl_store && parent->allow_openssl_store;
648+
w->allow_worker_threads =
649+
w->allow_worker_threads && parent->allow_worker_threads;
650+
FilterPathListToParentSubset(env, w, &w->allow_fs_read, parent->allow_fs_read);
651+
FilterPathListToParentSubset(
652+
env, w, &w->allow_fs_write, parent->allow_fs_write);
653+
}
654+
655+
static void ClampWorkerPermissionToParent(Environment* env,
656+
PerIsolateOptions* worker_opts) {
657+
if (worker_opts == nullptr || !env->permission()->enabled()) {
658+
return;
659+
}
660+
EnvironmentOptions* parent = env->isolate_data()->options()->get_per_env_options();
661+
EnvironmentOptions* w = worker_opts->get_per_env_options();
662+
if (parent == nullptr || w == nullptr) {
663+
return;
664+
}
665+
if (!WorkerConfiguredPermission(w)) {
666+
ApplyParentPermissionCeiling(w, parent);
667+
} else {
668+
IntersectPermissionGrants(env, w, parent);
669+
}
670+
}
671+
672+
static bool IsPermissionCliToken(const std::string& a) {
673+
if (a == "--permission" || a == "--permission-audit") {
674+
return true;
675+
}
676+
static const char* kFlags[] = {
677+
"--allow-fs-read",
678+
"--allow-fs-write",
679+
"--allow-addons",
680+
"--allow-inspector",
681+
"--allow-child-process",
682+
"--allow-net",
683+
"--allow-wasi",
684+
"--allow-ffi",
685+
"--allow-openssl-store",
686+
"--allow-worker",
687+
};
688+
for (const char* flag : kFlags) {
689+
const size_t n = std::char_traits<char>::length(flag);
690+
if (a == flag) {
691+
return true;
692+
}
693+
if (a.size() > n && a.compare(0, n, flag) == 0 && a[n] == '=') {
694+
return true;
695+
}
696+
}
697+
return false;
698+
}
699+
700+
static void RebuildExecArgvOutFromPermissionOptions(
701+
PerIsolateOptions* worker_opts, std::vector<std::string>* exec_argv_out) {
702+
if (worker_opts == nullptr || exec_argv_out == nullptr) {
703+
return;
704+
}
705+
EnvironmentOptions* w = worker_opts->get_per_env_options();
706+
if (w == nullptr || !w->permission) {
707+
return;
708+
}
709+
710+
std::vector<std::string> out;
711+
out.emplace_back("");
712+
for (size_t i = 1; i < exec_argv_out->size(); i++) {
713+
if (!IsPermissionCliToken((*exec_argv_out)[i])) {
714+
out.push_back((*exec_argv_out)[i]);
715+
}
716+
}
717+
out.push_back("--permission");
718+
if (w->permission_audit) {
719+
out.push_back("--permission-audit");
720+
}
721+
if (w->allow_addons) {
722+
out.push_back("--allow-addons");
723+
}
724+
if (w->allow_inspector) {
725+
out.push_back("--allow-inspector");
726+
}
727+
if (w->allow_child_process) {
728+
out.push_back("--allow-child-process");
729+
}
730+
if (w->allow_net) {
731+
out.push_back("--allow-net");
732+
}
733+
if (w->allow_wasi) {
734+
out.push_back("--allow-wasi");
735+
}
736+
if (w->allow_ffi) {
737+
out.push_back("--allow-ffi");
738+
}
739+
if (w->allow_openssl_store) {
740+
out.push_back("--allow-openssl-store");
741+
}
742+
if (w->allow_worker_threads) {
743+
out.push_back("--allow-worker");
744+
}
745+
for (const std::string& path : w->allow_fs_read) {
746+
out.push_back("--allow-fs-read=" + path);
747+
}
748+
for (const std::string& path : w->allow_fs_write) {
749+
out.push_back("--allow-fs-write=" + path);
750+
}
751+
*exec_argv_out = std::move(out);
752+
}
753+
754+
507755
void Worker::New(const FunctionCallbackInfo<Value>& args) {
508756
Environment* env = Environment::GetCurrent(args);
509757
THROW_IF_INSUFFICIENT_PERMISSIONS(
@@ -683,6 +931,14 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
683931
per_isolate_opts = env->isolate_data()->options()->Clone();
684932
}
685933

934+
// Explicit execArgv (including []): clamp grants and rebuild argv so
935+
// CreateEnvironment enables Permission with the parent ceiling.
936+
if (env->permission()->enabled() && per_isolate_opts && args[2]->IsArray()) {
937+
ClampWorkerPermissionToParent(env, per_isolate_opts.get());
938+
RebuildExecArgvOutFromPermissionOptions(per_isolate_opts.get(),
939+
&exec_argv_out);
940+
}
941+
686942
// Internal workers should not wait for inspector frontend to connect or
687943
// break on the first line of internal scripts. Module loader threads are
688944
// essential to load user codes and must not be blocked by the inspector

0 commit comments

Comments
 (0)