Skip to content

fix: require bearer tokens for org config, docs and audit API routes (#59) - #127

Merged
OkiriGabriel merged 1 commit into
mainfrom
fix/api-auth-59
Oct 24, 2026
Merged

OkiriGabriel merged 1 commit into
mainfrom
fix/api-auth-59

Conversation

@OkiriGabriel

Copy link
Copy Markdown
Collaborator

Description

Fixes #59 for the HTTP API. The MCP part of #59 (configure_org_credentials over SSE/HTTP) needs a different mechanism and is tracked in detail in #116.

Anyone who could reach the API port could write any tenant's credentials (PUT /orgs/{org}/config), read which integrations each org has configured, upload, read or delete runbooks, and read the full incident audit trail.

New api/api_auth.py gates all eight routes: /audit, /orgs/{org}/config, /orgs/{org}/config/status, and the five /orgs/{org}/docs* routes.

Token Valid for
API_ADMIN_TOKEN every org (platform operator)
API_ORG_TOKENS=acme=tok1,globex=tok2 only its own org
Request Response
Neither variable set 503: fails closed. API_AUTH_DISABLED=true is an explicit opt-out for local dev and logs a warning at startup
No token, unknown token, or not Bearer 401 + WWW-Authenticate: Bearer
Valid org token used for a different org 403
Admin token, or the org's own token handled normally

Tokens are compared in constant time (reusing _signatures_equal), and every org token is compared so timing doesn't reveal which org matched. For /audit the org is ?org_id= or ORG_ID. Webhooks keep their own signature auth, and /health stays open for probes.

⚠️ Breaking change

After upgrading, the org config, docs and audit routes return 503 until API_ADMIN_TOKEN or API_ORG_TOKENS is set. This is deliberate: failing open would leave existing deployments exposed. Webhooks, Slack and /health are unaffected. .env.example documents the new variables.

Type of Change

  • Bug fix (security)
  • Breaking change (management API now requires a token)
  • Documentation update

Testing

  • New tests/test_api_auth.py (64 tests). Every protected route × missing token, unknown token, Basic scheme, admin token on any org, org token on its own org, org token on another org (403), not configured (503), explicit disable. Also: a rejected credential write stores nothing, /audit authorizes against ORG_ID when org_id is omitted, the multipart upload route, API_ORG_TOKENS parsing, and webhooks//health unaffected.
  • Against the old server.py, 38 of the 64 fail: every rejection case. The 26 that pass are the "authorized request succeeds" cases, which old and new both allow.
  • Existing tests updated to send a token (test_api.py, test_basic.py, and test_agent.py, which calls get_audit directly).
  • CI integration job: /audit must return 401 without a token and 200 with one.
  • Full suite: 901 passed, 4 skipped. Flake8 critical: 0. Bandit -ll: clean. mkdocs build --strict plus link check: 0 errors.

Docs

  • docs/API_REFERENCE.md: auth table plus a new "Management API tokens" section.
  • README.md BYOK section explains $API_TOKEN.
  • All ~30 curl examples for these routes across the docs now send -H "Authorization: Bearer $API_TOKEN".
  • SECURITY_GUARANTEES.md: the statements that the API has no authentication (true until now, per docs: correct the unbacked claims left in SECURITY_GUARANTEES.md #113) are updated.

🤖 Generated with Claude Code

PUT /orgs/{org}/config (tenant credentials), the config status, the
org docs routes and /audit were reachable by anyone who could reach
the API port (#59).

New api/api_auth.py gates all eight routes:
- API_ADMIN_TOKEN: platform operator token, valid for every org
- API_ORG_TOKENS: org=token pairs, each valid only for its own org;
  a token for another org gets 403
- missing/unknown/non-Bearer token: 401 with WWW-Authenticate
- neither configured: 503 (fail closed); API_AUTH_DISABLED=true is an
  explicit local-development opt-out and logs a warning at startup
Tokens are compared in constant time. Webhooks keep their signature
checks and /health stays open.

CI's integration job now asserts /audit is 401 without a token and 200
with one. Docs and curl examples send the token; SECURITY_GUARANTEES
no longer says the API is unauthenticated.

The MCP configure_org_credentials gap is tracked separately in #116.

Fixes #59

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@OkiriGabriel
OkiriGabriel merged commit 966db71 into main Oct 24, 2026
12 checks passed
@OkiriGabriel
OkiriGabriel deleted the fix/api-auth-59 branch November 26, 2026 10:13
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.

[SECURITY] Authenticate credential, audit, and docs API endpoints

1 participant