Four rules that keep a Python tool that runs as root honest (from building Noust)
Four rules that keep a Python tool that runs as root honest (from building Noust)
Noust is a free (AGPL) Python 3.10+ CLI and FastAPI service that deploys web apps onto Linux servers. It runs as root, so a few classes of bugs are expensive. The project ended up with four rules, each enforced by a test, and each one exists because its absence shipped a real bug:
1. Nothing runs a process except one module. Only core/runner.py may import subprocess. Argv only, never a shell, timeouts mandatory, secrets through env= or input= (never argv, which every local user can read in ps). The test suite's conftest makes any real process call fail, so code that bypasses the runner can't even be tested. --dry-run lives there too, which is why it can't be honoured in one path and ignored in another.
2. except Exception only at an error boundary, and it must log. There used to be 302 of them, 149 silent; that's how calls to methods that didn't exist shipped for whole releases. mypy blocks CI.
3. One implementation of each thing. The web API is a client of the same managers the CLI uses, never a second implementation.
4. Guards at the chokepoint. Unit ownership is checked in the service manager, read-only SQL is enforced by the database server, escaping by the template engine.
The contributing guide explains each one with the bug behind it: https://github.com/Perkybeet/noust/blob/main/CONTRIBUTING.md
Interested in how others structure similar tools.
Comments (2)