Skip to content

Extract portable PyPI metadata transformations - #732

Merged
andrew merged 3 commits into
git-pkgs:mainfrom
simonchrz:feat/pypi-metadata
Nov 16, 2026
Merged

andrew merged 3 commits into
git-pkgs:mainfrom
simonchrz:feat/pypi-metadata

Conversation

@simonchrz

@simonchrz simonchrz commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Part of #501, the PyPI step you asked for there. Builds on #722, which changes the same PyPI filters; until that merges this PR shows its commits too, and the PyPI change is the last commit.

As in #612 for npm, the PyPI metadata transformations move out of internal/handler into internal/metadata/pypi:

  • RewriteSimpleHTML, RewriteDownloadHrefs and FilterSimpleHTMLLinks for the simple API's HTML, and RewriteSimpleJSON for PEP 691 JSON
  • RewriteJSON for the JSON API, package and version documents, with the denylist and cooldown it applies itself
  • ReleaseTimes, which reads each release's newest upload time
  • FilenameIdentity and the wheel, sdist, egg and Windows installer parsers, with MetadataSuffix

The package takes its inputs explicitly in Options: proxy and download URLs, the cooldown policy, the time cooldown is evaluated at, and a denylist check. It reports what the JSON API rewrite leaves out through OnFiltered and OnWithheld, which the handler logs and records as before. It doesn't depend on handler.Proxy.

What stays in the handler: fetching, the rewrite cache, choosing which versions the simple index filters (the JSON API lookup and versionInCooldown), downloads, and the streaming href rewrite for the root index (replacingReader). The handler's method names stay as thin wrappers, so the handler tests run unchanged against the real routes.

Other changes:

  • handler.PyPIFilenameIdentity is gone; internal/mirror calls pypi.FilenameIdentity. handler.PyPIMetadataSuffix stays, set from pypi.MetadataSuffix, for internal/server.
  • The simple index rewrites get only the URLs, so a handler built without a proxy, as TestPyPIHandlerRewritesConfiguredDownloadUpstream does, still works.
  • Cooldown is checked with Evaluate at one EvaluatedAt per rewrite instead of IsAllowed per release, as in internal/metadata/npm. The handler passes time.Now(), so results are the same unless a rewrite runs across a cooldown boundary.

Portability: GOOS=wasip1 GOARCH=wasm go build ./internal/metadata/pypi works. imports_test.go fails on any non-standard import outside an allowlist (cooldown, purl, vers, packageurl-go, internal/packageurl, and golang.org/x/net/html for the link filter) and on net, net/http, log/slog or database/sql.

Benchmarks (internal/handler, #722's head vs this branch, test binaries run alternately, n=8, medians):

base branch
PyPISimpleHTMLRequest 1.025 ms 1.026 ms +0.1%
PyPISimpleHTMLFilteredRequest 1.095 ms 1.100 ms +0.4%
PyPISimpleJSONRequest 0.977 ms 0.963 ms −1.5%
PyPIJSONAPIRequest 1.025 ms 1.022 ms −0.2%
PyPIRewriteSimpleHTML 251 µs 250 µs −0.4%
PyPIRewriteSimpleHTMLFiltered 72.6 ms 72.7 ms +0.2%

Bytes and allocations per op are unchanged within a few allocations.

New tests in internal/metadata/pypi cover the JSON API rewrite with cooldown evaluated at EvaluatedAt by a release's newest file, the denylist, both callbacks, a disabled policy and no policy, the simple index in HTML and JSON, release times and filename identity. They were mutation-checked. go test ./... and golangci-lint run ./... pass.

Summary by CodeRabbit

  • Refactor
    • PyPI package listings and metadata now apply consistent filtering across supported formats, including hiding denied or temporarily withheld releases.
    • PyPI download links are consistently redirected through the proxy.
    • PyPI artifact names and versions are identified consistently from filenames, with existing fallback behavior preserved.

Move the simple API rewrites (HTML and JSON), the JSON API rewrite, the
release-time parser and the filename parsers out of internal/handler into
internal/metadata/pypi. The package takes its inputs explicitly (proxy and
download URLs, cooldown policy, evaluation time, a denylist check) and
reports what the JSON API rewrite holds back through callbacks the handler
logs and records. It imports no handler, database, HTTP or logging code, and
builds for wasip1.

Cooldown is checked with Evaluate at one EvaluatedAt per rewrite, as in
internal/metadata/npm.
@coderabbitai

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

PyPI metadata rewriting, release-time parsing, and filename identity parsing now reside in internal/metadata/pypi. The handler delegates PyPI rewrites to that package, and the mirror uses its filename parser for PyPI artifact identity.

Changes: PyPI metadata handling

Layer / File(s) Summary
Filename identity parsing and use
internal/metadata/pypi/filename.go, internal/metadata/pypi/metadata_test.go, internal/handler/pypi.go, internal/mirror/files.go
The package adds parsing for supported PyPI filename types. The handler and mirror use the shared parser; the mirror retains its fallback when the parser returns an empty name.
Metadata rewrite and filtering
internal/metadata/pypi/metadata.go, internal/metadata/pypi/metadata_test.go, internal/metadata/pypi/imports_test.go
The package adds HTML and JSON rewriting, release-time parsing, URL rewriting, and denylist and cooldown filtering. Tests cover these behaviors and check the package’s dependencies.
Handler delegation
internal/handler/pypi.go, internal/handler/pypi_denylist.go
The handler delegates metadata rewriting and release-time parsing to the shared package. The former local rewrite helpers and simple-HTML denylist file are removed.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Refactor

Possibly related issues

  • Issue 501 proposes moving PyPI rewrite and filename parsing functions into packages with explicit inputs. This PR moves those functions into internal/metadata/pypi and supplies rewrite options.
  • Issue 127 requests a generalized filter interface and reporting of filtered versions. This PR adds filtered and withheld callbacks for PyPI JSON rewriting, but does not add the generalized cross-ecosystem interface.

Possibly related PRs

  • git-pkgs/proxy#681: Both changes affect PyPI JSON filtering, including handling of filtered versions.
  • git-pkgs/proxy#693: Both changes modify PyPI release-time parsing; this PR moves the parsing and adds tests for it.

Suggested reviewers: andrew, wickedone, abhinavgautam01

Merge Risk: Minimal · up to 9671f

This refactor moves PyPI metadata transformations into a separate internal package without intended behavior changes. No concrete defects were identified, and the reported tests and benchmarks suggest no regression, so it is ready to merge.

Security architecture risk: Minimal · up to 25c77

The change centralizes PyPI metadata processing without adding an external interface or weakening the inspected filtering, cache identity or failure-handling behavior. No introduced or worsened security risk was established.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The affected security scope is PyPI metadata filtering, rewritten download destinations and filename-derived artifact identity within the existing proxy and mirror. The inspected extraction does not give registry-controlled metadata additional authority over policy configuration or database operations.

Trust Boundaries and Controls

  • observed — The handler continues to supply the denylist decision and recording cooldown policy; upstream document fields do not select those controls. The version-specific JSON route retains its denylist check. On rewrite failure, the handler retains its existing error response when metadata filtering is required, rather than serving the original document.

Resilience and Maintainability Implications

  • observed — The new transformation contract evaluates cooldown at one caller-supplied time. The handler still records held decisions for cache expiry under synchronization, separately from recording withheld releases in the database. Concurrent observation can shorten another rewrite's cache lifetime, and the existing expiry cap remains in place.
Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "Extract portable PyPI metadata transformations" matches the main change. The PR moves PyPI metadata rewriting, release-time parsing, and filename identity logic out of internal/handler into…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "Extract portable PyPI metadata transformations" matches the main change. The PR moves PyPI metadata rewriting, release-time parsing, and filename identity logic out of internal/handler into the new internal/metadata/pypi package, and the objectives describe the same extraction. The title is specific and concise, and it does not include file lists or vague wording.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c73f8751-3501-45c3-ae5c-63ff8772057a
Commits

Reviewing files that changed from the base of the PR and between 25c7737 and 9671fd5.

Files selected for processing: 2

  • internal/handler/pypi.go
  • internal/metadata/pypi/filename.go
Files with no reviewable changes: 1

  • internal/handler/pypi.go
Files skipped from review as they are similar to previous changes: 1

  • internal/metadata/pypi/filename.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 5 remain after this review.

@simonchrz
simonchrz marked this pull request as ready for review November 16, 2026 11:50
@andrew
andrew merged commit 7b41291 into git-pkgs:main Nov 16, 2026
8 checks passed
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.

2 participants