From 7fcc00f13793cf86c51cd71c00b63e9b76fd4de9 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 28 Jun 2017 08:33:06 -0700 Subject: [PATCH] Add spec for app.enableMixedSandbox() --- atom/browser/api/atom_api_app.cc | 26 ++++++------- spec/api-app-spec.js | 42 +++++++++++++++++++++ spec/api-browser-window-spec.js | 24 ------------ spec/fixtures/api/mixed-sandbox-app/main.js | 4 ++ 4 files changed, 58 insertions(+), 38 deletions(-) diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index 16f714fe325..af7b64667fe 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -1034,23 +1034,21 @@ void App::EnableMixedSandbox(mate::Arguments* args) { "before app is ready"); return; } + auto command_line = base::CommandLine::ForCurrentProcess(); if (command_line->HasSwitch(::switches::kNoSandbox)) { - // Remove the --no-sandbox switch - using StringType = base::CommandLine::StringType; - using StringVector = base::CommandLine::StringVector; - using CharType = base::CommandLine::CharType; - auto argv = command_line->argv(); - StringVector modified_command_line; - #if defined(OS_WIN) - const CharType* kNoSandboxArg = L"--no-sandbox"; - #else - const CharType* kNoSandboxArg = "--no-sandbox"; - #endif +#if defined(OS_WIN) + const base::CommandLine::CharType* noSandboxArg = L"--no-sandbox"; +#else + const base::CommandLine::CharType* noSandboxArg = "--no-sandbox"; +#endif - for (const StringType& arg : argv) { - if (arg.compare(kNoSandboxArg) != 0) - modified_command_line.push_back(arg); + // Remove the --no-sandbox switch + base::CommandLine::StringVector modified_command_line; + for (auto& arg : command_line->argv()) { + if (arg.compare(noSandboxArg) != 0) { + modified_command_line.push_back(arg); + } } command_line->InitFromArgv(modified_command_line); } diff --git a/spec/api-app-spec.js b/spec/api-app-spec.js index cda0fbea210..4b04f5b026d 100644 --- a/spec/api-app-spec.js +++ b/spec/api-app-spec.js @@ -566,4 +566,46 @@ describe('app module', function () { assert.equal(typeof features.gpu_compositing, 'string') }) }) + + describe('mixed sandbox option', function () { + let appProcess + + afterEach(function () { + if (appProcess != null) { + appProcess.kill() + } + }) + + describe('when app.enableMixedSandbox() is called', () => { + it('adds --enable-sandbox to render processes created with sandbox: true', (done) => { + const appPath = path.join(__dirname, 'fixtures', 'api', 'mixed-sandbox-app') + appProcess = ChildProcess.spawn(remote.process.execPath, [appPath], {stdio: ['ignore', 'ipc', 'ignore']}) + appProcess.once('message', (argv) => { + assert.equal(argv.sandbox.includes('--enable-sandbox'), true) + assert.equal(argv.sandbox.includes('--no-sandbox'), false) + + assert.equal(argv.noSandbox.includes('--enable-sandbox'), false) + assert.equal(argv.noSandbox.includes('--no-sandbox'), true) + + done() + }) + }) + }) + + describe('when the app is launched with --enable-mixed-sandbox', () => { + it('adds --enable-sandbox to render processes created with sandbox: true', (done) => { + const appPath = path.join(__dirname, 'fixtures', 'api', 'mixed-sandbox-app') + appProcess = ChildProcess.spawn(remote.process.execPath, [appPath, '--enable-mixed-sandbox'], {stdio: ['ignore', 'ipc', 'ignore']}) + appProcess.once('message', (argv) => { + assert.equal(argv.sandbox.includes('--enable-sandbox'), true) + assert.equal(argv.sandbox.includes('--no-sandbox'), false) + + assert.equal(argv.noSandbox.includes('--enable-sandbox'), false) + assert.equal(argv.noSandbox.includes('--no-sandbox'), true) + + done() + }) + }) + }) + }) }) diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index e419c3327cf..379577571fa 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -1260,30 +1260,6 @@ describe('BrowserWindow module', function () { }) }) - describe('mixed sandbox option', function () { - let appProcess - - afterEach(function () { - if (appProcess != null) { - appProcess.kill() - } - }) - - it('adds --enable-sandbox to render processes created with sandbox: true', (done) => { - const appPath = path.join(__dirname, 'fixtures', 'api', 'mixed-sandbox-app') - appProcess = ChildProcess.spawn(remote.process.execPath, [appPath, '--enable-mixed-sandbox'], {stdio: ['ignore', 'ipc', 'ignore']}) - appProcess.once('message', (argv) => { - assert.equal(argv.sandbox.includes('--enable-sandbox'), true) - assert.equal(argv.sandbox.includes('--no-sandbox'), false) - - assert.equal(argv.noSandbox.includes('--enable-sandbox'), false) - assert.equal(argv.noSandbox.includes('--no-sandbox'), true) - - done() - }) - }) - }) - describe('nativeWindowOpen option', () => { beforeEach(() => { w.destroy() diff --git a/spec/fixtures/api/mixed-sandbox-app/main.js b/spec/fixtures/api/mixed-sandbox-app/main.js index 7823208b72e..a06823318d8 100644 --- a/spec/fixtures/api/mixed-sandbox-app/main.js +++ b/spec/fixtures/api/mixed-sandbox-app/main.js @@ -5,6 +5,10 @@ process.on('uncaughtException', () => { app.exit(1) }) +if (!process.argv.includes('--enable-mixed-sandbox')) { + app.enableMixedSandbox() +} + let sandboxWindow let noSandboxWindow