Files
gohoarder/pkg/proxy/pypi/original_url_binding_test.go
T
lukaszraczylo 501bf1aec8 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).
2026-11-23 17:08:20 +00:00

51 lines
2.1 KiB
Go

package pypi
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
// The cache key is pypi/<name>/<filename>, so original_url must name that same file.
func TestHandlePackageFile_OriginalURLFilenameBound(t *testing.T) {
const path = "/example/example-1.0.0-py3-none-any.whl"
tests := []struct {
name string
path string
originalURL string
wantReject bool
}{
{"other file on allowed host", path, "https://files.pythonhosted.org/packages/ab/cd/ef/evil-9.9.9-py3-none-any.whl", true},
{"other project same version", path, "https://files.pythonhosted.org/packages/ab/cd/ef/other-1.0.0-py3-none-any.whl", true},
{"sidecar of another file", path + ".metadata", "https://files.pythonhosted.org/packages/ab/cd/ef/evil-9.9.9-py3-none-any.whl", true},
{"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()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
q := url.Values{}
q.Set("original_url", tt.originalURL)
req := httptest.NewRequest(http.MethodGet, tt.path+"?"+q.Encode(), nil)
w := httptest.NewRecorder()
// A nil cache panics once the guards pass; that means "not rejected".
func() {
defer func() { _ = recover() }()
h.handlePackageFile(req.Context(), w, req, tt.path)
}()
rejected := w.Code == http.StatusBadRequest
if rejected != tt.wantReject {
t.Errorf("rejected=%v want %v (code=%d body=%q)", rejected, tt.wantReject, w.Code, w.Body.String())
}
})
}
}