T09 · Insecure Skill Coding Practices
- Location
main.py:12- Finding
Unrestricted User-Controlled URL Fetching Enables Server-Side Request Forgery
- Content
View full analysis
Vulnerability Details
File Location:
main.py, lines 12–16; user input originates at lines 49–53
Vulnerability Type: Server-Side Request Forgery (SSRF)
Risk Level: HighVulnerable Code
python def scrape_web(url): """抓取网页""" try: import requests response = requests.get(url, timeout=10)The URL is taken directly from command-line input and passed to the vulnerable function:
python action = sys.argv[1] url = sys.argv[2] if action == "scrape": result = scrape_web(url)Technical Analysis
The application passes an attacker-controlled URL directly to
requests.get()without validating the URL scheme, hostname, resolved IP address, destination port, or redirect targets.Consequently, an attacker able to invoke the scraper can make requests from the application's network context to destinations that may not be reachable from the attacker's own system, including:
- Loopback services
- Private-network services
- Link-local addresses
- Cloud instance metadata services
- Services exposed on nonstandard ports
The
requestslibrary follows redirects by default. Therefore, checking only the initially supplied hostname would be insufficient: a public endpoint could redirect the request to a prohibited internal address.Although the full response body is not written to the report, the application records the response status, body length, HTML title, and link count. These values can act as an information-disclosure oracle and support internal service discovery.
Attack Path
- An attacker supplies a URL pointing to an internal or otherwise protected HTTP service, for example:
bash python3 main.py scrape http://127.0.0.1:8080/ main()reads the untrusted value fromsys.argv[2].- The value is passed unchanged to
scrape_web(). requests.get()sends the request using the process's network access.- The target's status code, content length, title, and link count are deriv ...[truncated 1202 chars]
- Remediation
View remediation
Remediation Suggestions
Implement layered SSRF defenses rather than relying on a simple hostname string check:
-
Restrict URL schemes
- Permit only
httpandhttps. - Reject credentials embedded in URLs and all unsupported schemes.
- Permit only
-
Use an explicit destination allowlist
- Where the product permits it, restrict requests to approved domains and ports.
- Normalize hostnames before allowlist comparison.
-
Validate resolved addresses
- Resolve the hostname before connecting.
- Reject loopback, private, link-local, multicast, unspecified, reserved, and other non-public address ranges for both IPv4 and IPv6.
- Validate every returned address, not only the first one.
-
Defend against DNS rebinding
- Ensure the address validated is the address used for the connection.
- Prefer enforcement through a controlled outbound proxy or network firewall.
-
Control redirects
- Disable automatic redirects with
allow_redirects=False, or validate the scheme, hostname, resolved address, and port of every redirect hop. - Impose a low redirect limit.
- Disable automatic redirects with
-
Restrict destination ports
- Allow only ports required for the feature, normally 80 and 443.
-
Apply network egress controls
- Block access from the scraper runtime to loopback, internal networks, metadata services, and administrative networks.
- Run the process in a minimally privileged, isolated environment.
-
Harden request behavior
- Set explicit connection and read timeouts.
- Limit response size to prevent resource exhaustion.
- Disable environment-derived proxy configuration when it is not intentionally required, such as by using a configured
requests.Sessionwithtrust_env = False.
-
Reduce information exposure
- Avoid returning or storing internal target metadata.
- Record rejected requests in security logs without storing sensitive URL credentials.
-
Add security tests
- Test loopback, RFC 1918, IPv6 ...[truncated 142 chars]
-
