Ushka

ushka.guard.integrations.django

Ushka Guard for Django.

# settings.py
MIDDLEWARE = ["ushka.guard.integrations.django.GuardMiddleware", ...]

# e.g. in apps.py ready(), or manage.py / wsgi.py
from ushka.guard.integrations import django as guard
guard.install(rewrite_packages=["myproject"])

The middleware makes what a client sends Untrusted: request.GET, request.POST, request.COOKIES, request.headers, the URL keyword arguments of your view, and request.json() (added by the middleware).

install() makes these sinks refuse such a string: HttpResponse with HTML, every HttpResponseRedirect (and so redirect()) and mark_safe (which is what the |safe filter calls). It patches Django's classes for the whole process, and uninstall() restores them.

rewrite_packages lists your own packages: the guard recompiles them on import so the mark survives f-strings (see ushka.guard.rewrite). Without it, HttpResponse(f"<p>{name}</p>") is a plain str and slips past.

In this module

class GuardMiddlewareMarks the request's client data as Untrusted.
def install()Turns the sink checks on for Django. Calling it twice is harmless.
def uninstall()Restores Django's own classes.

class GuardMiddleware

Marks the request's client data as Untrusted.

process_view

def process_view(request, view_func, view_args, view_kwargs)

def install

def install(rewrite_packages: Iterable[str] = ()) -> None

Turns the sink checks on for Django. Calling it twice is harmless.

def uninstall

def uninstall() -> None

Restores Django's own classes.