SSRF (Server-Side Request Forgery)
Your server fetches a URL the user gave it (webhooks, "import from URL", link previews, PDF generators). The attacker gives an internal URL instead:
http://169.254.169.254/latest/meta-data/iam/security-credentials/: steals cloud credentials (the Capital One breach)http://localhost:6379: talks to internal Redishttp://10.0.0.5/admin: internal admin panel
Packages
| Package | Use |
|---|---|
ssrf-req-filter | HTTP agent that blocks private/internal IPs (works with axios/node-fetch) |
ipaddr.js | Check whether an IP is private, loopback or link-local |
❌ Vulnerable
app.post('/preview', async (req, res) => {
const r = await fetch(req.body.url); // any URL, including internal ones
res.send(await r.text());
});✅ Secure
const axios = require('axios');
const ssrfFilter = require('ssrf-req-filter');
app.post('/preview', async (req, res) => {
let url;
try { url = new URL(req.body.url); } catch { return res.status(400).end(); }
if (!['http:', 'https:'].includes(url.protocol)) return res.status(400).end(); // no file://, gopher://
const r = await axios.get(url.href, {
httpAgent: ssrfFilter(url.href),
httpsAgent: ssrfFilter(url.href), // blocks private IPs at connect time (stops DNS rebinding)
maxRedirects: 0, // a redirect could point to an internal IP
timeout: 5000,
maxContentLength: 1_000_000,
});
res.json({ title: extractTitle(r.data) });
});Best option: when you know the destinations, use an allowlist of hosts.
Defense in depth (infrastructure)
- AWS: enforce IMDSv2 (needs a token, which blocks simple SSRF)
- Run URL fetchers in an isolated network with no access to internal services
- Firewall egress traffic from app servers
Checklist
- Allowlist hosts where possible
- Block private, loopback and link-local ranges (IPv4 and IPv6)
- Check the IP after DNS resolution, not only the hostname
- No redirects (or re-check each hop)
- Only
http/https - Timeouts and size limits