Skip to content

Commit 5a0dce6

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 when execArgv is an explicit array: clamp EnvironmentOptions and rebuild exec_argv_out for CreateEnvironment. Refs: #65359 Signed-off-by: yunshingng <yunshingng25@gmail.com>
1 parent 76bb3f7 commit 5a0dce6

4 files changed

Lines changed: 567 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: 260 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,257 @@ 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+
// Rebuild argv from clamped options. Do not assume argv[0] layout from Parse.
701+
static void RebuildExecArgvOutFromPermissionOptions(
702+
PerIsolateOptions* worker_opts, std::vector<std::string>* exec_argv_out) {
703+
if (worker_opts == nullptr || exec_argv_out == nullptr) {
704+
return;
705+
}
706+
EnvironmentOptions* w = worker_opts->get_per_env_options();
707+
if (w == nullptr || !w->permission) {
708+
return;
709+
}
710+
711+
std::vector<std::string> out;
712+
out.emplace_back(""); // program-name placeholder for parsers that expect it
713+
for (const std::string& tok : *exec_argv_out) {
714+
if (tok.empty()) {
715+
continue;
716+
}
717+
if (!IsPermissionCliToken(tok)) {
718+
out.push_back(tok);
719+
}
720+
}
721+
out.push_back("--permission");
722+
if (w->permission_audit) {
723+
out.push_back("--permission-audit");
724+
}
725+
if (w->allow_addons) {
726+
out.push_back("--allow-addons");
727+
}
728+
if (w->allow_inspector) {
729+
out.push_back("--allow-inspector");
730+
}
731+
if (w->allow_child_process) {
732+
out.push_back("--allow-child-process");
733+
}
734+
if (w->allow_net) {
735+
out.push_back("--allow-net");
736+
}
737+
if (w->allow_wasi) {
738+
out.push_back("--allow-wasi");
739+
}
740+
if (w->allow_ffi) {
741+
out.push_back("--allow-ffi");
742+
}
743+
if (w->allow_openssl_store) {
744+
out.push_back("--allow-openssl-store");
745+
}
746+
if (w->allow_worker_threads) {
747+
out.push_back("--allow-worker");
748+
}
749+
for (const std::string& path : w->allow_fs_read) {
750+
out.push_back("--allow-fs-read=" + path);
751+
}
752+
for (const std::string& path : w->allow_fs_write) {
753+
out.push_back("--allow-fs-write=" + path);
754+
}
755+
*exec_argv_out = std::move(out);
756+
}
757+
758+
507759
void Worker::New(const FunctionCallbackInfo<Value>& args) {
508760
Environment* env = Environment::GetCurrent(args);
509761
THROW_IF_INSUFFICIENT_PERMISSIONS(
@@ -683,6 +935,14 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
683935
per_isolate_opts = env->isolate_data()->options()->Clone();
684936
}
685937

938+
// Only explicit execArgv (including []): clamp options + rebuild argv.
939+
// Default Worker path (clone parent) is left unchanged.
940+
if (env->permission()->enabled() && per_isolate_opts && args[2]->IsArray()) {
941+
ClampWorkerPermissionToParent(env, per_isolate_opts.get());
942+
RebuildExecArgvOutFromPermissionOptions(per_isolate_opts.get(),
943+
&exec_argv_out);
944+
}
945+
686946
// Internal workers should not wait for inspector frontend to connect or
687947
// break on the first line of internal scripts. Module loader threads are
688948
// essential to load user codes and must not be blocked by the inspector

0 commit comments

Comments
 (0)