Skip to content

About

Shared Flatpak extension that lets sandboxed apps run host toolchains transparently (LD_PRELOAD + HostCommand)

Resources

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

flatpark-spawn

flatpark-spawn lets a Flatpak app use the host's command-line tools as if they were installed in its sandbox: system commands, ~/.local/bin, and what mise, nvm or cargo put in the user's login-shell PATH (node, go, gh, ...). The app itself is not changed. When it runs git status, the host's git runs, with the user's configuration, credential helpers and PATH; its exit code, output and signals reach the app as if it were a local child.

It is shipped as one Flatpak extension, org.flatpark.Spawn, shared by every app that declares it, on any runtime (freedesktop, GNOME, KDE).

Why this exists, and what it costs

A great many apps depend on toolchains from inside their sandbox: editors and IDEs, and above all AI agent apps, which run git, node, python, gh, language servers and whatever the user has installed. In a Flatpak sandbox those tools are missing or are the wrong versions, and the app is crippled.

Be clear about what this is: flatpark-spawn takes the Flatpak sandbox apart completely. A command forwarded through it runs on the host as the user, with everything the user can reach. Narrowing the app's other permissions (no --filesystem=home, no --filesystem=host) does not contain it.

We made it anyway, following FlatPark's principle that users trust FlatPark because they trust the upstream app. If a user is willing to install an app on the host with no restrictions at all, what they want from Flatpak is not the sandbox but a better way to install and update it. On distributions without the AUR, and especially on immutable ones, that matters a lot.

What we promise in return:

  • Every FlatPark app that uses this extension is audited as completely as we can. For closed-source apps from well-known vendors, where we cannot audit the code, we guarantee only that we do not modify the original distribution.
  • flatpark-spawn itself is open source and auditable. It may have bugs, and it may have vulnerabilities, but we will never do anything deliberately malicious in this layer.

Mechanically: the permission is --talk-name=org.freedesktop.Flatpak, granted by the app's manifest (which FlatPark reviews) or by the user, and the user can take it away with flatpak override --user --no-talk-name=org.freedesktop.Flatpak <app-id>. Without that permission the extension does nothing at all. With it, you choose per app how much of its normal operation goes to the host (its level); but only taking the permission away is a security boundary.

Using it in an app

A few lines in the manifest and one in the launcher. The extension is then installed along with the app.

finish-args:
  - --talk-name=org.freedesktop.Flatpak
add-extensions:
  org.flatpark.Spawn:
    directory: lib/flatpark-spawn
    version: '1'
    # no `no-autodownload`: it comes with the app
    # no `autodelete: true`: that would delete it under the other apps using it
modules:
  - name: app
    build-commands:
      - mkdir -p /app/lib/flatpark-spawn   # the mount point must exist
      # for `flatpak run --command=flatpark-spawn APP check|set ...`
      - ln -s /app/lib/flatpark-spawn/bin/flatpark-spawn /app/bin/flatpark-spawn
      # ...

In the launcher, before it execs the app:

[ -r /app/lib/flatpark-spawn/enable.sh ] && . /app/lib/flatpark-spawn/enable.sh
exec /app/extra/vendor/app "$@"

The line is harmless when the extension is not installed (the launcher starts normally) and when the app lacks the permission (nothing is set). It works under set -eu. For Electron apps on the Electron BaseApp, enable.sh also sets ZYPAK_LD_PRELOAD, so the library reaches the processes zypak starts.

Some programs remove LD_PRELOAD before they start a child, and the library is gone from that child and everything below it. VS Code (and Cursor, Windsurf and other forks) does this for its extension host and pty host. There, commands an extension runs, and same-name commands in the integrated terminal (git), stay in the sandbox. Only bash's fallback for commands the sandbox lacks still works. flatpark-spawn does not put the library back (see DESIGN.md, 否决的做法). For the commands that matter, use shims instead.

A command the app must keep running from its own sandbox even though the host has one of the same name goes in FLATPARK_SPAWN_LOCAL, set in the launcher (the user's local: in config.yaml does the same; the two add up):

export FLATPARK_SPAWN_LOCAL=python3:ssh

Levels

Each app runs at one level, set in the host's config.yaml (default: for all apps, apps.<id>.level for one):

Level What the app's normal operation runs on the host What malicious code in the app can do
off nothing: flatpark-spawn sets nothing up and refuses every request run any command on the host as you, as long as the app still has the permission. For a real off, also take the permission away (flatpak override --user --no-talk-name=org.freedesktop.Flatpak <app-id>)
explicit only what is asked for explicitly: flatpark-spawn CMD, host-spawn, a terminal's host shell, and shims run any command on the host as you
listed the above, and the commands listed under forward run any command on the host as you
fallback (the default) what the sandbox lacks (git, node, gh), and what forward lists; the sandbox's own sh, cat, python3 stay in the sandbox run any command on the host as you
full everything the host has, the host's copy first, also for names the sandbox has too (0.2's behaviour) run any command on the host as you

The levels from explicit to full limit what the app's ordinary work puts on the host, for instance whether a git status the app runs on a project it just opened runs that project's hooks on the host or in the sandbox. They do not stop code in the app that wants to reach the host: with --talk-name=org.freedesktop.Flatpak it can call the session helper itself. Only off together with removing the permission is a boundary.

0.2 had only full. An app without its own entry now runs at fallback; set default: full to keep 0.2's behaviour.

Changing levels: set and check APP-ID

The FlatPark manager does this in its window. On the command line, run flatpark-spawn through an app that has the extension, VS Code here; it does the work on the host, for that app or any other:

flatpak run --command=flatpark-spawn com.visualstudio.code check com.visualstudio.code
flatpak run --command=flatpark-spawn com.visualstudio.code set com.visualstudio.code level=listed forward+=node

The first time, while VS Code does not have the permission yet (FlatPark apps come without it), give it the permission for that one run; setting a level then grants VS Code what the level needs:

flatpak run --talk-name=org.freedesktop.Flatpak --command=flatpark-spawn com.visualstudio.code set com.visualstudio.code level=listed forward+=node

More examples, shortened with alias fps='flatpak run --command=flatpark-spawn com.visualstudio.code':

fps set com.visualstudio.code shims-=nvim local+=python3
fps set com.visualstudio.code level=default   # follow default: again
fps set com.visualstudio.code --reset          # remove its entry altogether
fps set --default level=explicit
fps set --default level=unset                  # remove default: (fallback)
  • The app you run it through must be able to reach the host, that is have --talk-name=org.freedesktop.Flatpak. Without it (level off, or as FlatPark apps come by default) the command exits 2 and says what to run instead: first the same command with flatpak run --talk-name=org.freedesktop.Flatpak, the permission for that one run, as above; or flatpak override --user --talk-name=org.freedesktop.Flatpak <app-id> and the command again, which also switches flatpark-spawn on for that app from then on (at its level, default: unless it has its own). Setting a level grants or removes the permission as the level needs (see below).
  • level=LEVEL; level=default removes the app's own level; --reset removes the app's whole entry (apps.<id>), and set --default level=unset removes default: itself. forward+=NAME / forward-=NAME (per app only), local+= / local-=, shims+= / shims-=; one name per argument, as many arguments as you like, applied together or not at all.
  • For an app, local and shims add to the global lists: removing a name the app inherits writes -NAME in its own list, adding it back removes the -NAME. A list that becomes empty is removed, and so is an app entry with nothing left in it.
  • It edits the host's config.yaml in place: your comments, the order of the keys and the layout of everything else stay as they are (new keys go after their siblings, new lists as [a, b]). It does not touch a file with an error (fix it first; check shows it), and writes the file atomically.
  • Setting an app's level also sets its permission, with the host's flatpak override --user: off removes --talk-name=org.freedesktop.Flatpak, any other level grants it (if the app does not have it already). Lists alone do not touch permissions, and neither does --reset (nor a level=default that leaves nothing of the app's entry): the app then follows default: with the permission you gave it, and set says so, warning when that is off with the permission still there. set --default changes no permission; it lists the installed apps whose permission no longer matches their level.
  • It prints what changed and when the app follows: a wider or a narrower setting within about 30 s (restart the app to apply it at once); off and back, shims, and anything wider from explicit after a restart (at explicit the app asks the host only through shims and explicit calls, so a wider setting would otherwise wait for the next of those).
  • --command=flatpark-spawn needs the command in the app's PATH; apps using the extension link it there (see Using it in an app). For one that does not, use --command=/app/lib/flatpark-spawn/bin/flatpark-spawn.

check APP-ID shows the app's level and where it comes from (apps.<id>.level or default:), its lists with what is inherited from the global ones, whether its permission matches the level, and the host side's status file: what it found on the host for each forward and shim name. It exits 1 when something is marked ✗.

On the host directly, the same binary does the same without flatpak run: it is in the extension's deployment, ~/.local/share/flatpak/runtime/org.flatpark.Spawn/x86_64/1/active/files/bin/flatpark-spawn (/var/lib/flatpak/... for a system installation, aarch64 on ARM).

What runs where

The app runs off explicit listed fallback full
a bare name the sandbox has too (cat, sh) sandbox sandbox host if under forward sandbox; host if under forward host
a bare name only the host has (git, node), by exec or through bash's command not found sandbox (not found) sandbox (not found) host if under forward host host
an absolute path missing in the sandbox (/usr/bin/nvim, ~/.local/bin/x) sandbox (ENOENT) sandbox (ENOENT) host if the path, or its basename, is under forward host host
a shim (nvim with shims: [nvim]), from any process, with or without the library not made host host host host
flatpark-spawn CMD, host-spawn refused host host host host

Wherever the table says host:

  • a bare name is found in the login-shell PATH of the command's working directory (so a project's mise/asdf pins apply) and include_path; when the host does not have it after all, the sandbox's own copy runs, and with neither it fails with ENOENT / 127, exactly as without flatpark-spawn;
  • a command whose arguments or working directory are sandbox-only paths (cat /app/x, sh -c 'cat /tmp/x') stays in the sandbox, if the sandbox has it;
  • names under local (and in FLATPARK_SPAWN_LOCAL) stay in the sandbox, also when forward lists them;
  • an absolute path missing there too gives an ENOENT message and exit 127.

At every level:

  • an absolute path that exists in the sandbox (/usr/bin/python3) runs there;
  • an absolute path missing in the sandbox under a location the host does not have (/app/x, /run/host/x, a private /tmp/x) stays there (ENOENT);
  • a relative path (./x) runs in the sandbox.

Sandbox-only paths in arguments and the working directory: /app, /usr, /bin, /sbin, /lib*, /tmp and /var/tmp (unless the app has --filesystem=/tmp), /run/flatpak, /run/host, /.flatpak-info, /dev/fd, /proc/self/fd. /etc is not on the list. For the program being run, the list is shorter: /usr, /bin, /sbin and /lib* are left out, since the host has those trees too and a file the runtime lacks there can only be the host's. The reasoning, and how a command's host environment is put together, are in DESIGN.md.

Configuration and shims

The host's ~/.config/flatpark-spawn/config.yaml ($XDG_CONFIG_HOME), if it exists, applies to every app that uses flatpark-spawn. Only the host side reads it, so the app needs no access to your home directory. Without the file every app runs at level fallback with nothing listed.

# The level of an app without an entry below (off, explicit, listed,
# fallback, full); fallback when not set.
default: fallback

# Where to look for the commands apps run. First the login shell's PATH
# (what your terminal has, project pins from mise/asdf included), then
# include_path. exclude_path is skipped, also when the login PATH has it.
use_login_shell_path: true
include_path:
  - /opt/tools/bin
exclude_path:
  - /snap/bin

# Shims: a file per command in the app's PATH that runs it on the host.
# For programs that look for a command on disk before running it, and for
# processes without the preload library (VS Code's extension host).
shims:
  - nvim
  - rg

# Names that always stay in the sandbox, for every app.
local: []

# Per app: its level and lists. forward: names (or absolute host paths)
# that always go to the host; local and shims are added to the global
# lists, and -name removes a global entry for this app.
apps:
  com.visualstudio.code:
    level: listed
    forward: [git, gh, mise]
    shims: [rg, -nvim]
    local: [python3]
  com.example.Terminal:
    level: explicit
  • apps.<id> takes only level, forward, local and shims; include_path, exclude_path and use_login_shell_path are for all apps. local wins over forward. forward is ignored at explicit (check warns).
  • include_path, exclude_path: absolute paths or ~/....
  • A bare command name is run only if it is found in the search path: the login PATH (unless use_login_shell_path: false), then include_path, minus exclude_path. The command gets that search path as its PATH.
  • An absolute path (/usr/bin/nvim) needs no include_path. Under an exclude_path directory it is treated as missing on the host.
  • A changed file applies to running apps without a restart, at their next request to the host (a lookup, a forwarded command, flatpark-spawn CMD). An app at listed, fallback or full whose policy is 30 s old also asks for it again before its next command, so a wider level or list arrives within about 30 s even where the old one would never ask the host (a name not under forward). For a narrower one, a command the app ran in the 30 s before may still be started on the host directly until then. Restarting the app applies it at once. At explicit, a wider setting waits for the next shim or explicit call (restart to be sure); switching between off and another level needs a restart.
  • Shims are made when the app starts, only for the names found in the search path (looked up in the directory the app was started in). Restart the app after adding one. A name that is not found gets no shim, and the app says so on stderr when it starts. A shim also respects FLATPARK_SPAWN_LOCAL and the sandbox-only paths of What runs where, like the library.
  • A mistake in the file (an unknown key, a wrong type, a YAML error) makes flatpark-spawn ignore the whole file and print a warning. Commands then run as without the file (level fallback), except when the file has apps: or default:: then every app runs at explicit until it is fixed, so that a typo cannot widen what you narrowed. Find the problem with:
$ flatpak run --command=flatpark-spawn com.visualstudio.code check
config: ~/.config/flatpark-spawn/config.yaml (found)

default                fallback
this app               listed (com.visualstudio.code, from apps.com.visualstudio.code)
use_login_shell_path   ✓ 14 directories (login shell in ~)
include_path
  ✓ /opt/tools/bin
exclude_path
  - /snap/bin

shims
  ✓ nvim → /usr/bin/nvim
  ✗ rg   not in the search path; not created

in this app (/run/user/1000/app/com.visualstudio.code/flatpark-spawn/shims)
  nvim

It shows the configuration as the host side reads it, and for the app it runs in, its level and its current shims (the app needs the permission; check APP-ID above is about one app's policy instead). It exits 1 when something is marked ✗ or the file has an error. Run on the host directly, it shows the configuration only. flatpark-spawn -- check runs a host command named check.

For each app, the host side also records what it found for the app's shims and forward names, its level and any error in the file, in ~/.local/state/flatpark-spawn/status/<app-id>.json ($XDG_STATE_HOME), when the app starts and after the file changes. The FlatPark manager reads it. It never records which commands ran.

vscode-neovim. The extension runs in VS Code's extension host, which has no preload library. It looks for nvim on disk before running it, so neither the library nor bash's fallback can help. With

shims:
  - nvim

it finds the shim and starts the host's nvim with its default settings. Leave vscode-neovim.neovimExecutablePaths.linux unset (or set it to nvim). An absolute path such as /usr/bin/nvim does not work there: the extension host has no library to send it to the host.

To switch flatpark-spawn off for one app, set its level to off, which also removes its permission:

flatpak run --command=flatpark-spawn com.visualstudio.code set com.visualstudio.code level=off
# or the permission alone:
flatpak override --user --no-talk-name=org.freedesktop.Flatpak com.visualstudio.code

The command

The extension also has the command itself, for apps and users that run host commands explicitly:

/app/lib/flatpark-spawn/bin/flatpark-spawn [options] [command [args...]]

Without a command it opens the user's login shell on the host. A pseudo-terminal is allocated when stdin and stdout are both terminals and the command runs in the terminal's foreground, never for pipes. Options: -pty, -no-pty, -cwd DIR, -env VAR,... (variables to pass in addition to the filtered environment), -version.

host-spawn compatibility

Called as host-spawn (the extension ships bin/host-spawn; an app can link /app/bin/host-spawn to /app/lib/flatpark-spawn/bin/flatpark-spawn) it takes host-spawn's command line: -pty, -no-pty, -cwd, -env (default TERM, and only the listed variables are passed), -version, and no command opens $SHELL. A PTY is allocated when stdout is a terminal, as upstream does. A symlink with any other name runs that command on the host, as host-spawn's shims do. Existing terminal configurations such as VS Code's "path": "host-spawn" keep working.

Known limits

  • Levels other than off are no security boundary (see Levels): they narrow what the app's normal operation does on the host, not what code in an app with the permission can do.
  • The level is enforced by the host side and cached in the app's runtime directory. A changed level takes effect at the app's next request to the host, or within about 30 s (at listed and up the app asks for its policy again when it is that old); until then a command it ran in the last 30 s can still start on the host directly. Restart the app to apply it at once. off and back needs a restart.
  • At explicit and listed, bash's fallback for a command the sandbox lacks prints sh: NAME: command not found (127) itself, in English.
  • When the host does not answer within 5 s (a hung session bus, proxy or agent), a command that was waiting for it runs as if the host did not have it: the sandbox's copy, or not found. In a signal handler (a crash reporter's) the host is not asked at all: a name not yet known to be on the host runs in the sandbox.
  • Sockets of a family the sandbox refuses (AF_VSOCK) are created on the host from level listed up.
  • Statically linked callers (Go programs, musl Rust such as Codex) are not seen by LD_PRELOAD; what they run through sh -c or another dynamic program still is.
  • Shells other than bash that search PATH themselves (zsh, fish inside the sandbox) do not forward a missing command; bash, sh (bash in the freedesktop runtimes) and anything that calls exec do.
  • The host cannot see the sandbox: a script in /app, an argument naming an /app file, or a callback such as GIT_ASKPASS=/app/... does not work on the host.
  • fexecve/execveat are not forwarded.
  • A process whose parent removed LD_PRELOAD has no library: VS Code's extension host and pty host and everything they start. There only shims (shims: in config.yaml) and bash's fallback for commands the sandbox lacks reach the host; a same-name command (git) runs in the sandbox.
  • Shims are made when the app starts: a newly configured shim appears after a restart, and a tool only a project directory has (mise, direnv) gets none unless the app was started there. Each app start costs one trip to the host (about 10 ms) that asks for the permission, the level and the shims at once.
  • An absolute path missing in the sandbox and on the host alike (/usr/bin/nope) has already been handed to the host when that turns out: the caller sees No such file or directory and exit 127 (a shell reports 127 as it would anyway), but Python's subprocess gets returncode 127 rather than FileNotFoundError. A bare name missing everywhere still fails exactly as without flatpark-spawn.
  • From a terminal, a forwarded command gets a terminal of its own on the host (so /dev/tty, sudo and ssh prompts, editors, fzf and less work), and Ctrl-C interrupts it. Ctrl-Z typed on the terminal does nothing; a stop sent by the calling shell (kill -TSTP) works. Keys typed ahead while such a command runs go to it, and are lost if it does not read them. A background job of the terminal gets no terminal of its own (as a native background job, it should not use one).
  • A forwarded command costs about 5 ms (a D-Bus round trip through the Flatpak session helper); a local one about 50 µs more than without flatpark-spawn. The first command in a directory not seen before costs one run of the user's login shell more (about 75 ms here), since the login PATH is taken per directory.
  • After mise use switches a tool's version, the host may run the old one for up to about a minute and a half (cached login PATH).

Building and testing

make            # lint, unit tests, build
make test       # Go unit tests, including the C routing rules through cgo
make payload    # .build/dist/flatpark-spawn-<version>-<arch>.tar.gz
make e2e        # see below

The tests of flatpark-spawn set run it with a temporary HOME, XDG directories and Flatpak installation (FLATPAK_USER_DIR, FLATPAK_SYSTEM_DIR, FLATPAK_CONFIG_DIR), calling /usr/bin/flatpak or a fake one through FLATPARK_SPAWN_FLATPAK: your config.yaml and overrides are never touched. make e2e also runs set and check APP-ID from inside the test apps (flatpak run --command=flatpark-spawn), with and without the permission, writing its own config.yaml and a Flatpak user directory of its own.

make e2e builds the payload, the extension and two test apps (freedesktop 26.08 and GNOME 51), installs them from a temporary local remote with the payload served by a local HTTP server, runs the matrix (at level full), the checks of every level (tests/e2e/levels.sh) and the timings in both, and removes everything it added. It builds under ids of its own (org.flatpark.SpawnE2E*, remote flatpark-spawn-e2e-run), so an installed org.flatpark.Spawn and the apps using it are not touched. It needs flatpak, org.flatpak.Builder, the two runtimes with their SDKs, python3 and script(1), and installs the Electron BaseApp for the run if it is missing. The agent reads a config.yaml of the run's own and writes status files to a directory of the run's own (FLATPARK_SPAWN_CONFIG, FLATPARK_SPAWN_STATE, set in the test apps), never ~/.config/flatpark-spawn or ~/.local/state. E2E_KEEP=1 make e2e leaves the test setup installed; tests/e2e/run.sh --clean removes it.

Names (flatpark-spawn, FLATPARK_SPAWN_*, the mount point, ...) are defined once, in names.mk; make gen regenerates internal/names and spawnconf/names.go.

The configuration's schema, the per-app policy (Resolve), the routing rules (Route, the same as the library's, tested against it), the status file and changes to config.yaml that keep its comments and layout (Edit) are a public Go package, github.com/flatpark/flatpark-spawn/spawnconf (pure Go, no cgo), for the FlatPark manager and other tools.

Releases: pushing a v* tag builds the x86_64 and aarch64 tarballs and publishes them with their sha256 and sizes, which packaging/org.flatpark.Spawn.yml pins as extra-data.

Credits

The pseudo-terminal code (internal/pty), the host-spawn command line and the approach of running commands through the Flatpak session helper's HostCommand come from 1player/host-spawn by Stéphane Travostino, under the MIT-0 license. Thank you.

License

MIT, see LICENSE.

About

Shared Flatpak extension that lets sandboxed apps run host toolchains transparently (LD_PRELOAD + HostCommand)

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages