Registry / security / django-xff

django-xff

JSON →
library1.5.0pypypi✓ verified 85d ago

Middleware and utilities for handling X-Forwarded-For and related proxy headers in Django. Version 1.5.0 is current; releases are infrequent.

pip install django-xff
INSTALL
IMPORT
SIG · DJANGO-XFF
D
django-xff
securitypythonv1.5.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

XForwardedForMiddleware
from xff.middleware import XForwardedForMiddleware
from xff import XForwardedForMiddleware
Old import path caused ImportError starting in 1.0.0.

Add the middleware to INSTALLED_APPS (auto-discovered) or MIDDLEWARE. Set XFF_USE_PROXY = True when behind a trusted proxy to rewrite REMOTE_ADDR.

MIDDLEWARE = [ 'xff.middleware.XForwardedForMiddleware', # ... other middleware ] # settings.py XFF_USE_PROXY = True # required if behind a trusted proxy
Debug
Known issues
breakingIn version 1.0.0, the import path changed from 'xff.XForwardedForMiddleware' to 'xff.middleware.XForwardedForMiddleware'.
fix
Use 'from xff.middleware import XForwardedForMiddleware' or the dotted string 'xff.middleware.XForwardedForMiddleware' in MIDDLEWARE.
affects: >=1.0.0
gotchaThe middleware automatically rewrites request.META['REMOTE_ADDR'] only if XFF_USE_PROXY = True is set. Without it, the middleware does nothing.
fix
Set XFF_USE_PROXY = True in Django settings.
affects: all
gotchaThe middleware does not validate the number of proxies or perform any IP whitelisting by default. It trusts the leftmost IP in the X-Forwarded-For header blindly when XFF_USE_PROXY is True.
fix
Manually validate or use a custom middleware if you need strict proxy chain validation.
affects: all
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'xff'
Package not installed or virtual environment not activated.
fix
Run 'pip install django-xff'.
ImportError: cannot import name 'XForwardedForMiddleware' from 'xff'
Import path changed in version 1.0.0; using old 'from xff import XForwardedForMiddleware'.
fix
Change to 'from xff.middleware import XForwardedForMiddleware'.
AttributeError: 'WSGIRequest' object has no attribute 'META'
The middleware is applied to a non-HTTP request (e.g., test client or async).
fix
Ensure the middleware is only used with WSGI/ASGI HTTP requests.
Upgrade
Version history
1.5.0latest on PyPI · released Feb 13, 2025
Audit
Dependencies
djangorequiredRuntime dependency; requires Django >= 2.0? Not enforced via requires_python but tested against Django 2.2+.
Agent activity
20 hits · last 30 days
node
18
Resources
django-xff — pip install django-xff · libregistry