fix(formats): --trusted-host ignores the port on import and drops it on export - #3845
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3845 +/- ##
==========================================
+ Coverage 88.36% 88.41% +0.04%
==========================================
Files 121 121
Lines 13249 13256 +7
Branches 2251 2252 +1
==========================================
+ Hits 11708 11720 +12
+ Misses 969 966 -3
+ Partials 572 570 -2
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:
|
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.
--trusted-hostwith a port is ignored on import, and the port is dropped onexport, so an index on a non-default port does not round-trip.
requirements.txtwith--trusted-host mirror.example.com:8443and an index URLcarrying credentials:
The import side is the one that bites: the source is silently recorded as
verify_ssl = true, so pdm then rejects a certificate the user explicitly saidto trust.
Cause
Two halves of the same round trip.
On import, the trusted host is compared by membership against two values:
hosthas no port, somirror.example.com:8443never equals it.netlochasthe port but also the userinfo, so for
user:pw@mirror.example.com:8443it doesnot match either. It only works when the URL has a port and no credentials.
On export the port is thrown away:
pip does not treat a portless
--trusted-hostas covering an explicit port inthe URL, so the exported file does not reproduce the input.
The fix
Compare host and port explicitly, and keep the port on the way out. I took the
semantics from pip's own
is_secure_originandadd_trusted_hostrather thanguessing:
origin_host.lower() != secure_host.lower()parsed.hostnamesecure_port is not Nonecheck and its wildcard-port mountIPv6 literals are split with
urlsplitso[::1]:8443breaks on the rightcolon, and are re-bracketed on export. A malformed port is treated as absent
rather than raising, so a bad value in a requirements file cannot abort the
import.
Verification
Reverting the matching logic to the membership test fails the import test:
Reverting the export to
.hostnamefails the export tests, including the IPv6one:
tests/test_formats.pyis 46 passed. On the wider suite the baseline is 1346passed on a stashed tree and 1359 passed with this change, no failures either
way; the difference is the new tests.
Two tests are excluded from that count because they fail identically on an
unmodified tree here for environment reasons:
test_create_venv_in_project[venv-True]and the OIDC tests intests/cli/test_publish.py.ruff checkandruff format --checkare clean.Pull Request Checklist
news/describing what is new.Disclosure: written with AI assistance (Claude Code). I produced the before and after by running
pdm importandpdm export, read pip'sis_secure_originandadd_trusted_hostfrom an installed pip to confirm the matching rules rather than relying on memory, and ran both mutation checks and the suite baseline myself.