Why this matters
Security bugs are cheap to prevent and catastrophic to discover in production. Many of the worst AI incidents are classic web security failures in new clothing: injection, missing authorization, unvalidated output, servers fetching attacker-chosen URLs.
The classic list (OWASP Top 10, in practice)
| Risk | Prevent with |
|---|---|
| Broken access control (IDOR) | Object-level checks on every access; RLS (3.4) |
| Injection (SQL, command) | Parameterised queries; never build shell commands from input |
| Cross-site scripting (XSS) | Escape output by default; sanitise any HTML you render; CSP header |
| Security misconfiguration | DEBUG off in production; no default credentials; minimal exposed ports |
| Vulnerable dependencies | Lockfiles, automated updates, pip-audit / npm audit in CI |
| Authentication failures | Slow password hashing, MFA, rate-limited login |
| SSRF | Validate and restrict outbound URLs (below) |
| Logging failures | Log security events, never secrets |
SSRF: when your server fetches a URL
Any feature where the server fetches a URL someone else supplied (webhooks, "import from URL", image fetchers, vendor-issued download links, agent web tools) can be abused to reach internal addresses like cloud metadata endpoints or admin services.
Defences:
- Require
https. - Resolve the hostname and refuse private, loopback and link-local addresses (and re-check after redirects).
- Allow-list hosts where possible.
- Never send your API keys to storage or third-party hosts.
- Stream large downloads with size limits and timeouts.
Real examples: a clip service requires presigned storage URLs to be https and not private, loopback or link-local (P1); a lead-scoring platform checks every URL it calls on a user's behalf (P7). This is interview question Q29.
Secrets management
- Read secrets by name from the environment or a secrets manager; never commit them.
- Keep them out of
repr, logs and error bodies, and scrub provider error messages (they can echo the key). - Refuse to start with placeholder secrets (
changeme). - Rotate on any suspected exposure.
- See Q30 for the full answer.
Where AI adds attack surface
Prompt injection, sensitive data in prompts and logs, model output rendered as HTML, tools with too much power, and unbounded consumption (cost attacks). Part 10 covers the OWASP Top 10 for LLM applications (Q27).
Common mistakes
- Rendering user or model content with
dangerouslySetInnerHTMLwithout sanitising. - Error pages that print stack traces or environment variables.
- Allowing any URL in "fetch this page for me" features.
Interview follow-ups
- "How do you prevent SQL injection with an ORM?" The ORM parameterises queries, but watch raw SQL, string-built
ORDER BYor column names (allow-list them), and query-builder escape hatches. - "What's CSRF and is it still a risk?" A site tricks a logged-in browser into sending a request.
SameSitecookies largely mitigate it; use CSRF tokens for cookie-authenticated state changes anyway. - "How do you handle a leaked secret?" Rotate immediately, revoke the old one, check logs for misuse, remove it from history, add secret scanning to CI.
Check yourself
An "import from URL" feature fetches whatever URL a user pastes. What's the risk and the fix?
SSRF: the server can be pointed at internal services or cloud metadata. Require https, resolve the host and block private, loopback and link-local ranges (checking redirects too), allow-list where possible, set size and time limits, and never forward credentials.
How do you stop an API key leaking through errors?
Never include upstream error bodies verbatim in responses; scrub known secret patterns from logged errors; keep keys out of object representations; return a fixed message to clients and log details server-side.
Build it
Add SSRF protection to a URL-fetching endpoint and write tests for http://127.0.0.1, http://169.254.169.254, a private IP, and a redirect to a private IP.