The BEAM Edge · For Elixir developers
Phoenix 1.8 scopes: authorization the generators write
We generated a Phoenix 1.8 app with phx.gen.auth and phx.gen.live. Every bookings query is filtered by owner, and the generator wrote tests that check it.
Short answer In Phoenix 1.8, phx.gen.auth creates a scope and makes it the default, so phx.gen.live writes context functions that filter every query by the current user, set the owner from the scope, and recheck it on writes. It also generates the tests. Our generated app passed all 128 tests.
In our teardown of Lovable's April incident, the lesson was that access rules break quietly when nothing asserts them. Phoenix 1.8 takes a different route: it writes the rule into the code its generators produce, and writes the tests that assert it, before you or your AI assistant add a single line.
Phoenix calls this a scope, a struct that carries who is calling, passed as the first argument to every context function the resource generators write, such as list_bookings/1. The 1.8.0 release announcement describes scopes as "designed to make secure data access the default, not something you remember (or forget) to do later"Source 1. We generated a small clinic app to see what that means in code.
What phx.gen.auth and phx.gen.live wrote
We ran mix phx.new clinic --database sqlite3 --no-assets --no-dashboard --no-mailer --no-gettext (and added Swoosh and a Mailer when gen.auth asked for them), then mix phx.gen.auth Accounts User users --live, then mix phx.gen.live Bookings Booking bookings starts_at:utc_datetime notes:string, on Phoenix 1.8.15 and Elixir 1.20.4. The auth generator set its user scope as the default, so the second generator built a scoped resource without being askedSource 2. Here is the heart of what it wrote:
Elixir
# lib/clinic/bookings.ex, as generated
def list_bookings(%Scope{} = scope) do
Repo.all_by(Booking, user_id: scope.user.id)
end
def get_booking!(%Scope{} = scope, id) do
Repo.get_by!(Booking, id: id, user_id: scope.user.id)
end
def update_booking(%Scope{} = scope, %Booking{} = booking, attrs) do
true = booking.user_id == scope.user.id
# ...
end
# lib/clinic/bookings/booking.ex, as generated
def changeset(booking, attrs, user_scope) do
booking
|> cast(attrs, [:starts_at, :notes])
|> validate_required([:starts_at, :notes])
|> put_change(:user_id, user_scope.user.id)
endFour decisions are already made for you:
- Every read filters by owner.
list_bookings/1andget_booking!/2only ever see rows whoseuser_idis the scope's user. Another user's booking id raisesEcto.NoResultsError, the same as a missing row, so the browser gets a 404. - The owner comes from the scope, not the form. The changeset sets
user_idwithput_changeand does not cast it, so a crafted form field cannot reassign a booking. - Writes check ownership again.
update_booking/3anddelete_booking/2matchtrue = booking.user_id == scope.user.id, so a booking loaded the wrong way raises aMatchErrorinstead of being changed;change_booking/3has the same check. It is a crash, not an error tuple, so no caller can ignore it. - Broadcasts are scoped too. The generated PubSub topic is keyed by the user's id, so one user's LiveView does not receive another user's updates.
And the tests that keep it that way
The generator also wrote ten context tests for bookings, four of which are about scope: a list only returns the caller's rows, and reading, updating or deleting with a different user's scope raises. After adding the four routes inside the authenticated live_session, as the generator asked, the whole generated suite passed: 128 tests.
Those four tests are the first rows of the access matrix from our Lovable post, written for you at the context level. The six generated LiveView tests never try another user's booking, so add that row yourself: sign in as a second user, open the first user's /bookings/:id and /bookings/:id/edit with live/2, and assert_raise Ecto.NoResultsError. Keep all of them; when you or an agent later add an admin role or team sharing, these are the tests that tell you whether the old rule still holds.
Why this matters more with an AI assistant
Scopes are a convention that an assistant can follow without knowing your business. New Phoenix 1.8 projects also ship an AGENTS.md with guidelines for LLM-assisted developmentSource 1; and phx.gen.auth adds a section to it that says: "Always pass the assign current_scope to context modules as first argument. When performing queries, use current_scope.user to filter the query results". That instruction steers an agent toward the scoped path instead of a quick unscoped query.
The Phoenix guide is direct about the stakes: OWASP lists broken access control as a top-10 security risk, and most application data is private to a user, a team or an organizationSource 2. Scopes do not make an app secure on their own. They make the secure version the easy one to keep.
Keeping scopes intact
- Generate with scopes, then extend the struct, for example with a team or role, instead of adding unscoped functions next to the scoped ones.
- Keep the generated scope tests, and add a row each time you add a role.
- Treat an unscoped `Repo` call in a context as a review flag. If a function does not take a scope, someone should be able to say why.
- Point your agent at AGENTS.md, and add your own scope rules to it.
Next step
Get new posts as they come out
Every new post, in plain English and in depth. Follow the RSS feed.
Sources
- Phoenix 1.8.0 released (Phoenix blog). Retrieved . ↩ ↩
- Scopes (Phoenix 1.8.15 guides). Retrieved . ↩ ↩