Browse Source

Preload doesn't load in sandboxed render if preload path contains special chars (#12037) (#12643)

* Adding missing headers

* adding ut

* Removing the file path exists check

* fixing test

* exposing window.require in UT

(cherry picked from commit 2f4fd3324b43960bae1e4dbe7c1840560cd98d98)
Alexey Kuzmin 7 years ago
parent
commit
597c8964f6

+ 4 - 3
atom/renderer/atom_sandboxed_renderer_client.cc

@@ -15,6 +15,7 @@
 #include "atom/renderer/api/atom_api_renderer_ipc.h"
 #include "atom/renderer/atom_render_frame_observer.h"
 #include "base/command_line.h"
+#include "base/files/file_path.h"
 #include "chrome/renderer/printing/print_web_view_helper.h"
 #include "content/public/renderer/render_frame.h"
 #include "native_mate/dictionary.h"
@@ -152,9 +153,9 @@ void AtomSandboxedRendererClient::DidCreateScriptContext(
     return;
 
   base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
-  std::string preload_script = command_line->GetSwitchValueASCII(
+  base::FilePath preload_script_path = command_line->GetSwitchValuePath(
       switches::kPreloadScript);
-  if (preload_script.empty())
+  if (preload_script_path.empty())
     return;
 
   auto isolate = context->GetIsolate();
@@ -180,7 +181,7 @@ void AtomSandboxedRendererClient::DidCreateScriptContext(
   AddRenderBindings(isolate, binding);
   v8::Local<v8::Value> args[] = {
     binding,
-    mate::ConvertToV8(isolate, preload_script)
+    mate::ConvertToV8(isolate, preload_script_path.value())
   };
   // Execute the function with proper arguments
   ignore_result(func->Call(context, v8::Null(isolate), 2, args));

+ 18 - 1
spec/api-browser-window-spec.js

@@ -1197,7 +1197,24 @@ describe('BrowserWindow module', () => {
         w.loadURL('file://' + path.join(fixtures, 'api', 'preload.html'))
       })
 
-      it('exposes "exit" event to preload script', (done) => {
+      it('exposes ipcRenderer to preload script (path has special chars)', function (done) {
+        const preloadSpecialChars = path.join(fixtures, 'module', 'preload-sandboxæø åü.js')
+        ipcMain.once('answer', function (event, test) {
+          assert.equal(test, 'preload')
+          done()
+        })
+        w.destroy()
+        w = new BrowserWindow({
+          show: false,
+          webPreferences: {
+            sandbox: true,
+            preload: preloadSpecialChars
+          }
+        })
+        w.loadURL('file://' + path.join(fixtures, 'api', 'preload.html'))
+      })
+
+      it('exposes "exit" event to preload script', function (done) {
         w.destroy()
         w = new BrowserWindow({
           show: false,

+ 6 - 0
spec/fixtures/module/preload-sandboxæø åü.js

@@ -0,0 +1,6 @@
+(function () {
+  window.require = require
+  if (location.protocol === 'file:') {
+    window.test = 'preload'
+  }
+})()