Browse Source

chore: update force/warn context aware patch (#29011)

* chore: update force/warn context aware patch

* chore: update patches
Shelley Vohr 4 years ago
parent
commit
a264fc891d

+ 2 - 1
patches/node/.patches

@@ -14,7 +14,8 @@ fixme_comment_trace_event_macro.patch
 fix_key_gen_apis_are_not_available_in_boringssl.patch
 build_modify_js2c_py_to_allow_injection_of_original-fs_and_custom_embedder_js.patch
 refactor_allow_embedder_overriding_of_internal_fs_calls.patch
-chore_prevent_warn_non_context-aware_native_modules_being_loaded.patch
+chore_allow_the_node_entrypoint_to_be_a_builtin_module.patch
+chore_add_context_to_context_aware_module_prevention.patch
 chore_read_nobrowserglobals_from_global_not_process.patch
 enable_31_bit_smis_on_64bit_arch_and_ptr_compression.patch
 fix_use_crypto_impls_for_compat.patch

+ 35 - 0
patches/node/chore_add_context_to_context_aware_module_prevention.patch

@@ -0,0 +1,35 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: Samuel Attard <[email protected]>
+Date: Wed, 22 May 2019 13:34:18 -0700
+Subject: chore: add context to context aware module prevention
+
+This patch adds extra context to why we prevent non-context-aware Node.js
+modules from being used in the renderer process. This should be upstreamed as
+a customizable error message.
+
+diff --git a/src/node_binding.cc b/src/node_binding.cc
+index ca5a01f925a2ae69ba4295d82316e546f45c60cd..a0f6730de75b9b1dc58e2cec5ed64f9619162a2b 100644
+--- a/src/node_binding.cc
++++ b/src/node_binding.cc
+@@ -3,6 +3,7 @@
+ #include <atomic>
+ #include "env-inl.h"
+ #include "node_native_module_env.h"
++#include "node_process.h"
+ #include "util.h"
+ 
+ #if HAVE_OPENSSL
+@@ -463,7 +464,12 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
+       if (mp->nm_context_register_func == nullptr) {
+         if (env->force_context_aware()) {
+           dlib->Close();
+-          THROW_ERR_NON_CONTEXT_AWARE_DISABLED(env);
++          char errmsg[1024];
++          snprintf(errmsg,
++                   sizeof(errmsg),
++                   "Loading non-context-aware native module in renderer: '%s', but app.allowRendererProcessReuse is true. See https://github.com/electron/electron/issues/18397.",
++                   *filename);
++          env->ThrowError(errmsg);
+           return false;
+         }
+       }

+ 48 - 0
patches/node/chore_allow_the_node_entrypoint_to_be_a_builtin_module.patch

@@ -0,0 +1,48 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: Samuel Attard <[email protected]>
+Date: Wed, 22 May 2019 13:34:18 -0700
+Subject: chore: allow the node entrypoint to be a builtin_module
+
+This allows embedders to tell Node.js what the first "real" file is  when
+they use themselves as the entry point. We should try to upstream some form
+of this.
+
+diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js
+index af69dfc9e71b54a9d7eda0daa498396008aba610..f52e8a09f689bafcfdca39e71822ff3cbf898349 100644
+--- a/lib/internal/bootstrap/pre_execution.js
++++ b/lib/internal/bootstrap/pre_execution.js
+@@ -95,10 +95,12 @@ function patchProcessObject(expandArgv1) {
+   if (expandArgv1 && process.argv[1] &&
+       !StringPrototypeStartsWith(process.argv[1], '-')) {
+     // Expand process.argv[1] into a full path.
+-    const path = require('path');
+-    try {
+-      process.argv[1] = path.resolve(process.argv[1]);
+-    } catch {}
++    if (!process.argv[1] || !process.argv[1].startsWith('electron/js2c')) {
++      const path = require('path');
++      try {
++        process.argv[1] = path.resolve(process.argv[1]);
++      } catch {}
++    }
+   }
+ 
+   // TODO(joyeecheung): most of these should be deprecated and removed,
+diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js
+index 541c18d8032c5247433367f9e04c946cf203d475..b04e3f1fc56a86abe18f924e4d9b7fb517aea74d 100644
+--- a/lib/internal/modules/cjs/loader.js
++++ b/lib/internal/modules/cjs/loader.js
+@@ -1037,6 +1037,13 @@ Module.prototype._compile = function(content, filename) {
+   if (getOptionValue('--inspect-brk') && process._eval == null) {
+     if (!resolvedArgv) {
+       // We enter the repl if we're not given a filename argument.
++      // process._firstFileName is used by Embedders to tell node what
++      // the first "real" file is when they use themselves as the entry
++      // point
++      if (process._firstFileName) {
++        resolvedArgv = process._firstFileName
++        delete process._firstFileName
++      } else
+       if (process.argv[1]) {
+         try {
+           resolvedArgv = Module._resolveFilename(process.argv[1], null, false);

+ 0 - 141
patches/node/chore_prevent_warn_non_context-aware_native_modules_being_loaded.patch

@@ -1,141 +0,0 @@
-From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
-From: Samuel Attard <[email protected]>
-Date: Wed, 22 May 2019 13:34:18 -0700
-Subject: chore: prevent / warn non context-aware native modules being loaded
-
-This should be updated to take advantage of https://github.com/nodejs/node/pull/29631
-once we stop warning and begin to unilaterally prevent non-context aware modules
-from being loaded.
-
-diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js
-index af69dfc9e71b54a9d7eda0daa498396008aba610..f52e8a09f689bafcfdca39e71822ff3cbf898349 100644
---- a/lib/internal/bootstrap/pre_execution.js
-+++ b/lib/internal/bootstrap/pre_execution.js
-@@ -95,10 +95,12 @@ function patchProcessObject(expandArgv1) {
-   if (expandArgv1 && process.argv[1] &&
-       !StringPrototypeStartsWith(process.argv[1], '-')) {
-     // Expand process.argv[1] into a full path.
--    const path = require('path');
--    try {
--      process.argv[1] = path.resolve(process.argv[1]);
--    } catch {}
-+    if (!process.argv[1] || !process.argv[1].startsWith('electron/js2c')) {
-+      const path = require('path');
-+      try {
-+        process.argv[1] = path.resolve(process.argv[1]);
-+      } catch {}
-+    }
-   }
- 
-   // TODO(joyeecheung): most of these should be deprecated and removed,
-diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js
-index 541c18d8032c5247433367f9e04c946cf203d475..b04e3f1fc56a86abe18f924e4d9b7fb517aea74d 100644
---- a/lib/internal/modules/cjs/loader.js
-+++ b/lib/internal/modules/cjs/loader.js
-@@ -1037,6 +1037,13 @@ Module.prototype._compile = function(content, filename) {
-   if (getOptionValue('--inspect-brk') && process._eval == null) {
-     if (!resolvedArgv) {
-       // We enter the repl if we're not given a filename argument.
-+      // process._firstFileName is used by Embedders to tell node what
-+      // the first "real" file is when they use themselves as the entry
-+      // point
-+      if (process._firstFileName) {
-+        resolvedArgv = process._firstFileName
-+        delete process._firstFileName
-+      } else
-       if (process.argv[1]) {
-         try {
-           resolvedArgv = Module._resolveFilename(process.argv[1], null, false);
-diff --git a/src/env-inl.h b/src/env-inl.h
-index c1853f81b68bd22d20fb99877f4c500a384e9545..623e9d4e429c03bb267539a318166f3ef3b9c501 100644
---- a/src/env-inl.h
-+++ b/src/env-inl.h
-@@ -550,6 +550,14 @@ inline bool Environment::force_context_aware() const {
-   return options_->force_context_aware;
- }
- 
-+inline void Environment::set_warn_context_aware(bool value) {
-+  options_->warn_context_aware = value;
-+}
-+
-+inline bool Environment::warn_context_aware() const {
-+  return options_->warn_context_aware;
-+}
-+
- inline void Environment::set_abort_on_uncaught_exception(bool value) {
-   options_->abort_on_uncaught_exception = value;
- }
-diff --git a/src/env.h b/src/env.h
-index f89365a1aa7ffacbb423e01a68f484992751f76f..38d17f4e18aa38fde2c2f59a9816c8fb0f65fd51 100644
---- a/src/env.h
-+++ b/src/env.h
-@@ -949,6 +949,8 @@ class Environment : public MemoryRetainer {
- 
-   inline void set_force_context_aware(bool value);
-   inline bool force_context_aware() const;
-+  inline void set_warn_context_aware(bool value);
-+  inline bool warn_context_aware() const;
- 
-   // This stores whether the --abort-on-uncaught-exception flag was passed
-   // to Node.
-diff --git a/src/node_binding.cc b/src/node_binding.cc
-index ca5a01f925a2ae69ba4295d82316e546f45c60cd..f85ab2332a1c0267bd50d5f979d90e55c84a2990 100644
---- a/src/node_binding.cc
-+++ b/src/node_binding.cc
-@@ -3,6 +3,7 @@
- #include <atomic>
- #include "env-inl.h"
- #include "node_native_module_env.h"
-+#include "node_process.h"
- #include "util.h"
- 
- #if HAVE_OPENSSL
-@@ -463,8 +464,20 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
-       if (mp->nm_context_register_func == nullptr) {
-         if (env->force_context_aware()) {
-           dlib->Close();
--          THROW_ERR_NON_CONTEXT_AWARE_DISABLED(env);
-+          char errmsg[1024];
-+          snprintf(errmsg,
-+                   sizeof(errmsg),
-+                   "Loading non-context-aware native module in renderer: '%s', but app.allowRendererProcessReuse is true. See https://github.com/electron/electron/issues/18397.",
-+                   *filename);
-+          env->ThrowError(errmsg);
-           return false;
-+        } else if (env->warn_context_aware()) {
-+          char errmsg[1024];
-+          snprintf(errmsg,
-+                   sizeof(errmsg),
-+                   "Loading non-context-aware native module in renderer: '%s'. This is deprecated, see https://github.com/electron/electron/issues/18397.",
-+                   *filename);
-+          ProcessEmitWarningGeneric(env, errmsg, "Electron");
-         }
-       }
-       mp->nm_dso_handle = dlib->handle_;
-diff --git a/src/node_options.cc b/src/node_options.cc
-index e7dc220f5c3fcab40c9a669a79b3a41b7e9f8cbf..01143e122387db8220d6e0cd0124ae08e082d0c8 100644
---- a/src/node_options.cc
-+++ b/src/node_options.cc
-@@ -371,6 +371,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
-             "disable loading non-context-aware addons",
-             &EnvironmentOptions::force_context_aware,
-             kAllowedInEnvironment);
-+  AddOption("--warn-context-aware",
-+          "warn when loading non-context-aware addons",
-+          &EnvironmentOptions::warn_context_aware,
-+          kAllowedInEnvironment);
-   AddOption("--pending-deprecation",
-             "emit pending deprecation warnings",
-             &EnvironmentOptions::pending_deprecation,
-diff --git a/src/node_options.h b/src/node_options.h
-index bd63fcb0485c77e24becdf2392c453f3b2c4cdba..2af8f82ec5e6995b9ba340fddbc4943f0374198f 100644
---- a/src/node_options.h
-+++ b/src/node_options.h
-@@ -121,6 +121,7 @@ class EnvironmentOptions : public Options {
-   bool no_force_async_hooks_checks = false;
-   bool no_warnings = false;
-   bool force_context_aware = false;
-+  bool warn_context_aware = false;
-   bool pending_deprecation = false;
-   bool preserve_symlinks = false;
-   bool preserve_symlinks_main = false;

+ 4 - 4
patches/node/fix_allow_preventing_initializeinspector_in_env.patch

@@ -36,10 +36,10 @@ index 53b07052e43a09f29f863ee1b2287bdebe7b7a7f..c08fe4b32d4155badb572f15529f903c
  #endif
  
 diff --git a/src/env-inl.h b/src/env-inl.h
-index 623e9d4e429c03bb267539a318166f3ef3b9c501..52a122a51049238547ff662bed1a10b346f3af00 100644
+index c1853f81b68bd22d20fb99877f4c500a384e9545..578552c0d66f9fd09405d80376d5f97d812018ee 100644
 --- a/src/env-inl.h
 +++ b/src/env-inl.h
-@@ -837,6 +837,10 @@ inline bool Environment::tracks_unmanaged_fds() const {
+@@ -829,6 +829,10 @@ inline bool Environment::tracks_unmanaged_fds() const {
    return flags_ & EnvironmentFlags::kTrackUnmanagedFds;
  }
  
@@ -51,10 +51,10 @@ index 623e9d4e429c03bb267539a318166f3ef3b9c501..52a122a51049238547ff662bed1a10b3
    return emit_filehandle_warning_;
  }
 diff --git a/src/env.h b/src/env.h
-index 38d17f4e18aa38fde2c2f59a9816c8fb0f65fd51..4fe2eb3b7699efcab87c377743a955effbbfd9de 100644
+index f89365a1aa7ffacbb423e01a68f484992751f76f..5e5818eaed3cd4435f1e6b908e95c5953c8743f2 100644
 --- a/src/env.h
 +++ b/src/env.h
-@@ -1022,6 +1022,7 @@ class Environment : public MemoryRetainer {
+@@ -1020,6 +1020,7 @@ class Environment : public MemoryRetainer {
    inline bool owns_process_state() const;
    inline bool owns_inspector() const;
    inline bool tracks_unmanaged_fds() const;