GeezerVison is showing github.com — src,permission: add --permission-audit · windwiny/node@a8eb690
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/cli.md +11Lines changed: 11 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -2174,6 +2174,16 @@ following permissions are restricted: | 2174 | 2174 | * WASI - manageable through [`--allow-wasi`][] flag | 2175 | 2175 | * Addons - manageable through [`--allow-addons`][] flag | 2176 | 2176 |
| | 2177 | +### `--permission-audit` | | 2178 | + | | 2179 | +<!-- YAML | | 2180 | +added: REPLACEME | | 2181 | +--> | | 2182 | + | | 2183 | +Enable audit only for the permission model. When enabled, permission checks | | 2184 | +are performed but access is not denied. Instead, a warning is emitted for | | 2185 | +each permission violation via diagnostics channel. | | 2186 | + | 2177 | 2187 | ### `--preserve-symlinks` | 2178 | 2188 |
| 2179 | 2189 | <!-- YAML | @@ -3660,6 +3670,7 @@ one is included in the list below. | 3660 | 3670 | * `--openssl-legacy-provider` | 3661 | 3671 | * `--openssl-shared-config` | 3662 | 3672 | * `--pending-deprecation` | | 3673 | +* `--permission-audit` | 3663 | 3674 | * `--permission` | 3664 | 3675 | * `--preserve-symlinks-main` | 3665 | 3676 | * `--preserve-symlinks` | |
Collapse file Copy file name to clipboardExpand all lines: doc/node.1 +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1115,6 +1115,11 @@ WASI - manageable through \fB--allow-wasi\fR flag | 1115 | 1115 | Addons - manageable through \fB--allow-addons\fR flag | 1116 | 1116 | .El | 1117 | 1117 | . | | 1118 | +.It Fl -permission-audit | | 1119 | +Enable audit only for the permission model. When enabled, permission checks | | 1120 | +are performed but access is not denied. Instead, a warning is emitted for | | 1121 | +each permission violation via diagnostics channel. | | 1122 | +. | 1118 | 1123 | .It Fl -preserve-symlinks | 1119 | 1124 | Instructs the module loader to preserve symbolic links when resolving and | 1120 | 1125 | caching modules. | @@ -1978,6 +1983,8 @@ one is included in the list below. | 1978 | 1983 | .It | 1979 | 1984 | \fB--permission\fR | 1980 | 1985 | .It | | 1986 | +\fB--permission-audit\fR | | 1987 | +.It | 1981 | 1988 | \fB--preserve-symlinks-main\fR | 1982 | 1989 | .It | 1983 | 1990 | \fB--preserve-symlinks\fR | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/process/pre_execution.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -633,7 +633,7 @@ function setupDiagnosticsChannel() { | 633 | 633 | } | 634 | 634 |
| 635 | 635 | function initializePermission() { | 636 | | - const permission = getOptionValue('--permission'); | | 636 | + const permission = getOptionValue('--permission') || getOptionValue('--permission-audit'); | 637 | 637 | if (permission) { | 638 | 638 | process.binding = function binding(_module) { | 639 | 639 | throw new ERR_ACCESS_DENIED('process.binding'); | |
Collapse file Copy file name to clipboardExpand all lines: src/env.cc +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -918,8 +918,11 @@ Environment::Environment(IsolateData* isolate_data, | 918 | 918 | tracing::CastTracedValue(traced_value)); | 919 | 919 | } | 920 | 920 |
| 921 | | - if (options_->permission) { | | 921 | + if (options_->permission || options_->permission_audit) { | 922 | 922 | permission()->EnablePermissions(); | | 923 | + if (options_->permission_audit) { | | 924 | + permission()->EnableWarningOnly(); | | 925 | + } | 923 | 926 | // The process shouldn't be able to neither | 924 | 927 | // spawn/worker nor use addons or enable inspector | 925 | 928 | // unless explicitly allowed by the user | |
Collapse file Copy file name to clipboardExpand all lines: src/node_options.cc +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -635,6 +635,11 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() { | 635 | 635 | kAllowedInEnvvar, | 636 | 636 | false, | 637 | 637 | OptionNamespaces::kPermissionNamespace); | | 638 | + AddOption("--permission-audit", | | 639 | + "enable audit only for the permission system", | | 640 | + &EnvironmentOptions::permission_audit, | | 641 | + kAllowedInEnvvar, | | 642 | + false); | 638 | 643 | AddOption("--allow-fs-read", | 639 | 644 | "allow permissions to read the filesystem", | 640 | 645 | &EnvironmentOptions::allow_fs_read, | |
Collapse file Copy file name to clipboardExpand all lines: src/node_options.h +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -138,6 +138,7 @@ class EnvironmentOptions : public Options { | 138 | 138 | std::string input_type; // Value of --input-type | 139 | 139 | bool entry_is_url = false; | 140 | 140 | bool permission = false; | | 141 | + bool permission_audit = false; | 141 | 142 | std::vector<std::string> allow_fs_read; | 142 | 143 | std::vector<std::string> allow_fs_write; | 143 | 144 | bool allow_addons = false; | |
Collapse file Copy file name to clipboardExpand all lines: src/permission/permission.cc +93-1Lines changed: 93 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | #include "env-inl.h" | 4 | 4 | #include "memory_tracker-inl.h" | 5 | 5 | #include "node.h" | | 6 | +#include "node_diagnostics_channel.h" | 6 | 7 | #include "node_errors.h" | 7 | 8 | #include "node_external_reference.h" | 8 | 9 | #include "node_file.h" | @@ -27,6 +28,29 @@ namespace permission { | 27 | 28 |
| 28 | 29 | namespace { | 29 | 30 |
| | 31 | +constexpr std::string_view GetDiagnosticsChannelName(PermissionScope scope) { | | 32 | + switch (scope) { | | 33 | + case PermissionScope::kFileSystem: | | 34 | + case PermissionScope::kFileSystemRead: | | 35 | + case PermissionScope::kFileSystemWrite: | | 36 | + return "node:permission-model:fs"; | | 37 | + case PermissionScope::kChildProcess: | | 38 | + return "node:permission-model:child"; | | 39 | + case PermissionScope::kWorkerThreads: | | 40 | + return "node:permission-model:worker"; | | 41 | + case PermissionScope::kNet: | | 42 | + return "node:permission-model:net"; | | 43 | + case PermissionScope::kInspector: | | 44 | + return "node:permission-model:inspector"; | | 45 | + case PermissionScope::kWASI: | | 46 | + return "node:permission-model:wasi"; | | 47 | + case PermissionScope::kAddon: | | 48 | + return "node:permission-model:addon"; | | 49 | + default: | | 50 | + return {}; | | 51 | + } | | 52 | +} | | 53 | + | 30 | 54 | // permission.has('fs.in', '/tmp/') | 31 | 55 | // permission.has('fs.in') | 32 | 56 | static void Has(const FunctionCallbackInfo<Value>& args) { | @@ -70,7 +94,7 @@ PermissionScope Permission::StringToPermission(const std::string& perm) { | 70 | 94 | } | 71 | 95 | #undef V | 72 | 96 |
| 73 | | -Permission::Permission() : enabled_(false) { | | 97 | +Permission::Permission() : enabled_(false), warning_only_(false) { | 74 | 98 | std::shared_ptr<PermissionBase> fs = std::make_shared<FSPermission>(); | 75 | 99 | std::shared_ptr<PermissionBase> child_p = | 76 | 100 | std::make_shared<ChildProcessPermission>(); | @@ -175,6 +199,74 @@ void Permission::EnablePermissions() { | 175 | 199 | } | 176 | 200 | } | 177 | 201 |
| | 202 | +void Permission::EnableWarningOnly() { | | 203 | + if (!warning_only_) { | | 204 | + warning_only_ = true; | | 205 | + } | | 206 | +} | | 207 | + | | 208 | +bool Permission::is_scope_granted(Environment* env, | | 209 | + const PermissionScope permission, | | 210 | + const std::string_view& res) const { | | 211 | + auto perm_node = nodes_.find(permission); | | 212 | + bool result = false; | | 213 | + if (perm_node != nodes_.end()) { | | 214 | + result = perm_node->second->is_granted(env, permission, res); | | 215 | + } | | 216 | + | | 217 | + if (!result && !publishing_) { | | 218 | + auto channel_name = GetDiagnosticsChannelName(permission); | | 219 | + if (!channel_name.empty()) { | | 220 | + auto ch = GetOrCreateChannel(env, permission); | | 221 | + if (ch && ch->HasSubscribers()) { | | 222 | + publishing_ = true; | | 223 | + v8::Isolate* isolate = env->isolate(); | | 224 | + v8::HandleScope handle_scope(isolate); | | 225 | + v8::Local<v8::Context> context = env->context(); | | 226 | + v8::Local<v8::Object> msg = | | 227 | + v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0); | | 228 | + const char* perm_str = PermissionToString(permission); | | 229 | + msg->Set(context, | | 230 | + FIXED_ONE_BYTE_STRING(isolate, "permission"), | | 231 | + v8::String::NewFromUtf8(isolate, perm_str).ToLocalChecked()) | | 232 | + .Check(); | | 233 | + msg->Set(context, | | 234 | + FIXED_ONE_BYTE_STRING(isolate, "resource"), | | 235 | + v8::String::NewFromUtf8(isolate, | | 236 | + res.data(), | | 237 | + v8::NewStringType::kNormal, | | 238 | + static_cast<int>(res.size())) | | 239 | + .ToLocalChecked()) | | 240 | + .Check(); | | 241 | + ch->Publish(env, msg); | | 242 | + publishing_ = false; | | 243 | + } | | 244 | + } | | 245 | + } | | 246 | + | | 247 | + return result; | | 248 | +} | | 249 | + | | 250 | +BaseObjectPtr<diagnostics_channel::Channel> Permission::GetOrCreateChannel( | | 251 | + Environment* env, PermissionScope scope) const { | | 252 | + auto it = channels_.find(scope); | | 253 | + if (it != channels_.end()) { | | 254 | + // Promote weak ref to strong for the duration of this call. | | 255 | + BaseObjectPtr<diagnostics_channel::Channel> ptr(it->second.get()); | | 256 | + if (ptr) return ptr; | | 257 | + channels_.erase(it); | | 258 | + } | | 259 | + auto channel_name = GetDiagnosticsChannelName(scope); | | 260 | + diagnostics_channel::Channel* ch = | | 261 | + diagnostics_channel::Channel::Get(env, channel_name.data()); | | 262 | + if (ch != nullptr) { | | 263 | + channels_.emplace(scope, | | 264 | + BaseObjectWeakPtr<diagnostics_channel::Channel>(ch)); | | 265 | + return BaseObjectPtr<diagnostics_channel::Channel>(ch); | | 266 | + } | | 267 | + return {}; | | 268 | +} | | 269 | + | 178 | 270 | void Permission::Apply(Environment* env, | 179 | 271 | const std::vector<std::string>& allow, | 180 | 272 | PermissionScope scope) { | |
Collapse file Copy file name to clipboardExpand all lines: src/permission/permission.h +17-9Lines changed: 17 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | 5 | 5 |
| 6 | 6 | #include "debug_utils.h" | | 7 | +#include "node_diagnostics_channel.h" | 7 | 8 | #include "node_options.h" | 8 | 9 | #include "permission/addon_permission.h" | 9 | 10 | #include "permission/child_process_permission.h" | @@ -37,7 +38,7 @@ namespace permission { | 37 | 38 | [[unlikely]] { \ | 38 | 39 | node::permission::Permission::ThrowAccessDenied( \ | 39 | 40 | env__, perm__, resource__); \ | 40 | | - return __VA_ARGS__; \ | | 41 | + if (!env__->permission()->warning_only()) return __VA_ARGS__; \ | 41 | 42 | } \ | 42 | 43 | } while (0) | 43 | 44 |
| @@ -51,7 +52,7 @@ namespace permission { | 51 | 52 | [[unlikely]] { \ | 52 | 53 | node::permission::Permission::AsyncThrowAccessDenied( \ | 53 | 54 | env__, (wrap), perm__, resource__); \ | 54 | | - return __VA_ARGS__; \ | | 55 | + if (!env__->permission()->warning_only()) return __VA_ARGS__; \ | 55 | 56 | } \ | 56 | 57 | } while (0) | 57 | 58 |
| @@ -100,6 +101,8 @@ class Permission { | 100 | 101 |
| 101 | 102 | FORCE_INLINE bool enabled() const { return enabled_; } | 102 | 103 |
| | 104 | + FORCE_INLINE bool warning_only() const { return warning_only_; } | | 105 | + | 103 | 106 | static PermissionScope StringToPermission(const std::string& perm); | 104 | 107 | static const char* PermissionToString(PermissionScope perm); | 105 | 108 | static void ThrowAccessDenied(Environment* env, | @@ -115,20 +118,25 @@ class Permission { | 115 | 118 | const std::vector<std::string>& allow, | 116 | 119 | PermissionScope scope); | 117 | 120 | void EnablePermissions(); | | 121 | + void EnableWarningOnly(); | 118 | 122 |
| 119 | 123 | private: | 120 | 124 | COLD_NOINLINE bool is_scope_granted(Environment* env, | 121 | 125 | const PermissionScope permission, | 122 | | - const std::string_view& res = "") const { | 123 | | - auto perm_node = nodes_.find(permission); | 124 | | - if (perm_node != nodes_.end()) { | 125 | | - return perm_node->second->is_granted(env, permission, res); | 126 | | - } | 127 | | - return false; | 128 | | - } | | 126 | + const std::string_view& res = "") const; | | 127 | + | | 128 | + BaseObjectPtr<diagnostics_channel::Channel> GetOrCreateChannel( | | 129 | + Environment* env, PermissionScope scope) const; | 129 | 130 |
| 130 | 131 | std::unordered_map<PermissionScope, std::shared_ptr<PermissionBase>> nodes_; | 131 | 132 | bool enabled_; | | 133 | + bool warning_only_; | | 134 | + mutable bool publishing_ = false; | | 135 | + // Weak refs: BindingData (via BaseObjectPtr) is the sole owner of Channels. | | 136 | + // Using weak refs here avoids keeping Channels alive past Realm teardown. | | 137 | + mutable std::unordered_map<PermissionScope, | | 138 | + BaseObjectWeakPtr<diagnostics_channel::Channel>> | | 139 | + channels_; | 132 | 140 | }; | 133 | 141 |
| 134 | 142 | v8::MaybeLocal<v8::Value> CreateAccessDeniedError(Environment* env, | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-bootstrap-modules.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -105,6 +105,7 @@ expected.beforePreExec = new Set([ | 105 | 105 | 'Internal Binding module_wrap', | 106 | 106 | 'NativeModule internal/modules/cjs/loader', | 107 | 107 | 'NativeModule diagnostics_channel', | | 108 | + 'Internal Binding diagnostics_channel', | 108 | 109 | 'Internal Binding wasm_web_api', | 109 | 110 | 'NativeModule internal/events/abort_listener', | 110 | 111 | 'NativeModule internal/modules/typescript', | |
Collapse file Copy file name to clipboard +29Lines changed: 29 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// Flags: --permission --allow-fs-read=* | | 2 | +'use strict'; | | 3 | + | | 4 | +const common = require('../common'); | | 5 | +const { isMainThread } = require('worker_threads'); | | 6 | + | | 7 | +if (!isMainThread) { | | 8 | + common.skip('This test only works on a main thread'); | | 9 | +} | | 10 | + | | 11 | +const assert = require('node:assert'); | | 12 | +const dc = require('node:diagnostics_channel'); | | 13 | +const fs = require('node:fs'); | | 14 | + | | 15 | +const messages = []; | | 16 | +dc.subscribe('node:permission-model:fs', (msg) => { | | 17 | + messages.push(msg); | | 18 | +}); | | 19 | + | | 20 | +// Granted permission should not publish | | 21 | +fs.readFileSync(__filename); | | 22 | +assert.strictEqual(messages.length, 0); | | 23 | + | | 24 | +// Denied permission should publish | | 25 | +const hasWrite = process.permission.has('fs.write', '/tmp/test'); | | 26 | +assert.strictEqual(hasWrite, false); | | 27 | + | | 28 | +assert.ok(messages.length > 0, 'Expected at least one denied message'); | | 29 | +assert.strictEqual(messages[0].permission, 'FileSystemWrite'); |
|