From 676c02611d7f1557b9082b07316e9ed4b1c9673a Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Fri, 1 Mar 2019 07:24:18 +0100 Subject: [PATCH] spec: add missing checks to "validates process APIs access in sandboxed renderer" (#17129) --- spec/api-browser-window-spec.js | 36 ++++++++++++++++++------- spec/fixtures/module/preload-sandbox.js | 19 +++++++++++++ 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index 65a1f675e6c2..8b83847041d5 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -10,7 +10,6 @@ const qs = require('querystring') const http = require('http') const { closeWindow } = require('./window-helpers') const { emittedOnce } = require('./events-helpers') -const { resolveGetters } = require('./assert-helpers') const { ipcRenderer, remote } = require('electron') const { app, ipcMain, BrowserWindow, BrowserView, protocol, session, screen, webContents } = remote @@ -1923,14 +1922,30 @@ describe('BrowserWindow module', () => { it('validates process APIs access in sandboxed renderer', (done) => { ipcMain.once('answer', function (event, test) { - assert.strictEqual(test.arch, remote.process.arch) - assert.strictEqual(test.platform, remote.process.platform) - assert.deepStrictEqual(...resolveGetters(test.env, remote.process.env)) - assert.strictEqual(test.execPath, remote.process.helperExecPath) - assert.strictEqual(test.sandboxed, true) - assert.strictEqual(test.type, 'renderer') - assert.strictEqual(test.version, remote.process.version) - assert.deepStrictEqual(test.versions, remote.process.versions) + expect(test.hasCrash).to.be.true() + expect(test.hasHang).to.be.true() + expect(test.heapStatistics).to.be.an('object') + expect(test.processMemoryInfo).to.be.an('object') + expect(test.systemVersion).to.be.a('string') + expect(test.cpuUsage).to.be.an('object') + expect(test.ioCounters).to.be.an('object') + expect(test.arch).to.equal(remote.process.arch) + expect(test.platform).to.equal(remote.process.platform) + expect(test.env).to.deep.equal(remote.process.env) + expect(test.execPath).to.equal(remote.process.helperExecPath) + expect(test.sandboxed).to.be.true() + expect(test.type).to.equal('renderer') + expect(test.version).to.equal(remote.process.version) + expect(test.versions).to.deep.equal(remote.process.versions) + + if (process.platform === 'linux' && test.osSandbox) { + expect(test.creationTime).to.be.null() + expect(test.systemMemoryInfo).to.be.null() + } else { + expect(test.creationTime).to.be.a('number') + expect(test.systemMemoryInfo).to.be.an('object') + } + done() }) remote.process.env.sandboxmain = 'foo' @@ -1942,6 +1957,9 @@ describe('BrowserWindow module', () => { preload } }) + w.webContents.once('preload-error', (event, preloadPath, error) => { + done(error) + }) w.loadFile(path.join(fixtures, 'api', 'preload.html')) }) diff --git a/spec/fixtures/module/preload-sandbox.js b/spec/fixtures/module/preload-sandbox.js index 92cba0b3eb6b..ae73d25bab67 100644 --- a/spec/fixtures/module/preload-sandbox.js +++ b/spec/fixtures/module/preload-sandbox.js @@ -4,11 +4,30 @@ window.ipcRenderer = ipcRenderer window.setImmediate = setImmediate window.require = require + + function invoke (code) { + try { + return code() + } catch { + return null + } + } + if (location.protocol === 'file:') { window.test = 'preload' window.process = process if (process.env.sandboxmain) { window.test = { + osSandbox: !process.argv.includes('--no-sandbox'), + hasCrash: typeof process.crash === 'function', + hasHang: typeof process.hang === 'function', + creationTime: invoke(() => process.getCreationTime()), + heapStatistics: invoke(() => process.getHeapStatistics()), + processMemoryInfo: invoke(() => process.getProcessMemoryInfo()), + systemMemoryInfo: invoke(() => process.getSystemMemoryInfo()), + systemVersion: invoke(() => process.getSystemVersion()), + cpuUsage: invoke(() => process.getCPUUsage()), + ioCounters: invoke(() => process.getIOCounters()), env: process.env, execPath: process.execPath, pid: process.pid,