From bb07519df738e2fe215f664cae162fbfadf8a260 Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Tue, 24 Sep 2019 16:43:39 +0200 Subject: [PATCH] feat: add ELECTRON_ENABLE_API_FILTERING_LOGGING environment variable --- docs/api/environment-variables.md | 11 +++++++++ lib/browser/remote/server.ts | 37 ++++++++++++++++++++++------ lib/browser/rpc-server.js | 8 ++++-- lib/renderer/api/desktop-capturer.ts | 10 +++++++- lib/renderer/api/remote.js | 20 ++++++++++----- 5 files changed, 70 insertions(+), 16 deletions(-) diff --git a/docs/api/environment-variables.md b/docs/api/environment-variables.md index d31d769614f94..a01e1c1db73cc 100644 --- a/docs/api/environment-variables.md +++ b/docs/api/environment-variables.md @@ -108,6 +108,17 @@ Prints the stack trace to the console when Electron crashes. This environment variable will not work if the `crashReporter` is started. +### `ELECTRON_ENABLE_API_FILTERING_LOGGING` + +Captures the stack trace when following APIs are being filtered via events: +- `desktopCapturer.getSources()` / `desktop-capturer-get-sources` +- `remote.require()` / `remote-require` +- `remote.getGlobal()` / `remote-get-builtin` +- `remote.getBuiltin()` / `remote-get-global` +- `remote.getCurrentWindow()` / `remote-get-current-window` +- `remote.getCurrentWebContents()` / `remote-get-current-web-contents` +- `remote.getGuestWebContents()` / `remote-get-guest-web-contents` + ### `ELECTRON_DEFAULT_ERROR_MODE` _Windows_ Shows the Windows's crash dialog when Electron crashes. diff --git a/lib/browser/remote/server.ts b/lib/browser/remote/server.ts index 49b807f9535f9..60515ff94745a 100644 --- a/lib/browser/remote/server.ts +++ b/lib/browser/remote/server.ts @@ -392,7 +392,11 @@ handleRemoteCommand('ELECTRON_BROWSER_WRONG_CONTEXT_ERROR', function (event, con removeRemoteListenersAndLogWarning(event.sender, rendererFunctions.get(objectId)) }) -handleRemoteCommand('ELECTRON_BROWSER_REQUIRE', function (event, contextId, moduleName) { +handleRemoteCommand('ELECTRON_BROWSER_REQUIRE', function (event, contextId, moduleName, stack) { + if (stack) { + console.warn(`remote.require('${moduleName}')`, stack) + } + const customEvent = emitCustomEvent(event.sender, 'remote-require', moduleName) if (customEvent.returnValue === undefined) { @@ -406,7 +410,11 @@ handleRemoteCommand('ELECTRON_BROWSER_REQUIRE', function (event, contextId, modu return valueToMeta(event.sender, contextId, customEvent.returnValue) }) -handleRemoteCommand('ELECTRON_BROWSER_GET_BUILTIN', function (event, contextId, moduleName) { +handleRemoteCommand('ELECTRON_BROWSER_GET_BUILTIN', function (event, contextId, moduleName, stack) { + if (stack) { + console.warn(`remote.getBuiltin('${moduleName}')`, stack) + } + const customEvent = emitCustomEvent(event.sender, 'remote-get-builtin', moduleName) if (customEvent.returnValue === undefined) { @@ -420,7 +428,11 @@ handleRemoteCommand('ELECTRON_BROWSER_GET_BUILTIN', function (event, contextId, return valueToMeta(event.sender, contextId, customEvent.returnValue) }) -handleRemoteCommand('ELECTRON_BROWSER_GLOBAL', function (event, contextId, globalName) { +handleRemoteCommand('ELECTRON_BROWSER_GLOBAL', function (event, contextId, globalName, stack) { + if (stack) { + console.warn(`remote.getGlobal('${globalName}')`, stack) + } + const customEvent = emitCustomEvent(event.sender, 'remote-get-global', globalName) if (customEvent.returnValue === undefined) { @@ -434,7 +446,11 @@ handleRemoteCommand('ELECTRON_BROWSER_GLOBAL', function (event, contextId, globa return valueToMeta(event.sender, contextId, customEvent.returnValue) }) -handleRemoteCommand('ELECTRON_BROWSER_CURRENT_WINDOW', function (event, contextId) { +handleRemoteCommand('ELECTRON_BROWSER_CURRENT_WINDOW', function (event, contextId, stack) { + if (stack) { + console.warn('remote.getCurrentWindow()', stack) + } + const customEvent = emitCustomEvent(event.sender, 'remote-get-current-window') if (customEvent.returnValue === undefined) { @@ -448,7 +464,11 @@ handleRemoteCommand('ELECTRON_BROWSER_CURRENT_WINDOW', function (event, contextI return valueToMeta(event.sender, contextId, customEvent.returnValue) }) -handleRemoteCommand('ELECTRON_BROWSER_CURRENT_WEB_CONTENTS', function (event, contextId) { +handleRemoteCommand('ELECTRON_BROWSER_CURRENT_WEB_CONTENTS', function (event, contextId, stack) { + if (stack) { + console.warn('remote.getCurrentWebContents()', stack) + } + const customEvent = emitCustomEvent(event.sender, 'remote-get-current-web-contents') if (customEvent.returnValue === undefined) { @@ -549,9 +569,12 @@ handleRemoteCommand('ELECTRON_BROWSER_CONTEXT_RELEASE', (event, contextId) => { return null }) -handleRemoteCommand('ELECTRON_BROWSER_GUEST_WEB_CONTENTS', function (event, contextId, guestInstanceId) { - const guest = guestViewManager.getGuestForWebContents(guestInstanceId, event.sender) +handleRemoteCommand('ELECTRON_BROWSER_GUEST_WEB_CONTENTS', function (event, contextId, guestInstanceId, stack) { + if (stack) { + console.warn('remote.getGuestForWebContents()', stack) + } + const guest = guestViewManager.getGuestForWebContents(guestInstanceId, event.sender) const customEvent = emitCustomEvent(event.sender, 'remote-get-guest-web-contents', guest) if (customEvent.returnValue === undefined) { diff --git a/lib/browser/rpc-server.js b/lib/browser/rpc-server.js index a56c295e97e9a..83449d96e88f5 100644 --- a/lib/browser/rpc-server.js +++ b/lib/browser/rpc-server.js @@ -63,7 +63,11 @@ ipcMainUtils.handleSync('ELECTRON_BROWSER_CLIPBOARD', function (event, method, . if (features.isDesktopCapturerEnabled()) { const desktopCapturer = require('@electron/internal/browser/desktop-capturer') - ipcMainInternal.handle('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', function (event, ...args) { + ipcMainInternal.handle('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', function (event, options, stack) { + if (stack) { + console.warn('desktopCapturer.getSources()', stack) + } + const customEvent = emitCustomEvent(event.sender, 'desktop-capturer-get-sources') if (customEvent.defaultPrevented) { @@ -71,7 +75,7 @@ if (features.isDesktopCapturerEnabled()) { return [] } - return desktopCapturer.getSources(event, ...args) + return desktopCapturer.getSources(event, options) }) } diff --git a/lib/renderer/api/desktop-capturer.ts b/lib/renderer/api/desktop-capturer.ts index d8de9a35a45a6..b0c87dc79b5e5 100644 --- a/lib/renderer/api/desktop-capturer.ts +++ b/lib/renderer/api/desktop-capturer.ts @@ -7,6 +7,14 @@ function isValid (options: Electron.SourcesOptions) { return Array.isArray(types) } +function getCurrentStack () { + const target = {} + if (process.env.ELECTRON_ENABLE_API_FILTERING_LOGGING) { + Error.captureStackTrace(target) + } + return (target as any).stack +} + export async function getSources (options: Electron.SourcesOptions) { if (!isValid(options)) throw new Error('Invalid options') @@ -21,7 +29,7 @@ export async function getSources (options: Electron.SourcesOptions) { captureScreen, thumbnailSize, fetchWindowIcons - } as ElectronInternal.GetSourcesOptions) + } as ElectronInternal.GetSourcesOptions, getCurrentStack()) return sources.map(source => ({ id: source.id, diff --git a/lib/renderer/api/remote.js b/lib/renderer/api/remote.js index 94c2bcce336ed..94ca1cb0438e2 100644 --- a/lib/renderer/api/remote.js +++ b/lib/renderer/api/remote.js @@ -281,6 +281,14 @@ function handleMessage (channel, handler) { }) } +function getCurrentStack () { + const target = {} + if (process.env.ELECTRON_ENABLE_API_FILTERING_LOGGING) { + Error.captureStackTrace(target) + } + return target.stack +} + // Browser calls a callback in renderer. handleMessage('ELECTRON_RENDERER_CALLBACK', (id, args) => { callbacksRegistry.apply(id, metaToValue(args)) @@ -293,34 +301,34 @@ handleMessage('ELECTRON_RENDERER_RELEASE_CALLBACK', (id) => { exports.require = (module) => { const command = 'ELECTRON_BROWSER_REQUIRE' - const meta = ipcRendererInternal.sendSync(command, contextId, module) + const meta = ipcRendererInternal.sendSync(command, contextId, module, getCurrentStack()) return metaToValue(meta) } // Alias to remote.require('electron').xxx. exports.getBuiltin = (module) => { const command = 'ELECTRON_BROWSER_GET_BUILTIN' - const meta = ipcRendererInternal.sendSync(command, contextId, module) + const meta = ipcRendererInternal.sendSync(command, contextId, module, getCurrentStack()) return metaToValue(meta) } exports.getCurrentWindow = () => { const command = 'ELECTRON_BROWSER_CURRENT_WINDOW' - const meta = ipcRendererInternal.sendSync(command, contextId) + const meta = ipcRendererInternal.sendSync(command, contextId, getCurrentStack()) return metaToValue(meta) } // Get current WebContents object. exports.getCurrentWebContents = () => { const command = 'ELECTRON_BROWSER_CURRENT_WEB_CONTENTS' - const meta = ipcRendererInternal.sendSync(command, contextId) + const meta = ipcRendererInternal.sendSync(command, contextId, getCurrentStack()) return metaToValue(meta) } // Get a global object in browser. exports.getGlobal = (name) => { const command = 'ELECTRON_BROWSER_GLOBAL' - const meta = ipcRendererInternal.sendSync(command, contextId, name) + const meta = ipcRendererInternal.sendSync(command, contextId, name, getCurrentStack()) return metaToValue(meta) } @@ -339,7 +347,7 @@ exports.createFunctionWithReturnValue = (returnValue) => { // Get the guest WebContents from guestInstanceId. exports.getGuestWebContents = (guestInstanceId) => { const command = 'ELECTRON_BROWSER_GUEST_WEB_CONTENTS' - const meta = ipcRendererInternal.sendSync(command, contextId, guestInstanceId) + const meta = ipcRendererInternal.sendSync(command, contextId, guestInstanceId, getCurrentStack()) return metaToValue(meta) }