Sandbox Module¶
Core sandbox implementation for read-only command execution using Linux namespaces and Bubblewrap.
Overview¶
The sandbox module provides the foundational building blocks for creating secure, read-only execution environments. It offers declarative configuration through profiles and handles the low-level details of constructing Bubblewrap commands.
Key Components:
SandboxProfile- Immutable sandbox configuration describing allowed commands, bind mounts, and environmentSandboxManager- High-level interface for executing commands within a sandboxBubblewrapCommandBuilder- Translates profiles into Bubblewrap argument vectorsSandboxBind- Declarative bind mount specificationSandboxError- Exception raised for configuration or execution failures
Quick Start¶
from shannot import SandboxManager, load_profile_from_path
from pathlib import Path
# Load a profile
profile = load_profile_from_path("~/.config/shannot/minimal.json")
# Create a sandbox manager
manager = SandboxManager(profile, Path("/usr/bin/bwrap"))
# Execute a command
result = manager.run(["ls", "/"])
print(result.stdout)
Common Usage Patterns¶
Creating Profiles Programmatically¶
from shannot import SandboxProfile, SandboxBind
from pathlib import Path
profile = SandboxProfile(
name="custom",
allowed_commands=["ls", "cat", "grep"],
binds=[
SandboxBind(
source=Path("/usr"),
target=Path("/usr"),
read_only=True
)
],
tmpfs_paths=[Path("/tmp")],
environment={"PATH": "/usr/bin"},
network_isolation=True
)
Loading from Files¶
from shannot import load_profile_from_path, load_profile_from_mapping
# From JSON file
profile = load_profile_from_path("/etc/shannot/diagnostics.json")
# From dictionary
data = {
"name": "minimal",
"allowed_commands": ["ls"],
"binds": [{"source": "/usr", "target": "/usr", "read_only": True}],
"tmpfs_paths": ["/tmp"],
"environment": {"PATH": "/usr/bin"}
}
profile = load_profile_from_mapping(data)
Executing Commands¶
# Basic execution
result = manager.run(["cat", "/etc/os-release"])
# With custom environment
result = manager.run(["env"], env={"CUSTOM_VAR": "value"})
# Without automatic error checking
result = manager.run(["test", "-f", "/missing"], check=False)
if result.succeeded():
print("File exists")
Related Documentation¶
- Python API Guide - Comprehensive API usage examples
- Profile Configuration - Profile format and options
- CLI Usage - Command-line interface
- Process Module - Process execution utilities
API Reference¶
sandbox
¶
Foundational utilities for constructing and executing read-only sandbox sessions.
This module intentionally focuses on declarative configuration and pure data
structures so higher-level entrypoints (e.g. sandbox.py) can
remain small and testable. The initial implementation is designed around the
Bubblewrap (bwrap) tool because it offers fine-grained control over Linux
namespaces, bind mounts, and seccomp integration without depending on a daemon.
Key abstractions
SandboxProfile— Immutable description of the sandbox. Profiles are kept independent from execution details so they can be serialized, linted, and unit-tested in isolation.BubblewrapCommandBuilder— Translates aSandboxProfileplus the caller's requested command into a deterministicbwrapargument vector.SandboxManager— Coordinates validation and invocation; exact runtime mechanics will be implemented in a future revision once profile parsing, command building, and testing scaffolding are in place.
The goal of this scaffolding is to provide well-documented, type-checked hooks that subsequent work can extend without refactoring prior code.
Classes¶
SandboxError
¶
SandboxBind
dataclass
¶
Describe a bind mount that should be applied inside the sandbox.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path
|
Path on the host that will be mounted into the sandbox. |
required |
target
|
Path
|
Path inside the sandbox where the source will be exposed. |
required |
read_only
|
bool
|
Whether the bind should be read-only. Defaults to |
True
|
create_target
|
bool
|
Whether the target path should be created inside the sandbox prior to
the bind. When |
True
|
Source code in shannot/sandbox.py
SandboxProfile
dataclass
¶
Declarative description of the sandboxing policy.
Only simple, serializable datatypes are used so that profiles can be loaded
from YAML or JSON documents without custom hooks. Future iterations may add
convenience constructors (e.g. from_mapping) once the serialization
format is finalized.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Human-readable identifier for logging and auditing. |
required |
allowed_commands
|
Sequence[str]
|
Shell globs or absolute paths that the caller is permitted to execute. Allowlist enforcement is performed by higher-level orchestrators. |
tuple()
|
binds
|
Sequence[SandboxBind]
|
Collection of |
tuple()
|
tmpfs_paths
|
Sequence[Path]
|
Directories that should be backed by tmpfs for ephemeral storage. |
tuple()
|
environment
|
Mapping[str, str]
|
Environment variables to expose inside the sandbox. |
dict()
|
seccomp_profile
|
Path | None
|
Optional path to a seccomp JSON profile consumed by |
None
|
network_isolation
|
bool
|
When |
True
|
user_namespace_isolation
|
bool
|
When |
True
|
additional_args
|
Sequence[str]
|
Extra |
tuple()
|
Source code in shannot/sandbox.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | |
Functions¶
from_mapping(data, *, base_path=None)
classmethod
¶
Construct a SandboxProfile from a plain mapping.
Source code in shannot/sandbox.py
validate()
¶
Raise SandboxError if the profile contains invalid entries.
Source code in shannot/sandbox.py
BubblewrapCommandBuilder
¶
Convert a SandboxProfile into a Bubblewrap command invocation.
The builder performs deterministic argument ordering so unit tests can make stable assertions. At this stage the builder does not consider runtime details (e.g. existence of certain directories); those checks belong in the eventual executor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
profile
|
SandboxProfile
|
|
required |
command
|
Sequence[str]
|
Sequence representing the command to run inside the sandbox. This is
appended after |
required |
validate_paths
|
bool
|
Whether to validate that bind source paths exist locally. Set to False for remote execution where paths exist on the remote system only. Defaults to True for backward compatibility. |
True
|
Source code in shannot/sandbox.py
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 | |
Functions¶
build()
¶
Return the complete bwrap argument list without the executable name.
The caller is responsible for prepending the path to the Bubblewrap
binary (commonly /usr/bin/bwrap) prior to execution.
Source code in shannot/sandbox.py
SandboxManager
¶
Orchestrate sandbox validation and execution.
The SandboxManager can now use different execution strategies via the executor parameter. This allows for local execution (LocalExecutor) or remote execution (SSHExecutor) while maintaining backward compatibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
profile
|
SandboxProfile
|
The profile to use when launching sandboxed commands. |
required |
bubblewrap_path
|
Path | None
|
Filesystem location of the Bubblewrap executable. Used for backward compatibility when executor is not provided. |
None
|
executor
|
SandboxExecutor | None
|
Optional executor instance (LocalExecutor or SSHExecutor). If not provided, uses legacy direct execution with bubblewrap_path. |
None
|
Examples:
Legacy usage (backward compatible): >>> manager = SandboxManager(profile, Path("/usr/bin/bwrap")) >>> result = manager.run(["ls", "/"])
New usage with LocalExecutor: >>> from shannot.executors import LocalExecutor >>> executor = LocalExecutor() >>> manager = SandboxManager(profile, executor=executor) >>> result = manager.run(["ls", "/"])
New usage with SSHExecutor: >>> from shannot.executors import SSHExecutor >>> executor = SSHExecutor(host="prod.example.com") >>> manager = SandboxManager(profile, executor=executor) >>> result = manager.run(["ls", "/"])
Source code in shannot/sandbox.py
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 | |
Attributes¶
profile
property
¶
Return the active sandbox profile.
bubblewrap_path
property
¶
Return the resolved Bubblewrap executable path.
Returns None when using an executor instead of direct bubblewrap_path.
executor
property
¶
Return the executor if one is configured.
Functions¶
build_command(command)
¶
Construct the full Bubblewrap invocation for the requested command.
The returned list includes the Bubblewrap executable at index 0 followed
by the arguments produced by BubblewrapCommandBuilder.
Note: Only used in legacy mode (when executor is None).
Source code in shannot/sandbox.py
run(command, *, check=True, env=None)
¶
Execute command inside the sandbox and return a ProcessResult.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
Sequence[str]
|
Command and arguments to execute within the sandbox. |
required |
check
|
bool
|
When |
True
|
env
|
Mapping[str, str] | None
|
Optional environment overrides passed to the Bubblewrap launcher. |
None
|
Raises:
| Type | Description |
|---|---|
SandboxError
|
Raised when the command is not permitted or exits with a non-zero
status while |
Source code in shannot/sandbox.py
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 | |
run_async(command, *, check=True, timeout=30)
async
¶
Execute command inside the sandbox asynchronously using an executor.
This method requires an executor to be configured. It delegates execution to the executor, which can be LocalExecutor (for local execution) or SSHExecutor (for remote execution).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
Sequence[str]
|
Command and arguments to execute within the sandbox. |
required |
check
|
bool
|
When |
True
|
timeout
|
int
|
Command timeout in seconds (default: 30). |
30
|
Raises:
| Type | Description |
|---|---|
SandboxError
|
Raised when the command is not permitted, no executor is configured,
or the command exits with a non-zero status while |
Examples:
With LocalExecutor: >>> from shannot.executors import LocalExecutor >>> executor = LocalExecutor() >>> manager = SandboxManager(profile, executor=executor) >>> result = await manager.run_async(["ls", "/"])
With SSHExecutor: >>> from shannot.executors import SSHExecutor >>> executor = SSHExecutor(host="prod.example.com") >>> manager = SandboxManager(profile, executor=executor) >>> result = await manager.run_async(["ls", "/"])
Source code in shannot/sandbox.py
Functions¶
load_profile_from_mapping(data, *, base_path=None)
¶
Create a SandboxProfile from an in-memory mapping.
Source code in shannot/sandbox.py
load_profile_from_path(path)
¶
Load a SandboxProfile from a JSON configuration file.