0
Challenge · 69The Data API & Your Next.js App
Route protection in proxy.ts & the three-layer gate
Appused in ~80% of apps
An intern "protects" DevLog's compose page by returning null from the page component when there's no user. It looks secure — a signed-out visitor sees a blank screen instead of a form. Next.js's own docs call this exact pattern out by name as unsafe: the app has multiple entry points, and returning null from one component blocks nothing else. Then the intern adds proxy.ts and redirects signed-out visitors away from /compose — better, but if that becomes the ONLY check, the compose form's own Server Action is still one crafted POST request away from anyone who never loaded the page at all.
Core idea. Route protection is three independent layers, not one gate with a redirect in it. proxy.ts is a fast, coarse, cookie-only check that runs on every request and improves UX by keeping signed-out visitors off pages they can't use — but Next.js's own guidance calls this an "optimistic" check, never the only line of defense. The Server Action a protected page calls must independently re-verify identity and, crucially, ownership of the specific row it's about to touch — because it receives arguments the proxy never sees. RLS is the only layer that is an actual guarantee: it runs inside Postgres on every statement, regardless of what called it or whether either layer above remembered to check anything at all.
poke it, drag it,
try to break it
try to break it
no slides here.
you drive.
you drive.
Playground, this isn't a screenshot
Attacker: bob, trying to edit alice's post by calling
updatePost(alice_post_id, …) directly — no page load, straight to the action.1 · proxy.ts route guardintact
if (!user && pathname.startsWith('/compose')) redirect('/login')2 · Server Action re-checkintact
const { data } = await supabase.auth.getClaims()
if (!data) throw new Error('unauthorized')
if (post.user_id !== data.claims.sub) throw new Error('forbidden')3 · RLS policy (Postgres)cannot be disabled from here
using ( (select auth.uid()) = user_id )
with check ( (select auth.uid()) = user_id )Stopped at layer 1. bob never even reaches the action — the proxy redirected him before the request was routed. Fast, cheap, but this layer only ever sees requests that go through pages it matches.
↑ play a little first, then dig in