fix(config): refuse a scheme-less base_url so the origin guard stays real (#123) - #131
Merged
Merged
Conversation
…real (#123) `resolve_api_url` pins every request to the configured origin by comparing `_origin(url) != _origin(base_url)`, where `_origin` is (scheme, host, port) from `urlsplit`. Given a base_url with no scheme -- "api.oilpriceapi.com" -- urlsplit finds no authority, so the base origin is ("", "", 0); the URL resolved against it is relative, so its origin is ("", "", 0) too. The comparison then has nothing on either side and passes everything. That silently removes one of the two layers protecting the caller's API key -- the protection v1.14.0 exists to provide. The remaining layer, the literal "//" ban, still holds, so this is a defence-in-depth regression rather than a live leak; end to end httpx refuses the relative URL with UnsupportedProtocol. The error the caller saw named neither the setting nor the guard. `validated_base_url` is already shared by both clients (#120), so requiring an absolute http/https base there fixes sync and async identically and by construction. Also converts the raw ValueError from an out-of-range port into the documented ConfigurationError. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015ao5paex73xXvuM424Libo
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars. ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
#123) The new validation itself called urlsplit and .hostname/.port, both of which raise ValueError on an out-of-range port and on a non-ASCII netloc whose NFKC normalisation introduces one of /?#@: -- "https://℀evil.example". The constructor documents ConfigurationError, so wrap the whole parse. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015ao5paex73xXvuM424Libo
…xpression `parts.port` was evaluated purely for its ValueError side effect. Ruff B018 flags a bare attribute expression; bind it instead. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015ao5paex73xXvuM424Libo
This was referenced Sep 13, 2026
Merged
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.
Splits the one serious finding out of #123 and fixes it on its own.
The bug
_url.resolve_api_urlpins every request to the configured API origin:_originis(scheme, host, port)derived fromurlsplit. Give the client a scheme-lessbase_urlandurlsplitfinds no authority at all, so the base origin is("", "", 0). The URL resolved against it is relative, so its origin is("", "", 0)too. The guard compares nothing against nothing and passes everything.Measured on
mainat7982b0b, CPython 3.14.7:The first two return a relative URL from a function whose documented contract is "guaranteed to share
base_url's origin".How serious, stated honestly
The issue files this P3 and asks whether it is really the leak v1.14.0 shipped to close. It is not — and the PR should say so rather than overclaim.
I probed it end to end. The origin comparison is genuinely a tautology, but it is the second of two layers. The first is the literal
//ban at_url.py:100, which fires before the origin check and is untouched by this bug:No probe changed the origin. And the end state is a loud failure, not a silent send:
So: no credential reaches a wrong host today, and no request is silently misrouted. What is real is that one of the two layers is silently gone under a misconfiguration, the guard's documented guarantee is false for those inputs, and the error the caller actually sees names neither the setting they got wrong nor the guard. Defence-in-depth regression plus a bad diagnostic — worth fixing now and cheap to fix, but not the live leak.
The fix
validated_base_url(retry.py:117) is already shared by both clients since #120 —client.py:130andasync_client.py:109both call it. Adding the requirement there fixes sync and async identically and by construction, not by two parallel edits that can drift.It now requires an absolute
http/httpsbase with a non-empty host, and converts the rawValueErrorfrom an out-of-range port into the documentedConfigurationError. 38 added lines, one file.Red
Test written first, against unmodified
main:Re-proved red-capable after the fix by restoring the pre-fix source (
git checkout origin/main -- oilpriceapi/retry.py):Green
A follow-up commit on this branch also wraps the raw
ValueErrorthaturlsplit/.hostname/.portraise on an out-of-range port and on a non-ASCII netloc whose NFKC normalisation introduces/?#@:(https://\u2100evil.example). That leak was in this PR's own new validation, so it is fixed here rather than deferred. Red for those 6 added cases against pre-fix source:27 failed, 6 passed.Sync and async parity
Both clients route through the one shared
validated_base_url, so they cannot diverge. Pinned two ways:test_async_client_refuses_non_absolute_base_urlmirrorstest_sync_client_refuses_non_absolute_base_urlover the same 7 inputs.test_sync_and_async_share_one_base_url_validatorasserts structurally that both__init__bodies call the same validator, so a future edit to one cannot quietly drop it.Full suite
origin/main)+33 is exactly this PR's new tests. The 3 failures are
tests/integration/test_demo_contract.pymaking live calls and getting HTTP 429 — environmental, identical before and after, unrelated to this change.git diff --statvsorigin/main:oilpriceapi/retry.py | 54 +++++, plus the new test file. No CRLF normalisation ofresources/alerts.pyorresources/diesel.py.Scope
The remaining findings in #123 — the raw
ValueErrorstill escaping from_url._origin/resolve_api_urlitself, jitter exceeding the documented 60s cap, andValidationErrorhard-codingstatus_code=422— are a separate PR. Version deliberately left at 1.14.0; release decision is separate.Closes part 2 of #123.
🤖 Generated with Claude Code
https://claude.ai/code/session_015ao5paex73xXvuM424Libo