Skip to main content

A robust Python module to safely source a bash script in a sandboxed environment and collect its variables.

Project description

BashVar Sentry

BashVar Sentry is a Python utility module that securely extracts Bash variable declarations from scripts by sourcing them in a sandboxed environment. It supports sandboxing via chroot, bwrap, or fakechroot to reduce risk from untrusted content.


📦 Installation

Install from PyPI:

pip install bashvar-sentry

🧰 Usage

As a Python module

from bashvar_sentry import source_and_get_vars

variables = source_and_get_vars(
    "example.sh",
    sandbox_method="auto",           # "auto", "chroot", "bwrap", "fakechroot", "empty"
    jail_dir="/",                    # Optional: target root dir for sandbox
    extra_env={"MYVAR": "from_python"},
    additional_args=["one", "two"]
)

print(variables)

📄 Bash Script Requirements

Your script must be syntactically valid (bash -n is run first).


Example Script: example.sh

#!/bin/bash

ARG1=$1
ARG2=$2
ENV_CAPTURED="$MYVAR"

declare -a FRUITS=("apple" "banana split")
declare -A CONFIG=([host]="localhost" [port]="8080")

Output

{
  "ARG1": "one",
  "ARG2": "two",
  "ENV_CAPTURED": "from_python",
  "FRUITS": ["apple", "banana split"],
  "CONFIG": {"host": "localhost", "port": "8080"}
}

🔐 Sandbox Methods

Method Isolation Root Required Notes
chroot Full Yes Needs /usr/sbin/chroot
bwrap Strong No Needs bwrap binary
fakechroot Simulated No Must have fakechroot
empty Minimal No Sets PATH=""
auto Best fit No Picks the first available

🚫 Caveats

  • Scripts are sourced, not executed. This means:
    • Side effects can persist in the current shell context.
    • Background jobs, subshells, etc., may behave differently.
  • Environment is isolated if you use sandboxing. If not, it's up to you.

✅ Testing

Run tests with:

python -m pytest

To test sandbox fallbacks, install:

  • /usr/sbin/chroot and run as root (for chroot) - This auto skips if not root
  • bwrap
  • fakechroot

📄 License

Apache-2.0

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

bashvar_sentry-0.0.4.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

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

bashvar_sentry-0.0.4-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file bashvar_sentry-0.0.4.tar.gz.

File metadata

  • Download URL: bashvar_sentry-0.0.4.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for bashvar_sentry-0.0.4.tar.gz
Algorithm Hash digest
SHA256 620ea0e26c994828edad31663eb5e29ea318bd379198c6e2bf9f15d2227d4537
MD5 ba138f627f82c98c96a8c2b59ba38c82
BLAKE2b-256 37926a01e467d253a60b74d1bffdaa7a02b91f23f08c4c0e5c400e2388c14081

See more details on using hashes here.

Provenance

The following attestation bundles were made for bashvar_sentry-0.0.4.tar.gz:

Publisher: pypi-publish.yml on envolution/bashvar-sentry

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

File details

Details for the file bashvar_sentry-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: bashvar_sentry-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for bashvar_sentry-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 23b9203979f8d9438f404fd5caf0f892ad74cc58e7f1a769a30f78f5c294e8b9
MD5 2c177d3f17754af3babe58d3431aa1db
BLAKE2b-256 261a2d034d418c08be6d48ada9d00d662859810c45b2f83cd23afa2dab861f00

See more details on using hashes here.

Provenance

The following attestation bundles were made for bashvar_sentry-0.0.4-py3-none-any.whl:

Publisher: pypi-publish.yml on envolution/bashvar-sentry

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