Skip to main content

Secure, sandboxed bash tool access for AI agents

Project description

Safe, guardrailed shell access for AI agents.

PyPI Python CI License: MIT

safeshell gives your LLM agents (LangChain, PydanticAI, or custom) the power to run shell commands with built-in safety rails. While traditional subprocess wrappers are unsafe and Docker containers are heavy, safeshell provides a graduated security model that works instantly in any Python environment. It features built-in protection against dangerous patterns like rm -rf /, curl | sh, fork bombs, and more.

Roadmap

See our Detailed Roadmap for upcoming features like Windows support, Daemon mode, and OS-level sandboxing (macOS/Linux).

Use Cases

  • Agentic coding assistants: Allow agents to run ls, cat, grep, and mv to modify codebases safely.
  • Data analysis pipelines: Let LLMs explore CSV/JSON files using head, tail, jq, and awk without risking system stability.
  • Automated DevOps: Create restricted agents that can restart specific services (via allowlist) but cannot modify system configuration.
  • Educational tools: Provide a safe shell environment for students to practice bash commands.

Features

  • Security-first: Blocks 20+ dangerous command patterns (rm -rf /, fork bombs, etc.) out of the box.
  • Instant startup: No Docker or VMs required. Works anywhere Python runs.
  • Three security levels:
    • STANDARD: Blocks known exploits (Default).
    • PARANOID: Allowlist-only (e.g., only allow ls and grep).
    • PERMISSIVE: Logging-only for trusted environments.
  • Read-only mode: Agents can "modify" files in a temporary overlay without touching your actual disk.
  • Dynamic tool discovery: Automatically generates LLM-optimized prompts based on available tools (grep, jq, git, etc.).
  • Framework-agnostic: First-class support for generic Python, LangChain, and PydanticAI.

Installation

pip install safeshell

Quick Start

import asyncio
from safeshell import create_sandbox_tool, SecurityViolation

async def main():
    # Create a sandboxed toolkit
    toolkit = await create_sandbox_tool(source="./my_project")
    
    # Run commands safely
    result = await toolkit.bash("ls -la")
    print(result.stdout)
    
    # Dangerous commands are blocked automatically
    try:
        await toolkit.bash("rm -rf /")
    except SecurityViolation as e:
        print(f"Blocked: {e}")
    
    await toolkit.close()

asyncio.run(main())

Integrations

LangChain

Safeshell creates compatible StructuredTool objects for LangChain agents.

from safeshell import create_sandbox_tool
from safeshell.integrations.langchain import create_langchain_tools

toolkit = await create_sandbox_tool()
tools = create_langchain_tools(toolkit)  # Returns dict of tools

# Use with any LangChain agent
agent = create_react_agent(llm, list(tools.values()))

PydanticAI

Use the create_pydantic_ai_tools helper to inject tools into PydanticAI agents.

from safeshell import create_sandbox_tool
from safeshell.integrations.pydantic_ai import create_pydantic_ai_tools

toolkit = await create_sandbox_tool()
pydantic_tools = create_pydantic_ai_tools(toolkit)

# Register with your agent
for tool in pydantic_tools:
    agent.tool(tool)

Blocked Patterns

Safeshell blocks 20+ dangerous command patterns, including:

Category Pattern Description
Filesystem rm -rf / Recursive delete of root
mkfs Filesystem creation
RCE curl | sh Pipe remote script to shell
wget | python Pipe remote script to Python
Resource :(){ :|:& };: Fork bomb
yes | Infinite output pipe
Privilege sudo Sudo commands
su - Switch user
Disk dd of=/dev/sda Direct disk write
chmod 777 / Dangerous permissions

Contributing

We welcome contributions! Please follow these steps:

  1. Fork the repository.
  2. Install dependencies:
    pip install -e ".[dev]"
    
  3. Run tests:
    make test
    
  4. Submit a Pull Request.

License

MIT

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

safeshell-1.0.0.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

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

safeshell-1.0.0-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file safeshell-1.0.0.tar.gz.

File metadata

  • Download URL: safeshell-1.0.0.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for safeshell-1.0.0.tar.gz
Algorithm Hash digest
SHA256 97059be1a0ae053e2a04a90250003e89b7a4b08a1fc4befc0b711c89af7de876
MD5 2c5db6f7feb5605e12de2d92aa70f50c
BLAKE2b-256 5f8ebc3680b4a1967c28c8a0eec0d12d548f8294a053bccd54ea8f5f6612205b

See more details on using hashes here.

Provenance

The following attestation bundles were made for safeshell-1.0.0.tar.gz:

Publisher: publish.yml on Khadka-Bishal/safeshell

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

File details

Details for the file safeshell-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: safeshell-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 18.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for safeshell-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5882b784907a8f20ea807d7b7fb5f42a73cb66aa15045c78bf7e5dbb496b7c99
MD5 7c285ad32eafd0eac6fd8ca99f595b67
BLAKE2b-256 b7c25e988cecb62a817b64a5bbf9f768fd815c4d48cdeb7e8371031e894af0ee

See more details on using hashes here.

Provenance

The following attestation bundles were made for safeshell-1.0.0-py3-none-any.whl:

Publisher: publish.yml on Khadka-Bishal/safeshell

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

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