chatbot-app
View on GitHub ↗
Reference

Security

chatbot-app is an educational reference app, not a hardened production service — but the pieces that touch trust boundaries (auth, file paths, arbitrary-looking expressions, cross-origin requests) are deliberately defensive.

Security properties

Separate JWT and API secrets
JWT_SECRET_KEY signs issued tokens (HS256) and must differ from API_SECRET, the pre-shared secret exchanged for a token via POST /auth/token. That exchange is rate limited to 5/min to slow brute-forcing.
Path-traversal containment
session_id is constrained to ^[a-zA-Z0-9_-]{1,64}$ before it ever reaches session_store.py, since it becomes part of a file path. The agent's read_doc tool is likewise confined to docs/ and can't escape it.
AST-restricted calculator
No eval/exec — expressions are parsed to an AST and walked against an allowlist. _check_pow_cost also bounds ast.Pow's result size via log2 before evaluating, rejecting anything over _MAX_POW_RESULT_BITS so a request like 9**9**9 can't pin a CPU core computing a ~369-million-digit number.
Mandatory tool approval
Every agent tool call — web_search, calculator, read_doc — pauses on a LangGraph interrupt_before=["tools"] checkpoint. Nothing executes without explicit human approval, in the CLI or the API, and every attempt is logged to logs/agent.jsonl.
Fail-closed CORS
CORS_ORIGINS is comma-separated allowed origins; unset or empty means none allowed, not a permissive wildcard default.
ASGI-layer rate limiting
Chat and agent routes carry no per-route @limiter.limit decorator on purpose — slowapi's SlowAPIMiddleware enforces default_limits before FastAPI resolves Depends(verify_token), so unauthenticated requests can't slip past rate limiting. See REST API Reference.

Fail-fast configuration

Missing keys, unavailable local models, and incomplete SYSTEM_PROMPT placeholders are surfaced at startup rather than at request time. Each LLM provider validates only its own requirements (see Configuration & LLM Providers), and the Polish SYSTEM_PROMPT in constants.py must have every bracketed placeholder replaced before the app will start.

Prompt-injection framing on web_search

Search results are untrusted, model-facing text by construction — content returned by web_search can contain instructions crafted to manipulate the model. The tool's output is treated as data to reason about, not as instructions to follow, and every tool call (including web_search) still requires human approval before it runs at all.

Known, accepted limitations

The CLI uses one hardcoded session ID and is not a multi-user client. API authentication identifies one shared service client, not individual users — any valid token can address any valid session_id or pending_id. The local JSON session store isn't designed for concurrent distributed workers. Agent checkpoints and pending approvals don't survive process restarts (in-memory MemorySaver). RAG indexing is local and requires an explicit index rebuild after corpus changes.

Next steps