Files
mad-skills/pola/SKILL.md
T
dckcandClaude Opus 5.5 5e468ef525 pola: shaping an API: attenuate rather than allowlist, flower-shaped handles, mechanism vs policy
From designing a toybox Discord bot (dckc/vattr97#57): access by narrowed
capability, not a checked allowlist; the service hands out and narrows
access while its creator composes policy (a $1-per-user seller); narrow
then fire; narrowings follow the protocol (host patterns for resume URLs);
patterns as data rather than predicates; refs to secrets, redeemable only
where they belong; one timer primitive with schedules as a library.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 20:56:06 -06:00

71 lines
7.8 KiB
Markdown

---
name: pola
description: >-
Principle of Least Authority as the way to organize all code: trace the flow of authority, give each component only what its task needs, design APIs around attenuation. Use when writing or reviewing any code, tests included: makers and their parameters, module imports and exports, which objects share a scope, what a facet exposes; when designing a service's API (attenuation methods, flower-shaped narrow-then-fire handles, mechanism vs policy, secrets in payloads); when tempted to write an allowlist; when someone says "POLA", "least authority", "who can reach that?", "Bob shouldn't see Alice's purse", or "makeFoo-style actors". The table stakes (no clocks, fetch, or env reached for inside logic) are no-ambient-authority; this is the organizing principle on top.
---
# POLA: organize code by the flow of authority
Mark Miller, [SOSP 50th anniversary panel](https://www.youtube.com/watch?v=br9DwtjqmVI) (3 min):
> we design abstraction boundaries so that a component's interface only requires arguments that are somehow relevant to its task. If such argument passing were the only source of authority, we would have taken a huge step towards least authority.
Every reference to anything but immutable data is authority: a purse is the right to spend some assets; a mint, the right to create them; a seat, the right to take part in one deal; a DB handle, every row. Ask of every reference: who holds it, who gave it to them, and does their task need all of it?
## Recursively, at every level
[Navigating the Attack Surface](https://www.youtube.com/watch?v=wW9-KuezPp8) (15 min): the same move repeats at each level of composition (OS accounts, apps, modules, objects), and the benefit multiplies, like a Menger sponge. CapMail's main module "receives all of that authority … wiring them together, subdividing the authority … and handing it out piecemeal … written knowing that the address book does not need access to the PGP keyring, so the main module simply doesn't give it that access."
So at each level, code has two roles:
- **A wiring layer** (entry point, `main`, test setup, a maker) holds a lot of authority, and its job is to split it and hand out pieces. Keep it small and obvious: it is the TCB at its level.
- **Everything else** gets only the pieces its task needs, as arguments, and can't reach more.
## In practice
- **Makers take only what their task needs.** `makeAlice({ usdcPurse, stockDepositFacet })`, not `makeAlice({ ledger })`.
- **Attenuate at API boundaries.** Hand out the narrowest facet that works: a deposit facet rather than the purse; a seat rather than the escrow's admin; a read-only view rather than the DB. When designing an API, decide which facets it has and who should get each.
- **Separate parties get separate scopes.** If two parties' authority shares a scope, a bug or a careless line lets one act as the other. A scope holding both Alice's and Bob's purses is a design error.
- **Tests too.** Tests are where the code's intended authority flow is first written down. Write each party as a `makeFoo`-style actor holding its own authority privately; the test's top level is the wiring layer that creates the parties and introduces them ([sequence-diagram-actor-sim](https://github.com/Agoric/agoric-sdk/blob/master/.agents/skills/sequence-diagram-actor-sim/SKILL.md)).
- **The test takes the spontaneous step; actors carry out the consequences.** Give an actor what it knows at construction and let it decide how to act: `alice.run()`, or `alice.tradeWith(bob)` to introduce a counterparty. Don't drive it with arguments that decide for it, like `alice.pay(to, amount)`: that is the test acting with Alice's authority, as if it held her purse. What an actor hands others (a deposit facet, a shop) carries no authority to spend for it.
- **Modules decide what authority enters through imports and leaves through exports.** Export makers, not instances that close over power ([no-ambient-authority](../no-ambient-authority/SKILL.md)).
## Shaping an API: hand out authority, then narrow it
- **Attenuate; don't allowlist.** Access control is which narrowed capability you were handed, not a list some component consults. A bot doesn't check senders against an allowlist; its holder narrows "hear everyone" to "hear Dan" and hands the bot only that. Then the bot can't hear anyone else, and no code needs to check.
- **A service is mechanism; its creator sets policy.** A service knows how to hand out and narrow access, and nothing about who should get it, what they say with it, or on what terms. The party that creates the service composes its pieces with others. New kinds of authority come out of that composition: a governor that fences a mower by pushes, or a seller that sells "talk to user X" at $1, refusing after N sales. Neither the mower nor the Discord vat knows about fences or prices.
- **Make it flower-shaped: narrow, then fire.** A handle to something wide offers narrowings that each return a narrower handle, and the act comes last: `socket.pathjoin(p).withHeaders(h).connect(listener)`, not `connect(url, headers, listener)`. Each step can only narrow: patterns intersect, and a header that contradicts an earlier grant throws. The raw device can keep the wide call. A vat over it hands out the flower, as WebApp does over the fetch device.
- **Narrow along the lines the protocol needs.** Check where the authority actually has to go. Discord sends a resuming client to another host, which a handle narrowed by path can't reach. A narrowing by host pattern (`withinHost('*.discord.gg')`) covers exactly that, so there's no need to widen the handle back to "anywhere".
- **A narrowing is data, not a callback.** Pass a pattern (an Endo `M` pattern; it's passable, so it can cross vats) for the service to apply. A predicate object called on every item sees everything it filters, so it already holds the authority it was meant to narrow.
- **Hand out a reference to a secret, not the secret.** When the secret has to go inside a payload, such as a token in a WebSocket frame, a splicer holds the secret and gives out a ref exo it alone can redeem. Accept the ref only in the fields that carry it to its proper recipient. If a field Discord broadcasts would also accept it, anyone with the ref can publish the secret.
- **One primitive, with libraries on top.** A timer device offers `wakeAt(t, handler)`; repeaters and cron schedules are a library over it, and each schedule is its own object whose holder can cancel only that one. Reading something is authority too: `wake(now)` reveals the clock, so a vat that may wait but not read the time gets a delay-only facet.
```ts
// wrong: one scope holds everyone's purses; Bob's code could spend Alice's
const alice = { usdc: await E(usdc.issuer).makeEmptyPurse(), ... };
const bob = { usdc: await E(usdc.issuer).makeEmptyPurse(), ... };
await E(seatB).offer(await E(bob.stock).withdraw(shares(3n)), ...);
// right: each party holds its own; the test wires, drives, and observes
const makeAlice = async ({ usdcIssuer, stockIssuer, dollars }) => {
const usdc = await E(usdcIssuer).makeEmptyPurse(); // Alice's alone
...
return harden({
// Alice decides what to pay; Bob shows only his shop
tradeWith: async bob => E(bob.shop).sell(await E(usdc).withdraw(price), ...),
getBalances,
});
};
await alice.tradeWith(bob); // the one spontaneous step
```
## Review checklist
Before committing, for each new maker, scope, and export:
1. Draw (in your head or as a [wiring-diagram](../wiring-diagram/SKILL.md)) who holds what after this change.
2. Does any non-wiring scope hold two parties' authority, or more than its task needs?
3. Could a narrower facet replace what's passed? Does any facet let a caller spend for its owner, or does the test decide what an actor does?
4. Name the wiring layer at this level. Is it small?
5. Does any component check an allowlist, or take a policy decision that its creator could make by narrowing what it hands out?