Home › Security › API route security .claude/rules file

API route security .claude/rules file

A path-scoped Claude Code rules file for API routes and handlers: auth, ownership checks, validation, and safe errors.

Claude Code (CLAUDE.md)Security

How to use

Save this as .claude/rules/api-security.md in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

Note: Review and test before use. Provided as is, without warranty. See Terms.

.claude/rules/api-security.mdDownload  
---
paths:
  - "app/api/**"
  - "src/routes/**"
  - "src/api/**"
  - "**/controllers/**"
  - "**/handlers/**"
  - "**/routes/**"
---

# API handler security

Loaded when editing request handlers. Apply to every endpoint you add or change.

1. **Authenticate:** require a session/token unless the endpoint is intentionally public; say so in a comment if it is.
2. **Authorize the object:** load records scoped to the caller (`where owner_id = current_user.id` / tenant id). Never trust ids, roles, prices, or tenant ids from the body or query.
3. **Validate input** with a schema (types, lengths, enums, formats) before using it. Reject unknown fields on writes.
4. **Use safe sinks:** parameterized queries/ORM only; no shell, `eval`, or template rendering with raw input; fetch user URLs only through an allowlist.
5. **Limit:** paginate lists with a max page size; cap body size; rate-limit auth, signup, password reset, and expensive endpoints.
6. **Respond safely:** correct status codes; generic error messages to clients, details in server logs; never return secrets, password hashes, or internal stack traces.
7. **Webhooks:** verify signatures on the raw body and handle replays idempotently.
8. **Log** security-relevant events (denied access, failed logins) without tokens or PII.

Add tests for: unauthenticated (401), another user's resource (403/404), and invalid input (400/422).

More: all Security rules · all Claude Code (CLAUDE.md) rules

Related rules

Security auditor subagent (Claude Code)

A read-only Claude Code subagent that audits changes or a module for common web security vulnerabilities.

Claude Code (CLAUDE.md)Security

Secure coding AGENTS.md

AGENTS.md security baseline for AI agents in any web stack: secrets, injection, auth checks, dependencies, and logging.

AGENTS.mdSecurity

Security review prompt file (Copilot)

A Copilot prompt file (/security-review) that audits a file or the current diff for common vulnerabilities, read-only.

GitHub CopilotSecurity

Code reviewer subagent (Claude Code)

A Claude Code subagent definition that reviews the current diff for bugs, security, and tests, reporting by severity.

Claude Code (CLAUDE.md)Agent workflows

/commit slash command (Claude Code)

A Claude Code /commit slash command that reviews staged changes and writes a Conventional Commits message.

Claude Code (CLAUDE.md)Agent workflows

Context management for long agent sessions CLAUDE.md

CLAUDE.md practices for keeping Claude Code effective in long sessions: lean memory, notes files, subagents, and resets.

Claude Code (CLAUDE.md)Agent workflows

Get new rules weekly

One email a week: fresh rule files, agent tips, and what's changing in Cursor, Claude Code and Copilot. Free, unsubscribe anytime.

Opens the Rulestack Newsletter on beehiiv in a new tab. Or subscribe directly on beehiiv.