From 916e2b0aa5cbe018f7cf096b4c1c776772496857 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Tue, 24 Nov 2026 00:22:49 +0000 Subject: [PATCH] fix(proxy): keep package names that start with the registry name The app mounts each handler behind http.StripPrefix. The npm and PyPI handlers trimmed the registry prefix a second time. A request for npm-run-all reached upstream as -run-all, and the npm package itself became an empty path. PyPI trimmed /simple without a segment check. --- pkg/app/proxy_integration_test.go | 33 +++++++++++++++++++++++++++++++ pkg/proxy/npm/npm.go | 2 +- pkg/proxy/pypi/pypi.go | 8 +++++--- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/pkg/app/proxy_integration_test.go b/pkg/app/proxy_integration_test.go index 5ef88f2..b64fded 100644 --- a/pkg/app/proxy_integration_test.go +++ b/pkg/app/proxy_integration_test.go @@ -12,6 +12,7 @@ import ( "net/url" "os" "os/exec" + "slices" "strconv" "strings" "testing" @@ -316,3 +317,35 @@ func TestNPMMetadataTTLOverrideReachesCache(t *testing.T) { } t.Fatalf("no npm metadata entry with an expiry among %d packages", len(pkgs)) } + +// The mount already strips the registry prefix, so a package named after the registry must keep its name. +func TestPackagesNamedAfterTheRegistryKeepTheirName(t *testing.T) { + var npmSeen, pypiSeen []string + npmUp := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + npmSeen = append(npmSeen, r.URL.Path) + _, _ = w.Write([]byte(`{"name":"x","versions":{}}`)) + })) + t.Cleanup(npmUp.Close) + pypiUp := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + pypiSeen = append(pypiSeen, r.URL.Path) + w.Header().Set("Content-Type", "text/html") + _, _ = w.Write([]byte(``)) + })) + t.Cleanup(pypiUp.Close) + + a := newTestApp(t, func(c *config.Config) { + c.Handlers.NPM.UpstreamRegistry = npmUp.URL + c.Handlers.PyPI.SimpleAPIURL = pypiUp.URL + "/simple" + }) + for _, target := range []string{"/npm/npm-run-all", "/npm/npm", "/pypi/simple/pypi-a/", "/pypi/pypi-b/"} { + if code, _, body := doRequest(t, a, target, nil); code != http.StatusOK { + t.Fatalf("GET %s: status = %d, body %q", target, code, body) + } + } + if want := []string{"/npm-run-all", "/npm"}; !slices.Equal(npmSeen, want) { + t.Errorf("npm upstream paths = %v, want %v", npmSeen, want) + } + if want := []string{"/simple/pypi-a/", "/simple/pypi-b/"}; !slices.Equal(pypiSeen, want) { + t.Errorf("pypi upstream paths = %v, want %v", pypiSeen, want) + } +} diff --git a/pkg/proxy/npm/npm.go b/pkg/proxy/npm/npm.go index 9676604..1263356 100644 --- a/pkg/proxy/npm/npm.go +++ b/pkg/proxy/npm/npm.go @@ -58,7 +58,7 @@ func New(cacheManager *cache.Manager, client *network.Client, config Config) *Ha // ServeHTTP handles NPM registry requests func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ctx := r.Context() - path := strings.TrimPrefix(r.URL.Path, "/npm") + path := r.URL.Path log.Debug().Str("path", path).Str("method", r.Method).Msg("NPM proxy request") diff --git a/pkg/proxy/pypi/pypi.go b/pkg/proxy/pypi/pypi.go index 1918fc5..271c546 100644 --- a/pkg/proxy/pypi/pypi.go +++ b/pkg/proxy/pypi/pypi.go @@ -111,9 +111,11 @@ func New(cacheManager *cache.Manager, client *network.Client, config Config) *Ha // ServeHTTP handles PyPI Simple API requests func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ctx := r.Context() - path := strings.TrimPrefix(r.URL.Path, "/pypi") - // Also trim /simple prefix since upstream already includes it - path = strings.TrimPrefix(path, "/simple") + path := r.URL.Path + // Upstream already includes /simple; trim it only as a whole segment so a package such as simple-x keeps its name. + if path == "/simple" || strings.HasPrefix(path, "/simple/") { + path = path[len("/simple"):] + } log.Debug().Str("path", path).Str("method", r.Method).Msg("PyPI proxy request")