Skip to main content

Run commands in a restricted user systemd sandbox

Project description

sandboxa

sandboxa runs a command inside a restricted environment scoped to a target project directory.

It is designed to reduce accidental host exposure while still allowing practical project work.

What it does

  • Launches via systemd-run --user --pty.
  • Applies isolation properties:
    • ProtectHome=tmpfs
    • PrivateTmp=true
    • PrivateDevices=true
  • Sets the command working directory to the target project path.
  • Binds the target project path into the sandbox.
  • Optionally binds extra paths (--extra-data-path / YAML extra_data_paths).
  • Optionally binds OpenCode configuration paths when allow_settings includes opencode.
  • Applies InaccessiblePaths for YAML excluded_paths entries.
  • Refuses to run if a marker path exists in project (default: .profile).

Security model

  • Uses systemd-run --user isolation properties to reduce what the launched process can see from the host (ProtectHome=tmpfs, PrivateTmp=true, PrivateDevices=true).
  • Grants filesystem access by explicit bind mounts only:
    • target project path
    • optional OpenCode config paths when allow_settings includes opencode
    • optional extra_data_paths
  • Supports explicit deny-listing inside project scope via excluded_paths (InaccessiblePaths) for paths that currently exist.
  • Adds a safety guard against accidentally sandboxing a complete home-like directory by refusing to run when the marker path exists (default: .profile).
  • Applies strict config validation (pydantic, unknown YAML keys are rejected) to prevent silent misconfiguration.

Non-goals

  • Not a full container/VM boundary; this is a practical isolation wrapper, not a hardened multi-tenant sandbox.
  • Not a replacement for system-level mandatory access controls or seccomp policy authoring.
  • Not a network isolation tool; networking behavior is not managed here.
  • Not a privilege-escalation mechanism; it runs as the invoking user via systemd-run --user.
  • Not a generic policy engine; supported controls are intentionally narrow and centered on path exposure for local development workflows.

Requirements

  • Linux with user systemd session support (systemd-run --user).
  • Python 3.
  • Python packages:
    • ruamel.yaml
    • pydantic

Install dependencies:

pip install pydantic ruamel.yaml

Install sandboxa:

pipx install sandboxa

Usage

./sandboxa \
  [RUN_COMMAND] \
  [--project-path PATH] \
  [--extra-data-path PATH ...] \
  [--excluded-path PATH ...] \
  [--allow-settings NAME ...] \
  [--refuse-if-exists NAME] \
  [--config-file FILE] \
  [--no-reconnect] \
  [--dry-run] \
  [-- COMMAND_ARGS...]

Common examples:

# Run default command (opencode) in current directory
./sandboxa

# Run bash in current directory sandbox
./sandboxa bash

# Target explicit project directory
./sandboxa --project-path /path/to/project

# Pass args to run command
./sandboxa bash -- -lc "echo smoke-ok"

# Add extra bind paths
./sandboxa --extra-data-path /data/shared --extra-data-path /mnt/cache

# Override excluded paths from CLI
./sandboxa --excluded-path build/ --excluded-path secrets/

# Do not check for existing sessions to be resumed.
./sandboxa opencode --no-reconnect

# Preview effective config and command without executing
./sandboxa bash --dry-run -- -lc "echo smoke-ok"

Configuration file

By default the script reads user config from ~/.config/sandboxa.yaml and project config from .sandboxa.yaml in the target project directory. Use --config-file to override the project config path; absolute paths are supported.

Supported keys:

  • allow_settings: <list[string]> (see Settings Templates below)
  • extra_data_paths: <list[path]>
  • excluded_paths: <list[string]>
  • refuse_if_exists: <string>

Example:

allow_settings:
  - opencode

extra_data_paths: []

excluded_paths:
  - build/
  - secrets/

refuse_if_exists: .profile

Precedence:

  • CLI arguments override YAML values.
  • Project YAML values override user YAML values for most keys.
  • For allow_settings and excluded_paths, user and project YAML values are merged.
  • YAML values override built-in defaults.

Settings Templates (allow_settings)

Even in a constrained environment there is sometimes the need to use data from your real home directory. This could be the configuration of your favorite editor or the cache of your userspace package manager.

Some popular use-cases are implemented in sandboxa in order to ease the burden of finding the required files to be accessible in the sandbox.

The following contexts are supported.

Keyword for allow_settings Purpose Limitations
opencode OpenCode -
podman Podman not working for root-less containers (see issue)
ssh OpenSSH agent and key material exposes ~/.ssh and optionally SSH_AUTH_SOCK if set
vim VIM without history (use extra-data_path: [~/.vim/] if you really need it)

License

This project is licensed under GNU GPL v3 or later. See LICENSE for the full text.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sandboxa-0.1.0.tar.gz (23.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sandboxa-0.1.0-py3-none-any.whl (20.0 kB view details)

Uploaded Python 3

File details

Details for the file sandboxa-0.1.0.tar.gz.

File metadata

  • Download URL: sandboxa-0.1.0.tar.gz
  • Upload date:
  • Size: 23.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for sandboxa-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3d2b92d93c7700e2181729fd99beb7e42d9c3b2fd8872681e1667f645842b673
MD5 0eed35481e15738e9a5520a13360830d
BLAKE2b-256 ededa95055b85a1d06148936623dabcc7e06c969c2ad06e321504d537275bf0a

See more details on using hashes here.

File details

Details for the file sandboxa-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: sandboxa-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 20.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for sandboxa-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 08cccf9f23a52c03b7fe02960dcd66e4332a0bf160f7b37e19a35fa8c6a8903b
MD5 54bb1b323568d0443fc633fff73d5d3f
BLAKE2b-256 32da17c3910b150d56d1c7795f3e8a61fda4d0d906046fb3a719c251164b985e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page