Skip to content

Commit 3c17f16

Browse files
codebytereTrott
authored andcommitted
src: add buildflag to force context-aware addons
PR-URL: #29631 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 5058c7f commit 3c17f16

File tree

6 files changed

+29
-0
lines changed

6 files changed

+29
-0
lines changed

doc/api/cli.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,13 @@ added: v6.0.0
411411

412412
Silence all process warnings (including deprecations).
413413

414+
### `--force-context-aware`
415+
<!-- YAML
416+
added: REPLACEME
417+
-->
418+
419+
Disable loading non-context-aware native addons.
420+
414421
### `--openssl-config=file`
415422
<!-- YAML
416423
added: v6.9.0
@@ -980,6 +987,7 @@ Node.js options that are allowed are:
980987
* `--experimental-report`
981988
* `--experimental-vm-modules`
982989
* `--experimental-wasm-modules`
990+
* `--force-context-aware`
983991
* `--force-fips`
984992
* `--frozen-intrinsics`
985993
* `--heapsnapshot-signal`

doc/api/errors.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,6 +1611,11 @@ OpenSSL crypto support.
16111611
An attempt was made to use features that require [ICU][], but Node.js was not
16121612
compiled with ICU support.
16131613

1614+
<a id="ERR_NON_CONTEXT_AWARE_DISABLED"></a>
1615+
### ERR_NON_CONTEXT_AWARE_DISABLED
1616+
1617+
A non-context-aware native addon was loaded in a process that disallows them.
1618+
16141619
<a id="ERR_OUT_OF_RANGE"></a>
16151620
### ERR_OUT_OF_RANGE
16161621

src/node_binding.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "node_binding.h"
2+
#include "node_errors.h"
23
#include <atomic>
34
#include "env-inl.h"
45
#include "node_native_module_env.h"
@@ -462,6 +463,13 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
462463
}
463464

464465
if (mp != nullptr) {
466+
if (mp->nm_context_register_func == nullptr) {
467+
if (env->options()->force_context_aware) {
468+
dlib->Close();
469+
THROW_ERR_NON_CONTEXT_AWARE_DISABLED(env);
470+
return false;
471+
}
472+
}
465473
mp->nm_dso_handle = dlib->handle_;
466474
dlib->SaveInGlobalHandleMap(mp);
467475
} else {

src/node_errors.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ void PrintErrorString(const char* format, ...);
5252
V(ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST, TypeError) \
5353
V(ERR_MISSING_PASSPHRASE, TypeError) \
5454
V(ERR_MISSING_PLATFORM_FOR_WORKER, Error) \
55+
V(ERR_NON_CONTEXT_AWARE_DISABLED, Error) \
5556
V(ERR_MODULE_NOT_FOUND, Error) \
5657
V(ERR_OUT_OF_RANGE, RangeError) \
5758
V(ERR_SCRIPT_EXECUTION_INTERRUPTED, Error) \
@@ -96,6 +97,8 @@ void PrintErrorString(const char* format, ...);
9697
V(ERR_MISSING_PLATFORM_FOR_WORKER, \
9798
"The V8 platform used by this instance of Node does not support " \
9899
"creating Workers") \
100+
V(ERR_NON_CONTEXT_AWARE_DISABLED, \
101+
"Loading non context-aware native modules has been disabled") \
99102
V(ERR_SCRIPT_EXECUTION_INTERRUPTED, \
100103
"Script execution was interrupted by `SIGINT`") \
101104
V(ERR_TRANSFERRING_EXTERNALIZED_SHAREDARRAYBUFFER, \

src/node_options.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
385385
"silence all process warnings",
386386
&EnvironmentOptions::no_warnings,
387387
kAllowedInEnvironment);
388+
AddOption("--force-context-aware",
389+
"disable loading non-context-aware addons",
390+
&EnvironmentOptions::force_context_aware,
391+
kAllowedInEnvironment);
388392
AddOption("--pending-deprecation",
389393
"emit pending deprecation warnings",
390394
&EnvironmentOptions::pending_deprecation,

src/node_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class EnvironmentOptions : public Options {
116116
bool no_deprecation = false;
117117
bool no_force_async_hooks_checks = false;
118118
bool no_warnings = false;
119+
bool force_context_aware = false;
119120
bool pending_deprecation = false;
120121
bool preserve_symlinks = false;
121122
bool preserve_symlinks_main = false;

0 commit comments

Comments
 (0)