guides · 2026-11-14
SSRF in AI-Generated Code: When Your App Fetches Any URL a User Gives It
Published 2026-11-14 · Reviewed 2026-11-14 · By the Ingenious AI team
The short answer
If your server fetches a URL the user supplied, the user chooses where your server's requests go — including http://169.254.169.254, the cloud metadata address that hands out your server's own credentials. Fix it with an allow-list of hosts, blocking of private and link-local IP ranges after DNS resolution, and re-validating every redirect hop.
Check your own app free: paste your code or enter your site URL on the Vibe-Code Audit homepage — the free scan checks for exposed secrets, injection flaws, XSS patterns, missing headers and exposed .env/.git paths, with a copy-paste fix for every finding. No credit card.
The features that hide SSRF
SSRF rarely arrives labelled as such. It arrives as a link-preview generator ('paste a URL, we fetch the title'), an image importer, a webhook tester, an RSS reader, a site-audit tool — any feature whose job is fetching URLs. AI generators build these in one cheerful step: take the URL parameter, requests.get() it, return the result. Nothing in the demo distinguishes a blog post from your cloud provider's metadata service, because to the code they are the same operation.
Vibe-Code Audit detects the shape directly: requests/httpx or fetch called with a value traced from request input. The detector cannot know your network layout — it flags the capability, and the capability is the vulnerability.
The metadata endpoint is the real target
169.254.169.254 is a link-local address every major cloud exposes to the instance itself, serving temporary credentials for the server's IAM role. A fetch to http://169.254.169.254/latest/meta-data/iam/security-credentials/ returns tokens that can be extremely powerful — in misconfigured setups, full account access. Attackers also aim at localhost admin panels, internal dashboards, and the Docker or Kubernetes APIs. The public URL feature is just the delivery mechanism; your internal network is the prize.
Defences that only block the literal string '169.254' fail to DNS rebinding and redirects: a public hostname resolves to an internal address, or a harmless URL 302s to the metadata path. Validation has to happen on the resolved IP, at connection time, on every hop.
A fix that holds up
Prefer an allow-list: if the feature imports from known sources, only those hosts are fetchable, full stop. Where arbitrary URLs are the product, enforce: https/http schemes only; resolve the hostname and reject private, loopback, link-local and multicast ranges; disable redirects or re-run the same checks on each Location; cap response size and time; and fetch through an egress proxy when the architecture allows it. In cloud environments, require IMDSv2 (token-based metadata) so a bare GET returns nothing.
Then test like an attacker: submit the metadata URL, a localhost admin URL, a redirector you control pointing at 127.0.0.1, and a DNS name you control that rebinds. All four must fail before the fix counts.
What to do if you shipped it
Check your cloud credential logs for use from unexpected places, and rotate the instance role's credentials if the metadata endpoint was reachable without IMDSv2 — a successful SSRF read is silent in your app logs unless you log outbound fetches. Add that logging now: destination host, resolved IP and initiating user for every server-side fetch turns the next incident from archaeology into a query.
The internal targets nobody remembers are reachable
Teams block the famous metadata address and stop thinking — but the server's network position usually reaches far more: the admin panel bound to localhost 'because it is not exposed', the metrics endpoint, the database's HTTP interface, the container orchestrator's API, the staging environment on a neighbouring address, and cloud services link-local beyond the one famous IP. An SSRF does not need the crown jewels to be useful; an internal status page confirms versions for the next attack, and an unauthenticated internal action endpoint (restart, flush, debug) can be the whole incident. The inventory exercise is therefore part of the fix: from the application server, list what you can reach that the internet cannot — that list is the SSRF target catalogue, and it is usually longer and softer than anyone expected. Egress rules that only permit the destinations the feature needs shrink the catalogue to near zero, which is why allow-listing beats block-listing here too.
Worked example: LinkLens, a bookmarking tool
LinkLens (fictional), with 2,700 users, generated link previews by fetching submitted URLs server-side with no validation. A scan flagged 'SSRF risk (server-side request with user input)' as HIGH. The founder tested the metadata URL against staging and received the instance's role name back — proof of reachability, stopped before reading credentials. The fix shipped in a day: scheme check, DNS resolution with private-range rejection, redirects re-validated per hop (max 3), and IMDSv2 enforced on the host. All four attack probes then failed, the re-scan cleared the finding, and outbound-fetch logging was added to catch regressions.
Five actions to take today
- List every feature where your server fetches a user-influenced URL — previews, importers, webhooks, auditors — and treat each as an SSRF candidate.
- Allow-list hosts where you can; where you cannot, validate scheme and reject resolved IPs in private, loopback and link-local ranges.
- Re-validate every redirect hop against the same rules, and cap redirects, response size and fetch time.
- Enforce IMDSv2 on cloud hosts so the metadata endpoint ignores bare GET requests.
- Probe your own staging with the metadata URL, a localhost URL, a redirect-to-localhost and a rebinding hostname; all must fail.
What is Server-side request forgery (SSRF)?
a flaw where an attacker makes your server issue requests to destinations the attacker chooses. The requests carry your server's network position and credentials, reaching internal services and cloud metadata endpoints that are unreachable from the public internet.
Sources
- OWASP Top 10, the OWASP Foundation's standard awareness list of the most critical web application security risks: owasp.org, 2021
- CWE-89: Improper Neutralization of Special Elements used in an SQL Command, MITRE's weakness entry for SQL injection and its mitigations: cwe.mitre.org, 2026
- OWASP Cheat Sheet Series — Secrets Management, OWASP's best-practice guide to storing, rotating and auditing secrets: cheatsheetseries.owasp.org, 2026
Frequently asked questions
Is blocking 'localhost' and '169.254' in the URL string enough?
No. Attackers use DNS names that resolve internally, decimal or hex IP spellings, and redirects. The check must run on the resolved IP address, at fetch time, on every redirect hop.
My feature must fetch arbitrary URLs. Can it ever be safe?
It can be made defensible: scheme allow-list, resolved-IP range blocking, redirect re-validation, timeouts, size caps and an egress proxy. 'Arbitrary' is the risk factor — the controls are the price of offering it.
What is IMDSv2 and why does it help?
AWS's token-required metadata mode: a PUT first obtains a session token, which a simple SSRF GET cannot do. It neutralises the most damaging SSRF target even if a fetch slips through.
How do I know if SSRF was already abused?
Look for outbound fetches to unusual destinations in your logs (add logging if absent) and review cloud credential usage for unfamiliar origins or actions. Silence in app logs proves nothing without fetch logging.
Related guides
- Server-Side Template Injection in Flask Apps Built With AI
- Why pickle.loads and yaml.load Turn User Data Into Code Execution
- JWT alg=none and Key Confusion: How Attackers Forge Any User's Token
- How to Fix CORS Errors Without Opening Your API to Every Website
- Debug Mode in Production: What One Stack Trace Hands an Attacker
- Why AI-Generated Login and API Endpoints Ship Without Rate Limits
- 7 Security Mistakes AI Coders Keep Making
- Supply-Chain Attacks in the AI-Coding Era
- Secret Hygiene: Stop API Key Leaks
- What Code Scanners Can't Find
- Cursor-Generated Code Review Checklist
- Bolt.new Pre-Deploy Security Checklist
Browse the full library on the guides index, run the checks in the launch audit, or compare plans on pricing.
Informational explainer, not a security guarantee. Scan your code free before every deploy.