Claude Code Build Night · Part 9 · Coming soon

Lock It Down

Harden the app before anyone tests it for you.

How the night runs.

A short AI news briefing, then we harden What's for Dinner route by route with Claude Code, then five-minute demos from the room.

  1. 01

    Map

    Every route, who may call it, what it costs.

  2. 02

    Limit

    Rate limits in the app and at the edge.

  3. 03

    Lock

    Signed webhooks, row level security, encrypted secrets.

  4. 04

    Review

    Bugbot, /security-review and a second model.

The deck.

Every slide from the night, free to share. Open it full size to step through.

Lock It Down · slidesOpen full size →

Tonight's checklist.

The security checklist we build on the night: threats worst first, the controls on every route, and the checks that prove them. Copy it and swap in your own routes.

SECURITY.mdWhat's for Dinner

What's for Dinner

Type what's in your fridge. Get three dinners you can make tonight.

The jobMake every route checked, limited and logged, before strangers find it.

  • Vercel Firewall
  • Upstash
  • Better Auth
  • Neon
  • Resend
  • 1Password
  • Bugbot
  • AI Gateway
Done when
  • The 21st search in an hour gets a 429
  • Unsigned webhooks get a 400, repeats do nothing
  • User A can't read user B's dinners
  • No emails or ingredients in logs or analytics
  • No secret in the repo, every key a Vercel Secret
The full security checklist
# SECURITY.md · What's for Dinner

## Rules
- Every route checks who is calling on the server. Hiding a link or a URL is not a check.
- Every route that costs money (AI, email) has a rate limit.
- Every webhook is verified before it is read. Unsigned, invalid or stale requests get a 400. A repeated svix-id gets a 200 and no action, so the sender stops retrying.
- The app connects to Postgres as a role that cannot bypass row level security.
- No plaintext secrets or third-party tokens in the database, the repo, logs or chat.
- Personal data stays out of logs, analytics events and error messages. Use ids.

## Threats (worst first)
1. Someone scripts /api/dinners or the photo route and runs up the AI Gateway bill
2. Credential stuffing or brute force on sign-in
3. A forged webhook marks real users as bounced, or a forged cron call sends email
4. A bug in one query shows one user's dinners or favorites to another
5. A leaked key in the repo, a log or a screenshot
6. Emails from the leads table end up in PostHog, logs or a support screen

## Controls per route
| Route | Who | Auth check | Rate limit | Notes |
|---|---|---|---|---|
| /api/auth/* | anyone | Better Auth | Better Auth limiter, database storage, sign-in 3 per 10s | Generic error messages |
| POST /api/dinners | signed in | session | 20 per hour per user (Upstash), Firewall rule per IP | Validate ingredients with Zod |
| POST /api/photo | signed in | session | 10 per hour per user, Firewall rule per IP | Size and type checks before the AI call |
| POST /api/leads | anyone | none | 5 per hour per IP | Insert only, no reads |
| GET /api/cron/*, GET /api/at-risk | Vercel Cron | Bearer CRON_SECRET, constant-time compare | n/a | Excluded from Firewall deny rules |
| POST /api/plans/[id]/approve | plan owner | session plus owner check before resumeHook | n/a | A hook token is not authorization |
| POST /api/webhooks/resend | Resend | Svix signature on the raw body | n/a | 400 if unsigned, invalid or stale. Store svix-id; a repeat gets 200 and no action |
| /admin (later) | admins | role check on the server | n/a | Firewall allow list by IP. Reads through the server-only connection, after the role check |

## Edge rules (Vercel Firewall)
- Rate limit POST /api/dinners and /api/photo by IP, 429 on exceed
- Deny paths ending in .env or .bak, or containing /.git
- Deny /admin for any IP not on the allow list
- Every rule starts as Log for 10 minutes, then switches to its real action
- Exclude /api/cron and /api/at-risk from every deny or challenge rule

## Data
- App role `app_user`: no BYPASSRLS, owns no tables, only the grants it needs
- Row level security, enabled and forced, on dinners, favorites and plans: `user_id = current_setting('app.user_id', true)`
- Each request sets `app.user_id` from the Better Auth session inside a transaction, never from the request body. That needs Neon's Pool over WebSockets or `sql.transaction`, not single HTTP queries
- Cron jobs, workflow steps and the admin console read across users through one server-only connection, `lib/db-privileged.ts`. Every caller is listed here
- Leads: app_user can insert, not read. Support reads `leads_masked` (j***@example.com)
- Allergies or dietary notes, if stored: treat them like PHI. Owner and server only, never in analytics or AI logs

## Secrets
- Names only in this file. Values live in 1Password and in Vercel as Secret environment variables
- Local dev: `op run --env-file=.env.tpl -- npm run dev`
- Stored third-party tokens: AES-256-GCM in the app, key `APP_ENCRYPTION_KEY`, versioned for rotation
- Rotation table: key, where it lives, who rotates it, last rotated

## Review
- Claude Code: /security-review before every push
- Cursor Bugbot on every PR, with .cursor/BUGBOT.md built from this file
- A second model from a different lab audits the diff against this file

## Done when
- [ ] The 21st dinner search in an hour gets a 429 (25 calls: 20 pass, 5 get a 429)
- [ ] The 4th sign-in attempt in 10 seconds gets a 429, proven on a Vercel preview
- [ ] An unsigned webhook gets a 400, and a repeated svix-id gets a 200 with no second action
- [ ] Signed in as user A, user B's dinners return nothing, proven on a Neon branch
- [ ] No email, name or ingredient list in logs or PostHog events
- [ ] No secret in the repo or its history, and every key is a Vercel Secret

The prompts.

Every prompt from the deck, in order. Open one, copy it, paste it into Claude Code, Codex or any coding agent.

  1. 01
    Map every door
    Read SPEC.md, WORKFLOW.md and GROWTH.md, then the code under app/api, app/actions and lib. We're hardening What's for Dinner before more people use it.
    
    1. List every route and server action: what it does, who may call it, what it reads or writes, and what it costs (AI calls, emails).
    2. Interview me one question at a time about anything the code doesn't tell you, and wait for each answer.
    3. Write SECURITY.md with these sections: Rules, Threats (worst first), Controls per route (a table: route, who, auth check, rate limit, notes), Data (which columns hold personal data and who may read them), Secrets (names only, and where each one lives), Review, Done when.
    
    Never open, print or paste the contents of any .env file or any secret value. Refer to secrets by name only. Don't change any code. Stop and wait for my OK.
  2. 02
    Limit what costs money
    Read SECURITY.md, section Controls per route. Check the current docs first: better-auth.com/docs/concepts/rate-limit and upstash.com/docs/redis/sdks/ratelimit-ts/gettingstarted. Stop after each step.
    
    1. Better Auth: turn on rateLimit with storage: "database", because in-memory counters don't hold across serverless instances. Keep its stricter sign-in default and add customRules for sign-up and password reset. Two catches from the docs: the limiter is off in development by default, and server-side auth.api calls skip it. Check that sign-in goes through the Better Auth client or the /api/auth routes, not auth.api inside a server action. Read the IP address section, then check Vercel's docs for which client IP header Vercel sets, and configure ipAddressHeaders to match. Database storage needs a migration: show it to me before applying it.
    2. Tell me to create an Upstash Redis database myself and add UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN to .env.local and to the Vercel project. Wait until I tell you it's done.
    3. Run npm install @upstash/ratelimit @upstash/redis. In lib/ratelimit.ts create sliding-window limiters: 20 dinner searches an hour per user id, 10 fridge photos an hour per user id, 5 guide signups an hour per IP.
    4. Call them at the top of /api/dinners, the photo route and the leads route, before any AI call or database write. Return 429 with a Retry-After header and a friendly message the UI shows.
    5. Write a small script that calls /api/dinners 25 times as my test user on localhost and prints the status codes: 20 should pass and the 21st onward get a 429. Then prove the sign-in limit, 4 attempts in 10 seconds with the 4th getting a 429, on a Vercel preview, or locally with rateLimit enabled: true set temporarily, then remove it. Everyone on this Wi-Fi shares one IP, so test per-user limits and don't hammer per-IP limits on a shared app. Run it and stop.
  3. 03
    Rules at the edge
    Read SECURITY.md. We're adding rules in the Vercel Firewall, which I set up myself in the dashboard. Read vercel.com/docs/vercel-firewall/vercel-waf first, including rate limiting, custom rules and IP blocking, and check the plan limits for our plan.
    
    1. Write FIREWALL.md with each rule I should add: name, If conditions, Then action, and how to undo it. Include:
       - A rate limit on POST requests to /api/dinners and the photo route, keyed by IP. Hobby allows one rate limit rule, so put both paths in it.
       - A deny rule for paths ending in .env or .bak, or containing /.git (so /.git/config is caught too).
       - An allow list for /admin: deny any request to /admin from an IP that isn't on my list. We build the admin console in a later part.
       - Where to block one abusive IP or CIDR range under IP Blocking.
       - Exclude /api/cron and /api/at-risk (the at-risk email cron from Part 7) from every deny or challenge rule, so Vercel Cron still runs.
       - Note that everyone on one Wi-Fi shares an IP, so a per-IP limit tested from a room trips for everyone at once.
    2. Every rule starts with the Log action. Tell me how to watch it in the Firewall overview for 10 minutes before switching it to 429 or Deny.
    3. Tell me to add the rules, review the changes and publish them. Wait until I tell you it's done.
    4. Add an Edge rules section to SECURITY.md listing what's live. Never ask me for a Vercel token, and never put my IP addresses in a committed file.
  4. 04
    Verify every webhook
    Read SECURITY.md. We're receiving Resend webhooks for bounces and complaints, so we stop emailing those addresses. Read resend.com/docs/webhooks/verify-webhooks-requests first. Stop after each step.
    
    1. Tell me to create the webhook in Resend pointing at /api/webhooks/resend, and to add its signing secret as RESEND_WEBHOOK_SECRET to .env.local and to the Vercel project as a Secret. Wait until I tell you it's done.
    2. Build the route: read the raw body as text before any JSON parsing, verify it with resend.webhooks.verify using the svix-id, svix-timestamp and svix-signature headers, and return 400 on any failure without saying why.
    3. Replays: check whether the verifier already rejects old timestamps. If it doesn't, reject anything more than 5 minutes old with a 400. Store each svix-id with a unique constraint. A verified request whose svix-id is already stored returns 200 and does nothing: Resend retries any delivery that doesn't get a 2xx, so a 400 here would make it keep retrying. Show me the migration before applying it.
    4. On bounce and complaint events (check Resend's event names), mark that address suppressed, and make every send check it first.
    5. Change the cron routes' CRON_SECRET check to crypto.timingSafeEqual, comparing lengths first.
    6. Read the Workflow SDK hooks docs in node_modules/workflow/docs. A hook token is not authorization, so confirm the plan approval route takes the user from the Better Auth session and checks they own the plan before calling resumeHook.
    7. Send one unsigned request with curl and show me the 400. Then replay one signed test event from the Resend dashboard and show me the repeat gets a 200 with no second suppression or row. Stop.
  5. 05
    Rows only their owner sees
    Read SECURITY.md, section Data. Then read neon.com/docs/guides/rls-query-execution and the Postgres row security docs. Propose everything first; don't apply anything until I say so. Stop after each step.
    
    1. Check which role the app connects as today. Neon's owner role has BYPASSRLS, so policies would be ignored. Propose a login role app_user with no BYPASSRLS that owns no tables, and list exactly the grants it needs: what Better Auth's tables need, SELECT, INSERT, UPDATE and DELETE on dinners, favorites and plans, and INSERT only on leads. Migrations keep using the owner role.
    2. Write the migration: ENABLE and FORCE ROW LEVEL SECURITY on dinners, favorites and plans, with policies that match user_id to current_setting('app.user_id', true). Show it to me. Don't run it.
    3. Check which Neon driver the app uses. set_config(..., true) only lasts for one transaction, so the HTTP neon() function's single queries won't carry it. Use Pool over WebSockets for these queries, or sql.transaction([...]) with the set_config as the first query. Then write lib/db-as-user.ts: every user-facing query runs in a transaction that first calls set_config('app.user_id', userId, true), with the id taken from the Better Auth session, never from the request body. List the cron jobs, workflow steps and the admin console (Part 11) that read across users. They use lib/db-privileged.ts: one server-only connection (import "server-only") that client code can never import. Add every caller to SECURITY.md.
    4. Tell me to create a Neon branch, create the app_user role and its password myself, and put its connection string in .env.local as DATABASE_URL_APP. Wait until I tell you it's done.
    5. Run the migration on the branch only. Prove that signed in as user A, user B's dinners and favorites return nothing. Then tell me the steps for production and stop.
  6. 06
    Keep personal data out of sight
    Read SECURITY.md, section Data, and GROWTH.md, section Events.
    
    1. List every place an email, name, IP address, ingredient list or dietary note could leak: logs, error messages, PostHog events, AI request logs, emails. Show me file and line only, never the values.
    2. Propose a view leads_masked that shows each email as its first letter, three stars and the domain, plus the date and source. Support screens and the admin console will read the view, never the table. Show me the migration before applying it.
    3. PostHog: check posthog.com/docs/libraries/js/config, add email and name to property_denylist, and add a before_send that drops any property whose key contains email, phone or ingredients. Identify users by id only.
    4. Replace any log of a request body or user object with a log of ids.
    5. If we store allergies or dietary needs, treat them like PHI: only the owner and the server read them, and they never reach analytics, logs or error reports.
    
    Show me the diff and stop.
  7. 07
    Stored is not safe
    Read SECURITY.md, section Secrets. No token or credential we store goes in the database as plaintext. Stop after each step.
    
    1. Better Auth: if we add social sign-in, set account.encryptOAuthTokens to true. Check better-auth.com/docs/reference/options for the current option first.
    2. Write lib/crypto.ts with encrypt and decrypt using Node's crypto and aes-256-gcm: a random 12-byte IV per value, the auth tag stored with the ciphertext, and a key version prefix such as v1: so we can rotate keys.
    3. The key comes from APP_ENCRYPTION_KEY. Tell me to generate it myself with openssl rand -base64 32 and add it to .env.local and to the Vercel project as a Secret. Wait until I tell you it's done. Never print it or read it back.
    4. Find any column that stores a third-party token or key today. Propose a migration that encrypts existing rows in batches, and show it to me before running anything.
    5. Write ROTATION.md: add a v2 key, re-encrypt rows in batches, then retire v1.
    6. Tell me in two lines why we encrypt in the app here instead of with pgcrypto: the key would travel inside SQL that can end up in query logs. Stop.
  8. 08
    Secrets stay secret
    Read SECURITY.md, section Secrets. Never print a secret value. Report key names, files and line numbers only.
    
    1. Check .gitignore covers .env and .env*.local, and search the git history by filename for any committed .env file. If one was ever committed, list the key names I need to rotate.
    2. Search the code for hardcoded keys and tokens by pattern. Report locations only.
    3. Write .env.tpl with a 1Password secret reference for every key name, in the form op://<vault>/<item>/<field>, using the vault plan from Part 1, so I can run: op run --env-file=.env.tpl -- npm run dev
    4. Write me a checklist: in Vercel, make sure every API key, token and password is a Secret environment variable, not Config; in GitHub, check what secret scanning and push protection cover for this repo, since public and private repos differ. Wait until I tell you it's done.
    5. Add a Rotation table to SECURITY.md: each key name, where it lives, who rotates it and when it was last rotated. Stop.
  9. 09
    A second model audits the diff
    Read SECURITY.md. We're adding a second opinion from a cheaper model from a different lab: "deepseek/deepseek-v4.1-flash" through AI Gateway. Check the AI Gateway model list first that the ID is still current.
    
    1. Write scripts/audit-diff.ts: run git fetch, then get the diff with git diff origin/main...HEAD (ask me first if our default branch isn't main, and warn me if the diff is empty), drop any .env file, key file or lockfile, and send the rest with SECURITY.md to the model using generateText with output: Output.object and a Zod schema: findings, each with file, line, severity, which threat in SECURITY.md it maps to, and a suggested fix. If structured output fails with this model, ask for JSON in the prompt and parse it with the same Zod schema.
    2. Print the findings as a table, and exit with a non-zero code on any high-severity finding.
    3. AI_GATEWAY_API_KEY is already in .env.local. Don't print it.
    4. Run it on this branch. Check every finding against the code yourself and tell me which are real and which are noise. Don't fix anything yet. Stop.
  10. 10
    A second set of eyes on every PR
    Read SECURITY.md. Read cursor.com/docs/bugbot first.
    
    1. Write .cursor/BUGBOT.md from SECURITY.md: the threats, the rules every route follows, and what to flag: a route without a session check, a costly route without a rate limit, a webhook read before its signature is verified, personal data in logs or events, a user query that doesn't go through lib/db-as-user.ts, and any secret in code.
    2. Tell me to connect this repo to Bugbot in the Cursor dashboard, and to set up Security Review in Automations if our plan includes Cloud Agents. Wait until I tell you it's done.
    3. Open a small PR that adds .cursor/BUGBOT.md. Tell me to comment bugbot run on it, and wait until I tell you it's done.
    4. Add to the Review section of SECURITY.md: I type /security-review in Claude Code myself before every push. Check every Done when item in SECURITY.md, tell me which pass, and stop.

The toolkit.

The guards we add on the night, one job each.

Working is not safe. Lock it down.

Date to be announced. Dates go out in the Discord first.

Harbor at dusk · Phoenix, Arizona