Skip to main content

shellsafe

Run shell commands safely using Python 3.14 template strings. Values can never turn into commands.

from shellsafe import run

message = get_user_input()          # "fix; rm -rf ~"
run(t"git commit -m {message}")
# argv: ["git", "commit", "-m", "fix; rm -rf ~"]
# one command; the scary text is just an argument

Why this package exists

Python 3.14 added template strings (PEP 750). Now Python keeps your fixed text and your values separate at the language level.

Shell commands are the first place people want to use this. That is because f-strings inside shell commands have caused real security bugs for ten years:

subprocess.run(f"git commit -m {message}", shell=True)
# if message = "fix; rm -rf ~"  ->  two commands run. The second one is bad.

Python planned to solve this officially (PEP 787), but that PEP was deferred to at least Python 3.15. So today there is no standard way to run shell commands safely with templates. This package fills that gap.

Install

pip install shellsafe

Needs Python 3.14 or newer.

How to use

Run a command. Your values always stay one argument each. run() never invokes a shell, so injection is impossible on any platform:

from shellsafe import run

run(t"mkdir {path}")
run(t"docker build -t {tag} .", check=True, timeout=300)

Get the output as text:

from shellsafe import capture

res = capture(t"grep {pattern} {file}")
print(res.stdout, res.returncode)

Need pipes? Works on Linux and macOS. Your values are quoted safely first:

from shellsafe import shx

shx(t"cat {file} | wc -l")

Want to see exactly what will run?

from shellsafe import plan

print(plan(t"git commit -m {message}"))
# argv: ["git","commit","-m","fix; rm -rf ~"]

Safety rules

Case What happens
Any value you pass becomes one argument, exactly as given
Value used as the program name error: program names must be written as fixed text
Shell features on Windows error: we cannot make Windows safe this way, so we say no
RAW() misuse error: one value only, used as-is, no nesting

RAW(...) marks content you have already made safe by hand. It is loud and easy to find in code review, so trust is never hidden.

Find old dangerous patterns

Already have code using f-strings in shell commands? The scanner finds them:

shellsafe audit src/

It detects f-strings passed to os.system, subprocess.run(shell=True), and other shell executors. Get machine-readable output:

shellsafe audit src/ --json

Filter by severity:

shellsafe audit src/ --severity warning

Limits

  • Shell features (pipes, redirections) work on Linux and macOS only. Windows supports plain commands only.
  • run() refuses templates that contain shell metacharacters. Use shx() for those.
  • Byte values are rejected. Decode them first.
  • We keep your command safe to build and run. Testing what your command does is still your job.

Needs

  • Python 3.14 or newer
  • Linux, macOS, Windows (pipes work on Linux and macOS only)

More

License: MIT

Download files

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

Source Distribution

shellsafe-0.3.2.tar.gz (21.4 kB view details)

Uploaded Source

Built Distribution

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

shellsafe-0.3.2-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file shellsafe-0.3.2.tar.gz.

File metadata

  • Download URL: shellsafe-0.3.2.tar.gz
  • Upload date:
  • Size: 21.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for shellsafe-0.3.2.tar.gz
Algorithm Hash digest
SHA256 8b609ac736d36bf8c7a48fd5d10ee17133f40c689811a0c0af248e87d5557036
MD5 822ef77ad1648b2d4709ebfe8c85f6fc
BLAKE2b-256 1fbd12c6d054bf92d2fb411118a470213537d5850ca52891d41946f57bd74faa

See more details on using hashes here.

Provenance

The following attestation bundles were made for shellsafe-0.3.2.tar.gz:

Publisher: release.yml on rahulXs/shellsafe

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file shellsafe-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: shellsafe-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 15.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for shellsafe-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c031cd12c2cdba61de154445b2db7dd58323b3b79c64c1bf9b2de749d54ac6df
MD5 40e4de8f6f2dc5d70e03f3f3eddf38cf
BLAKE2b-256 1427f431dffbe5f871df604b49bcdb493c6015f49fe05cb374749af49e79be1e

See more details on using hashes here.

Provenance

The following attestation bundles were made for shellsafe-0.3.2-py3-none-any.whl:

Publisher: release.yml on rahulXs/shellsafe

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.3.2 This release

2 files

0.3.1

2 files

0.3.0

2 files

0.2.0

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page