noctcore-electron/no-shell-outside-allowlist
noctcore-electron/no-shell-outside-allowlist
- Package
- electron v0.1.0
- Preset
- none opt-in
- Severity
- off
- Autofix
- No
- Suggestions
- No
- Options
- Type info
- Not needed
Electron’s shell is used only in the module that checks what it hands to the operating system.
shell.openExternal(url) passes a URL to the operating system, which runs whatever is registered
for its scheme: file:, smb: or a custom protocol can start a program. shell.openPath(path)
opens a file with its default application. A URL from a link in rendered content, a message from
the renderer or a remote config must pass a scheme and path check first, and that check is only
reliable if it lives in one place. Every other file calls that module:
// rule options: {"allowIn":["**/src/main/external-links.ts"]}import { shell } from 'electron';
win.webContents.setWindowOpenHandler(({ url }) => { void shell.openExternal(url); return { action: 'deny' };});// rule options: {"allowIn":["**/src/main/external-links.ts"]}import { openExternalSafely } from './external-links';
win.webContents.setWindowOpenHandler(({ url }) => { void openExternalSafely(url); return { action: 'deny' };});What it flags
Section titled “What it flags”Outside the files in allowIn, every way of reaching shell from electron:
import { shell } from 'electron', aliased or not;electron.shellthrough a default or namespace import,const electron = require('electron')orrequire('electron').shell;const { shell } = electronandconst { shell } = require('electron').
// rule options: {"allowIn":["**/src/main/external-links.ts"]}import electron from 'electron';
export const helpItem = { label: 'Docs', click: () => electron.shell.openExternal(DOCS_URL) };What it does not flag
Section titled “What it does not flag”- Any use inside a file matched by
allowIn. - A
shellthat is not Electron’s:shelljs, a config field, a local variable.
// rule options: {"allowIn":["**/src/main/external-links.ts"]}import { shell } from 'electron';
export async function openExternalSafely(url: string): Promise<void> { if (!/^https?:/u.test(url)) return; await shell.openExternal(url);}Options
Section titled “Options”| Option | Type | Default | Meaning |
|---|---|---|---|
allowIn |
string[] (globs) |
[] |
Files that may use shell, matched against the absolute path and the path relative to the working directory or workspace root, so src/main/security.ts and **/main/security.ts both work. |
'noctcore-electron/no-shell-outside-allowlist': ['error', { allowIn: ['**/src/main/external-links.ts'] }]Without allowIn, every use is reported, the checked module included.
When not to use it
Section titled “When not to use it”An app that never calls shell gains nothing; one that has not yet written the checked module
should write it first and then turn this on.