GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — permission: reject empty reference in drop() · nodejs/node@3b8e249

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • doc/api
    • permissions.md
    • process.md
  • lib/internal/process
    • permission.js
  • src/permission
    • permission.cc
  • test/parallel
    • test-permission-drop-env-empty-reference.js
    • test-permission-drop-errors.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/permissions.md‎

Copy file name to clipboardExpand all lines: doc/api/permissions.md
+2Lines changed: 2 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ API call to drop permissions at runtime. This operation is **irreversible**.
118118

119119
When called without a reference, the entire scope is dropped. When called
120120
with a reference, only the permission for that specific resource is revoked.
121+
The reference must not be an empty string or an empty `Uint8Array`; otherwise
122+
an `ERR_INVALID_ARG_VALUE` error is thrown.
121123
Dropping a permission only affects future access checks. It does not close or
122124
revoke access to resources that are already open, such as file descriptors,
123125
network sockets, child processes, or worker threads. Applications are
Collapse file

‎doc/api/process.md‎

Copy file name to clipboardExpand all lines: doc/api/process.md
+2-1Lines changed: 2 additions & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -3231,7 +3231,8 @@ permissions.
32313231
When a reference is provided, only the permission for that specific resource
32323232
is dropped. For example, `process.permission.drop('fs.read', '/etc/myapp')`
32333233
will revoke read access to that directory while keeping other read
3234-
permissions intact.
3234+
permissions intact. The `reference` must not be an empty string or an empty
3235+
`Uint8Array`; otherwise an `ERR_INVALID_ARG_VALUE` error is thrown.
32353236
32363237
**Important:** You can only drop the exact resource that was explicitly
32373238
granted. The reference passed to `drop()` must match the original grant:
Collapse file

‎lib/internal/process/permission.js‎

Copy file name to clipboardExpand all lines: lib/internal/process/permission.js
+11-1Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ const permission = internalBinding('permission');
1010
const { validateString } = require('internal/validators');
1111
const { isUint8Array } = require('internal/util/types');
1212
const { isURL, fileURLToPath } = require('internal/url');
13+
const {
14+
codes: {
15+
ERR_INVALID_ARG_VALUE,
16+
},
17+
} = require('internal/errors');
1318

1419
let _permission;
1520
let _audit;
@@ -63,7 +68,12 @@ module.exports = ObjectFreeze({
6368
},
6469
drop(scope, reference) {
6570
validateString(scope, 'scope');
66-
permission.drop(scope, normalizeReference(scope, reference, 'reference'));
71+
reference = normalizeReference(scope, reference, 'reference');
72+
// An empty reference would otherwise drop the whole scope.
73+
if (reference != null && reference.length === 0) {
74+
throw new ERR_INVALID_ARG_VALUE('reference', reference, 'must not be empty');
75+
}
76+
permission.drop(scope, reference);
6777
},
6878
availableFlags() {
6979
if (_ffi === undefined) {
Collapse file

‎src/permission/permission.cc‎

Copy file name to clipboardExpand all lines: src/permission/permission.cc
+7-4Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,18 @@ static void Drop(const FunctionCallbackInfo<Value>& args) {
116116
"owns the process state");
117117
}
118118

119-
if (args.Length() > 1 && !args[1]->IsUndefined()) {
119+
if (args.Length() > 1 && !args[1]->IsNullOrUndefined()) {
120120
// BufferValue copies raw bytes out of a Buffer/TypedArray as-is instead
121121
// of forcing a (potentially lossy) UTF-8 string conversion, since paths
122122
// are not guaranteed to be valid UTF-8.
123123
BufferValue resource(env->isolate(), args[1]);
124-
if (resource.length() > 0) {
125-
env->permission()->Drop(env, scope, resource.ToStringView());
126-
return;
124+
if (resource.length() == 0) {
125+
// An empty reference must not fall through to dropping the whole scope.
126+
return THROW_ERR_INVALID_ARG_VALUE(
127+
env, "The \"reference\" argument must not be empty");
127128
}
129+
env->permission()->Drop(env, scope, resource.ToStringView());
130+
return;
128131
}
129132

130133
env->permission()->Drop(env, scope, "");
Collapse file

‎test/parallel/test-permission-drop-env-empty-reference.js‎

Copy file name to clipboard
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { isMainThread } = require('worker_threads');
5+
6+
if (!isMainThread) {
7+
common.skip('This test only works on a main thread');
8+
}
9+
10+
const assert = require('assert');
11+
const { spawnSync } = require('child_process');
12+
13+
// An empty reference must not drop every environment variable.
14+
const { status, stdout, stderr } = spawnSync(process.execPath, [
15+
'--permission',
16+
'--allow-env=A,B',
17+
'-e',
18+
`
19+
const assert = require('assert');
20+
assert.throws(() => process.permission.drop('env', ''), {
21+
code: 'ERR_INVALID_ARG_VALUE',
22+
});
23+
assert.throws(() => process.permission.drop('env', new Uint8Array(0)), {
24+
code: 'ERR_INVALID_ARG_VALUE',
25+
});
26+
assert.strictEqual(process.env.A, '1');
27+
assert.strictEqual(process.env.B, '2');
28+
`,
29+
], {
30+
env: { ...process.env, A: '1', B: '2' },
31+
encoding: 'utf8',
32+
});
33+
34+
assert.strictEqual(status, 0, `stdout: ${stdout}\nstderr: ${stderr}`);
Collapse file

‎test/parallel/test-permission-drop-errors.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-permission-drop-errors.js
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,25 @@ const assert = require('assert');
3939
}));
4040
}
4141

42+
{
43+
// An empty reference must not drop the whole scope.
44+
for (const scope of ['fs.read', 'fs.write', 'child', 'env']) {
45+
for (const reference of ['', new Uint8Array(0), Buffer.alloc(0)]) {
46+
assert.throws(() => {
47+
process.permission.drop(scope, reference);
48+
}, common.expectsError({
49+
code: 'ERR_INVALID_ARG_VALUE',
50+
}));
51+
}
52+
}
53+
54+
assert.strictEqual(process.permission.has('fs.read'), true);
55+
assert.strictEqual(process.permission.has('child'), false);
56+
57+
process.permission.drop('child', null);
58+
process.permission.drop('child', undefined);
59+
}
60+
4261
{
4362
process.permission.drop('invalid-scope');
4463
process.permission.drop('invalid-scope', '/tmp');

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.