ushka.guard.integrations.starlette
Ushka Guard for Starlette and FastAPI.
from ushka.guard.integrations import starlette as guard
guard.install(rewrite_packages=["myapp"]) # once, before the app handles requests
After that, everything a client sends is an Untrusted string:
request.query_params, request.headers, request.cookies,
request.path_params, await request.json() and await request.form().
FastAPI reads the same objects, so its str parameters arrive untrusted too.
And these sinks refuse such a string with an error that says how to fix it:
HTMLResponse, RedirectResponse and FileResponse.
install changes Starlette's classes for the whole process (that is the only
way to cover code that does not know about Ushka). uninstall() restores them.
rewrite_packages lists your own packages: the guard recompiles them on
import so the mark survives f-strings, .format and % (without it,
f"<p>{name}</p>" is a plain str and slips past). See ushka.guard.rewrite.
In this module
def install() | Turns the guard on for Starlette (and FastAPI). Calling it twice is harmless. |
def uninstall() | Restores Starlette's own classes. |
def install
def install(rewrite_packages: Iterable[str] = ()) -> NoneTurns the guard on for Starlette (and FastAPI). Calling it twice is harmless.
def uninstall
def uninstall() -> NoneRestores Starlette's own classes.