Security Considerations

When you turn a Shiny app into a desktop app, it gains a lot of power. On the web, the browser and a server fence in what your app can do. On someone’s laptop, your app can do anything they can: read their files, talk to the network, run programs. That power is what makes a desktop app useful, and it is also why a desktop app needs more security thought than a web one.

Three places things can go wrong, and the rest of this guide walks through each in order:

  1. The Electron window that shows your app. Without care, it can be tricked into running anything. shinyelectron sets safe defaults; your job is to leave them alone.
  2. Your Shiny code. Whatever your app can read or run, a determined user can probably get it to read or run something else. A few habits cover most of it.
  3. How the app reaches your users. Unsigned downloads, plain-HTTP update channels, and passwords baked into the bundle are the classic ways a release goes wrong.

Two related topics live in their own guides: code signing and auto-updates, where signing is what lets an app check who published an update.

Two processes, one trust boundary

An Electron app has two process types:

The rule is unconditional: the renderer never touches Node.js directly. With nodeIntegration: true, any script in the renderer, including one injected through an XSS bug or a transitive HTML widget, runs arbitrary OS commands. The defense is to give the renderer the narrowest possible gate to the main process, and to keep that gate auditable.

Diagram of the Electron trust boundary. On the left, the renderer process hosts Chromium with the Shiny UI, treated as an untrusted zone with nodeIntegration false, contextIsolation true, sandbox true, and webSecurity true. In the middle, a preload script using contextBridge exposes only a narrow IPC surface: lifecycle.onStatus, lifecycle.retry, and lifecycle.quit. On the right, the trusted main process runs Node.js with full OS access and spawns R via shiny runApp, Python via shiny run, container backends via docker run, or the Express server for Shinylive.Diagram of the Electron trust boundary. On the left, the renderer process hosts Chromium with the Shiny UI, treated as an untrusted zone with nodeIntegration false, contextIsolation true, sandbox true, and webSecurity true. In the middle, a preload script using contextBridge exposes only a narrow IPC surface: lifecycle.onStatus, lifecycle.retry, and lifecycle.quit. On the right, the trusted main process runs Node.js with full OS access and spawns R via shiny runApp, Python via shiny run, container backends via docker run, or the Express server for Shinylive.
Electron’s trust boundary: the renderer runs untrusted web content, the preload script exposes a small IPC surface, the main process holds full OS access.

The full picture lives in the Electron Security Checklist. The rest of this section explains how shinyelectron implements each item.

What shinyelectron locks down

The generated BrowserWindow ships with the safe choice for every webPreferences flag that has one. The block below is read live from the template at inst/electron/shared/main.js:

webPreferences: {
  nodeIntegration: false,
  contextIsolation: true,
  sandbox: true,
  preload: path.join(__dirname, 'preload.js'),
  enableRemoteModule: false,
  webSecurity: true,
  // Isolate each app's session to prevent Service Worker cache
  // cross-contamination between multiple shinyelectron apps
  partition: 'persist:<app_slug>'
}

Each setting, in one line:

Setting Value What it stops
nodeIntegration false Renderer cannot call require() or any Node API
contextIsolation true Page scripts cannot reach into the preload’s scope
sandbox true Renderer runs inside the Chromium OS sandbox
enableRemoteModule false Disables the deprecated remote module entirely
webSecurity true Same-origin policy stays on
partition per-app Each shinyelectron app gets its own session storage

The preload script is the only bridge. It exposes a short list of named IPC methods (lifecycle.onStatus, lifecycle.retry, lifecycle.quit, and a few peers) through contextBridge.exposeInMainWorld(). The renderer can call those and nothing else: it never sees ipcRenderer, never imports a Node module.

DevTools default to off. The DevTools menu item only appears when menu.show_dev_tools is true in _shinyelectron.yml (the default is false). Leave it that way for production. DevTools lets the user, or anything injected into the page, execute JavaScript and inspect any value the app holds.

A link in your app that leads to another site does not replace the app in its window. Links to http: and https: addresses, and mailto: addresses, open in the user’s default browser or mail client, and the app stays where it was. This holds for plain links and for links that ask for a new window, in the main window and in the app’s own frames, such as the one a Shinylive app runs in. Redirects are covered in the main window.

Links to any other scheme are blocked, so a tel: link, a custom myapp: address, or a file: address goes nowhere and the shell writes a warning to its log. Only the three schemes above are ever handed to the operating system. Links to the app’s own address, and Shiny downloads, work as before.

The control bridge

The preload script exposes the lifecycle and launcher actions (retry, quit, choosing an app, and the like) to the shell’s own pages. The main process accepts those actions only when they come from the main frame of one of those pages, which it loads from the app bundle. The page your Shiny app shows has no window.lifecycle, and a message that arrives from the app, from a frame inside it, or from any outside page is ignored.

Permissions

The shell answers every permission request itself, instead of leaving Electron to grant them all. A request is granted only while the window shows your app (or DevTools). While the window shows anything else, every request is refused, including camera, microphone, location, and the permission that lets a page pass a link to another program.

The page the window shows decides, not the frame that asks. Content your app embeds from an outside site, such as a video in an <iframe>, can therefore use whatever the allow attribute of your own frame gives it, because the window is showing your app. Leave out allow values you do not want such content to have. A page that tries to open an address through the browser’s own handler is checked against the same three schemes as links.

Fuses

Electron has switches, called fuses, that are flipped in the packaged binary when the app is built. By default they let anyone run a shipped app as a plain Node.js runtime, so shinyelectron turns off the three that matter:

Fuses apply to packaged builds only. run_electron_app() runs the unpackaged app and is not affected.

Playwright cannot drive a packaged build, because it attaches to the app through --inspect, which enableNodeCliInspectArguments turns off. It still works on the unpackaged app in the electron-app folder that export() writes.

Staying on a supported Electron

Electron releases a new major version every eight weeks and fixes security problems only in the newest three. An app built with an older one ships a browser engine that no longer gets those fixes. shinyelectron pins a recent Electron, but a pin goes out of date, and so does a version you set with dependencies.electron.version.

When you export a build, shinyelectron asks the npm registry for the newest Electron and warns, with class shinyelectron_electron_unsupported, if the version in use is more than two majors behind. app_check() reports the same problem as a warning. The lookup waits at most five seconds and is skipped silently when it fails, so an export never stops because you are offline. It is also skipped when the version is "latest".

To clear the warning, set dependencies.electron.version to a supported version, or update shinyelectron, which moves its pin forward in each release.

One copy per user

The app takes Electron’s single-instance lock, so a second launch exits at once without starting a server, a window, or a log file, and the running copy brings its window forward. Two copies cannot share a profile folder, which keeps them from corrupting each other’s browser storage or a runtime download.

Content-Security-Policy scope

The lifecycle window (lifecycle.html), the multi-app launcher (launcher.html), and the Resource Usage window (usage.html) each carry a Content-Security-Policy meta tag that restricts sources to 'self' with 'unsafe-inline' permitted for styles and scripts. That covers every page the shell itself shows: the startup, error, and shutdown screens, the list of apps in a suite, and the CPU and memory readings.

The Shiny application itself is loaded by navigating to a localhost URL (http://localhost:<port>). No CSP is injected for that content by the Electron shell; the CSP that applies to the running Shiny app is whatever the Shiny server sends in its own HTTP headers, which is none by default. Keep this in mind if the app loads any external scripts or embeds third-party widgets: the Electron webSecurity: true setting enforces the same-origin policy, but it does not substitute for a CSP on the app content.

Cross-origin headers (Shinylive only)

Shinylive runs WebR or Pyodide in the browser, which needs SharedArrayBuffer, which needs cross-origin isolation. The local Express server attaches the right headers automatically:

Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp
Cross-Origin-Resource-Policy: cross-origin

Native R and Python apps load over plain localhost and need no extra cross-origin header work: the content originates from a process you started, not a third party.

Rule of thumb. Avoid external scripts (CDNs, third-party widgets) unless the app genuinely needs them. Each external resource is a trust dependency you cannot audit. Bundle local copies when you can.

Runtime download integrity

Every runtime archive is verified against a SHA-256 checksum published by its upstream before extraction. A mismatch aborts the install with the expected and actual hashes.

If the published checksum cannot be fetched (for example, a transient network error), the installer prints a warning and continues without verification rather than failing the build, mirroring the Node.js behavior. To require verification, confirm the checksum file is reachable before building.

The auto-download strategy carries the same protection to the end user’s machine: the runtime manifest embedded in the app records the expected SHA-256 (when available at build time), and the Electron runtime-downloader verifies the archive it fetches on first launch before extracting it.

Where your app still has to think

shinyelectron secures the shell. Inside the shell, your Shiny code runs with the same OS permissions as the user who launched it. That is the whole point of a desktop app, and it is also the part you have to defend yourself.

Inputs are user-controlled

Every reactive input arrives from the renderer. A determined user can hand-craft any value, regardless of what the UI suggests. Two rules cover most of it.

Validate at the boundary. Check types, ranges, and shape before you act on a value, not after. Shiny’s req() and validate(need(...)) exist for this; use them.

Never build shell commands by string concatenation. Pass the program and its arguments as separate values so the shell never sees a chance to parse them. The pattern looks slightly different in each language but the rule is the same.

R. system() and system2() (when given a single command string) hand the assembled text to /bin/sh -c, where metacharacters like ;, |, and $ are interpreted. Use processx::run() instead, or system2() with the arguments as a character vector, so the program and each argument reach the OS as separate strings:

# Don't: paste() builds one string that the shell then parses
system(paste("convert", input$file, "out.png"))

# Do: program and args cross to the OS as separate values
processx::run("convert", c(input$file, "out.png"))

Python. subprocess.run() defaults to shell=False, which is the safe form. The list form passes the program and its arguments straight to the OS process API. Setting shell=True (or calling os.system()) routes through /bin/sh and re-introduces the parsing problem:

# Don't: shell=True hands the f-string to /bin/sh -c
subprocess.run(f"convert {input.file} out.png", shell=True)

# Do: list form skips the shell entirely
subprocess.run(["convert", input.file, "out.png"], check=True)

In both cases, the difference is whether ; rm -rf ~ (or any other crafted value the renderer hands you) is treated as data or as a command.

File access is user-wide

file.choose(), readLines(), Python’s open(): they inherit the launching user’s permissions and can reach anything that user can reach. SSH keys, browser history, the Documents folder. Expected for a desktop app, worth remembering when porting from a sandboxed Shiny Server where these calls quietly fail.

Least privilege still applies. If the app does not need to write files or spawn processes, do not include code that can. Removed code cannot be exploited.

Secrets and per-user credentials

A credential that belongs to one person, such as their token for a cloud data warehouse, has to come from their machine when the app starts, not from the installer. What your code can see at that point depends on the runtime strategy:

Strategy What reaches your code at launch
Native R (system, bundled, auto-download) The environment the app was launched with, plus the user’s ~/.Renviron
Native Python (system, bundled, auto-download) The environment the app was launched with
container Only PORT, HOST, and the container.env values from _shinyelectron.yml
shinylive Nothing from the machine, because no R or Python process runs there

For per-user credentials, use a native strategy.

R apps. The Electron shell passes its environment to Rscript without --vanilla, and R reads the user’s ~/.Renviron at startup like any other R session. That happens before shiny::runApp() switches into the app folder, so a .Renviron next to app.R is not read. On Windows, R’s ~ is usually the Documents folder; path.expand("~/.Renviron") shows the exact file.

Python apps. The environment passes through the same way, but nothing loads a .env file for you. If your app calls python-dotenv’s load_dotenv(), give it a path outside the app folder, such as a file in the user’s home directory. With no argument it searches upward from your code’s folder, which sits inside the installed app.

Container apps currently receive only PORT, HOST, and your container.env values. The user’s environment and ~/.Renviron stay on the host.

Giving each user their own token

Test the way users launch. run_electron_app() and export(run_after = TRUE) start Electron from your R session, so the app inherits that session’s environment, including variables R loaded from your own .Renviron. Starting the app’s executable from a terminal passes the terminal’s variables along, and R then prefers a .Renviron in the terminal’s current directory over ~/.Renviron. Before you ship, open the installed app from Finder, the Dock, or the Start menu, ideally from an account without your credentials.

Never bundle credentials

Anything you copy into the build is recoverable from the installed app, and .asar is not encryption. Native and container builds copy the whole app folder, hidden files included.

# These should NEVER be in your app directory
.env
.Renviron
credentials.json
service-account-key.json

.gitignore does not help here: it keeps these files out of your repository, but a local export() copies whatever is on disk. Values in container.env ship the same way, written into the built app, so keep tokens out of them too.

Strategy-specific notes

The runtime strategy you pick decides which sandbox actually contains your code.

Shinylive

The strongest isolation of any strategy. WebR or Pyodide run inside the browser’s WebAssembly sandbox, which has no syscalls of its own and no filesystem outside an in-memory shim. Shell injection and arbitrary file access are physically not on the menu. The trust boundary is the same as any other web page, plus the Electron defaults above.

System, bundled, auto-download

A real R or Python child process. Whatever is reachable from system() or subprocess is reachable from the app. The defense is the input-validation discipline from the previous section. None of these strategies adds additional sandboxing.

Container

Docker or Podman puts the app inside an OS-level container, which is a stronger boundary than process isolation but weaker than a virtual machine:

Containers buy isolation, not invulnerability. Mount only what you must, keep the engine patched, and treat the container’s filesystem as a useful constraint rather than a guarantee.

Distribution

Two release-time concerns. Each has its own vignette; the rules of thumb live here.

Sign your production builds. Unsigned macOS apps trip Gatekeeper, unsigned Windows installers trip SmartScreen, and an unsigned Windows app installs whatever its update address serves, without checking who published it. See Code Signing and Distribution.

Serve update manifests over HTTPS. electron-updater checks each download against the checksum in the manifest that names it, so the manifest must reach the user untampered. Plain HTTP lets an attacker swap that manifest. See Auto Updates.

What not to do

shinyelectron’s defaults block every common Electron footgun. You can re-enable any of them by editing the generated files. Do not.

Do not modify these settings in the generated Electron code

Summary

Layer Owned by What to do
Electron shell shinyelectron Defaults are safe; do not edit them
Cross-origin headers shinyelectron Set automatically for Shinylive
Shiny app code You Validate inputs, parameterize commands, avoid shell strings
Filesystem use You Least privilege; the code you remove cannot be exploited
Credentials You Never bundle; use ~/.Renviron, env vars, the OS keychain, or browser sign-in
Container image You Override USER, mount minimum, keep engine patched
Code signing You Sign release builds; HTTPS for update manifests

Further reading: the Electron Security documentation.