โ Cybersecurity for Founders
Day 2 of 7
Securing Your APIs
Authentication vs Authorisation โ Getting the Basics Right
Authentication answers 'who are you?' Authorisation answers 'what are you allowed to do?' Both must be correct. Confusing them is the most common API security mistake.
For Supabase-backed APIs: Supabase handles authentication (JWTs issued on login, verified on every request). Row Level Security (RLS) handles authorisation (users can only see their own data, enforced at the database level). Using both correctly means: even if your application layer has a bug that sends a request with the wrong user ID, the database will refuse to return the data.
JWT best practices: set short expiry times (15 minutes for access tokens, 7 days for refresh tokens). Validate the signature on every request โ never trust a JWT's payload without verifying the signature against your signing key. Store access tokens in memory (not localStorage โ XSS vulnerable) and refresh tokens in httpOnly cookies (not accessible to JavaScript). Supabase's client SDK handles this correctly by default โ don't override its storage mechanism.
Rate Limiting and Input Validation
Rate limiting protects your API from abuse โ whether accidental (a client with a bug making thousands of requests) or deliberate (a credential stuffing attack trying thousands of password combinations). Implement rate limiting at multiple levels: per IP, per user, per endpoint.
For Cloudflare Workers: Cloudflare's rate limiting is built in and costs $0.05 per million requests above the free tier. Configure rules like '100 requests per minute per IP on /auth/login' โ this directly blocks credential stuffing without any application code.
For Supabase: the built-in auth rate limiting protects the authentication endpoints. For your own API routes, implement rate limiting in middleware using an in-memory store (for single-instance) or Redis/Cloudflare KV (for distributed deployments).
Input validation: validate all input at the API boundary, not just in the client. Every field should have: type validation, length validation, format validation (email, UUID, etc.), and sanitisation for stored values. Use a schema validation library (Zod for TypeScript, Joi for JavaScript) on every API handler. A single missing validation on an assessment ID field could allow a user to enumerate other users' assessments โ a classic IDOR (Insecure Direct Object Reference) vulnerability.
โก Today's Action
Audit your three most sensitive API endpoints (probably: create assessment, fetch assessment, user login). For each: is authentication required? Is authorisation checked (can user A access user B's data)? Is input validated? Is rate limiting applied? Fix any gaps you find.
๐ก Pro Tip
Use Cloudflare WAF (Web Application Firewall) as your first line of defence โ it's included in the free Cloudflare plan and blocks the most common attack patterns (SQL injection, XSS, directory traversal) before they reach your application code.