GeezerVison is showing github.com — permission: reject empty reference in drop() · nodejs/node@ebde393
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/permissions.md +2Lines changed: 2 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -123,6 +123,8 @@ API call to drop permissions at runtime. This operation is **irreversible**. | 123 | 123 |
| 124 | 124 | When called without a reference, the entire scope is dropped. When called | 125 | 125 | with a reference, only the permission for that specific resource is revoked. | | 126 | +The reference must not be an empty string or an empty `Uint8Array`; otherwise | | 127 | +an `ERR_INVALID_ARG_VALUE` error is thrown. | 126 | 128 | Dropping a permission only affects future access checks. It does not close or | 127 | 129 | revoke access to resources that are already open, such as file descriptors, | 128 | 130 | network sockets, child processes, or worker threads. Applications are | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -3278,7 +3278,8 @@ permissions. | 3278 | 3278 | When a reference is provided, only the permission for that specific resource | 3279 | 3279 | is dropped. For example, `process.permission.drop('fs.read', '/etc/myapp')` | 3280 | 3280 | will revoke read access to that directory while keeping other read | 3281 | | -permissions intact. | | 3281 | +permissions intact. The `reference` must not be an empty string or an empty | | 3282 | +`Uint8Array`; otherwise an `ERR_INVALID_ARG_VALUE` error is thrown. | 3282 | 3283 |
| 3283 | 3284 | **Important:** You can only drop the exact resource that was explicitly | 3284 | 3285 | granted. The reference passed to `drop()` must match the original grant: | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/process/permission.js +11-1Lines changed: 11 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,6 +10,11 @@ const permission = internalBinding('permission'); | 10 | 10 | const { validateString } = require('internal/validators'); | 11 | 11 | const { isUint8Array } = require('internal/util/types'); | 12 | 12 | const { isURL, fileURLToPath } = require('internal/url'); | | 13 | +const { | | 14 | + codes: { | | 15 | + ERR_INVALID_ARG_VALUE, | | 16 | + }, | | 17 | +} = require('internal/errors'); | 13 | 18 |
| 14 | 19 | let _permission; | 15 | 20 | let _audit; | @@ -65,7 +70,12 @@ module.exports = ObjectFreeze({ | 65 | 70 | }, | 66 | 71 | drop(scope, reference) { | 67 | 72 | validateString(scope, 'scope'); | 68 | | - permission.drop(scope, normalizeReference(scope, reference, 'reference')); | | 73 | + reference = normalizeReference(scope, reference, 'reference'); | | 74 | + // An empty reference would otherwise drop the whole scope. | | 75 | + if (reference != null && reference.length === 0) { | | 76 | + throw new ERR_INVALID_ARG_VALUE('reference', reference, 'must not be empty'); | | 77 | + } | | 78 | + permission.drop(scope, reference); | 69 | 79 | }, | 70 | 80 | availableFlags() { | 71 | 81 | if (_ffi === undefined) { | |
Collapse file Copy file name to clipboardExpand all lines: src/permission/permission.cc +15-12Lines changed: 15 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -116,23 +116,26 @@ static void Drop(const FunctionCallbackInfo<Value>& args) { | 116 | 116 | "owns the process state"); | 117 | 117 | } | 118 | 118 |
| 119 | | - if (args.Length() > 1 && !args[1]->IsUndefined()) { | | 119 | + if (args.Length() > 1 && !args[1]->IsNullOrUndefined()) { | 120 | 120 | // BufferValue copies raw bytes out of a Buffer/TypedArray as-is instead | 121 | 121 | // of forcing a (potentially lossy) UTF-8 string conversion, since paths | 122 | 122 | // are not guaranteed to be valid UTF-8. | 123 | 123 | BufferValue resource(env->isolate(), args[1]); | 124 | | - if (resource.length() > 0) { | 125 | | - // A wildcard would drop more than one variable at once. Dropping the | 126 | | - // whole scope, without a reference, is the only way to do that. | 127 | | - if (scope == PermissionScope::kEnv && | 128 | | - resource.ToStringView().back() == '*') { | 129 | | - return THROW_ERR_INVALID_ARG_VALUE( | 130 | | - env, | 131 | | - "Wildcards are not supported when dropping environment variables"); | 132 | | - } | 133 | | - env->permission()->Drop(env, scope, resource.ToStringView()); | 134 | | - 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"); | 135 | 128 | } | | 129 | + // A wildcard would drop more than one variable at once. Dropping the | | 130 | + // whole scope, without a reference, is the only way to do that. | | 131 | + if (scope == PermissionScope::kEnv && | | 132 | + resource.ToStringView().back() == '*') { | | 133 | + return THROW_ERR_INVALID_ARG_VALUE( | | 134 | + env, | | 135 | + "Wildcards are not supported when dropping environment variables"); | | 136 | + } | | 137 | + env->permission()->Drop(env, scope, resource.ToStringView()); | | 138 | + return; | 136 | 139 | } | 137 | 140 |
| 138 | 141 | env->permission()->Drop(env, scope, ""); | |
Collapse file Copy file name to clipboard +34Lines changed: 34 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 Copy file name to clipboardExpand all lines: test/parallel/test-permission-drop-errors.js +19Lines changed: 19 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,6 +39,25 @@ const assert = require('assert'); | 39 | 39 | })); | 40 | 40 | } | 41 | 41 |
| | 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 | + | 42 | 61 | { | 43 | 62 | process.permission.drop('invalid-scope'); | 44 | 63 | process.permission.drop('invalid-scope', '/tmp'); | |
|