Menu

Fetching a URL a stranger gave you: a safe pattern for PHP and curl
📰

Fetching a URL a stranger gave you: a safe pattern for PHP and curl

DEV Community: php·Vignesh·14 days ago
#TF7vLHei
#devto#dev#address#name#curl
Long article (1822 words) — no AI summary yet.
Reading 0:00
15s threshold

Some tools take a web address from a visitor and fetch it from your server. SEO audits do this. So do link previews, webhook testers and "import from URL" buttons.

That hands the visitor control over where your server connects. If nothing limits it, they can aim your server at places only it can reach. That includes a service on localhost, machines on your private network, and the cloud metadata address, where cloud providers can expose credentials. This attack is called server-side request forgery, or SSRF. The OWASP SSRF prevention cheat sheet is the usual reference.

We run a free website audit tool, so our server fetches whatever address a stranger types in. This is the pattern we use: five rules and about 90 lines of PHP.

Why checking the hostname is not enough

The common first fix is to check that the hostname is public, then call curl with redirects switched on. It fails in two ways.

Redirects. A public site can answer with a 302 and a Location header that points to http://169.254.169.254/ .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.