feat(no-ambient-authority): Python past the checker: each part an object

A voice assistant's script passed the DisciplinedPython checker yet held
every power in one 300-line main of nested functions and threads. Dan's
review, refactoring it: no bespoke wrappers or fallible calls in
_script_io; main only wires; a class, not a function returning
functions; powers in __ attributes; designation bound to authority once;
asyncio, not threads; top-level imports bring data, not powers.

Generated-by: Claude Opus 5.5
This commit is contained in:
dckc committed 2026-12-01 12:44:25 -06:00
1 parent cde1632421
commit 035bf497c8
1 file changed
+41
+41
View File
@@ -101,6 +101,8 @@ before writing or refactoring a Python script; don't invent conventions.
Known checker bug: it flags `sqlite3.connect(':memory:')` as DPY010,
but an in-memory database is private to the process, not I/O; leave
that call in place.
- Beyond the checker: passing it is the floor. A script that grows
past one job needs the shape below too; see the next section.
- Calibration: Python can't enforce a capability boundary (name mangling
and introspection get around any wrapper), so for a small script,
attenuation that lives mostly in names is fine. An example: a read-only
@@ -108,6 +110,45 @@ before writing or refactoring a Python script; don't invent conventions.
note the gap honestly and move on. Robustness against adversaries
means leaving Python (e.g. for Hardened JavaScript).
## Python: each part an object, its constructor its authority list
A script that passes the checker can still hold every power in one
`main` full of nested functions. Factor it so each part's constructor
says what it can touch, and a reviewer needn't read its body:
- **`_script_io()` only snapshots.** Pass standard authorities as they
are: `gpiod.request_lines`, `socket.socket`,
`asyncio.create_subprocess_exec`, `dict(os.environ)`. No bespoke
wrappers (`socket_from_fd`, a `urlopen` that builds the request), and
nothing that can fail: `socket.socket(fileno=3)`, or deciding from
`LISTEN_FDS`, happens in `main`.
- **`main` only wires.** Nested functions closing over powers are
objects waiting to be named: give each a class, in a module of its
own, constructed with just the powers it uses:
`HatLines(request_lines, chip)`, `Brain(ask_client, button)`.
- **A class, not a function returning functions.**
`open_lines(...) -> (fd, read_edges, set_led)` is an object with its
methods scattered; make it a class with `fileno()`, `read_edges()`,
`set_led()`.
- **Powers in `__` attributes:** `self.__client`, not `self._client`.
Name mangling enforces nothing (see Calibration), but it marks which
attributes are authority, and keeps neighbors from reaching in by habit.
- **Bind designation to authority once.** A `Command(exec, program,
*leading_args)` runs one program with its URL fixed, so the part that
holds it can't point it elsewhere; `poster(open_connection, url)`
reaches one HTTP server.
- **An event loop, not threads.** asyncio: no locks, no shared state
polled from threads, and a race written as one:
`asyncio.wait({work, press}, return_when=FIRST_COMPLETED)`.
Subprocesses through `create_subprocess_exec`, sockets through
`start_unix_server` and `open_connection`, a device's fd through
`loop.add_reader`.
- **Top-level imports bring data, not powers.** `from asyncio.subprocess
import PIPE`, or a library's data types (`gpiod.LineSettings`), are
fine at the top; the powers come in through `main`. Importing a module
that holds powers isn't fatal while they go unused, but importing just
the names used is cleaner.
## how this shows up in review
- `crypto.getRandomValues()` called inside an exported function →