Skip to content

Commit d453607

Browse files
committed
permission: clamp Worker grants for explicit execArgv (SEMVER-MAJOR)
When the parent runs with the Permission Model, an explicit Worker execArgv (including []) cannot drop or exceed the parent's permission-related grants. - No worker permission flags configured → apply parent grant ceiling - Worker permission flags set → intersect (restrict OK, escalate no) - Rebuild permission argv from clamped options; drop space-form path tokens - Default Worker (omit execArgv) unchanged Signed-off-by: yunshingng <yunshingng25@gmail.com>
1 parent 9f1e44c commit d453607

4 files changed

Lines changed: 928 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: 259 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,254 @@ Worker::~Worker() {
504505
Debug(this, "Worker %llu destroyed", thread_id_.id);
505506
}
506507

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

935+
//
936+
937+
// Explicit execArgv only (including []). Default Worker path unchanged.
938+
if (env->permission()->enabled() && per_isolate_opts &&
939+
args[2]->IsArray()) {
940+
ClampWorkerPermissionToParent(env, per_isolate_opts.get());
941+
RebuildExecArgvOutFromPermissionOptions(per_isolate_opts.get(),
942+
&exec_argv_out);
943+
}
944+
686945
// Internal workers should not wait for inspector frontend to connect or
687946
// break on the first line of internal scripts. Module loader threads are
688947
// essential to load user codes and must not be blocked by the inspector

0 commit comments

Comments
 (0)