Middleware for CSRF protection. This middleware will check for a CSRF token
in the headers of any POST, PUT, PATCH, or DELETE request. If the token is
not present or does not match the token stored in the database for the
client, the request will be rejected with a 403 status code.
Source code in src/prefect/server/api/middleware.py
classCsrfMiddleware(BaseHTTPMiddleware):""" Middleware for CSRF protection. This middleware will check for a CSRF token in the headers of any POST, PUT, PATCH, or DELETE request. If the token is not present or does not match the token stored in the database for the client, the request will be rejected with a 403 status code. """asyncdefdispatch(self,request:Request,call_next:NextMiddlewareFunction)->Response:""" Dispatch method for the middleware. This method will check for the presence of a CSRF token in the headers of the request and compare it to the token stored in the database for the client. If the token is not present or does not match, the request will be rejected with a 403 status code. """request_needs_csrf_protection=request.methodin{"POST","PUT","PATCH","DELETE",}if(settings.PREFECT_SERVER_CSRF_PROTECTION_ENABLED.value()andrequest_needs_csrf_protection):incoming_token=request.headers.get("Prefect-Csrf-Token")incoming_client=request.headers.get("Prefect-Csrf-Client")ifincoming_tokenisNone:returnJSONResponse({"detail":"Missing CSRF token."},status_code=status.HTTP_403_FORBIDDEN,)ifincoming_clientisNone:returnJSONResponse({"detail":"Missing client identifier."},status_code=status.HTTP_403_FORBIDDEN,)db=provide_database_interface()asyncwithdb.session_context()assession:token=awaitmodels.csrf_token.read_token_for_client(session=session,client=incoming_client)iftokenisNoneortoken.token!=incoming_token:returnJSONResponse({"detail":"Invalid CSRF token or client identifier."},status_code=status.HTTP_403_FORBIDDEN,headers={"Access-Control-Allow-Origin":"*"},)returnawaitcall_next(request)
Dispatch method for the middleware. This method will check for the
presence of a CSRF token in the headers of the request and compare it
to the token stored in the database for the client. If the token is not
present or does not match, the request will be rejected with a 403
status code.
Source code in src/prefect/server/api/middleware.py
asyncdefdispatch(self,request:Request,call_next:NextMiddlewareFunction)->Response:""" Dispatch method for the middleware. This method will check for the presence of a CSRF token in the headers of the request and compare it to the token stored in the database for the client. If the token is not present or does not match, the request will be rejected with a 403 status code. """request_needs_csrf_protection=request.methodin{"POST","PUT","PATCH","DELETE",}if(settings.PREFECT_SERVER_CSRF_PROTECTION_ENABLED.value()andrequest_needs_csrf_protection):incoming_token=request.headers.get("Prefect-Csrf-Token")incoming_client=request.headers.get("Prefect-Csrf-Client")ifincoming_tokenisNone:returnJSONResponse({"detail":"Missing CSRF token."},status_code=status.HTTP_403_FORBIDDEN,)ifincoming_clientisNone:returnJSONResponse({"detail":"Missing client identifier."},status_code=status.HTTP_403_FORBIDDEN,)db=provide_database_interface()asyncwithdb.session_context()assession:token=awaitmodels.csrf_token.read_token_for_client(session=session,client=incoming_client)iftokenisNoneortoken.token!=incoming_token:returnJSONResponse({"detail":"Invalid CSRF token or client identifier."},status_code=status.HTTP_403_FORBIDDEN,headers={"Access-Control-Allow-Origin":"*"},)returnawaitcall_next(request)