Skip to content

Match the patch mirror by its base in the Pipenv, script and Hatch rewriters and the vendored Pipenv backend - #2255

Merged
Mikola Lysenko (mikolalysenko) merged 2 commits into
mainfrom
mirror-pipenv
Nov 25, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 2 commits into
mainfrom
mirror-pipenv

Conversation

@mikolalysenko

@mikolalysenko Mikola Lysenko (mikolalysenko) commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

What this fixes

The experimental patch mirror (#2180, --patch-mirror-url / SOCKET_PATCH_MIRROR_URL) left two items under "Not covered". Both are about matching a mirror by its full base URL (origin plus path), never by its host alone, which is how every other hosted reader already matches it.

1. The Pipenv, inline-script and Hatch rewriters matched an earlier mirror pin by host

These rewriters take over an earlier hosted pin in place when it is in the patch server's path shape for the same release, on Socket's patch server or on the grant's own origin (replaceable_hosted_pin). With a mirror, the grant's origin is an artifact host that also serves the project's own packages. So a URL in that shape in another repository of the host was rotated as if it were ours.

Now a pin on the grant's origin must also sit under the base the grant is served from (origin::same_hosted_base). That is the rule the yarn berry, Bun, Poetry and PDM rewriters already apply. URLs are compared parsed, so <base>/../other/... and %2e%2e do not count as under the base.

A pin on Socket's patch server is still ours and still moves onto the mirror in place, so Pipenv's behavior for a project pinned directly on the patch server does not change.

The rule is shared, so the fix covers:

  • Pipfile.lock (redirect::pipenv)
  • pyproject.toml [tool.uv.sources] and PEP 723 inline scripts (python_script::same_hosted_artifact)
  • Hatch direct references (utils::hatch)

2. The vendored Pipenv backend built its hosted origins from --patch-server-url only

vendor_pypi_with_pipenv_version built its patch-server origins from --patch-server-url alone. The hosted-to-vendored takeover and the scan / get --mode vendored preview already used the run's full list, with the mirror's base included.

VendorServiceConfig now has a patch_server_origins field. It is set from hosted_unwind::patch_server_origins, which lists the mirror as mirror+<base>. The backend reads it, so:

  • its guard calls a live pin under the mirror's base a hosted pin and gives the rollback remedy;
  • it still calls a pin elsewhere on the mirror's host a user-declared file reference;
  • the preview and the real run now agree.

Not reproducible end to end on main. The "refused" in #2180's note did not reproduce through the CLI. On main, the per-package takeover already restores a mirrored Pipfile.lock entry before the backend runs. vendor, scan --mode vendored and get --mode vendored with only the mirror set all vendor such a project. The backend's origin list only decided which refusal message a live pin got. This PR fixes that list and adds a CLI test that holds the takeover in place.

CLI_CONTRACT.md's patch-mirror section now names the Pipenv, pyproject / inline-script and Hatch rewriters next to yarn berry, Bun, Poetry and PDM.

Behavior change outside the mirror

A path-prefixed --patch-server-url deployment (https://host/prefix/patch/pypi/...) now has these three rewriters take over an earlier pin only under that same prefix. Before, any path on that host counted. That is how the yarn berry, Bun, Poetry and PDM rewriters already treat such a deployment. A pin on Socket's patch server, or on a --patch-server-url served from the root of its host, is matched as before.

Tests

Unit tests (core):

  • replaceable_hosted_pin_under_a_mirror_needs_the_mirror_base
  • owned_url_under_a_mirror_needs_the_mirror_base (Pipenv, through plan)
  • an_earlier_source_under_a_mirror_needs_the_mirror_base (pyproject and inline script)
  • an_earlier_reference_under_a_mirror_needs_the_mirror_base (Hatch)
  • pipenv_guard_reads_a_mirrored_pin_as_hosted_only_under_the_mirror_base (vendored backend)

Each one is checked both ways:

  • Ours: an earlier pin under the mirror's base, and one on patch.socket.dev.
  • Not ours: a same-shaped URL in another repository of the mirror's host, and <base>/../other/....

All five fail with the source change reverted.

CLI tests in tests/mode_migration_pypi.rs:

  • pipenv_hosted_through_a_mirror_leaves_a_fork_on_its_host_alone: a hosted run through a mirror refuses a same-shaped Pipfile.lock pin in another repository (redirect_pipenv_refused) and leaves the lock byte-identical. It fails on main.
  • pipenv_hosted_through_a_mirror_to_vendored: hosted through the mirror, then vendor --patch-mirror-url only, ends wired to the vendored wheel.
  • pipenv_vendor_leaves_a_pin_elsewhere_on_the_mirrors_host_alone: vendor refuses a fork in another repository as user-declared and leaves the lock as it was.

Local runs on the head commit:

  • cargo clippy --locked --workspace --all-features -- -D warnings: clean.
  • cargo check --workspace --all-targets --all-features: clean, apart from two unused-import warnings already on main in test files this PR does not touch.
  • Core unit tests: 8,582 passed.
  • CLI unit tests: 1,070 passed.
  • CLI suites: mode_migration_pypi (87), patch_mirror (5), in_process_redirect_pipenv (11), in_process_vendor (157), in_process_vendor_pypi_takeover (9), command_module_layering (4) and cli_global_args (13), all passed.

Code review

/code-review high returned five findings. Three are fixed in the second commit:

  • The contract left out the lock takeover. The uv.lock / pylock.toml direct-URL takeover asks the same shared question, so the contract now names it. It also states the second rule the pyproject / script rewriters apply: the same URL with only the grant token rotated. The script test also checks same_hosted_artifact directly, which is the check the lock takeover calls.
  • A stale comment. owned_url's doc comment still said any pin on the grant's host counts.

Two are not fixed:

  • An operator's --patch-server-url pin is not moved onto a mirror (marked pre-existing). The scenario: a Pipenv or script pin on the operator's own patch server, then a run that adds --patch-mirror-url. That pin is refused, while a pin on patch.socket.dev is moved. This was already the behavior before this PR: the old check only accepted Socket's host or the grant's origin, and the grant is on the mirror. Fixing it means passing the run's configured origins into the rewriters, which take only the grant's URL today. That is a separate change; rollback and a re-run is the remedy.
  • same_hosted_base compares percent-encoded paths as text, so socket%2Dpatch and socket-patch count as different bases. The check is shared with the yarn berry, Bun, Poetry and PDM rewriters, and the run writes every mirrored pin with the operator's base byte for byte. A base given in two spellings is not a case worth changing that shared check for here.

No CHANGELOG.md entry, per the repository rule.

🤖 Generated with Claude Code

…writers and in the vendored Pipenv backend

Two gaps left by the experimental patch mirror (#2180):

- The Pipenv, pyproject / inline-script (PEP 723) and Hatch rewriters
  take over an earlier hosted pin in place when it is in the patch
  server's path shape for the same release, on Socket's patch server or
  on the grant's own origin. Under a mirror the grant's origin is an
  artifact host that serves the project's own packages too, so a URL in
  that shape in another repository of the host was rotated as if it were
  ours. A pin on the grant's origin now also has to sit under the base the
  grant is served from (origin::same_hosted_base, the rule the yarn berry,
  Bun, Poetry and PDM rewriters already apply). A pin on Socket's patch
  server is still ours and still moves onto the mirror in place.

- The vendored Pipenv backend built its patch-server origins from
  --patch-server-url only, while the hosted-to-vendored takeover and the
  scan / get preview read the run's full list, the mirror's base
  included. VendorServiceConfig now carries that list
  (patch_server_origins, built by hosted_unwind::patch_server_origins),
  so the backend's guard names a live pin under the mirror's base as a
  hosted pin with the rollback remedy, and still calls a pin elsewhere on
  the mirror's host user-declared. The takeover itself already restored a
  mirrored Pipenv pin before the backend ran; a test now holds that.

Tests: unit tests for replaceable_hosted_pin, the Pipenv rewriter, the
pyproject / inline-script rewriter, the Hatch rewriter and the vendored
Pipenv guard, and three CLI tests in mode_migration_pypi (hosted fork
left alone, mirrored Pipenv takeover, vendor leaves a fork alone). Each
new unit test and the hosted fork test fail with the source change
reverted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e comment

- CLI_CONTRACT names the uv.lock / pylock.toml direct-URL takeover, which
  asks the same shared question, and the pyproject / script rewriters'
  second rule (the same URL with only the grant token rotated).
- The Pipenv rewriter's owned_url comment says a pin on the grant's host
  is ours only under the base the grant is served from.
- The script test also checks same_hosted_artifact directly, the check the
  uv.lock / pylock takeover calls.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review November 25, 2026 19:38
@cursor

cursor Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes hosted-pin takeover logic across multiple Python lockfile rewriters and vendored Pipenv guards; wrong base matching could refuse valid pins or accept foreign URLs, but scope is mirror/path-prefixed deployments with broad test coverage.

Overview
Patch mirror setups now treat an earlier hosted pin as socket-patch’s only when it sits under the mirror’s full base URL (or on patch.socket.dev), not merely on the same artifact host. replaceable_hosted_pin and the Pipenv, PEP 723 / pyproject.toml, Hatch, and uv/pylock direct-URL paths share that rule via same_hosted_base, so same-shaped URLs in another Artifactory repo on the host stay user sources and are refused instead of being rotated.

VendorServiceConfig gains patch_server_origins, filled from the CLI’s full hosted-origin list (including the mirror base). The Pipenv vendored backend uses it so live pins under the mirror get the hosted/rollback remedy and agree with scan/get previews—previously it only honored --patch-server-url.

CLI_CONTRACT.md documents the expanded rewriter coverage; CLI and core tests cover mirror takeover, forks on the same host, and hosted→vendored through a mirror. Path-prefixed --patch-server-url deployments now require the same prefix for takeover, aligning with yarn/Bun/Poetry/PDM behavior.

Reviewed by Cursor Bugbot for commit c4a4592. Configure here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[agent] Reviewed the base-path matching rule and the origin list given to the vendored Pipenv backend. A sibling repository, a socket-patch-2 sibling and .. escapes are all refused, and a root-served patch server behaves as before. No findings.

Merged via the queue into main with commit b34d116 Nov 25, 2026
170 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the mirror-pipenv branch November 25, 2026 22:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants