Commit d4cafce

Browse files
RafaelGSSaduh95
authored andcommitted
lib,permission: add permission.drop
Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com> PR-URL: #62672 Backport-PR-URL: #65354 Refs: #62223 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent cd1eb3e commit d4cafce

30 files changed

Lines changed: 848 additions & 2 deletions

β€Ždoc/api/permissions.mdβ€Ž

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ flag. For WASI, use the [`--allow-wasi`][] flag.
7474

7575
When enabling the Permission Model through the [`--permission`][]
7676
flag a new property `permission` is added to the `process` object.
77-
This property contains one function:
77+
This property contains the following functions:
7878

7979
##### `permission.has(scope[, reference])`
8080

@@ -88,6 +88,40 @@ process.permission.has('fs.read'); // true
8888
process.permission.has('fs.read', '/home/rafaelgss/protected-folder'); // false
8989
```
9090

91+
##### `permission.drop(scope[, reference])`
92+
93+
API call to drop permissions at runtime. This operation is **irreversible**.
94+
95+
When called without a reference, the entire scope is dropped. When called
96+
with a reference, only the permission for that specific resource is revoked.
97+
Dropping a permission only affects future access checks. It does not close or
98+
revoke access to resources that are already open, such as file descriptors,
99+
child processes, or worker threads. Applications are responsible for closing
100+
or terminating those resources when they are no longer needed.
101+
102+
You can only drop the exact resource that was explicitly granted. The
103+
reference passed to `drop()` must match the original grant. If a permission
104+
was granted using a wildcard (`*`), only the entire scope can be dropped
105+
(by calling `drop()` without a reference). If a directory was granted
106+
(e.g. `--allow-fs-read=/my/folder`), you cannot drop individual files
107+
inside it - you must drop the same directory that was originally granted.
108+
109+
```js
110+
constfs=require('node:fs');
111+
112+
// Read config at startup while we still have permission
113+
constconfig=fs.readFileSync('/etc/myapp/config.json', 'utf8');
114+
115+
// Drop read access to /etc/myapp after initialization
116+
process.permission.drop('fs.read', '/etc/myapp');
117+
118+
// This will now throw ERR_ACCESS_DENIED
119+
process.permission.has('fs.read', '/etc/myapp/config.json'); // false
120+
121+
// Drop child process permission entirely
122+
process.permission.drop('child');
123+
```
124+
91125
#### File System Permissions
92126

93127
The Permission Model, by default, restricts access to the file system through the `node:fs` module.

β€Ždoc/api/process.mdβ€Ž

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3189,6 +3189,64 @@ process.permission.has('fs.read', './README.md');
31893189
process.permission.has('fs.read');
31903190
```
31913191
3192+
### `process.permission.drop(scope[, reference])`
3193+
3194+
<!-- YAML
3195+
added: REPLACEME
3196+
-->
3197+
3198+
> Stability: 1.1 - Active Development
3199+
3200+
* `scope` {string}
3201+
* `reference` {string}
3202+
3203+
Drops the specified permission from the current process. This operation is
3204+
**irreversible** β€” once a permission is dropped, it cannot be restored through
3205+
any Node.js API.
3206+
3207+
If no reference is provided, the entire scope is dropped. For example,
3208+
`process.permission.drop('fs.read')` will revoke ALL file system read
3209+
permissions.
3210+
3211+
When a reference is provided, only the permission for that specific resource
3212+
is dropped. For example, `process.permission.drop('fs.read', '/etc/myapp')`
3213+
will revoke read access to that directory while keeping other read
3214+
permissions intact.
3215+
3216+
**Important:** You can only drop the exact resource that was explicitly
3217+
granted. The reference passed to `drop()` must match the original grant:
3218+
3219+
* If a permission was granted using a wildcard (`*`), such as
3220+
`--allow-fs-read=*`, individual paths cannot be dropped - only the entire
3221+
scope can be dropped (by calling `drop()` without a reference).
3222+
* If a directory was granted (e.g. `--allow-fs-read=/my/folder`), you cannot
3223+
drop access to individual files inside it. You must drop the same directory
3224+
that was granted. Any remaining grants continue to apply.
3225+
3226+
The available scopes are the same as [`process.permission.has()`][]:
3227+
3228+
* `fs` - All File System (drops both read and write)
3229+
* `fs.read` - File System read operations
3230+
* `fs.write` - File System write operations
3231+
* `child` - Child process spawning operations
3232+
* `worker` - Worker thread spawning operation
3233+
* `inspector` - Inspector operations
3234+
* `wasi` - WASI operations
3235+
* `addon` - Native addon operations
3236+
3237+
```js
3238+
constfs=require('node:fs');
3239+
3240+
// Read configuration during startup
3241+
constconfig=fs.readFileSync('/etc/myapp/config.json', 'utf8');
3242+
3243+
// Drop read access to the config directory after initialization
3244+
process.permission.drop('fs.read', '/etc/myapp');
3245+
3246+
// This will now throw ERR_ACCESS_DENIED
3247+
fs.readFileSync('/etc/myapp/config.json');
3248+
```
3249+
31923250
## `process.pid`
31933251
31943252
<!-- YAML
@@ -4598,6 +4656,7 @@ cases:
45984656
[`process.hrtime()`]: #processhrtimetime
45994657
[`process.hrtime.bigint()`]: #processhrtimebigint
46004658
[`process.kill()`]: #processkillpid-signal
4659+
[`process.permission.has()`]: #processpermissionhasscope-reference
46014660
[`process.setUncaughtExceptionCaptureCallback()`]: #processsetuncaughtexceptioncapturecallbackfn
46024661
[`promise.catch()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
46034662
[`queueMicrotask()`]: globals.md#queuemicrotaskcallback

β€Žlib/internal/process/permission.jsβ€Ž

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ module.exports = ObjectFreeze({
3333

3434
returnpermission.has(scope,reference);
3535
},
36+
drop(scope,reference){
37+
validateString(scope,'scope');
38+
if(reference!=null){
39+
if(isBuffer(reference)){
40+
validateBuffer(reference,'reference');
41+
}else{
42+
validateString(reference,'reference');
43+
}
44+
}
45+
46+
permission.drop(scope,reference);
47+
},
3648
availableFlags(){
3749
return[
3850
'--allow-fs-read',

β€Žlib/internal/process/pre_execution.jsβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ function initializePermission() {
641641
};
642642
// Guarantee path module isn't monkey-patched to bypass permission model
643643
ObjectFreeze(require('path'));
644-
const{ has }=require('internal/process/permission');
644+
const{ has, drop}=require('internal/process/permission');
645645
constwarnFlags=[
646646
'--allow-addons',
647647
'--allow-child-process',
@@ -679,6 +679,7 @@ function initializePermission() {
679679
configurable: false,
680680
value: {
681681
has,
682+
drop,
682683
},
683684
});
684685
}else{

β€Žsrc/permission/addon_permission.ccβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ void AddonPermission::Apply(Environment* env,
1414
deny_all_ = true;
1515
}
1616

17+
voidAddonPermission::Drop(Environment* env,
18+
PermissionScope scope,
19+
const std::string_view& param) {
20+
deny_all_ = true;
21+
}
22+
1723
boolAddonPermission::is_granted(Environment* env,
1824
PermissionScope perm,
1925
const std::string_view& param) const {

β€Žsrc/permission/addon_permission.hβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class AddonPermission final : public PermissionBase {
1515
voidApply(Environment* env,
1616
const std::vector<std::string>& allow,
1717
PermissionScope scope) override;
18+
voidDrop(Environment* env,
19+
PermissionScope scope,
20+
const std::string_view& param = "") override;
1821
boolis_granted(Environment* env,
1922
PermissionScope perm,
2023
const std::string_view& param = "") constoverride;

β€Žsrc/permission/child_process_permission.ccβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ void ChildProcessPermission::Apply(Environment* env,
1515
deny_all_ = true;
1616
}
1717

18+
voidChildProcessPermission::Drop(Environment* env,
19+
PermissionScope scope,
20+
const std::string_view& param) {
21+
deny_all_ = true;
22+
}
23+
1824
boolChildProcessPermission::is_granted(Environment* env,
1925
PermissionScope perm,
2026
const std::string_view& param) const {

β€Žsrc/permission/child_process_permission.hβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class ChildProcessPermission final : public PermissionBase {
1515
voidApply(Environment* env,
1616
const std::vector<std::string>& allow,
1717
PermissionScope scope) override;
18+
voidDrop(Environment* env,
19+
PermissionScope scope,
20+
const std::string_view& param = "") override;
1821
boolis_granted(Environment* env,
1922
PermissionScope perm,
2023
const std::string_view& param = "") constoverride;

β€Žsrc/permission/fs_permission.ccβ€Ž

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,96 @@ void FSPermission::Apply(Environment* env,
154154
}
155155
}
156156

157+
voidFSPermission::Drop(Environment* env,
158+
PermissionScope scope,
159+
const std::string_view& param) {
160+
if (param.empty()) {
161+
// Drop all access for this scope
162+
if (scope == PermissionScope::kFileSystemRead ||
163+
scope == PermissionScope::kFileSystem) {
164+
deny_all_in_ = true;
165+
allow_all_in_ = false;
166+
granted_in_fs_.Clear();
167+
granted_paths_in_.clear();
168+
}
169+
if (scope == PermissionScope::kFileSystemWrite ||
170+
scope == PermissionScope::kFileSystem) {
171+
deny_all_out_ = true;
172+
allow_all_out_ = false;
173+
granted_out_fs_.Clear();
174+
granted_paths_out_.clear();
175+
}
176+
return;
177+
}
178+
179+
// When allowed with *, you can only drop * (no specific paths)
180+
std::string resolved = PathResolve(env, {param});
181+
if (scope == PermissionScope::kFileSystemRead ||
182+
scope == PermissionScope::kFileSystem) {
183+
if (!allow_all_in_) {
184+
RevokeAccess(PermissionScope::kFileSystemRead, resolved);
185+
}
186+
}
187+
if (scope == PermissionScope::kFileSystemWrite ||
188+
scope == PermissionScope::kFileSystem) {
189+
if (!allow_all_out_) {
190+
RevokeAccess(PermissionScope::kFileSystemWrite, resolved);
191+
}
192+
}
193+
}
194+
195+
voidFSPermission::RevokeAccess(PermissionScope perm, const std::string& res) {
196+
const std::string path = WildcardIfDir(res);
197+
if (perm == PermissionScope::kFileSystemRead) {
198+
auto it =
199+
std::find(granted_paths_in_.begin(), granted_paths_in_.end(), path);
200+
if (it != granted_paths_in_.end()) {
201+
granted_paths_in_.erase(it);
202+
RebuildTree(PermissionScope::kFileSystemRead);
203+
}
204+
} elseif (perm == PermissionScope::kFileSystemWrite) {
205+
auto it =
206+
std::find(granted_paths_out_.begin(), granted_paths_out_.end(), path);
207+
if (it != granted_paths_out_.end()) {
208+
granted_paths_out_.erase(it);
209+
RebuildTree(PermissionScope::kFileSystemWrite);
210+
}
211+
}
212+
}
213+
214+
voidFSPermission::RebuildTree(PermissionScope scope) {
215+
if (scope == PermissionScope::kFileSystemRead) {
216+
granted_in_fs_.Clear();
217+
if (granted_paths_in_.empty()) {
218+
deny_all_in_ = true;
219+
} else {
220+
for (constauto& path : granted_paths_in_) {
221+
granted_in_fs_.Insert(path);
222+
}
223+
}
224+
} elseif (scope == PermissionScope::kFileSystemWrite) {
225+
granted_out_fs_.Clear();
226+
if (granted_paths_out_.empty()) {
227+
deny_all_out_ = true;
228+
} else {
229+
for (constauto& path : granted_paths_out_) {
230+
granted_out_fs_.Insert(path);
231+
}
232+
}
233+
}
234+
}
235+
157236
voidFSPermission::GrantAccess(PermissionScope perm, const std::string& res) {
158237
const std::string path = WildcardIfDir(res);
159238
if (perm == PermissionScope::kFileSystemRead &&
160239
!granted_in_fs_.Lookup(path)) {
161240
granted_in_fs_.Insert(path);
241+
granted_paths_in_.push_back(path);
162242
deny_all_in_ = false;
163243
} elseif (perm == PermissionScope::kFileSystemWrite &&
164244
!granted_out_fs_.Lookup(path)) {
165245
granted_out_fs_.Insert(path);
246+
granted_paths_out_.push_back(path);
166247
deny_all_out_ = false;
167248
}
168249
}
@@ -196,6 +277,16 @@ FSPermission::RadixTree::~RadixTree() {
196277
FreeRecursivelyNode(root_node_);
197278
}
198279

280+
voidFSPermission::RadixTree::Clear() {
281+
for (auto& c : root_node_->children) {
282+
FreeRecursivelyNode(c.second);
283+
}
284+
root_node_->children.clear();
285+
delete root_node_->wildcard_child;
286+
root_node_->wildcard_child = nullptr;
287+
root_node_->is_leaf = false;
288+
}
289+
199290
boolFSPermission::RadixTree::Lookup(const std::string_view& s,
200291
bool when_empty_return) const {
201292
FSPermission::RadixTree::Node* current_node = root_node_;

β€Žsrc/permission/fs_permission.hβ€Ž

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class FSPermission final : public PermissionBase {
1818
voidApply(Environment* env,
1919
const std::vector<std::string>& allow,
2020
PermissionScope scope) override;
21+
voidDrop(Environment* env,
22+
PermissionScope scope,
23+
const std::string_view& param = "") override;
2124
boolis_granted(Environment* env,
2225
PermissionScope perm,
2326
const std::string_view& param) constoverride;
@@ -142,6 +145,7 @@ class FSPermission final : public PermissionBase {
142145
RadixTree();
143146
~RadixTree();
144147
voidInsert(const std::string& s);
148+
voidClear();
145149
boolLookup(const std::string_view& s) const { returnLookup(s, false); }
146150
boolLookup(const std::string_view& s, bool when_empty_return) const;
147151

@@ -151,10 +155,15 @@ class FSPermission final : public PermissionBase {
151155

152156
private:
153157
voidGrantAccess(PermissionScope scope, const std::string& param);
158+
voidRevokeAccess(PermissionScope scope, const std::string& param);
159+
voidRebuildTree(PermissionScope scope);
154160
// fs granted on startup
155161
RadixTree granted_in_fs_;
156162
RadixTree granted_out_fs_;
157163

164+
std::vector<std::string> granted_paths_in_;
165+
std::vector<std::string> granted_paths_out_;
166+
158167
bool deny_all_in_ = true;
159168
bool deny_all_out_ = true;
160169

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Commit d4cafce

Browse files
RafaelGSSaduh95
authored andcommitted
lib,permission: add permission.drop
Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com> PR-URL: #62672 Backport-PR-URL: #65354 Refs: #62223 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent cd1eb3e commit d4cafce

30 files changed

Lines changed: 848 additions & 2 deletions

β€Ždoc/api/permissions.mdβ€Ž

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ flag. For WASI, use the [`--allow-wasi`][] flag.
7474

7575
When enabling the Permission Model through the [`--permission`][]
7676
flag a new property `permission` is added to the `process` object.
77-
This property contains one function:
77+
This property contains the following functions:
7878

7979
##### `permission.has(scope[, reference])`
8080

@@ -88,6 +88,40 @@ process.permission.has('fs.read'); // true
8888
process.permission.has('fs.read', '/home/rafaelgss/protected-folder'); // false
8989
```
9090

91+
##### `permission.drop(scope[, reference])`
92+
93+
API call to drop permissions at runtime. This operation is **irreversible**.
94+
95+
When called without a reference, the entire scope is dropped. When called
96+
with a reference, only the permission for that specific resource is revoked.
97+
Dropping a permission only affects future access checks. It does not close or
98+
revoke access to resources that are already open, such as file descriptors,
99+
child processes, or worker threads. Applications are responsible for closing
100+
or terminating those resources when they are no longer needed.
101+
102+
You can only drop the exact resource that was explicitly granted. The
103+
reference passed to `drop()` must match the original grant. If a permission
104+
was granted using a wildcard (`*`), only the entire scope can be dropped
105+
(by calling `drop()` without a reference). If a directory was granted
106+
(e.g. `--allow-fs-read=/my/folder`), you cannot drop individual files
107+
inside it - you must drop the same directory that was originally granted.
108+
109+
```js
110+
constfs=require('node:fs');
111+
112+
// Read config at startup while we still have permission
113+
constconfig=fs.readFileSync('/etc/myapp/config.json', 'utf8');
114+
115+
// Drop read access to /etc/myapp after initialization
116+
process.permission.drop('fs.read', '/etc/myapp');
117+
118+
// This will now throw ERR_ACCESS_DENIED
119+
process.permission.has('fs.read', '/etc/myapp/config.json'); // false
120+
121+
// Drop child process permission entirely
122+
process.permission.drop('child');
123+
```
124+
91125
#### File System Permissions
92126

93127
The Permission Model, by default, restricts access to the file system through the `node:fs` module.

β€Ždoc/api/process.mdβ€Ž

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3189,6 +3189,64 @@ process.permission.has('fs.read', './README.md');
31893189
process.permission.has('fs.read');
31903190
```
31913191
3192+
### `process.permission.drop(scope[, reference])`
3193+
3194+
<!-- YAML
3195+
added: REPLACEME
3196+
-->
3197+
3198+
> Stability: 1.1 - Active Development
3199+
3200+
* `scope` {string}
3201+
* `reference` {string}
3202+
3203+
Drops the specified permission from the current process. This operation is
3204+
**irreversible** β€” once a permission is dropped, it cannot be restored through
3205+
any Node.js API.
3206+
3207+
If no reference is provided, the entire scope is dropped. For example,
3208+
`process.permission.drop('fs.read')` will revoke ALL file system read
3209+
permissions.
3210+
3211+
When a reference is provided, only the permission for that specific resource
3212+
is dropped. For example, `process.permission.drop('fs.read', '/etc/myapp')`
3213+
will revoke read access to that directory while keeping other read
3214+
permissions intact.
3215+
3216+
**Important:** You can only drop the exact resource that was explicitly
3217+
granted. The reference passed to `drop()` must match the original grant:
3218+
3219+
* If a permission was granted using a wildcard (`*`), such as
3220+
`--allow-fs-read=*`, individual paths cannot be dropped - only the entire
3221+
scope can be dropped (by calling `drop()` without a reference).
3222+
* If a directory was granted (e.g. `--allow-fs-read=/my/folder`), you cannot
3223+
drop access to individual files inside it. You must drop the same directory
3224+
that was granted. Any remaining grants continue to apply.
3225+
3226+
The available scopes are the same as [`process.permission.has()`][]:
3227+
3228+
* `fs` - All File System (drops both read and write)
3229+
* `fs.read` - File System read operations
3230+
* `fs.write` - File System write operations
3231+
* `child` - Child process spawning operations
3232+
* `worker` - Worker thread spawning operation
3233+
* `inspector` - Inspector operations
3234+
* `wasi` - WASI operations
3235+
* `addon` - Native addon operations
3236+
3237+
```js
3238+
constfs=require('node:fs');
3239+
3240+
// Read configuration during startup
3241+
constconfig=fs.readFileSync('/etc/myapp/config.json', 'utf8');
3242+
3243+
// Drop read access to the config directory after initialization
3244+
process.permission.drop('fs.read', '/etc/myapp');
3245+
3246+
// This will now throw ERR_ACCESS_DENIED
3247+
fs.readFileSync('/etc/myapp/config.json');
3248+
```
3249+
31923250
## `process.pid`
31933251
31943252
<!-- YAML
@@ -4598,6 +4656,7 @@ cases:
45984656
[`process.hrtime()`]: #processhrtimetime
45994657
[`process.hrtime.bigint()`]: #processhrtimebigint
46004658
[`process.kill()`]: #processkillpid-signal
4659+
[`process.permission.has()`]: #processpermissionhasscope-reference
46014660
[`process.setUncaughtExceptionCaptureCallback()`]: #processsetuncaughtexceptioncapturecallbackfn
46024661
[`promise.catch()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
46034662
[`queueMicrotask()`]: globals.md#queuemicrotaskcallback

β€Žlib/internal/process/permission.jsβ€Ž

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ module.exports = ObjectFreeze({
3333

3434
returnpermission.has(scope,reference);
3535
},
36+
drop(scope,reference){
37+
validateString(scope,'scope');
38+
if(reference!=null){
39+
if(isBuffer(reference)){
40+
validateBuffer(reference,'reference');
41+
}else{
42+
validateString(reference,'reference');
43+
}
44+
}
45+
46+
permission.drop(scope,reference);
47+
},
3648
availableFlags(){
3749
return[
3850
'--allow-fs-read',

β€Žlib/internal/process/pre_execution.jsβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ function initializePermission() {
641641
};
642642
// Guarantee path module isn't monkey-patched to bypass permission model
643643
ObjectFreeze(require('path'));
644-
const{ has }=require('internal/process/permission');
644+
const{ has, drop}=require('internal/process/permission');
645645
constwarnFlags=[
646646
'--allow-addons',
647647
'--allow-child-process',
@@ -679,6 +679,7 @@ function initializePermission() {
679679
configurable: false,
680680
value: {
681681
has,
682+
drop,
682683
},
683684
});
684685
}else{

β€Žsrc/permission/addon_permission.ccβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ void AddonPermission::Apply(Environment* env,
1414
deny_all_ = true;
1515
}
1616

17+
voidAddonPermission::Drop(Environment* env,
18+
PermissionScope scope,
19+
const std::string_view& param) {
20+
deny_all_ = true;
21+
}
22+
1723
boolAddonPermission::is_granted(Environment* env,
1824
PermissionScope perm,
1925
const std::string_view& param) const {

β€Žsrc/permission/addon_permission.hβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class AddonPermission final : public PermissionBase {
1515
voidApply(Environment* env,
1616
const std::vector<std::string>& allow,
1717
PermissionScope scope) override;
18+
voidDrop(Environment* env,
19+
PermissionScope scope,
20+
const std::string_view& param = "") override;
1821
boolis_granted(Environment* env,
1922
PermissionScope perm,
2023
const std::string_view& param = "") constoverride;

β€Žsrc/permission/child_process_permission.ccβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ void ChildProcessPermission::Apply(Environment* env,
1515
deny_all_ = true;
1616
}
1717

18+
voidChildProcessPermission::Drop(Environment* env,
19+
PermissionScope scope,
20+
const std::string_view& param) {
21+
deny_all_ = true;
22+
}
23+
1824
boolChildProcessPermission::is_granted(Environment* env,
1925
PermissionScope perm,
2026
const std::string_view& param) const {

β€Žsrc/permission/child_process_permission.hβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class ChildProcessPermission final : public PermissionBase {
1515
voidApply(Environment* env,
1616
const std::vector<std::string>& allow,
1717
PermissionScope scope) override;
18+
voidDrop(Environment* env,
19+
PermissionScope scope,
20+
const std::string_view& param = "") override;
1821
boolis_granted(Environment* env,
1922
PermissionScope perm,
2023
const std::string_view& param = "") constoverride;

β€Žsrc/permission/fs_permission.ccβ€Ž

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,96 @@ void FSPermission::Apply(Environment* env,
154154
}
155155
}
156156

157+
voidFSPermission::Drop(Environment* env,
158+
PermissionScope scope,
159+
const std::string_view& param) {
160+
if (param.empty()) {
161+
// Drop all access for this scope
162+
if (scope == PermissionScope::kFileSystemRead ||
163+
scope == PermissionScope::kFileSystem) {
164+
deny_all_in_ = true;
165+
allow_all_in_ = false;
166+
granted_in_fs_.Clear();
167+
granted_paths_in_.clear();
168+
}
169+
if (scope == PermissionScope::kFileSystemWrite ||
170+
scope == PermissionScope::kFileSystem) {
171+
deny_all_out_ = true;
172+
allow_all_out_ = false;
173+
granted_out_fs_.Clear();
174+
granted_paths_out_.clear();
175+
}
176+
return;
177+
}
178+
179+
// When allowed with *, you can only drop * (no specific paths)
180+
std::string resolved = PathResolve(env, {param});
181+
if (scope == PermissionScope::kFileSystemRead ||
182+
scope == PermissionScope::kFileSystem) {
183+
if (!allow_all_in_) {
184+
RevokeAccess(PermissionScope::kFileSystemRead, resolved);
185+
}
186+
}
187+
if (scope == PermissionScope::kFileSystemWrite ||
188+
scope == PermissionScope::kFileSystem) {
189+
if (!allow_all_out_) {
190+
RevokeAccess(PermissionScope::kFileSystemWrite, resolved);
191+
}
192+
}
193+
}
194+
195+
voidFSPermission::RevokeAccess(PermissionScope perm, const std::string& res) {
196+
const std::string path = WildcardIfDir(res);
197+
if (perm == PermissionScope::kFileSystemRead) {
198+
auto it =
199+
std::find(granted_paths_in_.begin(), granted_paths_in_.end(), path);
200+
if (it != granted_paths_in_.end()) {
201+
granted_paths_in_.erase(it);
202+
RebuildTree(PermissionScope::kFileSystemRead);
203+
}
204+
} elseif (perm == PermissionScope::kFileSystemWrite) {
205+
auto it =
206+
std::find(granted_paths_out_.begin(), granted_paths_out_.end(), path);
207+
if (it != granted_paths_out_.end()) {
208+
granted_paths_out_.erase(it);
209+
RebuildTree(PermissionScope::kFileSystemWrite);
210+
}
211+
}
212+
}
213+
214+
voidFSPermission::RebuildTree(PermissionScope scope) {
215+
if (scope == PermissionScope::kFileSystemRead) {
216+
granted_in_fs_.Clear();
217+
if (granted_paths_in_.empty()) {
218+
deny_all_in_ = true;
219+
} else {
220+
for (constauto& path : granted_paths_in_) {
221+
granted_in_fs_.Insert(path);
222+
}
223+
}
224+
} elseif (scope == PermissionScope::kFileSystemWrite) {
225+
granted_out_fs_.Clear();
226+
if (granted_paths_out_.empty()) {
227+
deny_all_out_ = true;
228+
} else {
229+
for (constauto& path : granted_paths_out_) {
230+
granted_out_fs_.Insert(path);
231+
}
232+
}
233+
}
234+
}
235+
157236
voidFSPermission::GrantAccess(PermissionScope perm, const std::string& res) {
158237
const std::string path = WildcardIfDir(res);
159238
if (perm == PermissionScope::kFileSystemRead &&
160239
!granted_in_fs_.Lookup(path)) {
161240
granted_in_fs_.Insert(path);
241+
granted_paths_in_.push_back(path);
162242
deny_all_in_ = false;
163243
} elseif (perm == PermissionScope::kFileSystemWrite &&
164244
!granted_out_fs_.Lookup(path)) {
165245
granted_out_fs_.Insert(path);
246+
granted_paths_out_.push_back(path);
166247
deny_all_out_ = false;
167248
}
168249
}
@@ -196,6 +277,16 @@ FSPermission::RadixTree::~RadixTree() {
196277
FreeRecursivelyNode(root_node_);
197278
}
198279

280+
voidFSPermission::RadixTree::Clear() {
281+
for (auto& c : root_node_->children) {
282+
FreeRecursivelyNode(c.second);
283+
}
284+
root_node_->children.clear();
285+
delete root_node_->wildcard_child;
286+
root_node_->wildcard_child = nullptr;
287+
root_node_->is_leaf = false;
288+
}
289+
199290
boolFSPermission::RadixTree::Lookup(const std::string_view& s,
200291
bool when_empty_return) const {
201292
FSPermission::RadixTree::Node* current_node = root_node_;

β€Žsrc/permission/fs_permission.hβ€Ž

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class FSPermission final : public PermissionBase {
1818
voidApply(Environment* env,
1919
const std::vector<std::string>& allow,
2020
PermissionScope scope) override;
21+
voidDrop(Environment* env,
22+
PermissionScope scope,
23+
const std::string_view& param = "") override;
2124
boolis_granted(Environment* env,
2225
PermissionScope perm,
2326
const std::string_view& param) constoverride;
@@ -142,6 +145,7 @@ class FSPermission final : public PermissionBase {
142145
RadixTree();
143146
~RadixTree();
144147
voidInsert(const std::string& s);
148+
voidClear();
145149
boolLookup(const std::string_view& s) const { returnLookup(s, false); }
146150
boolLookup(const std::string_view& s, bool when_empty_return) const;
147151

@@ -151,10 +155,15 @@ class FSPermission final : public PermissionBase {
151155

152156
private:
153157
voidGrantAccess(PermissionScope scope, const std::string& param);
158+
voidRevokeAccess(PermissionScope scope, const std::string& param);
159+
voidRebuildTree(PermissionScope scope);
154160
// fs granted on startup
155161
RadixTree granted_in_fs_;
156162
RadixTree granted_out_fs_;
157163

164+
std::vector<std::string> granted_paths_in_;
165+
std::vector<std::string> granted_paths_out_;
166+
158167
bool deny_all_in_ = true;
159168
bool deny_all_out_ = true;
160169

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit d4cafce

Browse files
RafaelGSSaduh95
authored andcommitted
lib,permission: add permission.drop
Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com> PR-URL: #62672 Backport-PR-URL: #65354 Refs: #62223 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent cd1eb3e commit d4cafce

30 files changed

Lines changed: 848 additions & 2 deletions

β€Ždoc/api/permissions.mdβ€Ž

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ flag. For WASI, use the [`--allow-wasi`][] flag.
7474

7575
When enabling the Permission Model through the [`--permission`][]
7676
flag a new property `permission` is added to the `process` object.
77-
This property contains one function:
77+
This property contains the following functions:
7878

7979
##### `permission.has(scope[, reference])`
8080

@@ -88,6 +88,40 @@ process.permission.has('fs.read'); // true
8888
process.permission.has('fs.read', '/home/rafaelgss/protected-folder'); // false
8989
```
9090

91+
##### `permission.drop(scope[, reference])`
92+
93+
API call to drop permissions at runtime. This operation is **irreversible**.
94+
95+
When called without a reference, the entire scope is dropped. When called
96+
with a reference, only the permission for that specific resource is revoked.
97+
Dropping a permission only affects future access checks. It does not close or
98+
revoke access to resources that are already open, such as file descriptors,
99+
child processes, or worker threads. Applications are responsible for closing
100+
or terminating those resources when they are no longer needed.
101+
102+
You can only drop the exact resource that was explicitly granted. The
103+
reference passed to `drop()` must match the original grant. If a permission
104+
was granted using a wildcard (`*`), only the entire scope can be dropped
105+
(by calling `drop()` without a reference). If a directory was granted
106+
(e.g. `--allow-fs-read=/my/folder`), you cannot drop individual files
107+
inside it - you must drop the same directory that was originally granted.
108+
109+
```js
110+
constfs=require('node:fs');
111+
112+
// Read config at startup while we still have permission
113+
constconfig=fs.readFileSync('/etc/myapp/config.json', 'utf8');
114+
115+
// Drop read access to /etc/myapp after initialization
116+
process.permission.drop('fs.read', '/etc/myapp');
117+
118+
// This will now throw ERR_ACCESS_DENIED
119+
process.permission.has('fs.read', '/etc/myapp/config.json'); // false
120+
121+
// Drop child process permission entirely
122+
process.permission.drop('child');
123+
```
124+
91125
#### File System Permissions
92126

93127
The Permission Model, by default, restricts access to the file system through the `node:fs` module.

β€Ždoc/api/process.mdβ€Ž

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3189,6 +3189,64 @@ process.permission.has('fs.read', './README.md');
31893189
process.permission.has('fs.read');
31903190
```
31913191
3192+
### `process.permission.drop(scope[, reference])`
3193+
3194+
<!-- YAML
3195+
added: REPLACEME
3196+
-->
3197+
3198+
> Stability: 1.1 - Active Development
3199+
3200+
* `scope` {string}
3201+
* `reference` {string}
3202+
3203+
Drops the specified permission from the current process. This operation is
3204+
**irreversible** β€” once a permission is dropped, it cannot be restored through
3205+
any Node.js API.
3206+
3207+
If no reference is provided, the entire scope is dropped. For example,
3208+
`process.permission.drop('fs.read')` will revoke ALL file system read
3209+
permissions.
3210+
3211+
When a reference is provided, only the permission for that specific resource
3212+
is dropped. For example, `process.permission.drop('fs.read', '/etc/myapp')`
3213+
will revoke read access to that directory while keeping other read
3214+
permissions intact.
3215+
3216+
**Important:** You can only drop the exact resource that was explicitly
3217+
granted. The reference passed to `drop()` must match the original grant:
3218+
3219+
* If a permission was granted using a wildcard (`*`), such as
3220+
`--allow-fs-read=*`, individual paths cannot be dropped - only the entire
3221+
scope can be dropped (by calling `drop()` without a reference).
3222+
* If a directory was granted (e.g. `--allow-fs-read=/my/folder`), you cannot
3223+
drop access to individual files inside it. You must drop the same directory
3224+
that was granted. Any remaining grants continue to apply.
3225+
3226+
The available scopes are the same as [`process.permission.has()`][]:
3227+
3228+
* `fs` - All File System (drops both read and write)
3229+
* `fs.read` - File System read operations
3230+
* `fs.write` - File System write operations
3231+
* `child` - Child process spawning operations
3232+
* `worker` - Worker thread spawning operation
3233+
* `inspector` - Inspector operations
3234+
* `wasi` - WASI operations
3235+
* `addon` - Native addon operations
3236+
3237+
```js
3238+
constfs=require('node:fs');
3239+
3240+
// Read configuration during startup
3241+
constconfig=fs.readFileSync('/etc/myapp/config.json', 'utf8');
3242+
3243+
// Drop read access to the config directory after initialization
3244+
process.permission.drop('fs.read', '/etc/myapp');
3245+
3246+
// This will now throw ERR_ACCESS_DENIED
3247+
fs.readFileSync('/etc/myapp/config.json');
3248+
```
3249+
31923250
## `process.pid`
31933251
31943252
<!-- YAML
@@ -4598,6 +4656,7 @@ cases:
45984656
[`process.hrtime()`]: #processhrtimetime
45994657
[`process.hrtime.bigint()`]: #processhrtimebigint
46004658
[`process.kill()`]: #processkillpid-signal
4659+
[`process.permission.has()`]: #processpermissionhasscope-reference
46014660
[`process.setUncaughtExceptionCaptureCallback()`]: #processsetuncaughtexceptioncapturecallbackfn
46024661
[`promise.catch()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
46034662
[`queueMicrotask()`]: globals.md#queuemicrotaskcallback

β€Žlib/internal/process/permission.jsβ€Ž

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ module.exports = ObjectFreeze({
3333

3434
returnpermission.has(scope,reference);
3535
},
36+
drop(scope,reference){
37+
validateString(scope,'scope');
38+
if(reference!=null){
39+
if(isBuffer(reference)){
40+
validateBuffer(reference,'reference');
41+
}else{
42+
validateString(reference,'reference');
43+
}
44+
}
45+
46+
permission.drop(scope,reference);
47+
},
3648
availableFlags(){
3749
return[
3850
'--allow-fs-read',

β€Žlib/internal/process/pre_execution.jsβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ function initializePermission() {
641641
};
642642
// Guarantee path module isn't monkey-patched to bypass permission model
643643
ObjectFreeze(require('path'));
644-
const{ has }=require('internal/process/permission');
644+
const{ has, drop}=require('internal/process/permission');
645645
constwarnFlags=[
646646
'--allow-addons',
647647
'--allow-child-process',
@@ -679,6 +679,7 @@ function initializePermission() {
679679
configurable: false,
680680
value: {
681681
has,
682+
drop,
682683
},
683684
});
684685
}else{

β€Žsrc/permission/addon_permission.ccβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ void AddonPermission::Apply(Environment* env,
1414
deny_all_ = true;
1515
}
1616

17+
voidAddonPermission::Drop(Environment* env,
18+
PermissionScope scope,
19+
const std::string_view& param) {
20+
deny_all_ = true;
21+
}
22+
1723
boolAddonPermission::is_granted(Environment* env,
1824
PermissionScope perm,
1925
const std::string_view& param) const {

β€Žsrc/permission/addon_permission.hβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class AddonPermission final : public PermissionBase {
1515
voidApply(Environment* env,
1616
const std::vector<std::string>& allow,
1717
PermissionScope scope) override;
18+
voidDrop(Environment* env,
19+
PermissionScope scope,
20+
const std::string_view& param = "") override;
1821
boolis_granted(Environment* env,
1922
PermissionScope perm,
2023
const std::string_view& param = "") constoverride;

β€Žsrc/permission/child_process_permission.ccβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ void ChildProcessPermission::Apply(Environment* env,
1515
deny_all_ = true;
1616
}
1717

18+
voidChildProcessPermission::Drop(Environment* env,
19+
PermissionScope scope,
20+
const std::string_view& param) {
21+
deny_all_ = true;
22+
}
23+
1824
boolChildProcessPermission::is_granted(Environment* env,
1925
PermissionScope perm,
2026
const std::string_view& param) const {

β€Žsrc/permission/child_process_permission.hβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class ChildProcessPermission final : public PermissionBase {
1515
voidApply(Environment* env,
1616
const std::vector<std::string>& allow,
1717
PermissionScope scope) override;
18+
voidDrop(Environment* env,
19+
PermissionScope scope,
20+
const std::string_view& param = "") override;
1821
boolis_granted(Environment* env,
1922
PermissionScope perm,
2023
const std::string_view& param = "") constoverride;

β€Žsrc/permission/fs_permission.ccβ€Ž

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,96 @@ void FSPermission::Apply(Environment* env,
154154
}
155155
}
156156

157+
voidFSPermission::Drop(Environment* env,
158+
PermissionScope scope,
159+
const std::string_view& param) {
160+
if (param.empty()) {
161+
// Drop all access for this scope
162+
if (scope == PermissionScope::kFileSystemRead ||
163+
scope == PermissionScope::kFileSystem) {
164+
deny_all_in_ = true;
165+
allow_all_in_ = false;
166+
granted_in_fs_.Clear();
167+
granted_paths_in_.clear();
168+
}
169+
if (scope == PermissionScope::kFileSystemWrite ||
170+
scope == PermissionScope::kFileSystem) {
171+
deny_all_out_ = true;
172+
allow_all_out_ = false;
173+
granted_out_fs_.Clear();
174+
granted_paths_out_.clear();
175+
}
176+
return;
177+
}
178+
179+
// When allowed with *, you can only drop * (no specific paths)
180+
std::string resolved = PathResolve(env, {param});
181+
if (scope == PermissionScope::kFileSystemRead ||
182+
scope == PermissionScope::kFileSystem) {
183+
if (!allow_all_in_) {
184+
RevokeAccess(PermissionScope::kFileSystemRead, resolved);
185+
}
186+
}
187+
if (scope == PermissionScope::kFileSystemWrite ||
188+
scope == PermissionScope::kFileSystem) {
189+
if (!allow_all_out_) {
190+
RevokeAccess(PermissionScope::kFileSystemWrite, resolved);
191+
}
192+
}
193+
}
194+
195+
voidFSPermission::RevokeAccess(PermissionScope perm, const std::string& res) {
196+
const std::string path = WildcardIfDir(res);
197+
if (perm == PermissionScope::kFileSystemRead) {
198+
auto it =
199+
std::find(granted_paths_in_.begin(), granted_paths_in_.end(), path);
200+
if (it != granted_paths_in_.end()) {
201+
granted_paths_in_.erase(it);
202+
RebuildTree(PermissionScope::kFileSystemRead);
203+
}
204+
} elseif (perm == PermissionScope::kFileSystemWrite) {
205+
auto it =
206+
std::find(granted_paths_out_.begin(), granted_paths_out_.end(), path);
207+
if (it != granted_paths_out_.end()) {
208+
granted_paths_out_.erase(it);
209+
RebuildTree(PermissionScope::kFileSystemWrite);
210+
}
211+
}
212+
}
213+
214+
voidFSPermission::RebuildTree(PermissionScope scope) {
215+
if (scope == PermissionScope::kFileSystemRead) {
216+
granted_in_fs_.Clear();
217+
if (granted_paths_in_.empty()) {
218+
deny_all_in_ = true;
219+
} else {
220+
for (constauto& path : granted_paths_in_) {
221+
granted_in_fs_.Insert(path);
222+
}
223+
}
224+
} elseif (scope == PermissionScope::kFileSystemWrite) {
225+
granted_out_fs_.Clear();
226+
if (granted_paths_out_.empty()) {
227+
deny_all_out_ = true;
228+
} else {
229+
for (constauto& path : granted_paths_out_) {
230+
granted_out_fs_.Insert(path);
231+
}
232+
}
233+
}
234+
}
235+
157236
voidFSPermission::GrantAccess(PermissionScope perm, const std::string& res) {
158237
const std::string path = WildcardIfDir(res);
159238
if (perm == PermissionScope::kFileSystemRead &&
160239
!granted_in_fs_.Lookup(path)) {
161240
granted_in_fs_.Insert(path);
241+
granted_paths_in_.push_back(path);
162242
deny_all_in_ = false;
163243
} elseif (perm == PermissionScope::kFileSystemWrite &&
164244
!granted_out_fs_.Lookup(path)) {
165245
granted_out_fs_.Insert(path);
246+
granted_paths_out_.push_back(path);
166247
deny_all_out_ = false;
167248
}
168249
}
@@ -196,6 +277,16 @@ FSPermission::RadixTree::~RadixTree() {
196277
FreeRecursivelyNode(root_node_);
197278
}
198279

280+
voidFSPermission::RadixTree::Clear() {
281+
for (auto& c : root_node_->children) {
282+
FreeRecursivelyNode(c.second);
283+
}
284+
root_node_->children.clear();
285+
delete root_node_->wildcard_child;
286+
root_node_->wildcard_child = nullptr;
287+
root_node_->is_leaf = false;
288+
}
289+
199290
boolFSPermission::RadixTree::Lookup(const std::string_view& s,
200291
bool when_empty_return) const {
201292
FSPermission::RadixTree::Node* current_node = root_node_;

β€Žsrc/permission/fs_permission.hβ€Ž

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class FSPermission final : public PermissionBase {
1818
voidApply(Environment* env,
1919
const std::vector<std::string>& allow,
2020
PermissionScope scope) override;
21+
voidDrop(Environment* env,
22+
PermissionScope scope,
23+
const std::string_view& param = "") override;
2124
boolis_granted(Environment* env,
2225
PermissionScope perm,
2326
const std::string_view& param) constoverride;
@@ -142,6 +145,7 @@ class FSPermission final : public PermissionBase {
142145
RadixTree();
143146
~RadixTree();
144147
voidInsert(const std::string& s);
148+
voidClear();
145149
boolLookup(const std::string_view& s) const { returnLookup(s, false); }
146150
boolLookup(const std::string_view& s, bool when_empty_return) const;
147151

@@ -151,10 +155,15 @@ class FSPermission final : public PermissionBase {
151155

152156
private:
153157
voidGrantAccess(PermissionScope scope, const std::string& param);
158+
voidRevokeAccess(PermissionScope scope, const std::string& param);
159+
voidRebuildTree(PermissionScope scope);
154160
// fs granted on startup
155161
RadixTree granted_in_fs_;
156162
RadixTree granted_out_fs_;
157163

164+
std::vector<std::string> granted_paths_in_;
165+
std::vector<std::string> granted_paths_out_;
166+
158167
bool deny_all_in_ = true;
159168
bool deny_all_out_ = true;
160169

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit d4cafce

Browse files
RafaelGSSaduh95
authored andcommitted
lib,permission: add permission.drop
Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com> PR-URL: #62672 Backport-PR-URL: #65354 Refs: #62223 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent cd1eb3e commit d4cafce

30 files changed

Lines changed: 848 additions & 2 deletions

β€Ždoc/api/permissions.mdβ€Ž

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ flag. For WASI, use the [`--allow-wasi`][] flag.
7474

7575
When enabling the Permission Model through the [`--permission`][]
7676
flag a new property `permission` is added to the `process` object.
77-
This property contains one function:
77+
This property contains the following functions:
7878

7979
##### `permission.has(scope[, reference])`
8080

@@ -88,6 +88,40 @@ process.permission.has('fs.read'); // true
8888
process.permission.has('fs.read', '/home/rafaelgss/protected-folder'); // false
8989
```
9090

91+
##### `permission.drop(scope[, reference])`
92+
93+
API call to drop permissions at runtime. This operation is **irreversible**.
94+
95+
When called without a reference, the entire scope is dropped. When called
96+
with a reference, only the permission for that specific resource is revoked.
97+
Dropping a permission only affects future access checks. It does not close or
98+
revoke access to resources that are already open, such as file descriptors,
99+
child processes, or worker threads. Applications are responsible for closing
100+
or terminating those resources when they are no longer needed.
101+
102+
You can only drop the exact resource that was explicitly granted. The
103+
reference passed to `drop()` must match the original grant. If a permission
104+
was granted using a wildcard (`*`), only the entire scope can be dropped
105+
(by calling `drop()` without a reference). If a directory was granted
106+
(e.g. `--allow-fs-read=/my/folder`), you cannot drop individual files
107+
inside it - you must drop the same directory that was originally granted.
108+
109+
```js
110+
constfs=require('node:fs');
111+
112+
// Read config at startup while we still have permission
113+
constconfig=fs.readFileSync('/etc/myapp/config.json', 'utf8');
114+
115+
// Drop read access to /etc/myapp after initialization
116+
process.permission.drop('fs.read', '/etc/myapp');
117+
118+
// This will now throw ERR_ACCESS_DENIED
119+
process.permission.has('fs.read', '/etc/myapp/config.json'); // false
120+
121+
// Drop child process permission entirely
122+
process.permission.drop('child');
123+
```
124+
91125
#### File System Permissions
92126

93127
The Permission Model, by default, restricts access to the file system through the `node:fs` module.

β€Ždoc/api/process.mdβ€Ž

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3189,6 +3189,64 @@ process.permission.has('fs.read', './README.md');
31893189
process.permission.has('fs.read');
31903190
```
31913191
3192+
### `process.permission.drop(scope[, reference])`
3193+
3194+
<!-- YAML
3195+
added: REPLACEME
3196+
-->
3197+
3198+
> Stability: 1.1 - Active Development
3199+
3200+
* `scope` {string}
3201+
* `reference` {string}
3202+
3203+
Drops the specified permission from the current process. This operation is
3204+
**irreversible** β€” once a permission is dropped, it cannot be restored through
3205+
any Node.js API.
3206+
3207+
If no reference is provided, the entire scope is dropped. For example,
3208+
`process.permission.drop('fs.read')` will revoke ALL file system read
3209+
permissions.
3210+
3211+
When a reference is provided, only the permission for that specific resource
3212+
is dropped. For example, `process.permission.drop('fs.read', '/etc/myapp')`
3213+
will revoke read access to that directory while keeping other read
3214+
permissions intact.
3215+
3216+
**Important:** You can only drop the exact resource that was explicitly
3217+
granted. The reference passed to `drop()` must match the original grant:
3218+
3219+
* If a permission was granted using a wildcard (`*`), such as
3220+
`--allow-fs-read=*`, individual paths cannot be dropped - only the entire
3221+
scope can be dropped (by calling `drop()` without a reference).
3222+
* If a directory was granted (e.g. `--allow-fs-read=/my/folder`), you cannot
3223+
drop access to individual files inside it. You must drop the same directory
3224+
that was granted. Any remaining grants continue to apply.
3225+
3226+
The available scopes are the same as [`process.permission.has()`][]:
3227+
3228+
* `fs` - All File System (drops both read and write)
3229+
* `fs.read` - File System read operations
3230+
* `fs.write` - File System write operations
3231+
* `child` - Child process spawning operations
3232+
* `worker` - Worker thread spawning operation
3233+
* `inspector` - Inspector operations
3234+
* `wasi` - WASI operations
3235+
* `addon` - Native addon operations
3236+
3237+
```js
3238+
constfs=require('node:fs');
3239+
3240+
// Read configuration during startup
3241+
constconfig=fs.readFileSync('/etc/myapp/config.json', 'utf8');
3242+
3243+
// Drop read access to the config directory after initialization
3244+
process.permission.drop('fs.read', '/etc/myapp');
3245+
3246+
// This will now throw ERR_ACCESS_DENIED
3247+
fs.readFileSync('/etc/myapp/config.json');
3248+
```
3249+
31923250
## `process.pid`
31933251
31943252
<!-- YAML
@@ -4598,6 +4656,7 @@ cases:
45984656
[`process.hrtime()`]: #processhrtimetime
45994657
[`process.hrtime.bigint()`]: #processhrtimebigint
46004658
[`process.kill()`]: #processkillpid-signal
4659+
[`process.permission.has()`]: #processpermissionhasscope-reference
46014660
[`process.setUncaughtExceptionCaptureCallback()`]: #processsetuncaughtexceptioncapturecallbackfn
46024661
[`promise.catch()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
46034662
[`queueMicrotask()`]: globals.md#queuemicrotaskcallback

β€Žlib/internal/process/permission.jsβ€Ž

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ module.exports = ObjectFreeze({
3333

3434
returnpermission.has(scope,reference);
3535
},
36+
drop(scope,reference){
37+
validateString(scope,'scope');
38+
if(reference!=null){
39+
if(isBuffer(reference)){
40+
validateBuffer(reference,'reference');
41+
}else{
42+
validateString(reference,'reference');
43+
}
44+
}
45+
46+
permission.drop(scope,reference);
47+
},
3648
availableFlags(){
3749
return[
3850
'--allow-fs-read',

β€Žlib/internal/process/pre_execution.jsβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ function initializePermission() {
641641
};
642642
// Guarantee path module isn't monkey-patched to bypass permission model
643643
ObjectFreeze(require('path'));
644-
const{ has }=require('internal/process/permission');
644+
const{ has, drop}=require('internal/process/permission');
645645
constwarnFlags=[
646646
'--allow-addons',
647647
'--allow-child-process',
@@ -679,6 +679,7 @@ function initializePermission() {
679679
configurable: false,
680680
value: {
681681
has,
682+
drop,
682683
},
683684
});
684685
}else{

β€Žsrc/permission/addon_permission.ccβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ void AddonPermission::Apply(Environment* env,
1414
deny_all_ = true;
1515
}
1616

17+
voidAddonPermission::Drop(Environment* env,
18+
PermissionScope scope,
19+
const std::string_view& param) {
20+
deny_all_ = true;
21+
}
22+
1723
boolAddonPermission::is_granted(Environment* env,
1824
PermissionScope perm,
1925
const std::string_view& param) const {

β€Žsrc/permission/addon_permission.hβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class AddonPermission final : public PermissionBase {
1515
voidApply(Environment* env,
1616
const std::vector<std::string>& allow,
1717
PermissionScope scope) override;
18+
voidDrop(Environment* env,
19+
PermissionScope scope,
20+
const std::string_view& param = "") override;
1821
boolis_granted(Environment* env,
1922
PermissionScope perm,
2023
const std::string_view& param = "") constoverride;

β€Žsrc/permission/child_process_permission.ccβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ void ChildProcessPermission::Apply(Environment* env,
1515
deny_all_ = true;
1616
}
1717

18+
voidChildProcessPermission::Drop(Environment* env,
19+
PermissionScope scope,
20+
const std::string_view& param) {
21+
deny_all_ = true;
22+
}
23+
1824
boolChildProcessPermission::is_granted(Environment* env,
1925
PermissionScope perm,
2026
const std::string_view& param) const {

β€Žsrc/permission/child_process_permission.hβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class ChildProcessPermission final : public PermissionBase {
1515
voidApply(Environment* env,
1616
const std::vector<std::string>& allow,
1717
PermissionScope scope) override;
18+
voidDrop(Environment* env,
19+
PermissionScope scope,
20+
const std::string_view& param = "") override;
1821
boolis_granted(Environment* env,
1922
PermissionScope perm,
2023
const std::string_view& param = "") constoverride;

β€Žsrc/permission/fs_permission.ccβ€Ž

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,96 @@ void FSPermission::Apply(Environment* env,
154154
}
155155
}
156156

157+
voidFSPermission::Drop(Environment* env,
158+
PermissionScope scope,
159+
const std::string_view& param) {
160+
if (param.empty()) {
161+
// Drop all access for this scope
162+
if (scope == PermissionScope::kFileSystemRead ||
163+
scope == PermissionScope::kFileSystem) {
164+
deny_all_in_ = true;
165+
allow_all_in_ = false;
166+
granted_in_fs_.Clear();
167+
granted_paths_in_.clear();
168+
}
169+
if (scope == PermissionScope::kFileSystemWrite ||
170+
scope == PermissionScope::kFileSystem) {
171+
deny_all_out_ = true;
172+
allow_all_out_ = false;
173+
granted_out_fs_.Clear();
174+
granted_paths_out_.clear();
175+
}
176+
return;
177+
}
178+
179+
// When allowed with *, you can only drop * (no specific paths)
180+
std::string resolved = PathResolve(env, {param});
181+
if (scope == PermissionScope::kFileSystemRead ||
182+
scope == PermissionScope::kFileSystem) {
183+
if (!allow_all_in_) {
184+
RevokeAccess(PermissionScope::kFileSystemRead, resolved);
185+
}
186+
}
187+
if (scope == PermissionScope::kFileSystemWrite ||
188+
scope == PermissionScope::kFileSystem) {
189+
if (!allow_all_out_) {
190+
RevokeAccess(PermissionScope::kFileSystemWrite, resolved);
191+
}
192+
}
193+
}
194+
195+
voidFSPermission::RevokeAccess(PermissionScope perm, const std::string& res) {
196+
const std::string path = WildcardIfDir(res);
197+
if (perm == PermissionScope::kFileSystemRead) {
198+
auto it =
199+
std::find(granted_paths_in_.begin(), granted_paths_in_.end(), path);
200+
if (it != granted_paths_in_.end()) {
201+
granted_paths_in_.erase(it);
202+
RebuildTree(PermissionScope::kFileSystemRead);
203+
}
204+
} elseif (perm == PermissionScope::kFileSystemWrite) {
205+
auto it =
206+
std::find(granted_paths_out_.begin(), granted_paths_out_.end(), path);
207+
if (it != granted_paths_out_.end()) {
208+
granted_paths_out_.erase(it);
209+
RebuildTree(PermissionScope::kFileSystemWrite);
210+
}
211+
}
212+
}
213+
214+
voidFSPermission::RebuildTree(PermissionScope scope) {
215+
if (scope == PermissionScope::kFileSystemRead) {
216+
granted_in_fs_.Clear();
217+
if (granted_paths_in_.empty()) {
218+
deny_all_in_ = true;
219+
} else {
220+
for (constauto& path : granted_paths_in_) {
221+
granted_in_fs_.Insert(path);
222+
}
223+
}
224+
} elseif (scope == PermissionScope::kFileSystemWrite) {
225+
granted_out_fs_.Clear();
226+
if (granted_paths_out_.empty()) {
227+
deny_all_out_ = true;
228+
} else {
229+
for (constauto& path : granted_paths_out_) {
230+
granted_out_fs_.Insert(path);
231+
}
232+
}
233+
}
234+
}
235+
157236
voidFSPermission::GrantAccess(PermissionScope perm, const std::string& res) {
158237
const std::string path = WildcardIfDir(res);
159238
if (perm == PermissionScope::kFileSystemRead &&
160239
!granted_in_fs_.Lookup(path)) {
161240
granted_in_fs_.Insert(path);
241+
granted_paths_in_.push_back(path);
162242
deny_all_in_ = false;
163243
} elseif (perm == PermissionScope::kFileSystemWrite &&
164244
!granted_out_fs_.Lookup(path)) {
165245
granted_out_fs_.Insert(path);
246+
granted_paths_out_.push_back(path);
166247
deny_all_out_ = false;
167248
}
168249
}
@@ -196,6 +277,16 @@ FSPermission::RadixTree::~RadixTree() {
196277
FreeRecursivelyNode(root_node_);
197278
}
198279

280+
voidFSPermission::RadixTree::Clear() {
281+
for (auto& c : root_node_->children) {
282+
FreeRecursivelyNode(c.second);
283+
}
284+
root_node_->children.clear();
285+
delete root_node_->wildcard_child;
286+
root_node_->wildcard_child = nullptr;
287+
root_node_->is_leaf = false;
288+
}
289+
199290
boolFSPermission::RadixTree::Lookup(const std::string_view& s,
200291
bool when_empty_return) const {
201292
FSPermission::RadixTree::Node* current_node = root_node_;

β€Žsrc/permission/fs_permission.hβ€Ž

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class FSPermission final : public PermissionBase {
1818
voidApply(Environment* env,
1919
const std::vector<std::string>& allow,
2020
PermissionScope scope) override;
21+
voidDrop(Environment* env,
22+
PermissionScope scope,
23+
const std::string_view& param = "") override;
2124
boolis_granted(Environment* env,
2225
PermissionScope perm,
2326
const std::string_view& param) constoverride;
@@ -142,6 +145,7 @@ class FSPermission final : public PermissionBase {
142145
RadixTree();
143146
~RadixTree();
144147
voidInsert(const std::string& s);
148+
voidClear();
145149
boolLookup(const std::string_view& s) const { returnLookup(s, false); }
146150
boolLookup(const std::string_view& s, bool when_empty_return) const;
147151

@@ -151,10 +155,15 @@ class FSPermission final : public PermissionBase {
151155

152156
private:
153157
voidGrantAccess(PermissionScope scope, const std::string& param);
158+
voidRevokeAccess(PermissionScope scope, const std::string& param);
159+
voidRebuildTree(PermissionScope scope);
154160
// fs granted on startup
155161
RadixTree granted_in_fs_;
156162
RadixTree granted_out_fs_;
157163

164+
std::vector<std::string> granted_paths_in_;
165+
std::vector<std::string> granted_paths_out_;
166+
158167
bool deny_all_in_ = true;
159168
bool deny_all_out_ = true;
160169

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Commit d4cafce

Browse files
RafaelGSSaduh95
authored andcommitted
lib,permission: add permission.drop
Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com> PR-URL: #62672 Backport-PR-URL: #65354 Refs: #62223 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent cd1eb3e commit d4cafce

30 files changed

Lines changed: 848 additions & 2 deletions

β€Ždoc/api/permissions.mdβ€Ž

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ flag. For WASI, use the [`--allow-wasi`][] flag.
7474

7575
When enabling the Permission Model through the [`--permission`][]
7676
flag a new property `permission` is added to the `process` object.
77-
This property contains one function:
77+
This property contains the following functions:
7878

7979
##### `permission.has(scope[, reference])`
8080

@@ -88,6 +88,40 @@ process.permission.has('fs.read'); // true
8888
process.permission.has('fs.read', '/home/rafaelgss/protected-folder'); // false
8989
```
9090

91+
##### `permission.drop(scope[, reference])`
92+
93+
API call to drop permissions at runtime. This operation is **irreversible**.
94+
95+
When called without a reference, the entire scope is dropped. When called
96+
with a reference, only the permission for that specific resource is revoked.
97+
Dropping a permission only affects future access checks. It does not close or
98+
revoke access to resources that are already open, such as file descriptors,
99+
child processes, or worker threads. Applications are responsible for closing
100+
or terminating those resources when they are no longer needed.
101+
102+
You can only drop the exact resource that was explicitly granted. The
103+
reference passed to `drop()` must match the original grant. If a permission
104+
was granted using a wildcard (`*`), only the entire scope can be dropped
105+
(by calling `drop()` without a reference). If a directory was granted
106+
(e.g. `--allow-fs-read=/my/folder`), you cannot drop individual files
107+
inside it - you must drop the same directory that was originally granted.
108+
109+
```js
110+
constfs=require('node:fs');
111+
112+
// Read config at startup while we still have permission
113+
constconfig=fs.readFileSync('/etc/myapp/config.json', 'utf8');
114+
115+
// Drop read access to /etc/myapp after initialization
116+
process.permission.drop('fs.read', '/etc/myapp');
117+
118+
// This will now throw ERR_ACCESS_DENIED
119+
process.permission.has('fs.read', '/etc/myapp/config.json'); // false
120+
121+
// Drop child process permission entirely
122+
process.permission.drop('child');
123+
```
124+
91125
#### File System Permissions
92126

93127
The Permission Model, by default, restricts access to the file system through the `node:fs` module.

β€Ždoc/api/process.mdβ€Ž

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3189,6 +3189,64 @@ process.permission.has('fs.read', './README.md');
31893189
process.permission.has('fs.read');
31903190
```
31913191
3192+
### `process.permission.drop(scope[, reference])`
3193+
3194+
<!-- YAML
3195+
added: REPLACEME
3196+
-->
3197+
3198+
> Stability: 1.1 - Active Development
3199+
3200+
* `scope` {string}
3201+
* `reference` {string}
3202+
3203+
Drops the specified permission from the current process. This operation is
3204+
**irreversible** β€” once a permission is dropped, it cannot be restored through
3205+
any Node.js API.
3206+
3207+
If no reference is provided, the entire scope is dropped. For example,
3208+
`process.permission.drop('fs.read')` will revoke ALL file system read
3209+
permissions.
3210+
3211+
When a reference is provided, only the permission for that specific resource
3212+
is dropped. For example, `process.permission.drop('fs.read', '/etc/myapp')`
3213+
will revoke read access to that directory while keeping other read
3214+
permissions intact.
3215+
3216+
**Important:** You can only drop the exact resource that was explicitly
3217+
granted. The reference passed to `drop()` must match the original grant:
3218+
3219+
* If a permission was granted using a wildcard (`*`), such as
3220+
`--allow-fs-read=*`, individual paths cannot be dropped - only the entire
3221+
scope can be dropped (by calling `drop()` without a reference).
3222+
* If a directory was granted (e.g. `--allow-fs-read=/my/folder`), you cannot
3223+
drop access to individual files inside it. You must drop the same directory
3224+
that was granted. Any remaining grants continue to apply.
3225+
3226+
The available scopes are the same as [`process.permission.has()`][]:
3227+
3228+
* `fs` - All File System (drops both read and write)
3229+
* `fs.read` - File System read operations
3230+
* `fs.write` - File System write operations
3231+
* `child` - Child process spawning operations
3232+
* `worker` - Worker thread spawning operation
3233+
* `inspector` - Inspector operations
3234+
* `wasi` - WASI operations
3235+
* `addon` - Native addon operations
3236+
3237+
```js
3238+
constfs=require('node:fs');
3239+
3240+
// Read configuration during startup
3241+
constconfig=fs.readFileSync('/etc/myapp/config.json', 'utf8');
3242+
3243+
// Drop read access to the config directory after initialization
3244+
process.permission.drop('fs.read', '/etc/myapp');
3245+
3246+
// This will now throw ERR_ACCESS_DENIED
3247+
fs.readFileSync('/etc/myapp/config.json');
3248+
```
3249+
31923250
## `process.pid`
31933251
31943252
<!-- YAML
@@ -4598,6 +4656,7 @@ cases:
45984656
[`process.hrtime()`]: #processhrtimetime
45994657
[`process.hrtime.bigint()`]: #processhrtimebigint
46004658
[`process.kill()`]: #processkillpid-signal
4659+
[`process.permission.has()`]: #processpermissionhasscope-reference
46014660
[`process.setUncaughtExceptionCaptureCallback()`]: #processsetuncaughtexceptioncapturecallbackfn
46024661
[`promise.catch()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
46034662
[`queueMicrotask()`]: globals.md#queuemicrotaskcallback

β€Žlib/internal/process/permission.jsβ€Ž

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ module.exports = ObjectFreeze({
3333

3434
returnpermission.has(scope,reference);
3535
},
36+
drop(scope,reference){
37+
validateString(scope,'scope');
38+
if(reference!=null){
39+
if(isBuffer(reference)){
40+
validateBuffer(reference,'reference');
41+
}else{
42+
validateString(reference,'reference');
43+
}
44+
}
45+
46+
permission.drop(scope,reference);
47+
},
3648
availableFlags(){
3749
return[
3850
'--allow-fs-read',

β€Žlib/internal/process/pre_execution.jsβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ function initializePermission() {
641641
};
642642
// Guarantee path module isn't monkey-patched to bypass permission model
643643
ObjectFreeze(require('path'));
644-
const{ has }=require('internal/process/permission');
644+
const{ has, drop}=require('internal/process/permission');
645645
constwarnFlags=[
646646
'--allow-addons',
647647
'--allow-child-process',
@@ -679,6 +679,7 @@ function initializePermission() {
679679
configurable: false,
680680
value: {
681681
has,
682+
drop,
682683
},
683684
});
684685
}else{

β€Žsrc/permission/addon_permission.ccβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ void AddonPermission::Apply(Environment* env,
1414
deny_all_ = true;
1515
}
1616

17+
voidAddonPermission::Drop(Environment* env,
18+
PermissionScope scope,
19+
const std::string_view& param) {
20+
deny_all_ = true;
21+
}
22+
1723
boolAddonPermission::is_granted(Environment* env,
1824
PermissionScope perm,
1925
const std::string_view& param) const {

β€Žsrc/permission/addon_permission.hβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class AddonPermission final : public PermissionBase {
1515
voidApply(Environment* env,
1616
const std::vector<std::string>& allow,
1717
PermissionScope scope) override;
18+
voidDrop(Environment* env,
19+
PermissionScope scope,
20+
const std::string_view& param = "") override;
1821
boolis_granted(Environment* env,
1922
PermissionScope perm,
2023
const std::string_view& param = "") constoverride;

β€Žsrc/permission/child_process_permission.ccβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ void ChildProcessPermission::Apply(Environment* env,
1515
deny_all_ = true;
1616
}
1717

18+
voidChildProcessPermission::Drop(Environment* env,
19+
PermissionScope scope,
20+
const std::string_view& param) {
21+
deny_all_ = true;
22+
}
23+
1824
boolChildProcessPermission::is_granted(Environment* env,
1925
PermissionScope perm,
2026
const std::string_view& param) const {

β€Žsrc/permission/child_process_permission.hβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class ChildProcessPermission final : public PermissionBase {
1515
voidApply(Environment* env,
1616
const std::vector<std::string>& allow,
1717
PermissionScope scope) override;
18+
voidDrop(Environment* env,
19+
PermissionScope scope,
20+
const std::string_view& param = "") override;
1821
boolis_granted(Environment* env,
1922
PermissionScope perm,
2023
const std::string_view& param = "") constoverride;

β€Žsrc/permission/fs_permission.ccβ€Ž

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,96 @@ void FSPermission::Apply(Environment* env,
154154
}
155155
}
156156

157+
voidFSPermission::Drop(Environment* env,
158+
PermissionScope scope,
159+
const std::string_view& param) {
160+
if (param.empty()) {
161+
// Drop all access for this scope
162+
if (scope == PermissionScope::kFileSystemRead ||
163+
scope == PermissionScope::kFileSystem) {
164+
deny_all_in_ = true;
165+
allow_all_in_ = false;
166+
granted_in_fs_.Clear();
167+
granted_paths_in_.clear();
168+
}
169+
if (scope == PermissionScope::kFileSystemWrite ||
170+
scope == PermissionScope::kFileSystem) {
171+
deny_all_out_ = true;
172+
allow_all_out_ = false;
173+
granted_out_fs_.Clear();
174+
granted_paths_out_.clear();
175+
}
176+
return;
177+
}
178+
179+
// When allowed with *, you can only drop * (no specific paths)
180+
std::string resolved = PathResolve(env, {param});
181+
if (scope == PermissionScope::kFileSystemRead ||
182+
scope == PermissionScope::kFileSystem) {
183+
if (!allow_all_in_) {
184+
RevokeAccess(PermissionScope::kFileSystemRead, resolved);
185+
}
186+
}
187+
if (scope == PermissionScope::kFileSystemWrite ||
188+
scope == PermissionScope::kFileSystem) {
189+
if (!allow_all_out_) {
190+
RevokeAccess(PermissionScope::kFileSystemWrite, resolved);
191+
}
192+
}
193+
}
194+
195+
voidFSPermission::RevokeAccess(PermissionScope perm, const std::string& res) {
196+
const std::string path = WildcardIfDir(res);
197+
if (perm == PermissionScope::kFileSystemRead) {
198+
auto it =
199+
std::find(granted_paths_in_.begin(), granted_paths_in_.end(), path);
200+
if (it != granted_paths_in_.end()) {
201+
granted_paths_in_.erase(it);
202+
RebuildTree(PermissionScope::kFileSystemRead);
203+
}
204+
} elseif (perm == PermissionScope::kFileSystemWrite) {
205+
auto it =
206+
std::find(granted_paths_out_.begin(), granted_paths_out_.end(), path);
207+
if (it != granted_paths_out_.end()) {
208+
granted_paths_out_.erase(it);
209+
RebuildTree(PermissionScope::kFileSystemWrite);
210+
}
211+
}
212+
}
213+
214+
voidFSPermission::RebuildTree(PermissionScope scope) {
215+
if (scope == PermissionScope::kFileSystemRead) {
216+
granted_in_fs_.Clear();
217+
if (granted_paths_in_.empty()) {
218+
deny_all_in_ = true;
219+
} else {
220+
for (constauto& path : granted_paths_in_) {
221+
granted_in_fs_.Insert(path);
222+
}
223+
}
224+
} elseif (scope == PermissionScope::kFileSystemWrite) {
225+
granted_out_fs_.Clear();
226+
if (granted_paths_out_.empty()) {
227+
deny_all_out_ = true;
228+
} else {
229+
for (constauto& path : granted_paths_out_) {
230+
granted_out_fs_.Insert(path);
231+
}
232+
}
233+
}
234+
}
235+
157236
voidFSPermission::GrantAccess(PermissionScope perm, const std::string& res) {
158237
const std::string path = WildcardIfDir(res);
159238
if (perm == PermissionScope::kFileSystemRead &&
160239
!granted_in_fs_.Lookup(path)) {
161240
granted_in_fs_.Insert(path);
241+
granted_paths_in_.push_back(path);
162242
deny_all_in_ = false;
163243
} elseif (perm == PermissionScope::kFileSystemWrite &&
164244
!granted_out_fs_.Lookup(path)) {
165245
granted_out_fs_.Insert(path);
246+
granted_paths_out_.push_back(path);
166247
deny_all_out_ = false;
167248
}
168249
}
@@ -196,6 +277,16 @@ FSPermission::RadixTree::~RadixTree() {
196277
FreeRecursivelyNode(root_node_);
197278
}
198279

280+
voidFSPermission::RadixTree::Clear() {
281+
for (auto& c : root_node_->children) {
282+
FreeRecursivelyNode(c.second);
283+
}
284+
root_node_->children.clear();
285+
delete root_node_->wildcard_child;
286+
root_node_->wildcard_child = nullptr;
287+
root_node_->is_leaf = false;
288+
}
289+
199290
boolFSPermission::RadixTree::Lookup(const std::string_view& s,
200291
bool when_empty_return) const {
201292
FSPermission::RadixTree::Node* current_node = root_node_;

β€Žsrc/permission/fs_permission.hβ€Ž

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class FSPermission final : public PermissionBase {
1818
voidApply(Environment* env,
1919
const std::vector<std::string>& allow,
2020
PermissionScope scope) override;
21+
voidDrop(Environment* env,
22+
PermissionScope scope,
23+
const std::string_view& param = "") override;
2124
boolis_granted(Environment* env,
2225
PermissionScope perm,
2326
const std::string_view& param) constoverride;
@@ -142,6 +145,7 @@ class FSPermission final : public PermissionBase {
142145
RadixTree();
143146
~RadixTree();
144147
voidInsert(const std::string& s);
148+
voidClear();
145149
boolLookup(const std::string_view& s) const { returnLookup(s, false); }
146150
boolLookup(const std::string_view& s, bool when_empty_return) const;
147151

@@ -151,10 +155,15 @@ class FSPermission final : public PermissionBase {
151155

152156
private:
153157
voidGrantAccess(PermissionScope scope, const std::string& param);
158+
voidRevokeAccess(PermissionScope scope, const std::string& param);
159+
voidRebuildTree(PermissionScope scope);
154160
// fs granted on startup
155161
RadixTree granted_in_fs_;
156162
RadixTree granted_out_fs_;
157163

164+
std::vector<std::string> granted_paths_in_;
165+
std::vector<std::string> granted_paths_out_;
166+
158167
bool deny_all_in_ = true;
159168
bool deny_all_out_ = true;
160169

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit d4cafce

Browse files
RafaelGSSaduh95
authored andcommitted
lib,permission: add permission.drop
Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com> PR-URL: #62672 Backport-PR-URL: #65354 Refs: #62223 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent cd1eb3e commit d4cafce

30 files changed

Lines changed: 848 additions & 2 deletions

β€Ždoc/api/permissions.mdβ€Ž

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ flag. For WASI, use the [`--allow-wasi`][] flag.
7474

7575
When enabling the Permission Model through the [`--permission`][]
7676
flag a new property `permission` is added to the `process` object.
77-
This property contains one function:
77+
This property contains the following functions:
7878

7979
##### `permission.has(scope[, reference])`
8080

@@ -88,6 +88,40 @@ process.permission.has('fs.read'); // true
8888
process.permission.has('fs.read', '/home/rafaelgss/protected-folder'); // false
8989
```
9090

91+
##### `permission.drop(scope[, reference])`
92+
93+
API call to drop permissions at runtime. This operation is **irreversible**.
94+
95+
When called without a reference, the entire scope is dropped. When called
96+
with a reference, only the permission for that specific resource is revoked.
97+
Dropping a permission only affects future access checks. It does not close or
98+
revoke access to resources that are already open, such as file descriptors,
99+
child processes, or worker threads. Applications are responsible for closing
100+
or terminating those resources when they are no longer needed.
101+
102+
You can only drop the exact resource that was explicitly granted. The
103+
reference passed to `drop()` must match the original grant. If a permission
104+
was granted using a wildcard (`*`), only the entire scope can be dropped
105+
(by calling `drop()` without a reference). If a directory was granted
106+
(e.g. `--allow-fs-read=/my/folder`), you cannot drop individual files
107+
inside it - you must drop the same directory that was originally granted.
108+
109+
```js
110+
constfs=require('node:fs');
111+
112+
// Read config at startup while we still have permission
113+
constconfig=fs.readFileSync('/etc/myapp/config.json', 'utf8');
114+
115+
// Drop read access to /etc/myapp after initialization
116+
process.permission.drop('fs.read', '/etc/myapp');
117+
118+
// This will now throw ERR_ACCESS_DENIED
119+
process.permission.has('fs.read', '/etc/myapp/config.json'); // false
120+
121+
// Drop child process permission entirely
122+
process.permission.drop('child');
123+
```
124+
91125
#### File System Permissions
92126

93127
The Permission Model, by default, restricts access to the file system through the `node:fs` module.

β€Ždoc/api/process.mdβ€Ž

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3189,6 +3189,64 @@ process.permission.has('fs.read', './README.md');
31893189
process.permission.has('fs.read');
31903190
```
31913191
3192+
### `process.permission.drop(scope[, reference])`
3193+
3194+
<!-- YAML
3195+
added: REPLACEME
3196+
-->
3197+
3198+
> Stability: 1.1 - Active Development
3199+
3200+
* `scope` {string}
3201+
* `reference` {string}
3202+
3203+
Drops the specified permission from the current process. This operation is
3204+
**irreversible** β€” once a permission is dropped, it cannot be restored through
3205+
any Node.js API.
3206+
3207+
If no reference is provided, the entire scope is dropped. For example,
3208+
`process.permission.drop('fs.read')` will revoke ALL file system read
3209+
permissions.
3210+
3211+
When a reference is provided, only the permission for that specific resource
3212+
is dropped. For example, `process.permission.drop('fs.read', '/etc/myapp')`
3213+
will revoke read access to that directory while keeping other read
3214+
permissions intact.
3215+
3216+
**Important:** You can only drop the exact resource that was explicitly
3217+
granted. The reference passed to `drop()` must match the original grant:
3218+
3219+
* If a permission was granted using a wildcard (`*`), such as
3220+
`--allow-fs-read=*`, individual paths cannot be dropped - only the entire
3221+
scope can be dropped (by calling `drop()` without a reference).
3222+
* If a directory was granted (e.g. `--allow-fs-read=/my/folder`), you cannot
3223+
drop access to individual files inside it. You must drop the same directory
3224+
that was granted. Any remaining grants continue to apply.
3225+
3226+
The available scopes are the same as [`process.permission.has()`][]:
3227+
3228+
* `fs` - All File System (drops both read and write)
3229+
* `fs.read` - File System read operations
3230+
* `fs.write` - File System write operations
3231+
* `child` - Child process spawning operations
3232+
* `worker` - Worker thread spawning operation
3233+
* `inspector` - Inspector operations
3234+
* `wasi` - WASI operations
3235+
* `addon` - Native addon operations
3236+
3237+
```js
3238+
constfs=require('node:fs');
3239+
3240+
// Read configuration during startup
3241+
constconfig=fs.readFileSync('/etc/myapp/config.json', 'utf8');
3242+
3243+
// Drop read access to the config directory after initialization
3244+
process.permission.drop('fs.read', '/etc/myapp');
3245+
3246+
// This will now throw ERR_ACCESS_DENIED
3247+
fs.readFileSync('/etc/myapp/config.json');
3248+
```
3249+
31923250
## `process.pid`
31933251
31943252
<!-- YAML
@@ -4598,6 +4656,7 @@ cases:
45984656
[`process.hrtime()`]: #processhrtimetime
45994657
[`process.hrtime.bigint()`]: #processhrtimebigint
46004658
[`process.kill()`]: #processkillpid-signal
4659+
[`process.permission.has()`]: #processpermissionhasscope-reference
46014660
[`process.setUncaughtExceptionCaptureCallback()`]: #processsetuncaughtexceptioncapturecallbackfn
46024661
[`promise.catch()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
46034662
[`queueMicrotask()`]: globals.md#queuemicrotaskcallback

β€Žlib/internal/process/permission.jsβ€Ž

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ module.exports = ObjectFreeze({
3333

3434
returnpermission.has(scope,reference);
3535
},
36+
drop(scope,reference){
37+
validateString(scope,'scope');
38+
if(reference!=null){
39+
if(isBuffer(reference)){
40+
validateBuffer(reference,'reference');
41+
}else{
42+
validateString(reference,'reference');
43+
}
44+
}
45+
46+
permission.drop(scope,reference);
47+
},
3648
availableFlags(){
3749
return[
3850
'--allow-fs-read',

β€Žlib/internal/process/pre_execution.jsβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ function initializePermission() {
641641
};
642642
// Guarantee path module isn't monkey-patched to bypass permission model
643643
ObjectFreeze(require('path'));
644-
const{ has }=require('internal/process/permission');
644+
const{ has, drop}=require('internal/process/permission');
645645
constwarnFlags=[
646646
'--allow-addons',
647647
'--allow-child-process',
@@ -679,6 +679,7 @@ function initializePermission() {
679679
configurable: false,
680680
value: {
681681
has,
682+
drop,
682683
},
683684
});
684685
}else{

β€Žsrc/permission/addon_permission.ccβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ void AddonPermission::Apply(Environment* env,
1414
deny_all_ = true;
1515
}
1616

17+
voidAddonPermission::Drop(Environment* env,
18+
PermissionScope scope,
19+
const std::string_view& param) {
20+
deny_all_ = true;
21+
}
22+
1723
boolAddonPermission::is_granted(Environment* env,
1824
PermissionScope perm,
1925
const std::string_view& param) const {

β€Žsrc/permission/addon_permission.hβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class AddonPermission final : public PermissionBase {
1515
voidApply(Environment* env,
1616
const std::vector<std::string>& allow,
1717
PermissionScope scope) override;
18+
voidDrop(Environment* env,
19+
PermissionScope scope,
20+
const std::string_view& param = "") override;
1821
boolis_granted(Environment* env,
1922
PermissionScope perm,
2023
const std::string_view& param = "") constoverride;

β€Žsrc/permission/child_process_permission.ccβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ void ChildProcessPermission::Apply(Environment* env,
1515
deny_all_ = true;
1616
}
1717

18+
voidChildProcessPermission::Drop(Environment* env,
19+
PermissionScope scope,
20+
const std::string_view& param) {
21+
deny_all_ = true;
22+
}
23+
1824
boolChildProcessPermission::is_granted(Environment* env,
1925
PermissionScope perm,
2026
const std::string_view& param) const {

β€Žsrc/permission/child_process_permission.hβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class ChildProcessPermission final : public PermissionBase {
1515
voidApply(Environment* env,
1616
const std::vector<std::string>& allow,
1717
PermissionScope scope) override;
18+
voidDrop(Environment* env,
19+
PermissionScope scope,
20+
const std::string_view& param = "") override;
1821
boolis_granted(Environment* env,
1922
PermissionScope perm,
2023
const std::string_view& param = "") constoverride;

β€Žsrc/permission/fs_permission.ccβ€Ž

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,96 @@ void FSPermission::Apply(Environment* env,
154154
}
155155
}
156156

157+
voidFSPermission::Drop(Environment* env,
158+
PermissionScope scope,
159+
const std::string_view& param) {
160+
if (param.empty()) {
161+
// Drop all access for this scope
162+
if (scope == PermissionScope::kFileSystemRead ||
163+
scope == PermissionScope::kFileSystem) {
164+
deny_all_in_ = true;
165+
allow_all_in_ = false;
166+
granted_in_fs_.Clear();
167+
granted_paths_in_.clear();
168+
}
169+
if (scope == PermissionScope::kFileSystemWrite ||
170+
scope == PermissionScope::kFileSystem) {
171+
deny_all_out_ = true;
172+
allow_all_out_ = false;
173+
granted_out_fs_.Clear();
174+
granted_paths_out_.clear();
175+
}
176+
return;
177+
}
178+
179+
// When allowed with *, you can only drop * (no specific paths)
180+
std::string resolved = PathResolve(env, {param});
181+
if (scope == PermissionScope::kFileSystemRead ||
182+
scope == PermissionScope::kFileSystem) {
183+
if (!allow_all_in_) {
184+
RevokeAccess(PermissionScope::kFileSystemRead, resolved);
185+
}
186+
}
187+
if (scope == PermissionScope::kFileSystemWrite ||
188+
scope == PermissionScope::kFileSystem) {
189+
if (!allow_all_out_) {
190+
RevokeAccess(PermissionScope::kFileSystemWrite, resolved);
191+
}
192+
}
193+
}
194+
195+
voidFSPermission::RevokeAccess(PermissionScope perm, const std::string& res) {
196+
const std::string path = WildcardIfDir(res);
197+
if (perm == PermissionScope::kFileSystemRead) {
198+
auto it =
199+
std::find(granted_paths_in_.begin(), granted_paths_in_.end(), path);
200+
if (it != granted_paths_in_.end()) {
201+
granted_paths_in_.erase(it);
202+
RebuildTree(PermissionScope::kFileSystemRead);
203+
}
204+
} elseif (perm == PermissionScope::kFileSystemWrite) {
205+
auto it =
206+
std::find(granted_paths_out_.begin(), granted_paths_out_.end(), path);
207+
if (it != granted_paths_out_.end()) {
208+
granted_paths_out_.erase(it);
209+
RebuildTree(PermissionScope::kFileSystemWrite);
210+
}
211+
}
212+
}
213+
214+
voidFSPermission::RebuildTree(PermissionScope scope) {
215+
if (scope == PermissionScope::kFileSystemRead) {
216+
granted_in_fs_.Clear();
217+
if (granted_paths_in_.empty()) {
218+
deny_all_in_ = true;
219+
} else {
220+
for (constauto& path : granted_paths_in_) {
221+
granted_in_fs_.Insert(path);
222+
}
223+
}
224+
} elseif (scope == PermissionScope::kFileSystemWrite) {
225+
granted_out_fs_.Clear();
226+
if (granted_paths_out_.empty()) {
227+
deny_all_out_ = true;
228+
} else {
229+
for (constauto& path : granted_paths_out_) {
230+
granted_out_fs_.Insert(path);
231+
}
232+
}
233+
}
234+
}
235+
157236
voidFSPermission::GrantAccess(PermissionScope perm, const std::string& res) {
158237
const std::string path = WildcardIfDir(res);
159238
if (perm == PermissionScope::kFileSystemRead &&
160239
!granted_in_fs_.Lookup(path)) {
161240
granted_in_fs_.Insert(path);
241+
granted_paths_in_.push_back(path);
162242
deny_all_in_ = false;
163243
} elseif (perm == PermissionScope::kFileSystemWrite &&
164244
!granted_out_fs_.Lookup(path)) {
165245
granted_out_fs_.Insert(path);
246+
granted_paths_out_.push_back(path);
166247
deny_all_out_ = false;
167248
}
168249
}
@@ -196,6 +277,16 @@ FSPermission::RadixTree::~RadixTree() {
196277
FreeRecursivelyNode(root_node_);
197278
}
198279

280+
voidFSPermission::RadixTree::Clear() {
281+
for (auto& c : root_node_->children) {
282+
FreeRecursivelyNode(c.second);
283+
}
284+
root_node_->children.clear();
285+
delete root_node_->wildcard_child;
286+
root_node_->wildcard_child = nullptr;
287+
root_node_->is_leaf = false;
288+
}
289+
199290
boolFSPermission::RadixTree::Lookup(const std::string_view& s,
200291
bool when_empty_return) const {
201292
FSPermission::RadixTree::Node* current_node = root_node_;

β€Žsrc/permission/fs_permission.hβ€Ž

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class FSPermission final : public PermissionBase {
1818
voidApply(Environment* env,
1919
const std::vector<std::string>& allow,
2020
PermissionScope scope) override;
21+
voidDrop(Environment* env,
22+
PermissionScope scope,
23+
const std::string_view& param = "") override;
2124
boolis_granted(Environment* env,
2225
PermissionScope perm,
2326
const std::string_view& param) constoverride;
@@ -142,6 +145,7 @@ class FSPermission final : public PermissionBase {
142145
RadixTree();
143146
~RadixTree();
144147
voidInsert(const std::string& s);
148+
voidClear();
145149
boolLookup(const std::string_view& s) const { returnLookup(s, false); }
146150
boolLookup(const std::string_view& s, bool when_empty_return) const;
147151

@@ -151,10 +155,15 @@ class FSPermission final : public PermissionBase {
151155

152156
private:
153157
voidGrantAccess(PermissionScope scope, const std::string& param);
158+
voidRevokeAccess(PermissionScope scope, const std::string& param);
159+
voidRebuildTree(PermissionScope scope);
154160
// fs granted on startup
155161
RadixTree granted_in_fs_;
156162
RadixTree granted_out_fs_;
157163

164+
std::vector<std::string> granted_paths_in_;
165+
std::vector<std::string> granted_paths_out_;
166+
158167
bool deny_all_in_ = true;
159168
bool deny_all_out_ = true;
160169

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit d4cafce

Browse files
RafaelGSSaduh95
authored andcommitted
lib,permission: add permission.drop
Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com> PR-URL: #62672 Backport-PR-URL: #65354 Refs: #62223 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent cd1eb3e commit d4cafce

30 files changed

Lines changed: 848 additions & 2 deletions

β€Ždoc/api/permissions.mdβ€Ž

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ flag. For WASI, use the [`--allow-wasi`][] flag.
7474

7575
When enabling the Permission Model through the [`--permission`][]
7676
flag a new property `permission` is added to the `process` object.
77-
This property contains one function:
77+
This property contains the following functions:
7878

7979
##### `permission.has(scope[, reference])`
8080

@@ -88,6 +88,40 @@ process.permission.has('fs.read'); // true
8888
process.permission.has('fs.read', '/home/rafaelgss/protected-folder'); // false
8989
```
9090

91+
##### `permission.drop(scope[, reference])`
92+
93+
API call to drop permissions at runtime. This operation is **irreversible**.
94+
95+
When called without a reference, the entire scope is dropped. When called
96+
with a reference, only the permission for that specific resource is revoked.
97+
Dropping a permission only affects future access checks. It does not close or
98+
revoke access to resources that are already open, such as file descriptors,
99+
child processes, or worker threads. Applications are responsible for closing
100+
or terminating those resources when they are no longer needed.
101+
102+
You can only drop the exact resource that was explicitly granted. The
103+
reference passed to `drop()` must match the original grant. If a permission
104+
was granted using a wildcard (`*`), only the entire scope can be dropped
105+
(by calling `drop()` without a reference). If a directory was granted
106+
(e.g. `--allow-fs-read=/my/folder`), you cannot drop individual files
107+
inside it - you must drop the same directory that was originally granted.
108+
109+
```js
110+
constfs=require('node:fs');
111+
112+
// Read config at startup while we still have permission
113+
constconfig=fs.readFileSync('/etc/myapp/config.json', 'utf8');
114+
115+
// Drop read access to /etc/myapp after initialization
116+
process.permission.drop('fs.read', '/etc/myapp');
117+
118+
// This will now throw ERR_ACCESS_DENIED
119+
process.permission.has('fs.read', '/etc/myapp/config.json'); // false
120+
121+
// Drop child process permission entirely
122+
process.permission.drop('child');
123+
```
124+
91125
#### File System Permissions
92126

93127
The Permission Model, by default, restricts access to the file system through the `node:fs` module.

β€Ždoc/api/process.mdβ€Ž

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3189,6 +3189,64 @@ process.permission.has('fs.read', './README.md');
31893189
process.permission.has('fs.read');
31903190
```
31913191
3192+
### `process.permission.drop(scope[, reference])`
3193+
3194+
<!-- YAML
3195+
added: REPLACEME
3196+
-->
3197+
3198+
> Stability: 1.1 - Active Development
3199+
3200+
* `scope` {string}
3201+
* `reference` {string}
3202+
3203+
Drops the specified permission from the current process. This operation is
3204+
**irreversible** β€” once a permission is dropped, it cannot be restored through
3205+
any Node.js API.
3206+
3207+
If no reference is provided, the entire scope is dropped. For example,
3208+
`process.permission.drop('fs.read')` will revoke ALL file system read
3209+
permissions.
3210+
3211+
When a reference is provided, only the permission for that specific resource
3212+
is dropped. For example, `process.permission.drop('fs.read', '/etc/myapp')`
3213+
will revoke read access to that directory while keeping other read
3214+
permissions intact.
3215+
3216+
**Important:** You can only drop the exact resource that was explicitly
3217+
granted. The reference passed to `drop()` must match the original grant:
3218+
3219+
* If a permission was granted using a wildcard (`*`), such as
3220+
`--allow-fs-read=*`, individual paths cannot be dropped - only the entire
3221+
scope can be dropped (by calling `drop()` without a reference).
3222+
* If a directory was granted (e.g. `--allow-fs-read=/my/folder`), you cannot
3223+
drop access to individual files inside it. You must drop the same directory
3224+
that was granted. Any remaining grants continue to apply.
3225+
3226+
The available scopes are the same as [`process.permission.has()`][]:
3227+
3228+
* `fs` - All File System (drops both read and write)
3229+
* `fs.read` - File System read operations
3230+
* `fs.write` - File System write operations
3231+
* `child` - Child process spawning operations
3232+
* `worker` - Worker thread spawning operation
3233+
* `inspector` - Inspector operations
3234+
* `wasi` - WASI operations
3235+
* `addon` - Native addon operations
3236+
3237+
```js
3238+
constfs=require('node:fs');
3239+
3240+
// Read configuration during startup
3241+
constconfig=fs.readFileSync('/etc/myapp/config.json', 'utf8');
3242+
3243+
// Drop read access to the config directory after initialization
3244+
process.permission.drop('fs.read', '/etc/myapp');
3245+
3246+
// This will now throw ERR_ACCESS_DENIED
3247+
fs.readFileSync('/etc/myapp/config.json');
3248+
```
3249+
31923250
## `process.pid`
31933251
31943252
<!-- YAML
@@ -4598,6 +4656,7 @@ cases:
45984656
[`process.hrtime()`]: #processhrtimetime
45994657
[`process.hrtime.bigint()`]: #processhrtimebigint
46004658
[`process.kill()`]: #processkillpid-signal
4659+
[`process.permission.has()`]: #processpermissionhasscope-reference
46014660
[`process.setUncaughtExceptionCaptureCallback()`]: #processsetuncaughtexceptioncapturecallbackfn
46024661
[`promise.catch()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
46034662
[`queueMicrotask()`]: globals.md#queuemicrotaskcallback

β€Žlib/internal/process/permission.jsβ€Ž

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ module.exports = ObjectFreeze({
3333

3434
returnpermission.has(scope,reference);
3535
},
36+
drop(scope,reference){
37+
validateString(scope,'scope');
38+
if(reference!=null){
39+
if(isBuffer(reference)){
40+
validateBuffer(reference,'reference');
41+
}else{
42+
validateString(reference,'reference');
43+
}
44+
}
45+
46+
permission.drop(scope,reference);
47+
},
3648
availableFlags(){
3749
return[
3850
'--allow-fs-read',

β€Žlib/internal/process/pre_execution.jsβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ function initializePermission() {
641641
};
642642
// Guarantee path module isn't monkey-patched to bypass permission model
643643
ObjectFreeze(require('path'));
644-
const{ has }=require('internal/process/permission');
644+
const{ has, drop}=require('internal/process/permission');
645645
constwarnFlags=[
646646
'--allow-addons',
647647
'--allow-child-process',
@@ -679,6 +679,7 @@ function initializePermission() {
679679
configurable: false,
680680
value: {
681681
has,
682+
drop,
682683
},
683684
});
684685
}else{

β€Žsrc/permission/addon_permission.ccβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ void AddonPermission::Apply(Environment* env,
1414
deny_all_ = true;
1515
}
1616

17+
voidAddonPermission::Drop(Environment* env,
18+
PermissionScope scope,
19+
const std::string_view& param) {
20+
deny_all_ = true;
21+
}
22+
1723
boolAddonPermission::is_granted(Environment* env,
1824
PermissionScope perm,
1925
const std::string_view& param) const {

β€Žsrc/permission/addon_permission.hβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class AddonPermission final : public PermissionBase {
1515
voidApply(Environment* env,
1616
const std::vector<std::string>& allow,
1717
PermissionScope scope) override;
18+
voidDrop(Environment* env,
19+
PermissionScope scope,
20+
const std::string_view& param = "") override;
1821
boolis_granted(Environment* env,
1922
PermissionScope perm,
2023
const std::string_view& param = "") constoverride;

β€Žsrc/permission/child_process_permission.ccβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ void ChildProcessPermission::Apply(Environment* env,
1515
deny_all_ = true;
1616
}
1717

18+
voidChildProcessPermission::Drop(Environment* env,
19+
PermissionScope scope,
20+
const std::string_view& param) {
21+
deny_all_ = true;
22+
}
23+
1824
boolChildProcessPermission::is_granted(Environment* env,
1925
PermissionScope perm,
2026
const std::string_view& param) const {

β€Žsrc/permission/child_process_permission.hβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class ChildProcessPermission final : public PermissionBase {
1515
voidApply(Environment* env,
1616
const std::vector<std::string>& allow,
1717
PermissionScope scope) override;
18+
voidDrop(Environment* env,
19+
PermissionScope scope,
20+
const std::string_view& param = "") override;
1821
boolis_granted(Environment* env,
1922
PermissionScope perm,
2023
const std::string_view& param = "") constoverride;

β€Žsrc/permission/fs_permission.ccβ€Ž

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,96 @@ void FSPermission::Apply(Environment* env,
154154
}
155155
}
156156

157+
voidFSPermission::Drop(Environment* env,
158+
PermissionScope scope,
159+
const std::string_view& param) {
160+
if (param.empty()) {
161+
// Drop all access for this scope
162+
if (scope == PermissionScope::kFileSystemRead ||
163+
scope == PermissionScope::kFileSystem) {
164+
deny_all_in_ = true;
165+
allow_all_in_ = false;
166+
granted_in_fs_.Clear();
167+
granted_paths_in_.clear();
168+
}
169+
if (scope == PermissionScope::kFileSystemWrite ||
170+
scope == PermissionScope::kFileSystem) {
171+
deny_all_out_ = true;
172+
allow_all_out_ = false;
173+
granted_out_fs_.Clear();
174+
granted_paths_out_.clear();
175+
}
176+
return;
177+
}
178+
179+
// When allowed with *, you can only drop * (no specific paths)
180+
std::string resolved = PathResolve(env, {param});
181+
if (scope == PermissionScope::kFileSystemRead ||
182+
scope == PermissionScope::kFileSystem) {
183+
if (!allow_all_in_) {
184+
RevokeAccess(PermissionScope::kFileSystemRead, resolved);
185+
}
186+
}
187+
if (scope == PermissionScope::kFileSystemWrite ||
188+
scope == PermissionScope::kFileSystem) {
189+
if (!allow_all_out_) {
190+
RevokeAccess(PermissionScope::kFileSystemWrite, resolved);
191+
}
192+
}
193+
}
194+
195+
voidFSPermission::RevokeAccess(PermissionScope perm, const std::string& res) {
196+
const std::string path = WildcardIfDir(res);
197+
if (perm == PermissionScope::kFileSystemRead) {
198+
auto it =
199+
std::find(granted_paths_in_.begin(), granted_paths_in_.end(), path);
200+
if (it != granted_paths_in_.end()) {
201+
granted_paths_in_.erase(it);
202+
RebuildTree(PermissionScope::kFileSystemRead);
203+
}
204+
} elseif (perm == PermissionScope::kFileSystemWrite) {
205+
auto it =
206+
std::find(granted_paths_out_.begin(), granted_paths_out_.end(), path);
207+
if (it != granted_paths_out_.end()) {
208+
granted_paths_out_.erase(it);
209+
RebuildTree(PermissionScope::kFileSystemWrite);
210+
}
211+
}
212+
}
213+
214+
voidFSPermission::RebuildTree(PermissionScope scope) {
215+
if (scope == PermissionScope::kFileSystemRead) {
216+
granted_in_fs_.Clear();
217+
if (granted_paths_in_.empty()) {
218+
deny_all_in_ = true;
219+
} else {
220+
for (constauto& path : granted_paths_in_) {
221+
granted_in_fs_.Insert(path);
222+
}
223+
}
224+
} elseif (scope == PermissionScope::kFileSystemWrite) {
225+
granted_out_fs_.Clear();
226+
if (granted_paths_out_.empty()) {
227+
deny_all_out_ = true;
228+
} else {
229+
for (constauto& path : granted_paths_out_) {
230+
granted_out_fs_.Insert(path);
231+
}
232+
}
233+
}
234+
}
235+
157236
voidFSPermission::GrantAccess(PermissionScope perm, const std::string& res) {
158237
const std::string path = WildcardIfDir(res);
159238
if (perm == PermissionScope::kFileSystemRead &&
160239
!granted_in_fs_.Lookup(path)) {
161240
granted_in_fs_.Insert(path);
241+
granted_paths_in_.push_back(path);
162242
deny_all_in_ = false;
163243
} elseif (perm == PermissionScope::kFileSystemWrite &&
164244
!granted_out_fs_.Lookup(path)) {
165245
granted_out_fs_.Insert(path);
246+
granted_paths_out_.push_back(path);
166247
deny_all_out_ = false;
167248
}
168249
}
@@ -196,6 +277,16 @@ FSPermission::RadixTree::~RadixTree() {
196277
FreeRecursivelyNode(root_node_);
197278
}
198279

280+
voidFSPermission::RadixTree::Clear() {
281+
for (auto& c : root_node_->children) {
282+
FreeRecursivelyNode(c.second);
283+
}
284+
root_node_->children.clear();
285+
delete root_node_->wildcard_child;
286+
root_node_->wildcard_child = nullptr;
287+
root_node_->is_leaf = false;
288+
}
289+
199290
boolFSPermission::RadixTree::Lookup(const std::string_view& s,
200291
bool when_empty_return) const {
201292
FSPermission::RadixTree::Node* current_node = root_node_;

β€Žsrc/permission/fs_permission.hβ€Ž

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class FSPermission final : public PermissionBase {
1818
voidApply(Environment* env,
1919
const std::vector<std::string>& allow,
2020
PermissionScope scope) override;
21+
voidDrop(Environment* env,
22+
PermissionScope scope,
23+
const std::string_view& param = "") override;
2124
boolis_granted(Environment* env,
2225
PermissionScope perm,
2326
const std::string_view& param) constoverride;
@@ -142,6 +145,7 @@ class FSPermission final : public PermissionBase {
142145
RadixTree();
143146
~RadixTree();
144147
voidInsert(const std::string& s);
148+
voidClear();
145149
boolLookup(const std::string_view& s) const { returnLookup(s, false); }
146150
boolLookup(const std::string_view& s, bool when_empty_return) const;
147151

@@ -151,10 +155,15 @@ class FSPermission final : public PermissionBase {
151155

152156
private:
153157
voidGrantAccess(PermissionScope scope, const std::string& param);
158+
voidRevokeAccess(PermissionScope scope, const std::string& param);
159+
voidRebuildTree(PermissionScope scope);
154160
// fs granted on startup
155161
RadixTree granted_in_fs_;
156162
RadixTree granted_out_fs_;
157163

164+
std::vector<std::string> granted_paths_in_;
165+
std::vector<std::string> granted_paths_out_;
166+
158167
bool deny_all_in_ = true;
159168
bool deny_all_out_ = true;
160169

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Commit d4cafce

Browse files
RafaelGSSaduh95
authored andcommitted
lib,permission: add permission.drop
Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com> PR-URL: #62672 Backport-PR-URL: #65354 Refs: #62223 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent cd1eb3e commit d4cafce

30 files changed

Lines changed: 848 additions & 2 deletions

β€Ždoc/api/permissions.mdβ€Ž

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ flag. For WASI, use the [`--allow-wasi`][] flag.
7474

7575
When enabling the Permission Model through the [`--permission`][]
7676
flag a new property `permission` is added to the `process` object.
77-
This property contains one function:
77+
This property contains the following functions:
7878

7979
##### `permission.has(scope[, reference])`
8080

@@ -88,6 +88,40 @@ process.permission.has('fs.read'); // true
8888
process.permission.has('fs.read', '/home/rafaelgss/protected-folder'); // false
8989
```
9090

91+
##### `permission.drop(scope[, reference])`
92+
93+
API call to drop permissions at runtime. This operation is **irreversible**.
94+
95+
When called without a reference, the entire scope is dropped. When called
96+
with a reference, only the permission for that specific resource is revoked.
97+
Dropping a permission only affects future access checks. It does not close or
98+
revoke access to resources that are already open, such as file descriptors,
99+
child processes, or worker threads. Applications are responsible for closing
100+
or terminating those resources when they are no longer needed.
101+
102+
You can only drop the exact resource that was explicitly granted. The
103+
reference passed to `drop()` must match the original grant. If a permission
104+
was granted using a wildcard (`*`), only the entire scope can be dropped
105+
(by calling `drop()` without a reference). If a directory was granted
106+
(e.g. `--allow-fs-read=/my/folder`), you cannot drop individual files
107+
inside it - you must drop the same directory that was originally granted.
108+
109+
```js
110+
constfs=require('node:fs');
111+
112+
// Read config at startup while we still have permission
113+
constconfig=fs.readFileSync('/etc/myapp/config.json', 'utf8');
114+
115+
// Drop read access to /etc/myapp after initialization
116+
process.permission.drop('fs.read', '/etc/myapp');
117+
118+
// This will now throw ERR_ACCESS_DENIED
119+
process.permission.has('fs.read', '/etc/myapp/config.json'); // false
120+
121+
// Drop child process permission entirely
122+
process.permission.drop('child');
123+
```
124+
91125
#### File System Permissions
92126

93127
The Permission Model, by default, restricts access to the file system through the `node:fs` module.

β€Ždoc/api/process.mdβ€Ž

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3189,6 +3189,64 @@ process.permission.has('fs.read', './README.md');
31893189
process.permission.has('fs.read');
31903190
```
31913191
3192+
### `process.permission.drop(scope[, reference])`
3193+
3194+
<!-- YAML
3195+
added: REPLACEME
3196+
-->
3197+
3198+
> Stability: 1.1 - Active Development
3199+
3200+
* `scope` {string}
3201+
* `reference` {string}
3202+
3203+
Drops the specified permission from the current process. This operation is
3204+
**irreversible** β€” once a permission is dropped, it cannot be restored through
3205+
any Node.js API.
3206+
3207+
If no reference is provided, the entire scope is dropped. For example,
3208+
`process.permission.drop('fs.read')` will revoke ALL file system read
3209+
permissions.
3210+
3211+
When a reference is provided, only the permission for that specific resource
3212+
is dropped. For example, `process.permission.drop('fs.read', '/etc/myapp')`
3213+
will revoke read access to that directory while keeping other read
3214+
permissions intact.
3215+
3216+
**Important:** You can only drop the exact resource that was explicitly
3217+
granted. The reference passed to `drop()` must match the original grant:
3218+
3219+
* If a permission was granted using a wildcard (`*`), such as
3220+
`--allow-fs-read=*`, individual paths cannot be dropped - only the entire
3221+
scope can be dropped (by calling `drop()` without a reference).
3222+
* If a directory was granted (e.g. `--allow-fs-read=/my/folder`), you cannot
3223+
drop access to individual files inside it. You must drop the same directory
3224+
that was granted. Any remaining grants continue to apply.
3225+
3226+
The available scopes are the same as [`process.permission.has()`][]:
3227+
3228+
* `fs` - All File System (drops both read and write)
3229+
* `fs.read` - File System read operations
3230+
* `fs.write` - File System write operations
3231+
* `child` - Child process spawning operations
3232+
* `worker` - Worker thread spawning operation
3233+
* `inspector` - Inspector operations
3234+
* `wasi` - WASI operations
3235+
* `addon` - Native addon operations
3236+
3237+
```js
3238+
constfs=require('node:fs');
3239+
3240+
// Read configuration during startup
3241+
constconfig=fs.readFileSync('/etc/myapp/config.json', 'utf8');
3242+
3243+
// Drop read access to the config directory after initialization
3244+
process.permission.drop('fs.read', '/etc/myapp');
3245+
3246+
// This will now throw ERR_ACCESS_DENIED
3247+
fs.readFileSync('/etc/myapp/config.json');
3248+
```
3249+
31923250
## `process.pid`
31933251
31943252
<!-- YAML
@@ -4598,6 +4656,7 @@ cases:
45984656
[`process.hrtime()`]: #processhrtimetime
45994657
[`process.hrtime.bigint()`]: #processhrtimebigint
46004658
[`process.kill()`]: #processkillpid-signal
4659+
[`process.permission.has()`]: #processpermissionhasscope-reference
46014660
[`process.setUncaughtExceptionCaptureCallback()`]: #processsetuncaughtexceptioncapturecallbackfn
46024661
[`promise.catch()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
46034662
[`queueMicrotask()`]: globals.md#queuemicrotaskcallback

β€Žlib/internal/process/permission.jsβ€Ž

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ module.exports = ObjectFreeze({
3333

3434
returnpermission.has(scope,reference);
3535
},
36+
drop(scope,reference){
37+
validateString(scope,'scope');
38+
if(reference!=null){
39+
if(isBuffer(reference)){
40+
validateBuffer(reference,'reference');
41+
}else{
42+
validateString(reference,'reference');
43+
}
44+
}
45+
46+
permission.drop(scope,reference);
47+
},
3648
availableFlags(){
3749
return[
3850
'--allow-fs-read',

β€Žlib/internal/process/pre_execution.jsβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ function initializePermission() {
641641
};
642642
// Guarantee path module isn't monkey-patched to bypass permission model
643643
ObjectFreeze(require('path'));
644-
const{ has }=require('internal/process/permission');
644+
const{ has, drop}=require('internal/process/permission');
645645
constwarnFlags=[
646646
'--allow-addons',
647647
'--allow-child-process',
@@ -679,6 +679,7 @@ function initializePermission() {
679679
configurable: false,
680680
value: {
681681
has,
682+
drop,
682683
},
683684
});
684685
}else{

β€Žsrc/permission/addon_permission.ccβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ void AddonPermission::Apply(Environment* env,
1414
deny_all_ = true;
1515
}
1616

17+
voidAddonPermission::Drop(Environment* env,
18+
PermissionScope scope,
19+
const std::string_view& param) {
20+
deny_all_ = true;
21+
}
22+
1723
boolAddonPermission::is_granted(Environment* env,
1824
PermissionScope perm,
1925
const std::string_view& param) const {

β€Žsrc/permission/addon_permission.hβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class AddonPermission final : public PermissionBase {
1515
voidApply(Environment* env,
1616
const std::vector<std::string>& allow,
1717
PermissionScope scope) override;
18+
voidDrop(Environment* env,
19+
PermissionScope scope,
20+
const std::string_view& param = "") override;
1821
boolis_granted(Environment* env,
1922
PermissionScope perm,
2023
const std::string_view& param = "") constoverride;

β€Žsrc/permission/child_process_permission.ccβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ void ChildProcessPermission::Apply(Environment* env,
1515
deny_all_ = true;
1616
}
1717

18+
voidChildProcessPermission::Drop(Environment* env,
19+
PermissionScope scope,
20+
const std::string_view& param) {
21+
deny_all_ = true;
22+
}
23+
1824
boolChildProcessPermission::is_granted(Environment* env,
1925
PermissionScope perm,
2026
const std::string_view& param) const {

β€Žsrc/permission/child_process_permission.hβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class ChildProcessPermission final : public PermissionBase {
1515
voidApply(Environment* env,
1616
const std::vector<std::string>& allow,
1717
PermissionScope scope) override;
18+
voidDrop(Environment* env,
19+
PermissionScope scope,
20+
const std::string_view& param = "") override;
1821
boolis_granted(Environment* env,
1922
PermissionScope perm,
2023
const std::string_view& param = "") constoverride;

β€Žsrc/permission/fs_permission.ccβ€Ž

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,96 @@ void FSPermission::Apply(Environment* env,
154154
}
155155
}
156156

157+
voidFSPermission::Drop(Environment* env,
158+
PermissionScope scope,
159+
const std::string_view& param) {
160+
if (param.empty()) {
161+
// Drop all access for this scope
162+
if (scope == PermissionScope::kFileSystemRead ||
163+
scope == PermissionScope::kFileSystem) {
164+
deny_all_in_ = true;
165+
allow_all_in_ = false;
166+
granted_in_fs_.Clear();
167+
granted_paths_in_.clear();
168+
}
169+
if (scope == PermissionScope::kFileSystemWrite ||
170+
scope == PermissionScope::kFileSystem) {
171+
deny_all_out_ = true;
172+
allow_all_out_ = false;
173+
granted_out_fs_.Clear();
174+
granted_paths_out_.clear();
175+
}
176+
return;
177+
}
178+
179+
// When allowed with *, you can only drop * (no specific paths)
180+
std::string resolved = PathResolve(env, {param});
181+
if (scope == PermissionScope::kFileSystemRead ||
182+
scope == PermissionScope::kFileSystem) {
183+
if (!allow_all_in_) {
184+
RevokeAccess(PermissionScope::kFileSystemRead, resolved);
185+
}
186+
}
187+
if (scope == PermissionScope::kFileSystemWrite ||
188+
scope == PermissionScope::kFileSystem) {
189+
if (!allow_all_out_) {
190+
RevokeAccess(PermissionScope::kFileSystemWrite, resolved);
191+
}
192+
}
193+
}
194+
195+
voidFSPermission::RevokeAccess(PermissionScope perm, const std::string& res) {
196+
const std::string path = WildcardIfDir(res);
197+
if (perm == PermissionScope::kFileSystemRead) {
198+
auto it =
199+
std::find(granted_paths_in_.begin(), granted_paths_in_.end(), path);
200+
if (it != granted_paths_in_.end()) {
201+
granted_paths_in_.erase(it);
202+
RebuildTree(PermissionScope::kFileSystemRead);
203+
}
204+
} elseif (perm == PermissionScope::kFileSystemWrite) {
205+
auto it =
206+
std::find(granted_paths_out_.begin(), granted_paths_out_.end(), path);
207+
if (it != granted_paths_out_.end()) {
208+
granted_paths_out_.erase(it);
209+
RebuildTree(PermissionScope::kFileSystemWrite);
210+
}
211+
}
212+
}
213+
214+
voidFSPermission::RebuildTree(PermissionScope scope) {
215+
if (scope == PermissionScope::kFileSystemRead) {
216+
granted_in_fs_.Clear();
217+
if (granted_paths_in_.empty()) {
218+
deny_all_in_ = true;
219+
} else {
220+
for (constauto& path : granted_paths_in_) {
221+
granted_in_fs_.Insert(path);
222+
}
223+
}
224+
} elseif (scope == PermissionScope::kFileSystemWrite) {
225+
granted_out_fs_.Clear();
226+
if (granted_paths_out_.empty()) {
227+
deny_all_out_ = true;
228+
} else {
229+
for (constauto& path : granted_paths_out_) {
230+
granted_out_fs_.Insert(path);
231+
}
232+
}
233+
}
234+
}
235+
157236
voidFSPermission::GrantAccess(PermissionScope perm, const std::string& res) {
158237
const std::string path = WildcardIfDir(res);
159238
if (perm == PermissionScope::kFileSystemRead &&
160239
!granted_in_fs_.Lookup(path)) {
161240
granted_in_fs_.Insert(path);
241+
granted_paths_in_.push_back(path);
162242
deny_all_in_ = false;
163243
} elseif (perm == PermissionScope::kFileSystemWrite &&
164244
!granted_out_fs_.Lookup(path)) {
165245
granted_out_fs_.Insert(path);
246+
granted_paths_out_.push_back(path);
166247
deny_all_out_ = false;
167248
}
168249
}
@@ -196,6 +277,16 @@ FSPermission::RadixTree::~RadixTree() {
196277
FreeRecursivelyNode(root_node_);
197278
}
198279

280+
voidFSPermission::RadixTree::Clear() {
281+
for (auto& c : root_node_->children) {
282+
FreeRecursivelyNode(c.second);
283+
}
284+
root_node_->children.clear();
285+
delete root_node_->wildcard_child;
286+
root_node_->wildcard_child = nullptr;
287+
root_node_->is_leaf = false;
288+
}
289+
199290
boolFSPermission::RadixTree::Lookup(const std::string_view& s,
200291
bool when_empty_return) const {
201292
FSPermission::RadixTree::Node* current_node = root_node_;

β€Žsrc/permission/fs_permission.hβ€Ž

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class FSPermission final : public PermissionBase {
1818
voidApply(Environment* env,
1919
const std::vector<std::string>& allow,
2020
PermissionScope scope) override;
21+
voidDrop(Environment* env,
22+
PermissionScope scope,
23+
const std::string_view& param = "") override;
2124
boolis_granted(Environment* env,
2225
PermissionScope perm,
2326
const std::string_view& param) constoverride;
@@ -142,6 +145,7 @@ class FSPermission final : public PermissionBase {
142145
RadixTree();
143146
~RadixTree();
144147
voidInsert(const std::string& s);
148+
voidClear();
145149
boolLookup(const std::string_view& s) const { returnLookup(s, false); }
146150
boolLookup(const std::string_view& s, bool when_empty_return) const;
147151

@@ -151,10 +155,15 @@ class FSPermission final : public PermissionBase {
151155

152156
private:
153157
voidGrantAccess(PermissionScope scope, const std::string& param);
158+
voidRevokeAccess(PermissionScope scope, const std::string& param);
159+
voidRebuildTree(PermissionScope scope);
154160
// fs granted on startup
155161
RadixTree granted_in_fs_;
156162
RadixTree granted_out_fs_;
157163

164+
std::vector<std::string> granted_paths_in_;
165+
std::vector<std::string> granted_paths_out_;
166+
158167
bool deny_all_in_ = true;
159168
bool deny_all_out_ = true;
160169

0 commit comments

Comments
Β (0)