pitcheye.dev← All 30 rules
severity: warnpack: clerkv1.0 core

clerk/ui-only-role-gate

Role is enforced in the UI, not on the server

npx @pitcheye/check . --pack clerk

whyItBreaks

Any signed-in user can call the endpoint directly. Hiding the button in React removes the affordance, not the capability — `curl` with the same session cookie performs the privileged write.

✖ Bad — triggers warnfixtures/clerk-ui-only-role-gate/bad
const { userId } = await auth();
if (!userId) return unauthorized();
await db.update(listings).set({ status: 'approved' });
✓ Good — passes checkfixtures/clerk-ui-only-role-gate/good
const { userId, sessionClaims } = await auth();
if (sessionClaims?.metadata?.role !== 'admin') return forbidden();
await db.update(listings).set({ status: 'approved' });

suggestedFix

Read the role from the session on the server (`const { sessionClaims } = await auth()`) and return 403 unless it matches, before the write. Set the role via Clerk public metadata, never from the request body.

explain_rule({"clerk/ui-only-role-gate"})

Agent & MCP JSON output contract

{
  "ruleId": "clerk/ui-only-role-gate",
  "pack": "clerk",
  "severity": "warn",
  "file": "apps/web/src/app/api/listings/route.ts",
  "line": 6,
  "message": "an API route that checks a session but never a role/permission, then writes to the database",
  "whyItBreaks": "Any signed-in user can call the endpoint directly. Hiding the button in React removes the affordance, not the capability — `curl` with the same session cookie performs the privileged write.",
  "suggestedFix": "Read the role from the session on the server (`const { sessionClaims } = await auth()`) and return 403 unless it matches, before the write. Set the role via Clerk public metadata, never from the request body.",
  "docsUrl": "https://pitcheye.dev/rules/clerk-ui-only-role-gate"
}

Markdown: curl -H "Accept: text/markdown" https://pitcheye.dev/rules/clerk-ui-only-role-gate

raw markdown
# clerk/ui-only-role-gate

- **severity:** warn
- **pack:** clerk
- **docs:** https://pitcheye.dev/rules/clerk-ui-only-role-gate

## whyItBreaks

Any signed-in user can call the endpoint directly. Hiding the button in React removes the affordance, not the capability — `curl` with the same session cookie performs the privileged write.

## bad

```ts
const { userId } = await auth();
if (!userId) return unauthorized();
await db.update(listings).set({ status: 'approved' });
```

## good

```ts
const { userId, sessionClaims } = await auth();
if (sessionClaims?.metadata?.role !== 'admin') return forbidden();
await db.update(listings).set({ status: 'approved' });
```

## suggestedFix

Read the role from the session on the server (`const { sessionClaims } = await auth()`) and return 403 unless it matches, before the write. Set the role via Clerk public metadata, never from the request body.

## commands

```bash
npx @pitcheye/check . --pack clerk
```

```json
{"tool":"explain_rule","arguments":{"ruleId":"clerk/ui-only-role-gate"}}
```
← clerk/unprotected-mutationBack to rule catalogclerk/userprofile-page-missing-props →