Skip to content

Commit

Permalink
feat: add ELECTRON_ENABLE_API_FILTERING_STACKS environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak committed Sep 24, 2019
1 parent c25f0a1 commit 6f6b515
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 22 deletions.
23 changes: 23 additions & 0 deletions docs/api/app.md
Expand Up @@ -423,80 +423,103 @@ Returns:

* `event` Event
* `webContents` [WebContents](web-contents.md)
* `callerStack` String | undefined

Emitted when `desktopCapturer.getSources()` is called in the renderer process of `webContents`.
Calling `event.preventDefault()` will make it return empty sources.

**Note:** `callerStack` is only available when [this environment variable][electron_enable_api_filtering_stacks] is set.

### Event: 'remote-require'

Returns:

* `event` Event
* `webContents` [WebContents](web-contents.md)
* `moduleName` String
* `callerStack` String | undefined

Emitted when `remote.require()` is called in the renderer process of `webContents`.
Calling `event.preventDefault()` will prevent the module from being returned.
Custom value can be returned by setting `event.returnValue`.

**Note:** `callerStack` is only available when [this environment variable][electron_enable_api_filtering_stacks] is set.

### Event: 'remote-get-global'

Returns:

* `event` Event
* `webContents` [WebContents](web-contents.md)
* `globalName` String
* `callerStack` String | undefined

Emitted when `remote.getGlobal()` is called in the renderer process of `webContents`.
Calling `event.preventDefault()` will prevent the global from being returned.
Custom value can be returned by setting `event.returnValue`.

**Note:** `callerStack` is only available when [this environment variable][electron_enable_api_filtering_stacks] is set.

### Event: 'remote-get-builtin'

Returns:

* `event` Event
* `webContents` [WebContents](web-contents.md)
* `moduleName` String
* `callerStack` String | undefined

Emitted when `remote.getBuiltin()` is called in the renderer process of `webContents`.
Calling `event.preventDefault()` will prevent the module from being returned.
Custom value can be returned by setting `event.returnValue`.

**Note:** `callerStack` is only available when [this environment variable][electron_enable_api_filtering_stacks] is set.

### Event: 'remote-get-current-window'

Returns:

* `event` Event
* `webContents` [WebContents](web-contents.md)
* `callerStack` String | undefined

Emitted when `remote.getCurrentWindow()` is called in the renderer process of `webContents`.
Calling `event.preventDefault()` will prevent the object from being returned.
Custom value can be returned by setting `event.returnValue`.

**Note:** `callerStack` is only available when [this environment variable][electron_enable_api_filtering_stacks] is set.

### Event: 'remote-get-current-web-contents'

Returns:

* `event` Event
* `webContents` [WebContents](web-contents.md)
* `callerStack` String | undefined

Emitted when `remote.getCurrentWebContents()` is called in the renderer process of `webContents`.
Calling `event.preventDefault()` will prevent the object from being returned.
Custom value can be returned by setting `event.returnValue`.

**Note:** `callerStack` is only available when [this environment variable][electron_enable_api_filtering_stacks] is set.

### Event: 'remote-get-guest-web-contents'

Returns:

* `event` Event
* `webContents` [WebContents](web-contents.md)
* `guestWebContents` [WebContents](web-contents.md)
* `callerStack` String | undefined

Emitted when `<webview>.getWebContents()` is called in the renderer process of `webContents`.
Calling `event.preventDefault()` will prevent the object from being returned.
Custom value can be returned by setting `event.returnValue`.

**Note:** `callerStack` is only available when [this environment variable][electron_enable_api_filtering_stacks] is set.

[electron_enable_api_filtering_stacks]: environment-variables.md#electron_enable_api_filtering_stacks

## Methods

The `app` object has the following methods:
Expand Down
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_STACKS`

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
23 changes: 23 additions & 0 deletions docs/api/web-contents.md
Expand Up @@ -743,74 +743,97 @@ Emitted when the renderer process sends a synchronous message via `ipcRenderer.s
Returns:

* `event` Event
* `callerStack` String | undefined

Emitted when `desktopCapturer.getSources()` is called in the renderer process.
Calling `event.preventDefault()` will make it return empty sources.

**Note:** `callerStack` is only available when [this environment variable][electron_enable_api_filtering_stacks] is set.

#### Event: 'remote-require'

Returns:

* `event` IpcMainEvent
* `moduleName` String
* `callerStack` String | undefined

Emitted when `remote.require()` is called in the renderer process.
Calling `event.preventDefault()` will prevent the module from being returned.
Custom value can be returned by setting `event.returnValue`.

**Note:** `callerStack` is only available when [this environment variable][electron_enable_api_filtering_stacks] is set.

#### Event: 'remote-get-global'

Returns:

* `event` IpcMainEvent
* `globalName` String
* `callerStack` String | undefined

Emitted when `remote.getGlobal()` is called in the renderer process.
Calling `event.preventDefault()` will prevent the global from being returned.
Custom value can be returned by setting `event.returnValue`.

**Note:** `callerStack` is only available when [this environment variable][electron_enable_api_filtering_stacks] is set.

#### Event: 'remote-get-builtin'

Returns:

* `event` IpcMainEvent
* `moduleName` String
* `callerStack` String | undefined

Emitted when `remote.getBuiltin()` is called in the renderer process.
Calling `event.preventDefault()` will prevent the module from being returned.
Custom value can be returned by setting `event.returnValue`.

**Note:** `callerStack` is only available when [this environment variable][electron_enable_api_filtering_stacks] is set.

#### Event: 'remote-get-current-window'

Returns:

* `event` IpcMainEvent
* `callerStack` String | undefined

Emitted when `remote.getCurrentWindow()` is called in the renderer process.
Calling `event.preventDefault()` will prevent the object from being returned.
Custom value can be returned by setting `event.returnValue`.

**Note:** `callerStack` is only available when [this environment variable][electron_enable_api_filtering_stacks] is set.

#### Event: 'remote-get-current-web-contents'

Returns:

* `event` IpcMainEvent
* `callerStack` String | undefined

Emitted when `remote.getCurrentWebContents()` is called in the renderer process.
Calling `event.preventDefault()` will prevent the object from being returned.
Custom value can be returned by setting `event.returnValue`.

**Note:** `callerStack` is only available when [this environment variable][electron_enable_api_filtering_stacks] is set.

#### Event: 'remote-get-guest-web-contents'

Returns:

* `event` IpcMainEvent
* `guestWebContents` [WebContents](web-contents.md)
* `callerStack` String | undefined

Emitted when `<webview>.getWebContents()` is called in the renderer process.
Calling `event.preventDefault()` will prevent the object from being returned.
Custom value can be returned by setting `event.returnValue`.

**Note:** `callerStack` is only available when [this environment variable][electron_enable_api_filtering_stacks] is set.

[electron_enable_api_filtering_stacks]: environment-variables.md#electron_enable_api_filtering_stacks

### Instance Methods

#### `contents.loadURL(url[, options])`
Expand Down
24 changes: 12 additions & 12 deletions lib/browser/remote/server.js
Expand Up @@ -293,8 +293,8 @@ handleRemoteCommand('ELECTRON_BROWSER_WRONG_CONTEXT_ERROR', function (event, con
removeRemoteListenersAndLogWarning(event.sender, rendererFunctions.get(objectId))
})

handleRemoteCommand('ELECTRON_BROWSER_REQUIRE', function (event, contextId, moduleName) {
const customEvent = emitCustomEvent(event.sender, 'remote-require', moduleName)
handleRemoteCommand('ELECTRON_BROWSER_REQUIRE', function (event, contextId, moduleName, stack) {
const customEvent = emitCustomEvent(event.sender, 'remote-require', moduleName, stack)

if (customEvent.returnValue === undefined) {
if (customEvent.defaultPrevented) {
Expand All @@ -307,8 +307,8 @@ handleRemoteCommand('ELECTRON_BROWSER_REQUIRE', function (event, contextId, modu
return valueToMeta(event.sender, contextId, customEvent.returnValue)
})

handleRemoteCommand('ELECTRON_BROWSER_GET_BUILTIN', function (event, contextId, moduleName) {
const customEvent = emitCustomEvent(event.sender, 'remote-get-builtin', moduleName)
handleRemoteCommand('ELECTRON_BROWSER_GET_BUILTIN', function (event, contextId, moduleName, stack) {
const customEvent = emitCustomEvent(event.sender, 'remote-get-builtin', moduleName, stack)

if (customEvent.returnValue === undefined) {
if (customEvent.defaultPrevented) {
Expand All @@ -321,8 +321,8 @@ handleRemoteCommand('ELECTRON_BROWSER_GET_BUILTIN', function (event, contextId,
return valueToMeta(event.sender, contextId, customEvent.returnValue)
})

handleRemoteCommand('ELECTRON_BROWSER_GLOBAL', function (event, contextId, globalName) {
const customEvent = emitCustomEvent(event.sender, 'remote-get-global', globalName)
handleRemoteCommand('ELECTRON_BROWSER_GLOBAL', function (event, contextId, globalName, stack) {
const customEvent = emitCustomEvent(event.sender, 'remote-get-global', globalName, stack)

if (customEvent.returnValue === undefined) {
if (customEvent.defaultPrevented) {
Expand All @@ -335,8 +335,8 @@ handleRemoteCommand('ELECTRON_BROWSER_GLOBAL', function (event, contextId, globa
return valueToMeta(event.sender, contextId, customEvent.returnValue)
})

handleRemoteCommand('ELECTRON_BROWSER_CURRENT_WINDOW', function (event, contextId) {
const customEvent = emitCustomEvent(event.sender, 'remote-get-current-window')
handleRemoteCommand('ELECTRON_BROWSER_CURRENT_WINDOW', function (event, contextId, stack) {
const customEvent = emitCustomEvent(event.sender, 'remote-get-current-window', stack)

if (customEvent.returnValue === undefined) {
if (customEvent.defaultPrevented) {
Expand All @@ -349,8 +349,8 @@ handleRemoteCommand('ELECTRON_BROWSER_CURRENT_WINDOW', function (event, contextI
return valueToMeta(event.sender, contextId, customEvent.returnValue)
})

handleRemoteCommand('ELECTRON_BROWSER_CURRENT_WEB_CONTENTS', function (event, contextId) {
const customEvent = emitCustomEvent(event.sender, 'remote-get-current-web-contents')
handleRemoteCommand('ELECTRON_BROWSER_CURRENT_WEB_CONTENTS', function (event, contextId, stack) {
const customEvent = emitCustomEvent(event.sender, 'remote-get-current-web-contents', stack)

if (customEvent.returnValue === undefined) {
if (customEvent.defaultPrevented) {
Expand Down Expand Up @@ -450,8 +450,8 @@ 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) {
const guest = guestViewManager.getGuestForWebContents(guestInstanceId, event.sender, stack)

const customEvent = emitCustomEvent(event.sender, 'remote-get-guest-web-contents', guest)

Expand Down
6 changes: 3 additions & 3 deletions lib/browser/rpc-server.js
Expand Up @@ -63,15 +63,15 @@ 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) {
const customEvent = emitCustomEvent(event.sender, 'desktop-capturer-get-sources')
ipcMainInternal.handle('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', function (event, options, stack) {
const customEvent = emitCustomEvent(event.sender, 'desktop-capturer-get-sources', stack)

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_STACKS) {
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_STACKS) {
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 6f6b515

Please sign in to comment.