Repository navigation
fix: small pre-release findings of 0.20.0 - #652
Conversation
- GuardedClient reports a header that can't be encoded as Latin-1 as invalid_header rather than invalid_url, and so does the fake client. - Warn at the first token validation about issuers reached without TLS, except on the loopback interface. - Test sent and not sent over real TLS, with a local certificate. - Add an origin note to the Casbin adapter. Closes #647 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqMUX3EU35poH13xMgG
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqMUX3EU35poH13xMgG
Header names are encoded as ASCII, so matching Latin-1 missed them, and credentials reach the Authorization header. Do the same in the fake client, also for auth, and accept a reset in the handshake test. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqMUX3EU35poH13xMgG
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqMUX3EU35poH13xMgG
Reviewer's GuideThe PR tightens HTTP error classification for unencodable request data, adds diagnostics for insecure non-loopback token issuers, and validates request delivery semantics with real TLS integration tests, alongside related documentation and provenance updates. Sequence diagram for HTTP request validation and error classificationsequenceDiagram
participant Client
participant GuardedClient
participant Requests
participant HTTP
Client->>GuardedClient: request(method, url, params, headers, auth)
GuardedClient->>Requests: prepare request
Requests-->>GuardedClient: prepared request or encoding error
alt unencodable header or credential
GuardedClient-->>Client: InvalidRequest(reason=invalid_header)
else valid request
GuardedClient->>HTTP: send prepared request
HTTP-->>GuardedClient: response or transport error
GuardedClient-->>Client: response or classified HttpError
end
Sequence diagram for insecure token issuer warningsequenceDiagram
participant AuthConfig
participant AuthWarnings
participant Logger
AuthConfig->>AuthWarnings: log_auth_config_warnings(conf)
loop each issuer in conf.issuer_urls
AuthWarnings->>AuthWarnings: _reached_without_tls(issuer)
alt HTTP issuer outside loopback
AuthWarnings->>Logger: warning(issuer reached without TLS)
end
end
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #652 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 56 56
Lines 7142 7164 +22
=========================================
+ Hits 7142 7164 +22
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqMUX3EU35poH13xMgG
There was a problem hiding this comment.
Hey - I've found 5 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="foca/security/auth.py" line_range="314" />
<code_context>
+ logger.warning(
+ "Trusted issuer %s is reached without TLS: anyone on the network "
+ "can change its signing keys, and so forge what it signs.",
+ escape_log_value(issuer),
+ )
if not conf.verify_audience and conf.clients is None:
</code_context>
<issue_to_address>
**Issuer secrets reach logs**
When a configured HTTP issuer URL contains credentials or secret query parameters, `escape_log_value(issuer)` preserves printable URL secrets, so `log_auth_config_warnings` writes credentials or tokens to warning logs and exposes them to log readers.
Redact URL userinfo and sensitive query parameters before logging the issuer.
</issue_to_address>
### Comment 2
<location path="foca/plugins/testing.py" line_range="553-556" />
<code_context>
prepared = requests.Request(
method.upper(), url, params=params, headers=dict(headers or {})
).prepare()
+ for name, value in prepared.headers.items():
+ name.encode("ascii")
+ if isinstance(value, str):
+ value.encode("latin-1")
return str(prepared.url)
</code_context>
<issue_to_address>
**Fake accepts invalid URL credentials**
When URL-embedded credentials cannot be encoded for Basic Authorization, `_prepared_url` does not validate the Authorization header generated from URL userinfo, so `FakeHttp.request` records the request and can return a canned response while `GuardedClient` raises `invalid_header`.
Validate URL-embedded credentials using the same encoding checks as the prepared Authorization header.
Also at `foca/plugins/testing.py:484-486`.
</issue_to_address>
### Comment 3
<location path="foca/plugins/testing.py" line_range="485-486" />
<code_context>
url = _prepared_url(method, url, params, headers)
- except requests.exceptions.InvalidHeader as exc:
+ if auth is not None:
+ prepared = requests.Request(method, url, auth=auth).prepare()
+ _check_response_hooks(prepared.hooks)
+ except (requests.exceptions.InvalidHeader, UnicodeEncodeError) as exc:
raise InvalidRequest(
</code_context>
<issue_to_address>
**Fake accepts unencodable auth headers**
When a custom auth handler adds a header name or value that cannot be encoded for sending, `FakeHttp` checks only response hooks on the prepared auth request, so it records the request and can return a canned response instead of raising `InvalidRequest` as `GuardedClient` does.
Check the prepared auth request's header names and values for send-time encoding errors.
</issue_to_address>
### Comment 4
<location path="tests/utils/test_http_tls.py" line_range="242" />
<code_context>
+ stop.wait(10)
+
+ with pytest.raises(RequestMaybeSent):
+ client(timeout=0.5).post(serve(behaviour), json={})
</code_context>
<issue_to_address>
**TLS test times out before sending**
When local scheduling or TLS setup takes more than 0.5 seconds before the request is sent, `test_hang_after_sending` expects `RequestMaybeSent` at the `client(timeout=0.5)` call. If local TCP/TLS setup takes longer than 0.5 seconds, the correct client raises `DeadlineExceeded` before sending, so the test fails.
Use a longer timeout or synchronize the timeout phase so the test starts its wait only after the server has received the request.
</issue_to_address>
### Comment 5
<location path="foca/security/auth.py" line_range="312-314" />
<code_context>
+ for issuer in conf.issuer_urls:
+ if _reached_without_tls(issuer):
+ logger.warning(
+ "Trusted issuer %s is reached without TLS: anyone on the network "
+ "can change its signing keys, and so forge what it signs.",
+ escape_log_value(issuer),
+ )
if not conf.verify_audience and conf.clients is None:
</code_context>
<issue_to_address>
**Issuer warning appears twice**
When concurrent requests perform their first token validation using the same config, `validate_token` can have multiple calls observe `_warnings_logged` as false before either sets it, so each calls `log_auth_config_warnings` and emits the issuer warning.
Synchronize the `_warnings_logged` check, warning call, and flag update.
</issue_to_address>Linked issues
#647 fix: small pre-release findings of 0.20.0 · 4 of 4 met
| # | Requirement | Status | Notes |
|---|---|---|---|
| 1 | Report unencodable headers as InvalidRequest with reason invalid_header in GuardedClient and the fake client. |
✅ Met | GuardedClient maps InvalidHeader and UnicodeEncodeError to invalid_header; the fake client checks header-name/value encodings and maps encoding errors, including auth credentials, the same way. |
| 2 | Warn at the first token validation about http:// issuers, except for loopback hosts. |
✅ Met | The warning is logged from log_auth_config_warnings, called once at the start of validate_token; the check excludes localhost and loopback IP addresses. |
| 3 | Add a local TLS fixture covering sent-versus-not-sent behavior during the handshake and after sending. | ✅ Met | The local TLS tests cover handshake close, timeout and certificate failure as not sent, and connection close and timeout after reading the request as maybe sent. |
| 4 | Add an origin note to the Casbin adapter. | ✅ Met | The adapter docstring attributes its origin to casbin-pymongo-adapter and names its Apache License 2.0. |
Sourcery assessment
Approval pending. 5 findings to address first.
Blocking findings: foca/security/auth.py:314, foca/plugins/testing.py:556, foca/plugins/testing.py:486, tests/utils/test_http_tls.py:242, foca/security/auth.py:314
Show the issuer without credentials and query, and check the encoding of headers added by auth handlers in the fake client. Give the test of a server that hangs after the request more time. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqMUX3EU35poH13xMgG
The adapter derives from casbin-pymongo-adapter (Apache-2.0). Its module docstring already says so (#652); add a NOTICE file with the entry, and ship it in the distributions and the image build. Closes #655 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Summary
Small findings of the pre-release review of 0.20.0:
GuardedClientand the fake client report names, values and credentials that can't be encoded asinvalid_headerrather thaninvalid_url.tests/utils/test_http_tls.py).NOTICEfile is needed too is the maintainer's call.Closes #647
How it was verified
coverage run -m pytestwith MongoDB 7.0.34, Python 3.13: 3346 passed, 1 skipped, 100% coverage.ruff,mypy,ast-grep,semgrep,mkdocs build --strict: passed.Declined review findings
httpvalues ofjwks_urianduserinfo_endpoint(adversarial, low): needs a change in the token path, for a release in which it is otherwise unchanged; for a later release.foo.localhostor127.1count as remote and warn (adversarial, low): log noise in local setups only.paramsordatais reported asinvalid_header(adversarial, low): unusual input, and it is not sent either way.requests.adapters.DEFAULT_CA_BUNDLE_PATH(quality, medium): the session doesn't trust the environment, so the named variables can't affect it, and a change inrequestsmakes the tests fail, not pass.🤖 Generated with Claude Code
https://claude.ai/code/session_011FSoqMUX3EU35poH13xMgG
Summary by Sourcery
Improve pre-release HTTP error classification and authentication security warnings, with expanded TLS and validation coverage.
Bug Fixes:
Enhancements:
Tests: