Part 4 of 13 · 2 min read
A derived grant can only be narrower than the one it came from
A delegation step that could accidentally widen access is a bug waiting for the one time someone gets the arguments backwards.
Think of photocopying a visitor pass and handing the copy to someone else. A legitimate copier only ever makes a pass that opens the same doors or fewer, for the same hours or less. A copier that could somehow produce a pass with more access than the original would defeat the entire point of having an original.
attenuation means deriving a grant that is a subset of its parent, and the derivation itself refuses to run if any requested field. A wider resource pattern, an extra action, a higher cap, a later expiry, more allowed uses -- would make the child broader than the parent. That means a sub-agent or a per-task grant built this way can never accidentally exceed what it was derived from.
A worked example. Deriving a per-task grant from a parent that covers customers/* with a read action, requesting the narrower resource customers/3, succeeds and produces a grant scoped to just that one customer. Attempting to derive a grant from a parent capped at max_amount=100 while asking for max_amount=101 raises a ValueError immediately -- the attenuation function refuses outright rather than silently clamping the value down or ignoring the request.
The key lines. From code/mini-privilege/privilege.py:
# from code/mini-privilege/privilege.py
def attenuate(parent, resource=None, actions=None, max_amount=None, expires_at=None, max_uses=None):
"""Derive a narrower grant. Raises ValueError if any field would be broader than the parent's."""
res = resource or parent.resource
if res != parent.resource and not fnmatch.fnmatchcase(res, parent.resource):
raise ValueError("resource broader than parent")
acts = frozenset(actions) if actions is not None else parent.actions
if not acts <= parent.actions:
raise ValueError("actions broader than parent")
Read it as: every field gets its own explicit "would this be broader" check before the new grant is built. There are five separate guard clauses in this function, one per field. Each one raises rather than silently correcting. That is the difference between "this delegation step is safe by construction" and "this delegation step is safe as long as the caller passes sensible arguments.".
Attenuation
Attenuation is deriving a grant that is a subset of its parent.
Example: a per-task grant built by attenuating a standing customers/* read grant down to one customer, three uses and an expiry is attenuation doing exactly what Part 1's 53-unit worst case depends on -- the narrow grant isn't hand-written from scratch, it's provably derived from something broader in a way that cannot widen it back out.