Metadata-Version: 2.4
Name: network-sandbox-engine
Version: 2.2.0
Summary: Headless Python engine for isolated network namespace testing and PCAP assertions.
Author: onyks
License-Expression: MIT
Project-URL: Homepage, https://github.com/onyks-os/NetworkSandboxEngine
Project-URL: Repository, https://github.com/onyks-os/NetworkSandboxEngine
Project-URL: Issues, https://github.com/onyks-os/NetworkSandboxEngine/issues
Keywords: networking,namespace,testing,nftables,pcap,scapy
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: System :: Networking
Classifier: Topic :: Software Development :: Testing
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: scapy>=2.5.0
Requires-Dist: pydantic>=2.0.0
Provides-Extra: cli
Requires-Dist: pyyaml>=6.0; extra == "cli"
Provides-Extra: dev
Requires-Dist: pytest>=8.0.0; extra == "dev"
Requires-Dist: pytest-asyncio>=0.23.0; extra == "dev"
Requires-Dist: pyyaml>=6.0; extra == "dev"
Requires-Dist: pytest-cov>=5.0.0; extra == "dev"
Requires-Dist: ruff; extra == "dev"
Requires-Dist: import-linter; extra == "dev"
Requires-Dist: mypy; extra == "dev"
Requires-Dist: mkdocs>=1.5.0; extra == "dev"
Requires-Dist: mkdocs-material>=9.5.0; extra == "dev"
Requires-Dist: mkdocstrings[python]>=0.24.0; extra == "dev"
Requires-Dist: build>=1.0.0; extra == "dev"
Requires-Dist: twine>=7.0.0; extra == "dev"
Requires-Dist: pip-audit>=2.7.0; extra == "dev"
Dynamic: license-file

<!--
Copyright (c) 2026 onyks-os
SPDX-License-Identifier: MIT
-->

<p align="center">
  <img src="https://raw.githubusercontent.com/onyks-os/NetworkSandboxEngine/main/assets/logo.svg" alt="Network Sandbox Engine" width="720">
</p>

<!-- --- -->

<!-- <h1 align="center">
  Network Sandbox Engine (NSE)
</h1> -->

<h4 align="center">A Linux engine for deterministic <b>nftables firewall testing</b> inside isolated network namespaces.</h4>

<p align="center">
  <img src="https://img.shields.io/badge/OS-Linux-blue?style=for-the-badge&logo=linux" alt="Linux">
  <img src="https://img.shields.io/badge/Python-3.10+-yellow?style=for-the-badge&logo=python" alt="Python">
  <a href="https://github.com/onyks-os/NetworkSandboxEngine/actions/workflows/ci.yml"><img src="https://img.shields.io/github/actions/workflow/status/onyks-os/NetworkSandboxEngine/ci.yml?style=for-the-badge&logo=github" alt="CI Status"></a>
  <a href="https://onyks-os.github.io/nse/"><img src="https://img.shields.io/badge/docs-mkdocs-526CFE?style=for-the-badge&logo=materialformkdocs&logoColor=white" alt="Documentation"></a>
  <a href="https://pypi.org/project/network-sandbox-engine/"><img src="https://img.shields.io/pypi/v/network-sandbox-engine?style=for-the-badge&logo=pypi" alt="PyPI"></a>
  <img src="https://img.shields.io/badge/License-MIT-green?style=for-the-badge" alt="License">
</p>

<p align="center">
  <a href="#why-nse">Why NSE?</a> •
  <a href="#the-oracle-contract">Oracle contract</a> •
  <a href="#asserting-on-the-wire">Wire assertions</a> •
  <a href="#features">Features</a> •
  <a href="#requirements">Requirements</a> •
  <a href="#installation">Installation</a> •
  <a href="#quickstart">Quickstart</a> •
  <a href="#examples">Examples</a> •
  <a href="#how-it-works">How It Works</a> •
  <a href="#project-structure">Project Structure</a>
</p>

## Why NSE?

Testing firewall rulesets on a live Linux system poses significant risks: malformed rules can drop SSH management sessions, leak cleartext traffic during testing, or leave orphan firewall tables active on the host.

Network Sandbox Engine (NSE) provides a safe, reproducible testing harness. It constructs ephemeral Linux network namespaces, wires virtual ethernet pairs, compiles `nftables` rulesets, and injects synthetic Layer 2 and Layer 3 packets using Scapy. All evaluation happens inside the sandbox namespace: host firewall state is never altered.

Key architectural properties:

* **The host firewall is never touched**: the ruleset under test is loaded only inside the sandbox namespaces. What a run does create on the host is temporary and declared; see [What a run changes on the host](#what-a-run-changes-on-the-host).
* **Self-verifying Oracle**: Every run injects a *canary* packet before the test packets and again after them, and reports a result only if the kernel trace for both was observed. See [The oracle contract](#the-oracle-contract).
* **Dual-Stack and Topologies**: Native support for IPv4 and IPv6 traffic, plus multi-namespace Gateway topologies for router, NAT, and forwarding ruleset validation.
* **Small, auditable surface**: One package, no web server, no JavaScript, with 98% test coverage enforced by a ratchet.

### What a run changes on the host

| Changed | Not changed |
| :--- | :--- |
| Network namespaces `nse_*` (`/var/run/netns`) | The host's nftables or iptables ruleset |
| The host end of each veth link (`vhr-*`), with its addresses | Host sysctls: forwarding and DAD settings are set inside the namespaces |
| In the gateway topology, routes to the sandbox subnets via that link | Existing interfaces, addresses and routes |
| Ownership journals and locks under `/run/nse` | Anything outside the run's own records |

Every one of these is recorded in an ownership journal before it is created,
and removed at teardown, or by the next NSE start if a run was killed.
Processes still running in a sandbox namespace are stopped before it is deleted.

Setup refuses to start rather than shadow anything: an interface name already in
use, or a sandbox subnet overlapping an existing host route, is an error before
the kernel is touched. A cleanup that cannot prove ownership preserves the
resource and says so.

### Requires root

NSE creates network namespaces, loads nftables rulesets and reads kernel trace
events, so it runs as root. It does not open a socket, a port or an RPC endpoint
of any kind — it is a library and a CLI that you invoke, and it holds privileges
only for the duration of a run.

Version 2.1.0 removed the FastAPI/Svelte web interface that earlier releases
shipped. That interface ran in-process as root from 2.0.0 onward, which was a
large attack surface for a testing tool; the code remains in the git history at
tag `v2.0.0` if you need it.

---

## The oracle contract

A firewall test is a negative assertion — *"this packet did not get through"* —
and a negative assertion is worth nothing unless the instrument is known to be
working. A trace monitor that never attached to the kernel and a firewall that
blocked everything produce byte-identical output.

NSE therefore refuses to report a verdict it cannot show it measured:

| Guarantee | Mechanism |
| :--- | :--- |
| The monitor was attached **before** the first test packet | A readiness canary is injected and re-injected until its kernel trace is observed. No observation, no run. |
| The monitor was **still** attached after the last one | A liveness canary runs after injection. If it is missed, the verdict stream is declared truncated. |
| The parser understood what the kernel said | Trace lines that no pattern matches are counted, and any count above zero is an error rather than a debug log. |
| The monitor did not die quietly | The read loop records *why* it ended — clean stop, unexpected EOF, timeout or crash — and only a clean stop is acceptable. |
| A missing verdict is not a pass | The CLI runner fails when the number of observed verdicts differs from the number expected, in either direction. |

Canary packets are excluded from results by trace id, so they never appear in
your verdict stream.

The suite proves this holds, rather than asserting it: `make test-blind` forces
the parser to understand nothing, and the build fails unless the runner exits
non-zero. That job runs in CI on every push.

---

## Asserting on the wire

`nft monitor trace` reports what the ruleset *decided*. It cannot report what
left an interface the ruleset never matched on, which is the question a
zero-leak claim actually asks. `PCAPAsserter` covers that: it captures on a
named interface, so a test can assert that a stimulus produced **no** frame.

```python
from nse import PCAPAsserter

asserter = PCAPAsserter(iface="vhr-...")
await asserter.start()   # returns once the kernel socket is filtering and bound
# ... send the stimulus ...
leaked = await asserter.stop()   # raises CaptureError if the capture broke
assert not leaked, leaked
```

`stop()` raises when the capture itself failed, and `stop_report()` says
whether the window was complete. Long captures can go to a PCAP file with
bounded memory.

None of that proves the capture was watching the path the stimulus took.
Readiness means the socket was armed on *that* interface; it does not mean the
stimulus crosses it. That evidence is the caller's to provide: capture the
stimulus once where it must be seen, and require it to be *seen* before
asserting its absence. [`examples/broken_observer.py`](examples/broken_observer.py)
shows the claim "nothing leaves" passing on the wrong interface, and its
positive control rejecting it.

**The default filter suppresses link-local noise, and nothing else.**
`DEFAULT_FILTER` excludes ARP and ICMPv6 types 133-137 - Neighbour Discovery,
which carries a hop limit of 255 and cannot reach a remote observer. Everything
else is captured, including ICMPv6 echo to a globally routable address. A
`filter` argument is combined with that default; `replace_default_filter=True`
uses yours alone. The escape hatch exists because the filter is compiled into
BPF and evaluated in the kernel: a packet it excludes never reaches userspace,
so no consumer can recover it downstream. Until 2.1.2 the default read `not arp
and not icmp6`, which discarded routable ICMPv6 as well - see
[issue #14](https://github.com/onyks-os/NetworkSandboxEngine/issues/14).

---

## Features

* **In-Process Engine**: Direct Python API (`run_test_pipeline`) returning structured Pydantic models (`TestRequest`, `TraceEvent`).
* **Scapy Packet Injection**: Forge arbitrary TCP (with custom SYN, ACK, FIN, RST flags), UDP, ICMP, and ICMPv6 packets.
* **Isolated Topologies**:
  * **Simple**: Single sandbox namespace (`nse_<id>`) wired directly to the host.
  * **Gateway**: Router (`nse_router_<id>`) and Server (`nse_server_<id>`) chain for forwarding and NAT testing.
* **Wire-level Leak Assertions**: `PCAPAsserter` captures on a named interface so a test can assert that a stimulus produced no frame at all. See [Asserting on the wire](#asserting-on-the-wire).
* **Ownership-aware Cleanup**: Private journals and owner locks protect active runs. Startup recovery removes only verified abandoned resources; prefix matches without ownership evidence are preserved. Failed teardowns retain their records for retry. See [Namespace lifecycle](docs/web/explanation/namespace-lifecycle.md).
* **CLI YAML Test Runner**: Execute declarative YAML test suites for automated CI/CD pipelines (`nse-runner`). Exits non-zero on a wrong verdict, on a verdict it failed to observe, and on a run that did not clean up. `--report-dir` keeps a JSON report and a SHA-256 manifest.
* **Reproducible experiment reports**: `run_test_pipeline_report()` records input, environment, outcome, observations and timings, so a CI artifact is enough to reconstruct a run.
* **Managed application processes**: real servers and clients run in the sandbox with a verified UID, GID and capabilities, timeouts, and a namespace-wide process boundary.
* **Strict Quality Standards**: Full static type checking (`mypy --strict`), architectural boundary enforcement (`import-linter`), `ruff` formatting, and a coverage ratchet (`make coverage`, floor 98%).

---

## Requirements

* **Linux OS** (Kernel 5.4 or later with network namespace and nftables support)
* **Python 3.10+**
* **nftables** (`nft`)
* **iproute2** (`ip`)
* **Root privileges** (required for `ip netns` and kernel trace operations)

On Debian or Ubuntu systems:

```bash
sudo apt update && sudo apt install -y nftables iproute2 conntrack
```

---

## Installation

### 1. PyPI Package (Recommended)

Install the core engine with CLI support:

```bash
pip install "network-sandbox-engine[cli]"
```

Upgrading from 2.1.x needs no code change: the 2.1.2 API is checked by a test
and on the installed packages. Some silent failures are now errors, and some
workarounds are no longer needed. See
[Upgrading to 2.2](https://onyks-os.github.io/nse/release-notes/upgrading-to-2.2/).

### 2. Manual Source Install

For local development:

```bash
git clone https://github.com/onyks-os/NetworkSandboxEngine.git
cd NetworkSandboxEngine
make setup
```

---

## Quickstart

### 1. Headless Python Library

```python
import asyncio
from nse.core.netns_controller import NetnsController
from nse.core.pipeline import run_test_pipeline
from nse.models.test_request import TestRequest, PacketSpec

rules = """
table ip filter {
    chain input {
        type filter hook input priority 0; policy drop;
        tcp dport 80 accept
    }
}
"""

request = TestRequest(
    rules=rules,
    packets=[
        PacketSpec(protocol="tcp", src_ip="10.0.0.1", dst_ip="10.0.0.2", dst_port=80),
        PacketSpec(protocol="tcp", src_ip="10.0.0.1", dst_ip="10.0.0.2", dst_port=22),
    ],
)


async def main():
    controller = NetnsController()
    events = await run_test_pipeline(request=request, controller=controller)
    for evt in events:
        if evt.verdict:
            print(f"[{evt.chain}] Verdict: {evt.verdict}")


asyncio.run(main())
```

### 2. YAML Test Suite Runner (CLI)

Create a test file `firewall_test.yaml`:

```yaml
tests:
  - name: "Allow HTTP Port 80, Drop SSH Port 22"
    topology: simple
    rules: |
      table ip filter {
        chain input {
          type filter hook input priority 0; policy drop;
          tcp dport 80 accept
        }
      }
    packets:
      - protocol: tcp
        src_ip: 10.0.0.1
        dst_ip: 10.0.0.2
        dst_port: 80
        expected_verdict: ACCEPT
      - protocol: tcp
        src_ip: 10.0.0.1
        dst_ip: 10.0.0.2
        dst_port: 22
        expected_verdict: DROP
```

`expected_verdict` is per packet. Unknown keys are rejected rather than
defaulted, so a typo fails the suite instead of quietly becoming an expectation
you never wrote.

Run the suite with root privileges:

```bash
sudo nse-runner --file firewall_test.yaml
```

Exit codes: `0` all packets matched; `1` a verdict was wrong **or** the engine
could not observe one. Oracle errors are reported separately from firewall
failures, because they mean the measurement broke, not the ruleset.

### 3. In a container

```bash
podman build -t nse .
podman run --rm --cap-add=NET_ADMIN --cap-add=NET_RAW \
    -v "$PWD/firewall_test.yaml:/suite.yaml:ro" nse --file /suite.yaml
```

Useful for pinning the nftables version your rules are tested against.

---

## Examples

[`examples/`](examples/) holds complete experiments, each with its input and
expected output. CI runs every one of them on a real kernel:

| Example | Shows |
| :--- | :--- |
| [`workstation.yaml`](examples/workstation.yaml) | A drop-by-default workstation policy, IPv4 and IPv6 |
| [`ipv6.yaml`](examples/ipv6.yaml) | A service published over IPv6 only |
| [`coexistence.yaml`](examples/coexistence.yaml) | An accept in one table does not override a drop in another |
| [`gateway_nat.yaml`](examples/gateway_nat.yaml) | Forwarding and masquerade through the gateway topology |
| [`real_application.py`](examples/real_application.py) | A real TCP/UDP server as `nobody`, a bounded file capture, positive controls |
| [`broken_policy.py`](examples/broken_policy.py) | A wrong ruleset with a sound observer: caught |
| [`broken_observer.py`](examples/broken_observer.py) | A blind oracle and a misplaced capture: both caught |

```bash
sudo nse-runner --file examples/gateway_nat.yaml
sudo python3 examples/real_application.py
```

### Who relies on NSE, and what stays with them

NSE is qualified against two independent consumers. One is
[TransparentTorProxy](https://github.com/onyks-os/TransparentTorProxy) (TTP).
Its test suite drives NSE's controller, rule engine, trace harvester, mock
listeners and `PCAPAsserter` for rule and leak-containment tests. The other is
the NAT gateway example above, which needs nothing from TTP. NSE guarantees the
measurement: an observed verdict, a confirmed capture, a verified process
identity, a clean teardown. Some decisions stay with the consumer:

* which verdicts are correct, and what counts as a leak;
* which positive control proves that a capture watches the path a stimulus
  takes;
* the topology and addresses, including a host whose routes collide with the
  defaults;
* retries, and what a failed run blocks in their pipeline.

---

## How It Works

NSE orchestrates Linux kernel network subsystems and trace interfaces through a structured multi-stage execution pipeline:

```mermaid
graph TD
    subgraph Step1["1. Test Specification"]
        Req["<b>TestRequest</b><br/>ruleset + packets + topology"]
    end

    subgraph Step2["2. Ephemeral Netns Sandbox"]
        direction TB
        Netns["<b>Netns Setup</b><br/>nse_&lt;id&gt; & veth links"]
        RuleEng["<b>Rule Engine</b><br/>validate & load nftables"]
        Inject["<b>Scapy Injector</b><br/>L2/L3 packet injection"]
        NFT["<b>Kernel nftables</b><br/>meta nftrace set 1"]

        Netns --> RuleEng
        RuleEng --> Inject
        Inject --> NFT
    end

    subgraph Step3["3. Trace Evaluation & Oracle"]
        direction TB
        Harvester["<b>Trace Harvester</b><br/>nft monitor trace stream"]
        Oracle["<b>Deterministic Oracle</b><br/>TraceEvents & verdicts"]

        Harvester --> Oracle
    end

    Step1 --> Step2
    Step2 --> Step3
```

1. **Ruleset Validation**: `RuleEngine.validate()` dry-runs the ruleset using `nft --check -f`.
2. **Sandbox Provisioning**: `NetnsController` creates the isolated network namespace and configures virtual ethernet (`veth`) interfaces.
3. **Trace Initialization**: Rulesets are loaded into the namespace with kernel tracing armed (`meta nftrace set 1`).
4. **Packet Injection**: `ScapyInjector` injects synthetic frames across the veth link.
5. **Verdict Harvesting**: `TraceHarvester` captures `nft monitor trace` events and returns structured `TraceEvent` objects.
6. **Teardown**: The namespace and all associated veth interfaces are automatically deleted.

For complete technical specifications, see the [Technical Architecture Guide](docs/ARCHITECTURE.md).

---

## Project Structure

```text
NetworkSandboxEngine/
├── nse/                        # Core PyPI package (network-sandbox-engine)
│   ├── core/                   # Kernel primitives, pipeline, and naming rules
│   ├── models/                 # Pydantic models (TestRequest, PacketSpec, TraceEvent)
│   └── cli/                    # Headless YAML runner entrypoint
├── docs/                       # Architecture specs and MkDocs web documentation
├── tests/                      # Unit, golden file, and privileged e2e tests
│   └── fixtures/nft_trace/     # Golden `nft monitor trace` corpus
├── pyproject.toml              # Build backend configuration
└── Makefile                    # Local automation and CI workflow
```

---

## Releasing

One tag. `git push origin vX.Y.Z` builds, signs with Sigstore, publishes the
GitHub Release, uploads to TestPyPI, installs from TestPyPI and smoke-tests it,
and only then uploads to PyPI. Rehearse with `make release-dry`.

See [docs/RELEASING.md](docs/RELEASING.md).

## Documentation

Full interactive web documentation is available at:  
[https://onyks-os.github.io/nse/](https://onyks-os.github.io/nse/)

Build the documentation locally:

```bash
make docs
```

Serve the documentation with hot-reload on `http://127.0.0.1:8000`:

```bash
make docs-serve
```

---

## Testing & Local CI

Run static linting and unit tests:

```bash
make verify
```

Run the full local CI pipeline (lint, type check, import boundaries, unit tests, docs build, and the release artifacts):

```bash
make ci-local
```

---

## License

This project is licensed under the [MIT License](LICENSE).
