Making a forgotten check a compile error
The previous chapter made a value impossible to construct wrongly. This one does the same to an event: proving that a check happened.
The usual way to gate a web handler is to call something at the top:
// the shape indice does NOT useasync fn delete_crawl(state: State<Arc<AppState>>, headers: HeaderMap) -> Response { if !may_curate(&state, &headers) { return forbidden(); } // …}This works until the next handler someone adds forgets the first line. Nothing breaks or warns, and the endpoint is open.
A type as proof
Section titled “A type as proof”pub(super) struct Curator(Principal);pub(super) struct Admin(Principal);Two newtypes again, and again the field is private, this time to server::auth.
Nothing outside that module can construct one. You cannot build a Curator, you
can only receive one, and the extractor is the only thing that hands them out:
impl axum::extract::FromRequestParts<Arc<AppState>> for Curator { type Rejection = Denied; async fn from_request_parts(/* … */) -> Result<Self, Self::Rejection> { require(state, &parts.headers, Role::Curator, "curating this archive") .map(Curator) }}axum runs this for any handler argument of that type; if require fails the
handler never runs. So a handler declares its own privilege in its signature:
pub(super) async fn create_annotation( State(state): State<Arc<AppState>>, curator: Curator, // …) -> ResponseHolding a Curator is the proof that the check ran, because there is no other
way to have one.
The pattern is called a witness type, or a capability: a value whose
existence testifies that something was established. It costs nothing at runtime,
since it wraps a Principal the handler needed anyway, and the compiler, rather
than the person writing the handler, remembers the check.
A proof as a precondition
Section titled “A proof as a precondition”fn new_import_job( admin: &Admin, state: &Arc<AppState>,) -> (u64, mpsc::UnboundedSender<ProgressEvent>) { state.start_job(admin.principal())}Starting an import spends the operator’s own Browsertrix or Archive-It
credentials, so it should not be reachable from a handler where someone forgot an
extractor. Because this function takes an &Admin, it cannot be called without
one. The compiler enforces the precondition.
The proof does a second job here. It names who started the import, and only that
person may read its progress. start_index_job has the same shape with a
&Curator, and also reads it for principal().id(), because a crawl records who
accessioned it, so the one argument carries both the proof and the identity.
Sometimes the proof is all there is:
pub(super) async fn new_collection_form( State(state): State<Arc<AppState>>, _curator: Curator, headers: HeaderMap,) -> Response {This handler never reads its Curator. It asks for one so that a reader isn’t
shown a form whose every control would refuse them. When an argument exists only
to constrain who may call a function, write it as _name with a comment saying
so.
Roles, ordered
Section titled “Roles, ordered”Role::Reader < Role::Curator < Role::AdminA fieldless enum deriving Ord, so a check is one comparison and each role
contains the one below it. Two witness types rather than one
requires(role: Role) parameter, because two is the number there are, and a type
per role puts the privilege in the signature instead of in an argument you have
to read.
The rule it encodes is one sentence: curators add and describe; admins remove, moderate, and act as the instance.
A third type, for a different question
Section titled “A third type, for a different question”enum Evidence { Loopback, Proxy, Cookie }Identity and evidence are separate questions. indice sets a display-only cookie so ungated public pages can show workroom chrome to a signed-in curator. That cookie must never authorize a write, because a cookie is what a cross-site request brings along for free.
An enum rather than a comment puts the rule (Cookie renders, it does not
authorize) somewhere the compiler helps: a match that gains an arm, or forgets
one, is a build error rather than a silent hole.
The limit
Section titled “The limit”A witness type proves a check ran. It cannot prove it was the right one:
Curator on a handler that should have demanded Admin compiles fine. indice
covers that with a test that walks every management route and asserts the
privilege each one demands.
These patterns move forgotten checks from runtime to compile time, and you still write tests for the part that is a judgement call.
What to take forward
Section titled “What to take forward”- A private field plus a single constructor turns “this was checked” into a value you can pass around.
- An unused parameter can be a precondition;
_nameand a comment say so. - Enums make the cases exhaustive, so adding one breaks the code that needs updating.
- Types catch forgetting. Tests catch choosing wrong.