Skip to content

Sanitize redirect URLs to prevent XSS and header injection#101

Open
TristanInSec wants to merge 1 commit into
emikulic:masterfrom
TristanInSec:fix/redirect-xss-crlf
Open

Sanitize redirect URLs to prevent XSS and header injection#101
TristanInSec wants to merge 1 commit into
emikulic:masterfrom
TristanInSec:fix/redirect-xss-crlf

Conversation

@TristanInSec
Copy link
Copy Markdown

Summary

  • Fix reflected XSS in redirect response bodies (CWE-79)
  • Fix HTTP response splitting via CRLF injection in redirect Location header (CWE-113)

Both issues affect the redirect() function when --forward, --forward-all, or --https-redirect is configured. URL-decoded paths are placed unsanitized into both the HTML body and the Location header.

Changes

  • Add html_escape() to encode < > & " ' in the HTML response body
  • Add strip_crlf() to remove \r and \n from the Location header value
  • The existing append_escaped() in generate_dir_listing() uses the apbuf interface and cannot be reused here

Test

# Start with forwarding enabled
./darkhttpd /tmp/test --port 8080 --forward-all http://example.com

# XSS: body should show &lt;script&gt; (escaped), not raw <script>
curl -D- "http://127.0.0.1:8080/%3Cscript%3Ealert(1)%3C/script%3E"

# CRLF: Set-Cookie should appear inside Location value, not as separate header
curl -D- "http://127.0.0.1:8080/%0d%0aSet-Cookie:%20evil=1"

The redirect() function places URL-decoded paths directly into both
the HTML response body and the Location header. Percent-encoded HTML
characters (%3Cscript%3E) are decoded and reflected in the body
without escaping, enabling reflected XSS. Percent-encoded CRLF
(%0d%0a) is decoded and placed in the Location header, enabling
HTTP response splitting and header injection.

Add html_escape() to encode HTML-special characters in the response
body, and strip_crlf() to remove CR/LF from the Location header
value. The existing append_escaped() in generate_dir_listing() uses
the apbuf interface and cannot be reused here.

Fixes both issues when --forward, --forward-all, or --https-redirect
is configured.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant