Skip to content

Add API tests, CI workflow and engine/api hardening - #1

Open
jotive wants to merge 2 commits into
mainfrom
claude/fervent-wright-qggy51
Open

jotive wants to merge 2 commits into
mainfrom
claude/fervent-wright-qggy51

Conversation

@jotive

@jotive jotive commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Tests (api/tests, 62 cases, LLM stubbed): file -> engine -> provider -> schema flow end to end through the HTTP API; invalid config (bad numeric env, non-JSON ALLOWED_ORIGINS); error paths (unknown product, empty/oversized file, unsupported MIME, provider failure, malformed or schema-violating model output, retry/backoff in the Gemini provider); rate limiting; CORS; hostile slugs; usage tracking failure handling.
  • CI (.github/workflows/ci.yml): api-tests (Python 3.12, pytest) and web-build (Node 22, npm ci + npm run build) on pull requests and pushes to main. Read-only permissions.
  • Hardening (no change to routes, response shapes, status codes or config):
    • Upload is read with a bound (max_upload_bytes + 1) instead of fully into memory before the size check.
    • The blocking provider call runs in a threadpool so it no longer blocks the event loop.
    • Provider failures are logged server-side; the client still gets the same generic 502.
    • An "untrusted input" notice is appended to every prompt so instructions inside an uploaded file are not followed.
    • The rate limiter drops stale client entries once the table passes 10,000 keys (unbounded growth before).

Security review: findings not implemented (would change behavior or need a decision)

  1. Client IP behind Cloud Run: the rate limiter keys on request.client.host, which behind a proxy may be the proxy address, so all users could share one bucket. Fixing it means trusting X-Forwarded-For (spoofable unless the proxy hop is configured). Needs a deployment decision.
  2. Rate limiter is per instance and in memory: limits multiply with Cloud Run instances and reset on restart.
  3. MIME is taken from the client Content-Type: bytes are not sniffed. Rejecting mismatches could break clients that send a wrong type today.
  4. CORS defaults to * and .env.example ships it. Restrict ALLOWED_ORIGINS in production.
  5. /stats is unauthenticated and exposes per-product usage counts. Adding auth changes the public interface.
  6. No request body cap before parsing: multipart bodies are spooled by the parser before the handler runs. Enforce a body limit at the proxy/ingress or with a pure ASGI middleware.
  7. Prompt injection residual risk: the schema constrains output shape, but free-text fields (notes, vendor, description, ...) can still carry attacker-controlled text from the file. Downstream consumers (the web UI, the distributor integration) must treat them as untrusted and escape them. The prompt notice reduces, not eliminates, the risk.
  8. Dockerfile runs as root; add a non-root user.
  9. Dependencies are unpinned (>=); consider a lock file for reproducible builds and audits.
  10. HTTP_413_REQUEST_ENTITY_TOO_LARGE is deprecated in current Starlette (only a warning today).

Path handling: the API never touches the filesystem with user input; the product slug is only a dict lookup and uploads stay in memory. Tests cover traversal-style slugs.

Verification

  • python -m pytest -q in api/: 62 passed (Python 3.13 locally; CI pins 3.12).
  • npm ci && npm run build in web/: 15 pages built.
  • The workflow itself has not run on GitHub yet; its first run is this PR.

claude and others added 2 commits November 4, 2026 16:17
- tests for engine, provider, controller, rate limit, config, registry and usage with the LLM stubbed
- GitHub Actions workflow running pytest and the Astro build
- bounded upload read, threadpool execution of the blocking provider call, failure logging
- untrusted-input notice appended to every prompt
- rate limiter sweeps stale client entries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants