Skip to content

skillSecurity

CSRF and CORS Hardening

Sets up CORS and CSRF protection that fits how your app authenticates, with cookies or bearer tokens, and removes the wildcard shortcuts.

npx workspacetemplates add csrf-and-cors-hardening --tool claude-code

Writes 1 file to .claude/skills/csrf-and-cors-hardening/

.claude/skills/csrf-and-cors-hardening/SKILL.md32 lines · 1.7 KBFile
---
name: csrf-and-cors-hardening
description: Sets up CORS and CSRF protection that fits how your app authenticates, with cookies or bearer tokens, and removes the wildcard shortcuts.
---

# CSRF and CORS hardening

First decide how the app authenticates. The answer decides everything else.

## Steps

1. Identify the credential: cookie session, bearer token in a header, or both. List the state-changing routes (POST, PUT, PATCH, DELETE) and any GET that changes state.
2. CORS policy:
   - Allowlist exact origins from configuration. Never reflect the request `Origin` without checking it against that list, and never combine `*` with credentials.
   - Set `Access-Control-Allow-Credentials: true` only if cookies must cross origins.
   - Limit methods and headers to what the client uses, set `Vary: Origin`, and cache preflights with `Max-Age`.
3. Cookie sessions need CSRF defence. Layer these:
   - Cookie flags `HttpOnly`, `Secure`, `SameSite=Lax` (or `Strict` for sensitive apps).
   - A per-session CSRF token checked on every unsafe method, or the double-submit cookie pattern with a signed value.
   - Verify `Origin` or `Sec-Fetch-Site` on unsafe requests.
4. Bearer-token APIs that never read cookies are not exposed to CSRF. Confirm no route accepts a cookie as a fallback.
5. Make GET handlers safe: no state changes.
6. Add tests: a request from a foreign origin is refused; a POST without the token returns 403; preflight returns only the allowed list.

## Rules

- Do not "fix" a CORS error by widening the policy before you know which origin is calling.
- Use the framework's maintained middleware.

## Report

Auth model found, policy before and after, files changed, tests added.

What it does

CORS and CSRF are often mixed up, and the usual fix for a blocked request, allowing every origin, quietly removes protection. They solve different problems, and which one you need depends on whether the browser sends credentials automatically.

This skill first works out how your app authenticates, then applies the matching settings: strict origins and cookie flags for cookie sessions, and a lighter setup for bearer-token APIs. It leaves you with tests that prove the policy.

Example

you › Our SPA at app.acme.io calls api.acme.io with cookies. Lock this down.

agent › Auth is cookie based, so CSRF applies. api/cors.ts reflected any Origin with credentials; replaced with an allowlist of https://app.acme.io. Session cookie now Secure, HttpOnly, SameSite=Lax. Added a CSRF token header check on 9 unsafe routes and an Origin check as a second layer. 6 tests added, all passing.

Questions

Does SameSite=Lax replace CSRF tokens?
It blocks most cross-site form posts, but not same-site attacks from sibling subdomains or older browsers. Keep a token or Origin check for anything sensitive.
Why not just set the allow-origin header to a star?
A star cannot be combined with credentials, and for public data it is fine, but on an authenticated API it invites mistakes. An explicit list is easier to audit.
  • JWT Security

    SkillSecurity

    Free