Skip to content

Commit 6bfe8f4

Browse files
codebytereBridgeAR
authored andcommitted
src: add buildflag to force context-aware addons
PR-URL: #29631 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 3d88b76 commit 6bfe8f4

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
@@ -439,6 +439,13 @@ added: v6.0.0
439439

440440
Silence all process warnings (including deprecations).
441441

442+
### `--force-context-aware`
443+
<!-- YAML
444+
added: REPLACEME
445+
-->
446+
447+
Disable loading non-context-aware native addons.
448+
442449
### `--openssl-config=file`
443450
<!-- YAML
444451
added: v6.9.0
@@ -1009,6 +1016,7 @@ Node.js options that are allowed are:
10091016
* `--experimental-report`
10101017
* `--experimental-vm-modules`
10111018
* `--experimental-wasm-modules`
1019+
* `--force-context-aware`
10121020
* `--force-fips`
10131021
* `--frozen-intrinsics`
10141022
* `--heapsnapshot-signal`

doc/api/errors.md

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

1612+
<a id="ERR_NON_CONTEXT_AWARE_DISABLED"></a>
1613+
### ERR_NON_CONTEXT_AWARE_DISABLED
1614+
1615+
A non-context-aware native addon was loaded in a process that disallows them.
1616+
16121617
<a id="ERR_OUT_OF_RANGE"></a>
16131618
### ERR_OUT_OF_RANGE
16141619

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"
@@ -463,6 +464,13 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
463464
}
464465

465466
if (mp != nullptr) {
467+
if (mp->nm_context_register_func == nullptr) {
468+
if (env->options()->force_context_aware) {
469+
dlib->Close();
470+
THROW_ERR_NON_CONTEXT_AWARE_DISABLED(env);
471+
return false;
472+
}
473+
}
466474
mp->nm_dso_handle = dlib->handle_;
467475
dlib->SaveInGlobalHandleMap(mp);
468476
} 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
@@ -400,6 +400,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
400400
"silence all process warnings",
401401
&EnvironmentOptions::no_warnings,
402402
kAllowedInEnvironment);
403+
AddOption("--force-context-aware",
404+
"disable loading non-context-aware addons",
405+
&EnvironmentOptions::force_context_aware,
406+
kAllowedInEnvironment);
403407
AddOption("--pending-deprecation",
404408
"emit pending deprecation warnings",
405409
&EnvironmentOptions::pending_deprecation,

src/node_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ class EnvironmentOptions : public Options {
118118
bool no_deprecation = false;
119119
bool no_force_async_hooks_checks = false;
120120
bool no_warnings = false;
121+
bool force_context_aware = false;
121122
bool pending_deprecation = false;
122123
bool preserve_symlinks = false;
123124
bool preserve_symlinks_main = false;

0 commit comments

Comments
 (0)