Skip to content

FastAPI

The fastapi configuration adds the FastAPI rules of Ruff, and three Semgrep rules for the settings and errors of a FastAPI application. To check an OpenAPI description of the service, add the openapi configuration.

python/ruff runs three FastAPI rules at both levels:

Rule Reports
FAST001 A route with a response_model that repeats its return type.
FAST002 A dependency in a parameter default, instead of an Annotated type.
FAST003 A path parameter that the route function does not take as a parameter.

security/semgrep runs these three rules before each push, on macOS and Linux only:

Rule Reports
gspot.fastapi.cors-wildcard-with-credentials CORSMiddleware that allows every origin with allow_credentials=True.
gspot.fastapi.debug-enabled FastAPI(debug=True), which returns tracebacks to the client.
gspot.fastapi.exception-text-in-response An HTTPException whose detail holds the text of a caught exception.

For example, this module gets all three findings:

app/main.py
"""The API."""
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI(debug=True)
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True)
@app.get("/items/{item_id}")
def read_item(item_id: int) -> dict:
"""Read one item."""
try:
return {"id": item_id}
except ValueError as error:
raise HTTPException(status_code=400, detail=str(error)) from error
failed security/semgrep 3 findings
app/main.py:6:7 gspot.fastapi.debug-enabled Debug mode returns tracebacks to the client. Read the flag from configuration, off by default.
app/main.py:7:1 gspot.fastapi.cors-wildcard-with-credentials Every origin is allowed together with credentials, so any site reads an authenticated response. List the origins.
app/main.py:16:15 gspot.fastapi.exception-text-in-response The text of an internal exception goes to the client. Log it, and return a stable message.

gspot rejects a nosemgrep comment. To accept a finding, ignore the rule with a reason:

example
gspot ignore security/semgrep --rule gspot.fastapi.debug-enabled --paths "dev/server.py" --reason "The local server runs only on a developer machine."

To add rules of your own, follow Semgrep.