agentleFS
vibestackdevvibestackdev/vibe-stack/.cursor/rules/supabase-auth-security.mdc

vibe-stack / rules

Cursor rule8 starsChanged 8 months agoMITView on GitHub

Supabase auth security — enforces getUser() over getSession() on the server

The file

We split this supabase-auth-security.mdc into the rules it gives an agent

3 rules. About 0 are common, 0 are distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Always import from @supabase/ssr for cookie-based sessions.

    line 30
  2. Very specific:

    Every protected route requires middleware that calls updateSession().

    line 44
  3. Very specific:

    When implementing email signup, create a callback route at app/auth/confirm/route.ts that exchanges the token_hash using verifyOtp().

    line 48

≈ “Common” is an estimate: at least 1 in 5 of the 3,030 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
supabase-auth-security.mdc
---
description: Supabase auth security — enforces getUser() over getSession() on the server
globs: ["**/app/**", "**/actions/**", "**/api/**", "**/lib/supabase/**", "**/middleware.*"]
alwaysApply: false
---

# Supabase Auth Security

## RULE 1: NEVER Use getSession() on the Server

SECURITY CRITICAL: `getSession()` reads the JWT from cookies without revalidating it.
A malicious user can craft a fake session token that `getSession()` will silently accept.
`getUser()` sends a request to Supabase Auth to revalidate the JWT — it is cryptographically secure.

✅ SECURE (always use on the server):
```typescript
const { data: { user }, error } = await supabase.auth.getUser()
if (error || !user) redirect('/login')
```

❌ INSECURE (never use in server code):
```typescript
// VULNERABILITY: This does NOT verify the JWT with the auth server
const { data: { session } } = await supabase.auth.getSession()
```

## RULE 2: Always Use @supabase/ssr, Never @supabase/auth-helpers

The `@supabase/auth-helpers-nextjs` package is DEPRECATED.
Always import from `@supabase/ssr` for cookie-based sessions.

❌ DEPRECATED:
```typescript
import { createClientComponentClient } from '@supabase/auth-helpers-nextjs'
```

✅ CORRECT:
```typescript
import { createBrowserClient } from '@supabase/ssr'    // Client Components
import { createServerClient } from '@supabase/ssr'      // Server Components
```

## RULE 3: Middleware Must Refresh Sessions
Every protected route requires middleware that calls `updateSession()`.
Without this, user tokens expire silently and server components see null users.

## RULE 4: PKCE Flow for Email Confirmations
When implementing email signup, create a callback route at `app/auth/confirm/route.ts` 
that exchanges the `token_hash` using `verifyOtp()`. The PKCE flow requires the user
to complete verification in the same browser session.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.