Skip to content

fix: harden release PR #403's security and config-validation findings - #414

Merged
neuromechanist merged 11 commits into
developfrom
fix/harden-config-validation
Sep 19, 2026
Merged

neuromechanist merged 11 commits into
developfrom
fix/harden-config-validation

Conversation

@neuromechanist

Copy link
Copy Markdown
Member

Summary

Addresses the security/config-validation subset of the PR #403 review findings (backend group; citation-logic and knowledge/CI fixes are handled separately).

Important

  • Validate ANTHROPIC_WORKSPACE_ID/ANTHROPIC_BASE_URL at Settings construction instead of first request (Set up CI/CD with GitHub Actions #1)
  • Scope the BYOK auth bypass to endpoints that actually spend the credential: mirror management and session list/get/delete now use RequireAdminAuth, which has no BYOK bypass, instead of RequireAuth (Prepare for PyPI release v0.1.0 #2)
  • Warn on a default_model that passes format validation but won't resolve on either provider path (Create comprehensive README and documentation #3)
  • Add construction-time invariant enforcement to ByokCredential and ProviderChoice (Roadmap: Additional assistants and features #4)
  • Verify the BYOK key actually reaches the outbound Anthropic request in test_byok_http.py and test_anthropic_llm.py -- in the process, found and fixed a real gap: the installed anthropic SDK vendors its own isolated HTTP stack (httpx2/httpcore2), which respx cannot intercept, so these two HTTP-boundary tests were silently making real network calls to api.anthropic.com on every run. Rewrote them to mock langchain_anthropic's actual client-construction point instead (Phase 4: GitHub Actions CI/CD #8)
  • Update anthropic_llm.py's stale "Phase 2 is future work" docstring/comment (ci: Add GitHub Actions CI/CD workflows (closes #8) #10)

Suggestions

Nice-to-haves

Deferred to tracked issues (not fixed here)

Findings that were either unconfirmed, forward-looking, or pre-existing and out of this PR's diff got filed as follow-up issues instead of silently dropped: #404 (duplicate CI workflows), #405 (possible dropped citations on non-streaming path), #406 (shared cache-marker budget helper), #407 (pre-existing MagicMock tests in test_faq_summarizer.py), #408 (stale TODO in faq_summarizer.py).

Test plan

  • uv run ruff check . clean
  • Full suite (uv run pytest tests/ -m "not llm and not network and not integration and not slow"): 2261 passed, 8 skipped, 0 failed
  • One pre-existing, unrelated failure (test_version_shows_version, a terminal-color-rendering artifact in this shell) confirmed to also fail identically on unmodified develop -- not caused by this branch

…ential

Mirror management (mirrors.py) and session list/get/delete endpoints used
the same RequireAuth dependency as /ask and /chat, whose BYOK bypass is
only safe because those endpoints actually spend the caller-supplied key
against a real LLM. A syntactically-plausible header value on the mirror
or session endpoints granted access with no real credential at all. Both
now use RequireAdminAuth, which has no BYOK bypass.

Also adds construction-time invariant enforcement to ByokCredential and
ProviderChoice, closing a gap where a future caller passing an empty key
would silently run on the platform's key with cost enforcement disabled.
test_byok_http.py's two HTTP-boundary tests used respx to mock
api.anthropic.com, but the installed anthropic SDK (1.6.0) vendors its own
isolated httpx fork (httpx2/httpcore2), which respx cannot intercept.
Confirmed by direct testing: the respx route silently never matched and
both tests were making a real network call to api.anthropic.com on every
run, safely (a garbage test key just got a real 401, converted to a 500
by the endpoint's own exception handler, which happened to satisfy the
old, weaker status_code != 403 assertion regardless of whether any
mocking occurred at all).

Rewrites the mock to patch langchain_anthropic's own client-construction
function with an httpx2.MockTransport instead, and asserts the caller's
key actually reaches the mocked request -- closing the gap on the bug
class that already shipped twice (#393/#394): a silent fallback to the
platform's key would have passed every existing assertion.

Also strengthens the equivalent unit-level assertion in
test_anthropic_llm.py's BYOK construction test.
- Reuse anthropic_key_header/openrouter_key_header's header name in
  /ask and /chat instead of duplicating the literal string.
- Add a two-call regression test for the cache-breakpoint mutation fix,
  more directly mirroring the original bug report.
- Add a regression test proving an empty-string BYOK header does not
  bypass server auth.
- Document the accepted 'mock one layer above the HTTP boundary' pattern
  in .rules/testing_guidelines.md.
test.yml and tests.yml both installed dependencies via `pip install`/
`uv pip install --system` with no lockfile, so every CI run re-resolved
versions fresh from pyproject.toml's floor constraints instead of using
uv.lock - unlike deploy/Dockerfile, which correctly uses `uv sync --frozen`.

This silently drifted CI onto fastapi 0.141.1 / starlette 1.6.0 (a major
Starlette version bump) against the locked fastapi 0.128.0 / starlette
0.50.0 that local dev and production actually run. That newer Starlette
breaks route/dependant introspection in a way that made this PR's new
TestEndpointsThatDoNotSpendByokRequireAdminAuth tests see zero registered
routes for /mirrors and /sessions in CI, while passing locally and in a
container with locked versions - reproduced and confirmed both ways.

Switched every install step to `uv sync --frozen --extra dev` (`dev`
already depends on `[server]`, so this pulls in FastAPI etc. too) and the
corresponding run steps to `uv run pytest`/`uv run ruff`, so CI now tests
against the exact versions the lockfile pins and production deploys,
instead of whatever happens to be newest on a given day.
Same vulnerability class as mirrors.py and the session endpoints, missed
in the first pass: this endpoint never spends a BYOK credential against
an LLM (it returns a full per-community diagnostic dump -- API key
status, CORS origin count, document counts, warnings), so RequireAuth's
BYOK bypass let any syntactically-plausible header value read it with no
real credential at all. Switched to RequireAdminAuth.

Also adds a generalized structural test (in addition to a dedicated
health.py case): enumerates every route depending on verify_api_key and
asserts it's exactly /ask and /chat, so a future endpoint added with
RequireAuth by copy-paste fails immediately regardless of its path,
instead of needing a new dedicated test case added after the fact.
@neuromechanist
neuromechanist merged commit 1262655 into develop Sep 19, 2026
8 checks passed
@neuromechanist
neuromechanist deleted the fix/harden-config-validation branch September 19, 2026 20:39
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.

1 participant