# fastapi-ai-service-v1

> fastapi-ai-service-v1: 6 Keryn rules for python (1 error, 5 warning).

Source: https://keryn.dev/rules/fastapi-ai-service-v1/

6 rules · Keryn Software Licence · rules 0.8.0 · [all packs](https://keryn.dev/rules/)

**How to read a rule**

- **Severity**: How bad the finding is if it is real: `error` fails a default gate, `warning` and `info` do not.

- **Confidence**: **P0** exploitable as written; **P1** dangerous in context; **P2** a heuristic or a posture check: confirm by reading the code.

- **False-positive risk**: **low**: an exact API or flag, so hits are almost always real; **medium**: the pattern is legitimate in many programs, so check each hit; **high**: it finds a lead to confirm, so expect benign hits.

- **Edge**: The relationship the finding sits on. **reaches**: content or traffic gets to a thing; **calls**: one thing invokes another; **reads**: one thing reads from another; **writes**: one thing writes to another; **sends**: data leaves for another party; **grants**: authority is given or left unchecked; **depends_on**: something relies on a component it does not verify; **trusts**: one party believes another without evidence. The boundary it crosses, when there is one: **user->prompt** (untrusted input reaching a prompt); **model->code** (model output reaching code, a shell, a query or a page); **secret->outside** (a secret leaving the trusted side); **supply-chain** (code, models or prompts pulled in from outside).

- **Taxonomy chips**: References for readers who work in OWASP LLM 2025, OWASP Agentic, CWE or NIST AI RMF. They say where a rule belongs, not that a framework is satisfied.

## keryn.fastapi.cors-wildcard-with-credentials

`error` python fastapi, starlette, flask

CORS allows every origin together with credentials. Any web page the user visits can then call this service with the user's cookies or session, including its model endpoints: a page can drive the user's agent, read its answers and spend its budget. List the origins that may call the API, or drop credentials.

Confidence **P1**: dangerous in context. False-positive risk **low**: an exact API or flag, so hits are almost always real.

Edge inspected: [grants](https://keryn.dev/docs/edges/edge-kinds/#grants) authority is given or left unchecked.

[CWE-942](https://keryn.dev/rules/?cwe=CWE-942) [CWE-346](https://keryn.dev/taxonomies/cwe/#cwe-346-origin-validation-error) [LLM06](https://keryn.dev/taxonomies/owasp-llm/#llm06-excessive-agency) [LLM02](https://keryn.dev/taxonomies/owasp-llm/#llm02-sensitive-information-disclosure) [ASI03](https://keryn.dev/taxonomies/owasp-agentic/#asi03-identity-and-privilege-abuse) [ASI02](https://keryn.dev/taxonomies/owasp-agentic/#asi02-tool-misuse-and-exploitation)

**Fix:** Set allow\_origins (or Flask-CORS origins) to the exact origins of your front ends and keep allow\_credentials only for those; browsers refuse the wildcard with credentials anyway, so the current setting only works through an origin reflection that defeats the purpose.

**References:** [https://fastapi.tiangolo.com/tutorial/cors/](https://fastapi.tiangolo.com/tutorial/cors/) · [https://starlette.dev/middleware/#corsmiddleware](https://starlette.dev/middleware/#corsmiddleware) · [https://flask-cors.readthedocs.io/en/latest/api.html](https://flask-cors.readthedocs.io/en/latest/api.html)

## keryn.fastapi.llm-route-without-auth

`warning` python fastapi, starlette

This route calls a model but declares no authentication: no dependencies=[...] on the decorator, no Depends()/Security() parameter, no guarding decorator. Anyone who finds the URL can spend the model budget, send prompts under the service's identity and read whatever context the handler adds. Require a user or API-key dependency on every model route.

Confidence **P2**: a heuristic or a posture check: confirm by reading the code. False-positive risk **medium**: the pattern is legitimate in many programs, so check each hit.

Edge inspected: [grants](https://keryn.dev/docs/edges/edge-kinds/#grants) authority is given or left unchecked.

[CWE-306](https://keryn.dev/taxonomies/cwe/#cwe-306-missing-authentication-for-critical-function) [LLM10](https://keryn.dev/taxonomies/owasp-llm/#llm10-unbounded-consumption) [LLM01](https://keryn.dev/taxonomies/owasp-llm/#llm01-prompt-injection) [ASI03](https://keryn.dev/taxonomies/owasp-agentic/#asi03-identity-and-privilege-abuse) [ASI10](https://keryn.dev/taxonomies/owasp-agentic/#asi10-rogue-agents)

**Fix:** Add dependencies=[Depends(require\_user)] to the route (or to the APIRouter that owns it) or take the caller as a parameter (user: User = Depends(current\_user)); if the router applies the dependency, mark the route with a comment or use nosemgrep with this id.

**References:** [https://fastapi.tiangolo.com/tutorial/dependencies/dependencies-in-path-operation-decorators/](https://fastapi.tiangolo.com/tutorial/dependencies/dependencies-in-path-operation-decorators/) · [https://fastapi.tiangolo.com/tutorial/security/](https://fastapi.tiangolo.com/tutorial/security/)

## keryn.fastapi.llm-route-without-rate-limit

`warning` python fastapi, starlette, slowapi, fastapi-limiter

This route calls a model with no rate limit visible on it (no RateLimiter dependency, no limiter decorator). One client in a loop then consumes the whole model budget and the provider's rate limit for every other user. Put a per-caller limit on every model route, or on the router that owns it.

Confidence **P2**: a heuristic or a posture check: confirm by reading the code. False-positive risk **high**: it finds a lead to confirm, so expect benign hits.

Edge inspected: [grants](https://keryn.dev/docs/edges/edge-kinds/#grants) authority is given or left unchecked.

[CWE-770](https://keryn.dev/taxonomies/cwe/#cwe-770-allocation-of-resources-without-limits-or-throttling) [LLM10](https://keryn.dev/taxonomies/owasp-llm/#llm10-unbounded-consumption) [ASI08](https://keryn.dev/taxonomies/owasp-agentic/#asi08-cascading-failures)

**Fix:** Add dependencies=[Depends(RateLimiter(times=n, seconds=s))] (fastapi-limiter) or @limiter.limit("n/minute") (slowapi) to the route, keyed by user or API key rather than IP where callers share NAT; apply it at the router level for whole model APIs.

**References:** [https://github.com/laurentS/slowapi](https://github.com/laurentS/slowapi) · [https://github.com/long2ice/fastapi-limiter](https://github.com/long2ice/fastapi-limiter) · [https://fastapi.tiangolo.com/tutorial/dependencies/dependencies-in-path-operation-decorators/](https://fastapi.tiangolo.com/tutorial/dependencies/dependencies-in-path-operation-decorators/)

## keryn.flask.llm-route-without-auth

`warning` python flask

This Flask route calls a model and carries no guarding decorator (login\_required, jwt\_required, an auth wrapper). Anyone who finds the URL can spend the model budget and send prompts under the service's identity. Guard every model route.

Confidence **P2**: a heuristic or a posture check: confirm by reading the code. False-positive risk **medium**: the pattern is legitimate in many programs, so check each hit.

Edge inspected: [grants](https://keryn.dev/docs/edges/edge-kinds/#grants) authority is given or left unchecked.

[CWE-306](https://keryn.dev/taxonomies/cwe/#cwe-306-missing-authentication-for-critical-function) [LLM10](https://keryn.dev/taxonomies/owasp-llm/#llm10-unbounded-consumption) [LLM01](https://keryn.dev/taxonomies/owasp-llm/#llm01-prompt-injection) [ASI03](https://keryn.dev/taxonomies/owasp-agentic/#asi03-identity-and-privilege-abuse) [ASI10](https://keryn.dev/taxonomies/owasp-agentic/#asi10-rogue-agents)

**Fix:** Stack an authentication decorator under @app.route (login\_required, jwt\_required(), or your own wrapper that checks the session or API key) or enforce it in a before\_request hook for the blueprint that owns the model routes.

**References:** [https://flask.palletsprojects.com/en/stable/patterns/viewdecorators/](https://flask.palletsprojects.com/en/stable/patterns/viewdecorators/) · [https://flask-login.readthedocs.io/en/latest/#flask_login.login_required](https://flask-login.readthedocs.io/en/latest/#flask_login.login_required)

## keryn.fastapi.llm-exception-detail-to-client

`warning` python fastapi, starlette

The text of an exception raised by the model client is returned to the caller as the HTTP error detail. Provider errors carry the request id, the model, the organisation, rate-limit state and sometimes fragments of the prompt or the key; a caller learns which provider and account sit behind the service and can probe them. Log the exception and return a fixed message.

Confidence **P2**: a heuristic or a posture check: confirm by reading the code. False-positive risk **medium**: the pattern is legitimate in many programs, so check each hit.

Edge inspected: [sends](https://keryn.dev/docs/edges/edge-kinds/#sends) data leaves for another party.

[CWE-209](https://keryn.dev/rules/?cwe=CWE-209) [LLM02](https://keryn.dev/taxonomies/owasp-llm/#llm02-sensitive-information-disclosure) [LLM07](https://keryn.dev/taxonomies/owasp-llm/#llm07-system-prompt-leakage) [ASI09](https://keryn.dev/taxonomies/owasp-agentic/#asi09-human-agent-trust-exploitation)

**Fix:** log.exception(...) the provider error and raise HTTPException(status\_code=502, detail="upstream model error") with a correlation id the operator can look up; never interpolate the exception into detail.

**References:** [https://fastapi.tiangolo.com/tutorial/handling-errors/](https://fastapi.tiangolo.com/tutorial/handling-errors/)

## keryn.fastapi.upload-to-llm-without-size-check

`warning` python fastapi, starlette

An uploaded file is read whole with no check of its size before the bytes reach the model pipeline. A single large upload then consumes memory, tokens and the model's context budget at the caller's choice. Reject uploads above a limit before reading them.

Confidence **P2**: a heuristic or a posture check: confirm by reading the code. False-positive risk **medium**: the pattern is legitimate in many programs, so check each hit.

Edge inspected: [writes](https://keryn.dev/docs/edges/edge-kinds/#writes) one thing writes to another.

[CWE-770](https://keryn.dev/taxonomies/cwe/#cwe-770-allocation-of-resources-without-limits-or-throttling) [CWE-400](https://keryn.dev/rules/?cwe=CWE-400) [LLM10](https://keryn.dev/taxonomies/owasp-llm/#llm10-unbounded-consumption) [ASI08](https://keryn.dev/taxonomies/owasp-agentic/#asi08-cascading-failures)

**Fix:** Check file.size (or the Content-Length header, or read in bounded chunks) against a limit and raise HTTPException(413) before calling file.read(); also cap the number of pages, images or tokens you derive from it.

**References:** [https://fastapi.tiangolo.com/tutorial/request-files/](https://fastapi.tiangolo.com/tutorial/request-files/) · [https://starlette.dev/requests/#request-files](https://starlette.dev/requests/#request-files)
