
A deterministic network sandbox for testing nftables rules. It uses ephemeral Linux network namespaces (netns) and Scapy to validate firewall logic safely.
Why NSE? • Oracle contract • Wire assertions • Features • Requirements • Installation • Quickstart • Examples • How It Works • Project Structure
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:
nse.stimuli sends UDP, TCP, ICMP, DNS, QUIC-shaped and STUN traffic from a socket in a process running as a chosen UID/GID, so output-hook rules on skuid/skgid are tested as they run. nse.receivers are known servers that log every arrival. nse.observe() reports sent, on the wire, received and reply seen as separate facts, and "unknown" whenever the capture or the log cannot vouch for an absence. See stimuli, receivers and observing a stimulus.nse.streams.StimulusStream sends a numbered stimulus at a fixed rate while a rule reload or a restart happens, and correlate() says for each attempt whether it was not sent, refused, sent but not seen, on the wire or received. A stream that paused, a capture that missed a beacon frame or an incomplete log makes the window inconclusive, never clean. See streams.| 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 |
With create_namespace(..., isolated_host=True) the host end of the link is not
in the host either: it goes into a namespace of its own, nsh_<name>, created and
removed with the sandbox. By default, traffic from the sandbox to the host end
is local input for the host and crosses the host's own firewall. firewalld, for
one, drops it. NSE warns when the host ruleset may filter it; see
the host endpoint.
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.
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.
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.