Macaroon Capability Credentials
A macaroon (Birgisson et al., 2014, USENIX Security) is a bearer credential whose
holder can attenuate (narrow) but never broaden authority. The holder appends
caveats; each folds into a chained HMAC, so removing or tampering with one breaks the
signature. The root key that seeds the chain never leaves the minter — the holder can
present-and-verify but cannot forge or re-sign.
Canonical in-repo impl: core/kernel/pd-anchor/src/macaroon.rs (Rust, the source of
truth). Deprecated parity-target: lib/macaroon/ (TS). Schema: ADR-0053 Appendix A.
The chain (foundational — get the bytes exactly right)
sig_0 = HMAC_SHA256(root_key, identifier)
sig_i = HMAC_SHA256(sig_{i-1}, caveat_bytes_i)
first-party caveat_bytes = utf8(cid)
third-party caveat_bytes = vid_raw_bytes || utf8(cid) // vid first, then cid
No length prefixes, no type tags, UTF-8 for text. Any divergence in encoding or concat
order makes two implementations produce different signatures. Shared test vectors
(fixed keys + caveats → expected signature_hex) generated from the canonical impl and
asserted in BOTH test suites are the only real proof of byte-parity.
Decision point — which vid construction?
The third-party caveat's vid binds the discharge key into the chain. Two ways:
| Construction |
vid = |
Verifier needs |
Use when |
| HMAC commitment |
HMAC(chain_sig, caveat_key) |
the caveat key, from its own store (keyed by caveat id) |
the verifier IS the key-holder (daemon-is-verifier-and-keyholder). No AEAD. Port Daddy canonical. |
| AEAD sealing |
AES-GCM(KDF(chain_sig), caveat_key) |
only the root key (recovers caveat_key by decrypting the vid) |
decentralized verifiers with no key store (libmacaroons reference) |
Pick HMAC commitment when one trusted process mints, stores keys, and verifies — it's
simpler and needs no AEAD. Pick AEAD sealing only when verifiers can't share a key
store. Never run both in two live verifiers of the same credential — that's the
dual-runtime divergence (Port Daddy's TS once used AES-GCM, Rust uses HMAC commitment).
That divergence is now CLOSED (ADR-0054 Phase 6, verified 2026-07-14): lib/macaroon/
is realigned to the Rust HMAC-commitment vid and is a deprecated byte-parity fallback,
locked to the canonical impl by shared vectors in tests/fixtures/macaroon-parity-vectors.json
(asserted by both the Rust parity_vectors test and tests/unit/macaroon-parity.test.js).
Verification MUST be per-hop (the unsound shortcut)
Recompute the chain hop by hop. At a first-party caveat, check the predicate against the
request context. At a third-party caveat: resolve/confirm the caveat key, verify the
binding commitment, find the matching discharge (by identifier), recursively verify it
and require its signature to equal the request-bound value, then fold the caveat in.
The root is checked against its own signature; a discharge against
HMAC(BIND_KEY, root_sig || discharge_sig).
The naive "compare the final signature to a root-derived value" verifier is UNSOUND
for third-party/delegation chains — proven in ProVerif on
defense/anchor-attenuation-soundness. Always verify per-hop.
Request-binding (prepare_for_request): before presenting a discharge the holder rebinds
discharge.sig = HMAC(BIND_KEY, root_sig || discharge_sig) (BIND_KEY = 32 zero bytes),
so a stolen discharge can't be replayed against a different root.
Failure modes
| Symptom |
Cause |
Fix |
| A grant with two third-party caveats false-rejects the second as a "cycle" |
a never-popped seen visited-set used as a recursion guard |
use a depth bound (MAX_DISCHARGE_DEPTH), not a visited-set — siblings don't poison each other |
| Expired grants verify when a caller forgets the clock |
RequestContext::default() leaves now_ms = 0; 0 <= expires is always true (fail-open) |
expires check fails closed when now_ms <= 0; document the clock as required |
| Tag comparison leaks via timing |
early-return on first differing byte |
constant-time fold-XOR over full length |
| Rust and TS signatures differ on the same inputs |
byte-encoding / concat-order drift (UTF-16 vs UTF-8, cid||vid vs vid||cid) |
pin the exact bytes; assert against shared vectors |
| Hostile macaroon crashes the verifier |
unwrap/throw on malformed hex/JSON/oversized field |
fail closed — bounded hex decode, every parse returns a clean refusal, never throws/panics |
| Discharge replayed against a different grant |
discharge presented unbound |
require prepare_for_request binding; the verifier checks the bound signature |
Caveat grammar (Appendix A — structured, not free text)
Predicates field op value over a fixed, daemon-controlled field set; conjunctive
evaluation (every caveat must hold) is what makes authority one-directional — adding
spend_usd <= 100 over an existing <= 2 can't broaden, both hold and the tighter wins.
op = push|api-call · repo = <id> · branch = <glob> / branch != <name> ·
host = <fqdn> · spend_usd <= <n> · expires = <unix-ms> (fail-closed if now_ms<=0) ·
session = <id>. Glob via a small *-only two-pointer matcher (no regex). Fail closed on
an unparseable predicate or an absent context field.
Quality gates
NOT for
OAuth/OIDC/JWT web auth · centralized token servers (issue scoped tokens directly) ·
secrets inside caveats (caveats are constraints, not secrets) · confining a malicious
same-UID holder who can copy a live discharge mid-window (that needs OS/VM isolation —
Layer 3).
References
- Birgisson, Politz, Erlingsson, Taly, Vrable, Lentczner (2014), Macaroons: Cookies with Contextual Caveats for Decentralized Authorization in the Cloud, USENIX Security.
core/kernel/pd-anchor/src/macaroon.rs — canonical impl (HMAC-commitment vid, per-hop verify, depth bound, fail-closed clock, caveat grammar).
docs/adr/0053-out-of-band-enforcement.md Appendix A — the caveat schema + rent-paid discharge.
defense/anchor-attenuation-soundness (branch) — ProVerif proof that per-hop verification is sound and final-vs-root is not.
- libmacaroons (rescrv) — reference C impl, AEAD-sealing model, 32-byte BIND_KEY.
1---2name: macaroon-capability-credentials3description: >-4 Implement macaroons (Birgisson et al. 2014) as unforgeable, attenuate-only bearer5 capability credentials, with cross-language byte-parity. Use for capability tokens,6 bearer-credential attenuation, third-party caveats + discharge gates, delegation7 chains, "rent-paid"/compulsion caveats, or keeping a Rust and a TypeScript macaroon8 impl byte-identical via shared test vectors. Covers HMAC-chained first-party caveats,9 the two vid (verification-id) constructions (HMAC commitment vs AES-GCM sealing) and10 when to use each, per-hop verification soundness, request-binding, constant-time +11 fail-closed verification, and the structured caveat grammar. Pairs with12 agentic-zero-trust-security and rust-kernel-ffi. NOT for: OAuth/OIDC/JWT web-session13 auth, centralized token servers, or confining a malicious same-UID process (that needs14 OS/VM isolation — macaroons make the gate unforgeable, not the holder confined).15allowed-tools: Read, Write, Edit, Bash, Grep, Glob, WebSearch16io-contract:17 kind: deliverable18 produces:19 - kind: design-doc20 description: Capability-credential design with caveat/attenuation model, verification chain, and revocation strategy21 - kind: code22 description: Macaroon mint/attenuate/verify implementation for the target runtime23metadata:24 source: https://skillmd.com/skills/curiositech/macaroon-capability-credentials25---2627# Macaroon Capability Credentials2829A **macaroon** (Birgisson et al., 2014, USENIX Security) is a bearer credential whose30holder can **attenuate (narrow) but never broaden** authority. The holder appends31*caveats*; each folds into a chained HMAC, so removing or tampering with one breaks the32signature. The root key that seeds the chain never leaves the minter — the holder can33present-and-verify but cannot forge or re-sign.3435Canonical in-repo impl: **`core/kernel/pd-anchor/src/macaroon.rs`** (Rust, the source of36truth). Deprecated parity-target: `lib/macaroon/` (TS). Schema: ADR-0053 Appendix A.3738## The chain (foundational — get the bytes exactly right)3940```41sig_0 = HMAC_SHA256(root_key, identifier)42sig_i = HMAC_SHA256(sig_{i-1}, caveat_bytes_i)43first-party caveat_bytes = utf8(cid)44third-party caveat_bytes = vid_raw_bytes || utf8(cid) // vid first, then cid45```4647No length prefixes, no type tags, UTF-8 for text. Any divergence in encoding or concat48order makes two implementations produce different signatures. **Shared test vectors49(fixed keys + caveats → expected `signature_hex`) generated from the canonical impl and50asserted in BOTH test suites are the only real proof of byte-parity.**5152## Decision point — which vid construction?5354The third-party caveat's `vid` binds the discharge key into the chain. Two ways:5556| Construction | `vid = ` | Verifier needs | Use when |57|---|---|---|---|58| **HMAC commitment** | `HMAC(chain_sig, caveat_key)` | the caveat key, from its own store (keyed by caveat id) | the verifier IS the key-holder (daemon-is-verifier-and-keyholder). No AEAD. **Port Daddy canonical.** |59| **AEAD sealing** | `AES-GCM(KDF(chain_sig), caveat_key)` | only the root key (recovers caveat_key by decrypting the vid) | decentralized verifiers with no key store (libmacaroons reference) |6061Pick **HMAC commitment** when one trusted process mints, stores keys, and verifies — it's62simpler and needs no AEAD. Pick **AEAD sealing** only when verifiers can't share a key63store. **Never run both in two live verifiers of the same credential** — that's the64dual-runtime divergence (Port Daddy's TS once used AES-GCM, Rust uses HMAC commitment).65That divergence is now **CLOSED** (ADR-0054 Phase 6, verified 2026-07-14): `lib/macaroon/`66is realigned to the Rust HMAC-commitment `vid` and is a deprecated byte-parity fallback,67locked to the canonical impl by shared vectors in `tests/fixtures/macaroon-parity-vectors.json`68(asserted by both the Rust `parity_vectors` test and `tests/unit/macaroon-parity.test.js`).6970## Verification MUST be per-hop (the unsound shortcut)7172Recompute the chain hop by hop. At a first-party caveat, check the predicate against the73request context. At a third-party caveat: resolve/confirm the caveat key, verify the74binding commitment, find the matching discharge (by identifier), **recursively verify it75and require its signature to equal the request-bound value**, then fold the caveat in.76The root is checked against its own signature; a discharge against77`HMAC(BIND_KEY, root_sig || discharge_sig)`.7879> **The naive "compare the final signature to a root-derived value" verifier is UNSOUND**80> for third-party/delegation chains — proven in ProVerif on81> `defense/anchor-attenuation-soundness`. Always verify per-hop.8283Request-binding (`prepare_for_request`): before presenting a discharge the holder rebinds84`discharge.sig = HMAC(BIND_KEY, root_sig || discharge_sig)` (`BIND_KEY` = 32 zero bytes),85so a stolen discharge can't be replayed against a different root.8687## Failure modes8889| Symptom | Cause | Fix |90|---|---|---|91| A grant with two third-party caveats false-rejects the second as a "cycle" | a never-popped `seen` visited-set used as a recursion guard | use a **depth bound** (`MAX_DISCHARGE_DEPTH`), not a visited-set — siblings don't poison each other |92| Expired grants verify when a caller forgets the clock | `RequestContext::default()` leaves `now_ms = 0`; `0 <= expires` is always true (**fail-open**) | `expires` check fails closed when `now_ms <= 0`; document the clock as required |93| Tag comparison leaks via timing | early-return on first differing byte | constant-time fold-XOR over full length |94| Rust and TS signatures differ on the same inputs | byte-encoding / concat-order drift (UTF-16 vs UTF-8, `cid\|\|vid` vs `vid\|\|cid`) | pin the exact bytes; assert against shared vectors |95| Hostile macaroon crashes the verifier | unwrap/throw on malformed hex/JSON/oversized field | fail closed — bounded hex decode, every parse returns a clean refusal, never throws/panics |96| Discharge replayed against a different grant | discharge presented unbound | require `prepare_for_request` binding; the verifier checks the bound signature |9798## Caveat grammar (Appendix A — structured, not free text)99100Predicates `field op value` over a fixed, daemon-controlled field set; **conjunctive**101evaluation (every caveat must hold) is what makes authority one-directional — adding102`spend_usd <= 100` over an existing `<= 2` can't broaden, both hold and the tighter wins.103104`op = push|api-call` · `repo = <id>` · `branch = <glob>` / `branch != <name>` ·105`host = <fqdn>` · `spend_usd <= <n>` · `expires = <unix-ms>` (fail-closed if `now_ms<=0`) ·106`session = <id>`. Glob via a small `*`-only two-pointer matcher (no regex). Fail closed on107an unparseable predicate or an absent context field.108109## Quality gates110111- [ ] HMAC chain bytes match the spec exactly (utf8 cid; `vid||cid` for third-party); proven by shared vectors asserted in every language112- [ ] Per-hop verification (never final-vs-root); each discharge recursively verified + request-bound113- [ ] Constant-time tag compare; fail-closed on all malformed input (no throw/panic)114- [ ] Recursion is **depth-bounded**, not visited-set-guarded115- [ ] `expires` fails closed on an unset clock; verifier takes injected `now_ms`, never reads the system clock116- [ ] `prepare_for_request` binding required before a discharge is presented117- [ ] One canonical implementation; any second-language port is a byte-parity fallback locked by shared vectors, never an independent re-derivation118- [ ] Honest scope stated: unforgeable gate + verifiable audit, NOT same-UID confinement119120## NOT for121122OAuth/OIDC/JWT web auth · centralized token servers (issue scoped tokens directly) ·123secrets inside caveats (caveats are constraints, not secrets) · confining a malicious124same-UID holder who can copy a live discharge mid-window (that needs OS/VM isolation —125Layer 3).126127## References128129- Birgisson, Politz, Erlingsson, Taly, Vrable, Lentczner (2014), *Macaroons: Cookies with Contextual Caveats for Decentralized Authorization in the Cloud*, USENIX Security.130- `core/kernel/pd-anchor/src/macaroon.rs` — canonical impl (HMAC-commitment vid, per-hop verify, depth bound, fail-closed clock, caveat grammar).131- `docs/adr/0053-out-of-band-enforcement.md` Appendix A — the caveat schema + rent-paid discharge.132- `defense/anchor-attenuation-soundness` (branch) — ProVerif proof that per-hop verification is sound and final-vs-root is not.133- libmacaroons (rescrv) — reference C impl, AEAD-sealing model, 32-byte BIND_KEY.