ushka.guard.integrations.fastapi
Ushka Guard for FastAPI.
from ushka.guard.integrations import fastapi as guard
guard.install(rewrite_packages=["myapp"]) # once, before the app handles requests
It does everything ushka.guard.integrations.starlette does (the request
objects and the HTML, redirect and file sinks), and one more thing FastAPI
needs. FastAPI validates parameters with pydantic, which hands your endpoint a
plain str even when the request said otherwise. So the guard marks the
values FastAPI took from the request (path, query, header, cookie and body
parameters, inside pydantic models too) as Untrusted right before your
endpoint or dependency runs.
Only an exact str is marked. int, bool, UUID, Enum and
Literal parameters are validated values and stay as they are: that is
validation doing the sanitising.
This relies on a FastAPI internal (solve_dependencies). If a FastAPI
release changes it, install raises instead of quietly protecting nothing.
In this module
def install() | Turns the guard on for FastAPI. Calling it twice is harmless. |
def uninstall() | Restores FastAPI's and Starlette's own code. |
def install
def install(rewrite_packages: Iterable[str] = ()) -> NoneTurns the guard on for FastAPI. Calling it twice is harmless.
def uninstall
def uninstall() -> NoneRestores FastAPI's and Starlette's own code.