Files
node/lib/internal/process/permission.js
T
Seungmin Nam c4336d90c0 permission: support URL and Uint8Array as has()/drop() reference
normalizeReference() adds URL and Uint8Array support to has()/drop();
the existing string/Buffer behavior is unchanged. BufferValue replaces
Utf8Value so a Buffer/TypedArray reference is copied as raw bytes
instead of a lossy UTF-8 conversion.

Signed-off-by: seungmin Nam <nhjbest22@g.skku.edu>
PR-URL: https://github.com/nodejs/node/pull/65492
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2026-08-31 14:59:47 +00:00

93 lines
2.4 KiB
JavaScript

'use strict';
const {
ArrayPrototypePush,
ObjectFreeze,
StringPrototypeStartsWith,
} = primordials;
const permission = internalBinding('permission');
const { validateString } = require('internal/validators');
const { isUint8Array } = require('internal/util/types');
const { isURL, fileURLToPath } = require('internal/url');
let _permission;
let _audit;
let _ffi;
function normalizeReference(scope, reference, name) {
if (reference == null) {
return reference;
}
if (isURL(reference)) {
// Reference is only meaningful for fs.* scopes (FSPermission does
// per-path checks); every other scope ignores it (BooleanPermission),
// so only fs.* scopes require a file: URL.
if (StringPrototypeStartsWith(scope, 'fs')) {
return fileURLToPath(reference);
}
return reference.href;
}
if (isUint8Array(reference)) {
// Passed through as-is: the native binding copies the raw bytes
// directly instead of forcing a (potentially lossy) UTF-8 string
// conversion, since paths are not guaranteed to be valid UTF-8.
return reference;
}
validateString(reference, name);
return reference;
}
module.exports = ObjectFreeze({
__proto__: null,
isEnabled() {
if (_permission === undefined) {
const { getOptionValue } = require('internal/options');
_permission = getOptionValue('--permission') || getOptionValue('--permission-audit');
}
return _permission;
},
isAuditMode() {
if (_audit === undefined) {
const { getOptionValue } = require('internal/options');
_audit = getOptionValue('--permission-audit');
}
return _audit;
},
has(scope, reference) {
validateString(scope, 'scope');
return permission.has(scope, normalizeReference(scope, reference, 'reference'));
},
drop(scope, reference) {
validateString(scope, 'scope');
permission.drop(scope, normalizeReference(scope, reference, 'reference'));
},
availableFlags() {
if (_ffi === undefined) {
const { getOptionValue } = require('internal/options');
_ffi = getOptionValue('--experimental-ffi');
}
const flags = [
'--allow-fs-read',
'--allow-fs-write',
'--allow-addons',
'--allow-child-process',
'--allow-net',
'--allow-inspector',
'--allow-wasi',
'--allow-worker',
'--allow-openssl-store',
];
if (_ffi) {
ArrayPrototypePush(flags, '--allow-ffi');
}
return flags;
},
});