Cross-site scripting (XSS) via Django HttpResponse with user data

High Risk cross-site-scripting
pythondjangohttpresponsexssweb

What it is

XSS vulnerability in Django applications where user-controlled data is directly written to HttpResponse without proper escaping, bypassing Django's automatic template escaping and allowing script injection.

â„šī¸ Configuration Fix

Configuration changes required - see explanation below.

💡 Explanation

â„šī¸ Configuration Fix

Configuration changes required - see explanation below.

💡 Explanation

Why it happens

User input from requests is directly concatenated into HttpResponse content without escaping.

Root causes

Direct User Data in HttpResponse

User input from requests is directly concatenated into HttpResponse content without escaping.

Bypassing Template Engine Safety

Using HttpResponse instead of Django templates bypasses built-in XSS protections.

Dynamic HTML Generation

Building HTML responses programmatically with user data without proper encoding.

Fixes

1

Use Django Templates with Auto-Escaping

Use Django's template system which automatically escapes variables by default.

2

Manual HTML Escaping

If HttpResponse is necessary, manually escape all user data using Django's escape utilities.

3

Use JsonResponse for API Endpoints

For API endpoints, return JSON data instead of HTML to avoid XSS issues.

Detect This Vulnerability in Your Code

Sourcery automatically identifies cross-site scripting (xss) via django httpresponse with user data and many other security issues in your codebase.