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>
This commit is contained in:
1 parent
abde27c130
commit
5e468ef525
1 file changed
+12
-1
+12
-1
@@ -1,7 +1,7 @@
|
|||||||
---
|
---
|
||||||
name: pola
|
name: pola
|
||||||
description: >-
|
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 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.
|
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
|
# POLA: organize code by the flow of authority
|
||||||
@@ -30,6 +30,16 @@ So at each level, code has two roles:
|
|||||||
- **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.
|
- **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)).
|
- **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
|
```ts
|
||||||
// wrong: one scope holds everyone's purses; Bob's code could spend Alice's
|
// wrong: one scope holds everyone's purses; Bob's code could spend Alice's
|
||||||
const alice = { usdc: await E(usdc.issuer).makeEmptyPurse(), ... };
|
const alice = { usdc: await E(usdc.issuer).makeEmptyPurse(), ... };
|
||||||
@@ -57,3 +67,4 @@ Before committing, for each new maker, scope, and export:
|
|||||||
2. Does any non-wiring scope hold two parties' authority, or more than its task needs?
|
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?
|
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?
|
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?
|
||||||
Reference in new issue
Block a user