Repository navigation
fix: harden release PR #403's security and config-validation findings - #414
Merged
Merged
Conversation
…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.
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
ANTHROPIC_WORKSPACE_ID/ANTHROPIC_BASE_URLatSettingsconstruction instead of first request (Set up CI/CD with GitHub Actions #1)RequireAdminAuth, which has no BYOK bypass, instead ofRequireAuth(Prepare for PyPI release v0.1.0 #2)default_modelthat passes format validation but won't resolve on either provider path (Create comprehensive README and documentation #3)ByokCredentialandProviderChoice(Roadmap: Additional assistants and features #4)test_byok_http.pyandtest_anthropic_llm.py-- in the process, found and fixed a real gap: the installedanthropicSDK vendors its own isolated HTTP stack (httpx2/httpcore2), whichrespxcannot intercept, so these two HTTP-boundary tests were silently making real network calls toapi.anthropic.comon every run. Rewrote them to mocklangchain_anthropic's actual client-construction point instead (Phase 4: GitHub Actions CI/CD #8)anthropic_llm.py's stale "Phase 2 is future work" docstring/comment (ci: Add GitHub Actions CI/CD workflows (closes #8) #10)Suggestions
create_openrouter_llmwhen no API key is available, instead of constructing silently (feat: GitHub Actions CI/CD and deployment infrastructure #11)key_sourceasLiteralat_check_model_costandAssistantWithMetrics(feat: add Cloudflare Worker security proxy #15).rules/testing_guidelines.md(feat: improved chat widget with markdown, health status, and CI/CD #18)Nice-to-haves
default_model_providerwill be ignored (Release: Page context awareness feature #20)cache_ttlon reassignment viaConfigDict(validate_assignment=True)(feat: user-controlled page context toggle #21)X-Anthropic-API-Key/X-OpenRouter-Keyliterals (ci: add auto-release workflow #22)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 .cleanuv run pytest tests/ -m "not llm and not network and not integration and not slow"): 2261 passed, 8 skipped, 0 failedtest_version_shows_version, a terminal-color-rendering artifact in this shell) confirmed to also fail identically on unmodifieddevelop-- not caused by this branch