mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-25 06:35:21 +00:00
fix(pypi): bind original_url to the requested file name
The cache key is built from the request path, but the upstream fetch used the original_url query value. Any file on an allowed host could be stored under another package's key. The handler now rejects an original_url whose file name differs from the requested file.
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
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},
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
gopath "path"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -285,6 +286,17 @@ func (h *Handler) handlePackageFile(ctx context.Context, w http.ResponseWriter,
|
||||
http.Error(w, "original_url host not allowed", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
// The cache key is built from the request path, so the fetched file must
|
||||
// be the one the path names; otherwise any file on an allowed host could
|
||||
// be stored under another package's key.
|
||||
if gopath.Base(parsed.Path) != filename {
|
||||
log.Warn().
|
||||
Str("original_url", originalURL).
|
||||
Str("filename", filename).
|
||||
Msg("Rejected original_url naming a different file")
|
||||
http.Error(w, "original_url does not match requested file", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// A PEP 658 sidecar is a view of its wheel, not a package: stream it
|
||||
|
||||
Reference in New Issue
Block a user