Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process: use frozen array for process.allowedNodeEnvironmentFlags #37171

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -577,15 +577,15 @@ This feature is not available in [`Worker`][] threads.
added: v10.10.0
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
-->

* {Set}
* {string[]}

The `process.allowedNodeEnvironmentFlags` property is a special,
read-only `Set` of flags allowable within the [`NODE_OPTIONS`][]
read-only `Array` of flags allowable within the [`NODE_OPTIONS`][]
environment variable.

`process.allowedNodeEnvironmentFlags` extends `Set`, but overrides
`Set.prototype.has` to recognize several different possible flag
representations. `process.allowedNodeEnvironmentFlags.has()` will
`process.allowedNodeEnvironmentFlags` is a frozen `Array`, but overrides
`Array.prototype.includes` to recognize several different possible flag
representations. `process.allowedNodeEnvironmentFlags.includes()` will
return `true` in the following cases:

* Flags may omit leading single (`-`) or double (`--`) dashes; e.g.,
Expand Down Expand Up @@ -613,10 +613,6 @@ process.allowedNodeEnvironmentFlags.forEach((flag) => {
});
```

The methods `add()`, `clear()`, and `delete()` of
`process.allowedNodeEnvironmentFlags` do nothing, and will fail
silently.

If Node.js was compiled *without* [`NODE_OPTIONS`][] support (shown in
[`process.config`][]), `process.allowedNodeEnvironmentFlags` will
contain what *would have* been allowable.
Expand Down
99 changes: 49 additions & 50 deletions lib/internal/process/per_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@

const {
ArrayPrototypeEvery,
ArrayPrototypeIncludes,
ArrayPrototypeMap,
ArrayPrototypePush,
ArrayPrototypeSplice,
BigUint64Array,
Float64Array,
FunctionPrototype,
NumberMAX_SAFE_INTEGER,
ObjectDefineProperty,
ObjectFreeze,
ReflectApply,
ReflectGet,
RegExpPrototypeTest,
SafeSet,
StringPrototypeEndsWith,
StringPrototypeReplace,
StringPrototypeSlice,
Expand Down Expand Up @@ -293,55 +294,53 @@ function buildAllowedFlags() {

// Save these for comparison against flags provided to
// process.allowedNodeEnvironmentFlags.has() which lack leading dashes.
const nodeFlags = new SafeSet(ArrayPrototypeMap(allowedNodeEnvironmentFlags,
trimLeadingDashes));

class NodeEnvironmentFlagsSet extends SafeSet {
constructor(...args) {
super(...args);

// The super constructor consumes `add`, but
// disallow any future adds.
ObjectDefineProperty(this, 'add', {
value: () => this
});
}

delete() {
// No-op, `Set` API compatible
return false;
}

clear() {
// No-op
}

has(key) {
// This will return `true` based on various possible
// permutations of a flag, including present/missing leading
// dash(es) and/or underscores-for-dashes.
// Strips any values after `=`, inclusive.
// TODO(addaleax): It might be more flexible to run the option parser
// on a dummy option set and see whether it rejects the argument or
// not.
if (typeof key === 'string') {
key = StringPrototypeReplace(key, replaceUnderscoresRegex, '-');
if (RegExpPrototypeTest(leadingDashesRegex, key)) {
key = StringPrototypeReplace(key, trailingValuesRegex, '');
return super.has(key);
}
return nodeFlags.has(key);
const nodeFlags =
ArrayPrototypeMap(allowedNodeEnvironmentFlags, trimLeadingDashes);

return new Proxy(ObjectFreeze(allowedNodeEnvironmentFlags), {
get(target, key, receiver) {
switch (key) {
case 'add':
// No-op, `Set` API compatible
return () => receiver;

case 'delete':
// No-op, `Set` API compatible
return () => false;

case 'clear':
// No-op, `Set` API compatible
return FunctionPrototype;

case 'has':
case 'includes':
return (key) => {
// This will return `true` based on various possible
// permutations of a flag, including present/missing leading
// dash(es) and/or underscores-for-dashes.
// Strips any values after `=`, inclusive.
// TODO(addaleax): It might be more flexible to run the option
// parser on a dummy option set and see whether it rejects the
// argument or not.
if (typeof key === 'string') {
key = StringPrototypeReplace(key, replaceUnderscoresRegex, '-');
if (RegExpPrototypeTest(leadingDashesRegex, key)) {
key = StringPrototypeReplace(key, trailingValuesRegex, '');
return ArrayPrototypeIncludes(target, key);
}
return ArrayPrototypeIncludes(nodeFlags, key);
}
return false;
};

case 'size':
return target.length;

default:
return ReflectGet(target, key);
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
}
return false;
}
}

ObjectFreeze(NodeEnvironmentFlagsSet.prototype.constructor);
ObjectFreeze(NodeEnvironmentFlagsSet.prototype);

return ObjectFreeze(new NodeEnvironmentFlagsSet(
allowedNodeEnvironmentFlags
));
},
});
}

// Lazy load internal/trace_events_async_hooks only if the async_hooks
Expand Down
48 changes: 42 additions & 6 deletions test/parallel/test-process-env-allowed-flags.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

require('../common');
const common = require('../common');
const assert = require('assert');

// Assert legit flags are allowed, and bogus flags are disallowed
Expand Down Expand Up @@ -62,15 +62,51 @@ const assert = require('assert');
true);

process.allowedNodeEnvironmentFlags.add('foo');
assert.throws(
() => Set.prototype.add.call(process.allowedNodeEnvironmentFlags, 'foo'),
/Set\.prototype\.add called on incompatible receiver/);
assert.strictEqual(process.allowedNodeEnvironmentFlags.has('foo'), false);
process.allowedNodeEnvironmentFlags.forEach((flag) => {
assert.strictEqual(flag === 'foo', false);
});

process.allowedNodeEnvironmentFlags.clear();
assert.strictEqual(process.allowedNodeEnvironmentFlags.size > 0, true);
assert.throws(
() => process.allowedNodeEnvironmentFlags.push('foo'),
/object is not extensible/);
assert.strictEqual(process.allowedNodeEnvironmentFlags.includes('foo'),
false);

const thisArg = {};
process.allowedNodeEnvironmentFlags.forEach(
common.mustCallAtLeast(function(flag, _, arr) {
assert.notStrictEqual(flag, 'foo');
assert.strictEqual(this, thisArg);
assert.strictEqual(arr, process.allowedNodeEnvironmentFlags);
}),
thisArg
);

for (const flag of process.allowedNodeEnvironmentFlags.keys()) {
assert.notStrictEqual(flag, 'foo');
}
for (const flag of process.allowedNodeEnvironmentFlags.values()) {
assert.notStrictEqual(flag, 'foo');
}
for (const flag of process.allowedNodeEnvironmentFlags) {
assert.notStrictEqual(flag, 'foo');
}
for (const [flag] of process.allowedNodeEnvironmentFlags.entries()) {
assert.notStrictEqual(flag, 'foo');
}

const size = process.allowedNodeEnvironmentFlags.size;

assert.strictEqual(process.allowedNodeEnvironmentFlags.length, size);

process.allowedNodeEnvironmentFlags.clear();
assert.strictEqual(process.allowedNodeEnvironmentFlags.size, size);

process.allowedNodeEnvironmentFlags.delete('-r');
assert.strictEqual(process.allowedNodeEnvironmentFlags.size, size);

assert.throws(() => process.allowedNodeEnvironmentFlags.splice(0),
/Cannot delete property/);
assert.strictEqual(process.allowedNodeEnvironmentFlags.size, size);
}