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

clerk/unprotected-mutation

Mutating endpoint has no authentication

npx @pitcheye/check . --pack clerk

whyItBreaks

The handler writes to the database for any anonymous request. There is no session, so ownership checks downstream silently compare against undefined and either throw a 500 or, worse, write a row with a null owner.

✖ Bad — triggers errorfixtures/clerk-unprotected-mutation/bad
export async function POST(req: Request) {
  await db.insert(listings).values(await req.json());
}
✓ Good — passes checkfixtures/clerk-unprotected-mutation/good
export async function POST(req: Request) {
  const { userId } = await auth();
  if (!userId) return new Response('Unauthorized', { status: 401 });
  await db.insert(listings).values({ ...body, ownerId: userId });
}

suggestedFix

Call `const { userId } = await auth()` from `@clerk/nextjs/server` as the first statement of the handler and return 401 when it is missing (or convert the tRPC entry point to `protectedProcedure`).

explain_rule({"clerk/unprotected-mutation"})

Agent & MCP JSON output contract

{
  "ruleId": "clerk/unprotected-mutation",
  "pack": "clerk",
  "severity": "error",
  "file": "apps/web/src/app/api/listings/route.ts",
  "line": 6,
  "message": "exported POST/PUT/PATCH/DELETE, or a publicProcedure .mutation(), with no auth() call in the module",
  "whyItBreaks": "The handler writes to the database for any anonymous request. There is no session, so ownership checks downstream silently compare against undefined and either throw a 500 or, worse, write a row with a null owner.",
  "suggestedFix": "Call `const { userId } = await auth()` from `@clerk/nextjs/server` as the first statement of the handler and return 401 when it is missing (or convert the tRPC entry point to `protectedProcedure`).",
  "docsUrl": "https://pitcheye.dev/rules/clerk-unprotected-mutation"
}

Markdown: curl -H "Accept: text/markdown" https://pitcheye.dev/rules/clerk-unprotected-mutation

raw markdown
# clerk/unprotected-mutation

- **severity:** error
- **pack:** clerk
- **docs:** https://pitcheye.dev/rules/clerk-unprotected-mutation

## whyItBreaks

The handler writes to the database for any anonymous request. There is no session, so ownership checks downstream silently compare against undefined and either throw a 500 or, worse, write a row with a null owner.

## bad

```ts
export async function POST(req: Request) {
  await db.insert(listings).values(await req.json());
}
```

## good

```ts
export async function POST(req: Request) {
  const { userId } = await auth();
  if (!userId) return new Response('Unauthorized', { status: 401 });
  await db.insert(listings).values({ ...body, ownerId: userId });
}
```

## suggestedFix

Call `const { userId } = await auth()` from `@clerk/nextjs/server` as the first statement of the handler and return 401 when it is missing (or convert the tRPC entry point to `protectedProcedure`).

## commands

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

```json
{"tool":"explain_rule","arguments":{"ruleId":"clerk/unprotected-mutation"}}
```
Back to rule catalogclerk/ui-only-role-gate →