Response Handling and Information Disclosure
You'll learn why returning raw server responses or verbose error messages can leak sensitive internal details and help attackers exploit your system.
What Is This Risk?
When your server makes requests to internal services (like during SSRF attacks) or encounters errors, it often generates detailed responses containing stack traces, internal IP addresses, software versions, file paths, or database details. If you pass these raw responses directly back to the user, you've just handed an attacker a treasure map of your infrastructure.
Think of it like a restaurant kitchen: customers should only see the finished dish, not all the supplier invoices, chef's notes, and kitchen blueprints taped to the wall. Similarly, users should receive only the information they need—not your server's internal workings.
Why This Matters
After defending against SSRF attacks through validation and network controls, response handling is your last line of defense. An attacker who tricks your server into fetching an internal resource might not succeed if you:
- Sanitize error messages: Replace detailed stack traces with generic messages like "An error occurred"
- Limit response data: Don't echo back raw responses from internal services
- Strip metadata: Remove headers revealing server software, versions, or internal routing
- Normalize status codes: Return consistent user-facing codes, not raw backend responses
For example, if an SSRF attempt fetches http://169.254.169.254/metadata/, returning the full cloud metadata response would be catastrophic. Instead, log the incident, return a generic error, and reveal nothing about what happened internally.
Defense Strategy
Always treat server responses as sensitive. Filter, sanitize, and transform them before presenting anything to users. Log full details internally for debugging, but externally show only safe, generic information.
Key Takeaway:
Never return raw server responses or detailed error messages to users—sanitize all output to prevent leaking internal system information that attackers can exploit.