Top 20 Server-Side Request Forgery (SSRF) Interview Questions
Server-Side Request Forgery (SSRF) is a high-severity web security vulnerability that allows an attacker to induce the server-side application to make HTTP requests to an unintended location.
Whether you are preparing for a senior application security engineering role or hardening internal microservices against cloud metadata abuse, mastering SSRF mechanics, bypass techniques, and defense strategies is essential.
Core Mechanics of Server-Side Request Forgery
Understanding how SSRF vulnerabilities arise and how attackers abuse internal infrastructure requires examining backend request handling:
- Vulnerability Origin: Occurs when a web application fetches a remote resource via user-supplied URLs without properly validating or sanitizing the destination address.
- Cloud Metadata Abuse: Attackers target local cloud provider endpoints (such as AWS metadata at
169.254.169.254) to exfiltrate temporary IAM credentials and instance roles. - Internal Network Pivoting: Allows external actors to scan internal ports, interact with backend databases, and access restricted microservices behind a corporate firewall.
Insecure Request Handling vs. Protected URL Parsing
| Evaluation Pillar | Vulnerable Implementation | Secure Hardened Architecture |
|---|---|---|
| URL Validation | Accepts raw user strings directly into HTTP client libraries | Strict allowlisting of permitted domains and schemes (HTTPS only) |
| IP Address Resolution | Resolves domain names directly without checking target IP ranges | Resolves DNS and blocks private, loopback, and link-local IP blocks |
| Best Suited For | Uncontrolled remote fetch features and legacy applications | Secure web APIs, webhook dispatchers, and enterprise microservices |
Secure URL Validation & SSRF Prevention in Python
Here is how developers can validate user-supplied URLs and block requests to private IP ranges and cloud metadata endpoints in Python:
import ipaddress
import socket
from urllib.parse import urlparse
def is_safe_url(target_url):
parsed_url = urlparse(target_url)
# 1. Enforce secure schemes only
if parsed_url.scheme not in ['https']:
return False
hostname = parsed_url.hostname
if not hostname:
return False
try:
# 2. Resolve hostname to IP address
ip_str = socket.gethostbyname(hostname)
ip_obj = ipaddress.ip_address(ip_str)
# 3. Block loopback, private networks, and cloud metadata IPs (169.254.169.254)
if ip_obj.is_private or ip_obj.is_loopback or ip_obj.is_link_local:
return False
return True
except Exception:
return False
# Test URL validation
test_url = "https://api.github.com/v3/repos"
if is_safe_url(test_url):
print("[SUCCESS] URL passed security validation.")
else:
print("[BLOCKED] Potential SSRF attempt or unsafe destination.")
SSRF Prevention Best Practices
When designing web applications that process remote URLs or webhooks, follow these security guidelines:
- Enforce Strict Allowlisting: Never rely on blocklists; explicitly maintain a whitelist of approved domains and protocols that your application is allowed to contact.
- Disable HTTP Redirection: Configure your HTTP client libraries to disable automatic following of redirects, which attackers often use to bypass domain filters.
- Restrict Instance Metadata Service (IMDS): On cloud environments like AWS, enforce IMDSv2 and restrict container network permissions to minimize metadata exposure.