Interactive Playground
Experiment with the vulnerable code and security rule below. Edit the code to see how the rule detects different vulnerability patterns.
pathfinder scan --ruleset python/PYTHON-DJANGO-SEC-050 --project .About This Rule
Understanding the vulnerability and how it is detected
This rule detects Cross-Site Scripting (XSS) vulnerabilities in Django applications where untrusted user input from HTTP request parameters flows directly into HttpResponse() without HTML escaping.
Django's template engine provides automatic HTML escaping by default, which is why rendering user input through templates is safe. However, when developers bypass the template system and construct HTML strings manually in views, passing them to HttpResponse(), they take on the responsibility of escaping user input that the template engine would otherwise handle automatically.
XSS vulnerabilities allow attackers to inject malicious JavaScript that executes in the victim's browser context, enabling session theft, credential harvesting, keylogging, DOM manipulation, and phishing within the legitimate application's origin.
Security Implications
Potential attack scenarios if this vulnerability is exploited
Session Cookie Theft and Account Takeover
Injected JavaScript can access document.cookie and send session cookies to attacker-controlled servers. If the application's session cookies do not have the HttpOnly flag set, this leads to complete account takeover without requiring the user's credentials.
Credential Harvesting via DOM Manipulation
Injected scripts can dynamically modify the page DOM to add fake login forms or overlay phishing content within the trusted application domain. Users filling out these fake forms send credentials directly to the attacker, bypassing browser security warnings about fake sites.
Stored XSS as a Secondary Vector
If the user-controlled value eventually flows from the database to HttpResponse (second-order XSS), the injection payload persists and executes every time any user views the affected page, amplifying the attack across the entire user base.
CSRF Token Exfiltration
XSS can extract CSRF tokens from the page and use them to submit state-changing requests with full CSRF protection bypassed, enabling fund transfers, account changes, and other sensitive operations on behalf of authenticated users.
How to Fix
Recommended remediation steps
- 1Use Django's template system (render() or TemplateResponse) for all HTML responses; templates auto-escape variables by default.
- 2When HttpResponse must be used with dynamic content, escape all user-controlled values with django.utils.html.escape() before inclusion.
- 3For API responses that do not render HTML, use JsonResponse which escapes HTML entities in JSON values.
- 4Never concatenate request parameters directly into HTML strings, even for "simple" values like names or IDs.
- 5Ensure response Content-Type is set appropriately (text/html for templates, application/json for APIs) to prevent MIME sniffing-based XSS.
Detection Scope
How Code Pathfinder analyzes your code for this vulnerability
This rule performs inter-procedural taint analysis with global scope. Sources include calls("request.GET.get"), calls("request.POST.get"), calls("request.GET.__getitem__"), calls("request.POST.__getitem__"), calls("request.body"), and calls("request.read"). The sink is the first argument (HTML content, tracked via .tracks(0)) of calls("HttpResponse") and calls("django.http.HttpResponse"). Sanitizers include calls("django.utils.html.escape"), calls("django.utils.html.strip_tags"), and calls("bleach.clean"). The rule follows taint across file and module boundaries.
Compliance & Standards
Industry frameworks and regulations that require detection of this vulnerability
References
External resources and documentation
Similar Rules
Explore related security rules for Python
Django mark_safe() Usage Audit
mark_safe() bypasses Django's automatic HTML escaping. Audit all usages to confirm content is properly sanitized before being marked safe.
Django SafeString Subclass Audit
Class extends SafeString or SafeData, bypassing Django's auto-escaping for all instances. Audit to confirm the class properly sanitizes content.
Django XSS in HTML Email Body via EmailMessage
User input flows into HTML email body content without sanitization, enabling HTML injection in emails.
Frequently Asked Questions
Common questions about Django XSS via Direct HttpResponse with User Input
New feature
Get these findings posted directly on your GitHub pull requests
The Django XSS via Direct HttpResponse with User Input rule runs in CI and posts inline review comments on the exact lines — no dashboard, no SARIF viewer.