feat: opt-in API-key auth for saas_web via CODEC_CARVER_API_KEYS#193
Open
seonghobae wants to merge 1 commit into
Open
feat: opt-in API-key auth for saas_web via CODEC_CARVER_API_KEYS#193seonghobae wants to merge 1 commit into
seonghobae wants to merge 1 commit into
Conversation
Add an optional authentication middleware to the SaaS web app so it can be deployed beyond localhost with access control: - If env var CODEC_CARVER_API_KEYS is set (comma-separated keys), all requests except GET / (the upload UI page) require an X-API-Key header matching one of the configured keys. - If the variable is unset (or contains only empty entries), behavior is exactly as before: fully open. - Keys are read from the environment at request time, so rotation needs no restart and tests can patch the env easily. - Comparison uses hmac.compare_digest (constant-time); keys are never logged or echoed in responses. Failures return 401 JSON. - Whitespace around keys is stripped; empty entries are ignored. Gates: saas_web.py coverage 100%, interrogate 100%, no new test failures (baseline 5 macOS os.listxattr errors unchanged). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CJRVbDrp1vGYkJgNHMGPpG
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
Adds opt-in API-key authentication to the SaaS web app (
saas_web.py) — the minimal step toward controlled deployment beyond localhost.CODEC_CARVER_API_KEYSis set (comma-separated keys), all requests exceptGET /(the upload UI page) require anX-API-Keyheader matching one of the configured keys; failures get401 {"error": "Invalid or missing API key"}.os.environat request time, so key rotation needs no restart.@app.middleware("http")pattern and is registered between the size-limit and security-headers middlewares, so 401 responses still carry security headers and unauthenticated requests are rejected before body streaming.Security
hmac.compare_digestagainst each configured key.Tests
New
TestApiKeyAuthclass (10 tests) usingTestClient+patch.dict(os.environ):/shrinkopen as beforeGET /always 200 without a keyget_configured_api_keys()parsingGates
coverage report --include=saas_web.py→ 100% (114 stmts, 0 miss)interrogate -c pyproject.toml saas_web.py→ 100%unittest discover -s tests→ 123 tests, no new failures (only the baseline 5 macOSos.listxattrerrors)🤖 Generated with Claude Code