Skip to content

Commit

Permalink
feat: add ELECTRON_ENABLE_API_FILTERING_LOGGING environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak committed Oct 1, 2019
1 parent 969b4d1 commit bb07519
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 16 deletions.
11 changes: 11 additions & 0 deletions docs/api/environment-variables.md
Expand Up @@ -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.
Expand Down
37 changes: 30 additions & 7 deletions lib/browser/remote/server.ts
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 6 additions & 2 deletions lib/browser/rpc-server.js
Expand Up @@ -63,15 +63,19 @@ 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) {
console.error('Blocked desktopCapturer.getSources()')
return []
}

return desktopCapturer.getSources(event, ...args)
return desktopCapturer.getSources(event, options)
})
}

Expand Down
10 changes: 9 additions & 1 deletion lib/renderer/api/desktop-capturer.ts
Expand Up @@ -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')

Expand All @@ -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,
Expand Down
20 changes: 14 additions & 6 deletions lib/renderer/api/remote.js
Expand Up @@ -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))
Expand All @@ -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)
}

Expand All @@ -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)
}

Expand Down

0 comments on commit bb07519

Please sign in to comment.