Web Security for AI Coders
Protect your app and your users. Common threats explained simply, with practical steps you can ask AI to implement.
In this chapter
- The most common security threats facing small apps
- How to ask AI to lock down your forms, logins, and APIs
- Why secrets in frontend code is a disaster waiting to happen
- Simple habits that block 90% of attacks
Common Threats Explained
Protection Checklist
Security Headers
HTTP headers you add to your server responses that tell the browser how to protect your users. Add these in your Next.js config or proxy file (proxy.ts, called middleware.ts before Next.js 16).
Controls which resources (scripts, styles, images) your page can load. Blocks inline scripts by default, which stops most XSS attacks. The most powerful security header.
- Blocks unauthorized scripts
- Prevents inline JavaScript injection
- Controls which domains can serve content
Prevents your site from being loaded inside an iframe on another website. Set to DENY or SAMEORIGIN. Stops clickjacking attacks completely.
- DENY: never allow framing
- SAMEORIGIN: only your own site
- Prevents clickjacking attacks
Controls which other websites' code can read your API's responses in a browser. Browsers block this by default; CORS is how you open it up, so only allow trusted domains in Access-Control-Allow-Origin and never use * on private APIs.
- Only lets trusted websites use your API from the browser
- Does not stop bots or scripts (they ignore CORS), so you still need login checks and rate limits
- Configure per-route for flexibility
Rate Limiting
Environment Variables & Secrets
AI Prompts for Security
Copy-paste these prompts to quickly add security features to your project.
“Add rate limiting to my API routes: max 10 requests per minute per IP. Use Upstash Rate Limit. Return a 429 status with a friendly error message when the limit is exceeded.”
“Add security headers to my Next.js app: Content-Security-Policy, X-Frame-Options, X-Content-Type-Options, and Referrer-Policy. Configure them in next.config.js.”
“Audit my app for XSS vulnerabilities. Check all places where user input is rendered and make sure nothing uses dangerouslySetInnerHTML with unsanitized data. Add input validation to all forms.”
“Add CSRF protection to my form submissions. Generate a CSRF token on the server, include it as a hidden field in forms, and validate it on submission.”
“Set up environment variables properly. Move all API keys and secrets to .env.local, make sure nothing sensitive has the NEXT_PUBLIC_ prefix, and add .env.local to .gitignore.”
Frequently Asked Questions
Is my AI-built app secure?
AI tools generally follow security best practices, but they can make mistakes. The biggest risks: exposing API keys in frontend code, missing input validation, and not setting up authentication properly. Ask your AI tool to 'audit my app for security issues' as a final check.
What's the most important security step for beginners?
Keep your secrets secret. Never put API keys, database passwords, or tokens in your frontend code or commit them to Git. Use environment variables for everything sensitive. This single habit prevents the most common security disaster.
Do I need HTTPS?
Yes, always. HTTPS encrypts data between your users and your server. The good news: hosting platforms like Vercel and Netlify provide HTTPS automatically for free. You don't need to set up anything. It just works.