mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-26 06:37:07 +00:00
fix(pypi): serve pip's PEP 658 metadata requests
pip builds the PEP 658 URL by appending .metadata to the whole link, so the suffix lands on original_url, not the path. The filename binding from GH-01 then rejected the request with 400, and every pip install through the proxy failed. The handler now treats that form as the requested wheel's sidecar and streams it without storing it. A sidecar of another file is still refused. The Lean model and the ledger cover the new form (GH-23).
This commit is contained in:
@@ -22,6 +22,9 @@ func TestHandlePackageFile_OriginalURLFilenameBound(t *testing.T) {
|
||||
{"same file", path, "https://files.pythonhosted.org/packages/ab/cd/ef/example-1.0.0-py3-none-any.whl", false},
|
||||
{"same file with query", path, "https://files.pythonhosted.org/packages/ab/cd/ef/example-1.0.0-py3-none-any.whl?x=1", false},
|
||||
{"sidecar of same file", path + ".metadata", "https://files.pythonhosted.org/packages/ab/cd/ef/example-1.0.0-py3-none-any.whl", false},
|
||||
// pip appends ".metadata" to the whole link, so the suffix lands on original_url instead of the path.
|
||||
{"pip sidecar of same file", path, "https://files.pythonhosted.org/packages/ab/cd/ef/example-1.0.0-py3-none-any.whl.metadata", false},
|
||||
{"pip sidecar of another file", path, "https://files.pythonhosted.org/packages/ab/cd/ef/evil-9.9.9-py3-none-any.whl.metadata", true},
|
||||
}
|
||||
|
||||
h := newHandlerForSSRFTest()
|
||||
|
||||
@@ -288,6 +288,11 @@ func (h *Handler) handlePackageFile(ctx context.Context, w http.ResponseWriter,
|
||||
parsed.RawPath = ""
|
||||
originalURL = parsed.String()
|
||||
}
|
||||
// pip appends ".metadata" to the whole link, so the suffix lands on original_url, not the path.
|
||||
if !isSidecar && gopath.Base(parsed.Path) == filename+metadataSuffix {
|
||||
isSidecar = true
|
||||
filename += metadataSuffix
|
||||
}
|
||||
if !isAllowedPyPIHost(parsed.Host, h.allowedHosts) {
|
||||
log.Warn().
|
||||
Str("original_url", originalURL).
|
||||
|
||||
@@ -39,3 +39,31 @@ func TestSidecarRedirectOffAllowlistRefused(t *testing.T) {
|
||||
t.Errorf("redirect target was fetched (hits=%d)", hit.Load())
|
||||
}
|
||||
}
|
||||
|
||||
// pip requests the PEP 658 sidecar as <link>.metadata, which puts the suffix on original_url.
|
||||
// It must be streamed as a sidecar, never cached as the wheel.
|
||||
func TestPipSidecarFormIsStreamed(t *testing.T) {
|
||||
const meta = "Metadata-Version: 2.1\nName: example\n"
|
||||
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if !strings.HasSuffix(r.URL.Path, "/example-1.0.0-py3-none-any.whl.metadata") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
_, _ = w.Write([]byte(meta))
|
||||
}))
|
||||
defer upstream.Close()
|
||||
|
||||
client := network.NewClient(network.Config{Timeout: 5 * time.Second, MaxRetries: 1, AllowPrivateRedirects: true})
|
||||
// No cache: touching it would panic, so a 200 proves the request never reached the wheel path.
|
||||
h := &Handler{client: client, allowedHosts: []string{"127.0.0.1"}}
|
||||
|
||||
const path = "/example/example-1.0.0-py3-none-any.whl"
|
||||
q := url.Values{"original_url": {upstream.URL + "/files/example-1.0.0-py3-none-any.whl.metadata"}}
|
||||
req := httptest.NewRequest(http.MethodGet, path+"?"+q.Encode(), nil)
|
||||
w := httptest.NewRecorder()
|
||||
h.handlePackageFile(req.Context(), w, req, path)
|
||||
|
||||
if w.Code != http.StatusOK || w.Body.String() != meta {
|
||||
t.Errorf("code=%d body=%q, want 200 with the metadata", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user