SSRF
Your server will fetch any URL a user gives it
Webhooks, link previews, and import-from-URL features all make HTTP requests from inside your network. Point one at the cloud metadata endpoint and see what comes back.
In 2019, an attacker got a misconfigured web application firewall at Capital One to make requests on their behalf. By most accounts, one of those requests went to the EC2 metadata endpoint, which handed back temporary IAM credentials, and those credentials could read S3. Data on about 100 million people in the US and 6 million in Canada walked out, and regulators fined the bank $80 million. AWS shipped IMDSv2 that November.
Any feature where a user hands you a URL and your server fetches it is a candidate: webhook endpoints, avatar-from-URL, link previews, RSS imports, PDF generators that load remote images. The request comes from your server, inside your network, with whatever access your server has.
The basic bug
require "open-uri"
def create
current_user.avatar.attach( URI.open(params[]), "avatar")
redirect_to profile_path
end
On AWS with IMDSv1, http://169.254.169.254/latest/meta-data/iam/security-credentials/ returns the instance role’s name, and one more path segment returns live AWS credentials. Same idea for http://localhost:9200 (an Elasticsearch with no auth) or http://10.0.0.5/admin. The attacker never touches those hosts. Your server does it for them.
The basic fix
require "resolv"
require "ipaddr"
module SafeUrl
def self.public?(url)
uri = URI.parse(url)
return false unless .include?(uri.scheme) && uri.host
addresses = Resolv.getaddresses(uri.host).map { |address| IPAddr.new(address) }
addresses.any? && addresses.none? { |ip| ip.private? || ip.loopback? || ip.link_local? }
rescue URI::InvalidURIError, IPAddr::InvalidAddressError
false
end
end
Checking the hostname string isn’t enough, because totally-public.example can resolve to 169.254.169.254. Resolve it, then refuse private, loopback, and link-local addresses before you fetch anything.
The hardcore version
def create
return head unless SafeUrl.public?(params[])
current_user.avatar.attach( URI.open(params[]), "avatar")
redirect_to profile_path
end
Two ways past it. First, DNS rebinding: SafeUrl resolves the name, then URI.open resolves it again, and an attacker’s DNS server can answer with a public IP the first time and 169.254.169.254 the second. Second, redirects: the URL is genuinely public and returns a 302 to the metadata endpoint, and open-uri follows it without asking you.
The webhook version
class WebhookEndpoint < ApplicationRecord
validate
private
def url_is_public
errors.add(, "must be a public URL") unless SafeUrl.public?(url)
end
end
class WebhookDeliveryJob < ApplicationJob
def perform(endpoint, payload)
response = Faraday.post(endpoint.url, payload.to_json, "Content-Type" => "application/json")
endpoint.deliveries.create!( response.status, response.body.to_s.first())
end
end
Customer-configured webhooks are SSRF as a feature: your server sends requests to URLs other people pick, forever. The validation runs once, when the URL is saved. Deliveries run for months after that, long after the hostname could have been repointed somewhere internal. Then there’s the delivery log, the nice settings page that shows the status and response body for each attempt so customers can debug their endpoint. It turns a blind SSRF into one that reads the response back. (Faraday doesn’t follow redirects unless you add the middleware, which is the one thing going right here.)
class WebhookDeliveryJob < ApplicationJob
def perform(endpoint, payload)
response = SsrfFilter.post(endpoint.url, payload.to_json, { "Content-Type" => "application/json" }, )
endpoint.deliveries.create!( response.code.to_i)
rescue SsrfFilter::Error => error
endpoint.deliveries.create!( , error.class.name)
end
end
Check at delivery time, against the IP you actually connect to, every time. Don’t follow redirects; a webhook receiver has no reason to send one. Store the status code, not the body. If customers really want to see what their endpoint said, keep a few hundred bytes, which is plenty to debug with and far less useful to anyone fishing.
The PDF version
<h1>Invoice <%= @invoice.number %></h1>
<div class="notes"><%= raw @invoice.notes %></div>
def show
respond_to do |format|
format.html
format.pdf { render "invoice-#{@invoice.number}" }
end
end
The notes field allows formatting, so somebody used raw. In the browser that’s an XSS bug. In a PDF it’s worse. wicked_pdf hands the HTML to wkhtmltopdf, which is a headless browser running on your server, and it loads every image, stylesheet, and iframe in the document from inside your network. Point an iframe in the notes at the metadata endpoint and the response gets rendered into the PDF you download. wkhtmltopdf will also read local files unless you tell it not to, and its repository was archived in 2023, so those defaults aren’t getting better.
<h1>Invoice <%= @invoice.number %></h1>
<div class="notes"><%= sanitize @invoice.notes, tags: %w[p br strong em ul ol li], attributes: [] %></div>
Sanitize it like any other user HTML, and then treat the renderer as the untrusted browser it is: run it in its own container with no route to the metadata endpoint or your internal network, and only let it fetch from your own asset host. If you’re moving off wkhtmltopdf anyway (you should), Ferrum drives headless Chrome directly and can intercept requests and abort the ones you didn’t expect.
Fix #1: Validate the IP you actually connect to
def create
response = SsrfFilter.get(params[])
current_user.avatar.attach( StringIO.new(response.body), "avatar")
redirect_to profile_path
rescue SsrfFilter::Error
head
end
The ssrf_filter gem resolves the host once, checks the address against private ranges, then connects to that exact IP. It also re-runs the check on every redirect hop. That closes both gaps from the hardcore version without you writing socket code.
Fix #2: Make the metadata endpoint useless
aws ec2 modify-instance-metadata-options \
--instance-id i-0123456789abcdef0 \
--http-tokens required \
--http-put-response-hop-limit 1
IMDSv2 requires a PUT with a token header before it’ll answer anything, and almost no SSRF bug lets an attacker send a PUT with custom headers. A hop limit of 1 also keeps containers on the box from reaching it. Do this on every instance, even the ones you’re sure don’t fetch user URLs. Especially those, actually.
That’s the fix aimed squarely at Capital One’s bug. The firewall would still have been misconfigured, but a box that was tricked into fetching a URL almost certainly couldn’t have sent the PUT with a custom header that IMDSv2 wants first, so the credentials wouldn’t have come back.
Fix #3: Send outbound requests through an egress proxy
module OutboundHttp
def self.get(url)
connection = Faraday.new( ENV.fetch("EGRESS_PROXY_URL")) do |f|
f.options.timeout =
end
connection.get(url)
end
end
Run something like Stripe’s Smokescreen as the only way user-supplied URLs leave your network, with its own denylist for internal ranges. Then lock down the app servers’ security groups so they can’t open outbound connections except through it. Now a forgotten Net::HTTP.get in some admin tool can’t reach your internal network either, because the network itself says no. It’s infrastructure work, and it’s the only fix here that covers the code you haven’t written yet.
Then go find every URI.open, Net::HTTP, Faraday, and HTTParty call that takes a URL a user typed in. There are more than you think.