Skip to content

Commit

Permalink
Fix csrf (#16230)
Browse files Browse the repository at this point in the history
* Fix csrf check

* Simplify
  • Loading branch information
NickM-27 authored Jan 30, 2025
1 parent 7b65bcf commit cea210d
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions frigate/api/fastapi_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@
logger = logging.getLogger(__name__)


def check_csrf(request: Request):
def check_csrf(request: Request) -> bool:
if request.method in ["GET", "HEAD", "OPTIONS", "TRACE"]:
pass
return True
if "origin" in request.headers and "x-csrf-token" not in request.headers:
return JSONResponse(
content={"success": False, "message": "Missing CSRF header"},
status_code=401,
)
return False

return True


# Used to retrieve the remote-user header: https://starlette-context.readthedocs.io/en/latest/plugins.html#easy-mode
Expand Down Expand Up @@ -71,7 +70,12 @@ def create_fastapi_app(
@app.middleware("http")
async def frigate_middleware(request: Request, call_next):
# Before request
check_csrf(request)
if not check_csrf(request):
return JSONResponse(
content={"success": False, "message": "Missing CSRF header"},
status_code=401,
)

if database.is_closed():
database.connect()

Expand Down

0 comments on commit cea210d

Please sign in to comment.