Docs

Guide 06

Authorization

06. Authorization

Authorization in Avelon is 2 questions, 2 directories, and 2 enforcement points. Conflating them is why authorization bugs in most codebases are found in production.

QuestionLives inFailure looks like
PolicyMay this actor perform this actionapp/Policies/403, hidden button
WardWhich rows may this actor reach at allapp/Wards/empty result, or a data leak if wrong

Policies

Policies run in application code. They produce 403s and drive UI affordances.

// app/Policies/PostPolicy.ts
import type { PolicyAbilities } from '@avelonjs/core'
import type { Post } from '../Models/Post'

function authorId(post: unknown): string | undefined {
  return (post as Post | undefined)?.user_id
}

export const PostPolicy: PolicyAbilities = {
  viewAny: () => true,
  view: () => true,
  create: (actor) => actor !== null,
  update: (actor, post) => actor !== null && actor.id === authorId(post),
  delete: (actor, post) => actor !== null && actor.id === authorId(post),
}

A handler receives the actor and the resource the caller passed, typed unknown.

Register a Policy

Register the policy in registerApp() of app/Providers/AppProvider.ts.

reeve make:policy PostPolicy writes both the policy and the registration.

Check a Policy

From a controller:

import { Gate } from '@avelonjs/core'

await Gate.authorize('update', post) // throws Unauthenticated for a guest, Forbidden otherwise
if (await Gate.can('update', post)) {
  // render the edit link
}

Wards

Declared once in TypeScript. Enforced twice.

// app/Wards/PostWard.ts
import type { WardAbilities } from '@avelonjs/core'

export const PostWard: WardAbilities = {
  read: (actor) =>
    actor ? { or: [{ published: true }, { user_id: actor.id }] } : { published: true },
  insert: (actor) => (actor ? { user_id: actor.id } : false),
  update: (actor) => (actor ? { user_id: actor.id } : false),
  delete: (actor) => (actor ? { user_id: actor.id } : false),
}

Register a Ward

defineWard(Post, PostWard) in registerApp() registers it.

reeve make:model Post writes the ward and the registration along with the model.

reeve ward:explain Post prints the SQL condition each rule compiles to, for a guest and for an actor.

What a Ward Returns

A ward returns 1 of 3 things:

  • a predicate object that narrows the row set
  • true to allow all rows
  • false to deny everything

In the IR (D25):

  • true compiles to const true
  • false compiles to const false
  • the injected predicate is ANDed with the query's own where

A const false ward short-circuits without a database round trip.

Two Enforcement Paths

  1. reeve ward:sync calls the database driver's syncWards(). That compiles ward policies to Postgres row-level security and applies them. The database enforces them even when application code is wrong, which is the only protection that survives a bug in your own framework. The Supabase driver takes those policies as createSupabaseDatabase({ wards }).
  2. The query builder applies the same predicate as QueryIR.ward, so Post.query() is already scoped. Tests behave the same without a live policy, and results are consistent across environments.

A driver declares which paths it supports:

DriverrowSecurityPaths
Supabasetrueboth
Postgresfalsepath 2 only
Neonfalsepath 2 only

For Postgres and Neon, ward:sync reports that there's nothing to sync instead of silently doing half the job.

reeve ward:check

Fails when a model in app/Models has no matching ward in app/Wards. It prints the reeve make:ward command for each one.

Bailiff's require-ward rule fails the same model in bun run check, because a missing ward is a data leak rather than a bug.

Drift Checks

For tests that pin a ward's behavior, @avelonjs/core has 2 helpers:

  • detectWardDrift compares a compiled ward against fixture rows
  • detectPolicyDrift compares it against recorded policy SQL

Service Role Access

Deliberately ugly and deliberately narrow:

await Scrivener.unwarded(Post).query().get()

Bailiff restricts unwarded to these folders:

  • app/Errands/
  • database/seeds/

So it can never appear in a controller, a view, or an action reachable from a request.


Why the Separation Matters

Most stacks handle both questions in 1 policy class plus hand-written query scopes, and the two drift apart.

Say a policy allows only the author to update a post. A listing query forgets the where, so every user sees every draft. Nothing catches it, because the policy tests pass.

In Avelon the listing can't forget, because scoping is injected into the IR. On a driver with rowSecurity: true, the database refuses too, because the ward compiled to a policy.

That's 2 independent mechanisms from 1 declaration.


Correctness

The ward compiler is the only part of the framework where paranoia is proportionate. A predicate that compiles to the wrong policy is a data leak with no error message.

Required, and enforced in the tree:

  • Live driver suites compare ward scoping against database enforcement on identical fixtures. Any disagreement is a failure.
  • Those suites run against a live database, never a mock.
  • refusedWardShapes lists the predicate shapes the compiler refuses instead of approximating.