mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-26 06:37:07 +00:00
fix(network): vet every redirect hop against the upstream allowlist
This commit is contained in:
+14
-4
@@ -51,6 +51,16 @@ func isAllowedPyPIHost(host string, allowed []string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// get fetches from upstream; every redirect hop must be on the allowlist, as for original_url.
|
||||
func (h *Handler) get(ctx context.Context, rawURL string, headers map[string]string) (io.ReadCloser, int, error) {
|
||||
return h.client.GetWithRedirectCheck(ctx, rawURL, headers, func(u *url.URL) error {
|
||||
if !isAllowedPyPIHost(u.Host, h.allowedHosts) {
|
||||
return fmt.Errorf("redirect host %q not on allowlist", u.Hostname())
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// Handler implements the PyPI Simple API (PEP 503)
|
||||
type Handler struct {
|
||||
cache *cache.Manager
|
||||
@@ -130,7 +140,7 @@ func (h *Handler) handleIndex(ctx context.Context, w http.ResponseWriter, r *htt
|
||||
url := h.upstream + "/"
|
||||
|
||||
entry, err := h.cache.Get(ctx, "pypi", "index", "latest", func(ctx context.Context) (*cache.FetchResult, error) {
|
||||
body, statusCode, err := h.client.Get(ctx, url, nil)
|
||||
body, statusCode, err := h.get(ctx, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -178,7 +188,7 @@ func (h *Handler) handlePackagePage(ctx context.Context, w http.ResponseWriter,
|
||||
}
|
||||
|
||||
entry, err := h.cache.Get(ctx, "pypi", packageName, cacheVersion, func(ctx context.Context) (*cache.FetchResult, error) {
|
||||
body, statusCode, err := h.client.Get(ctx, url, upstreamHeaders)
|
||||
body, statusCode, err := h.get(ctx, url, upstreamHeaders)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -323,7 +333,7 @@ func (h *Handler) handlePackageFile(ctx context.Context, w http.ResponseWriter,
|
||||
headers["Authorization"] = credentials
|
||||
}
|
||||
|
||||
body, statusCode, err := h.client.Get(ctx, originalURL, headers)
|
||||
body, statusCode, err := h.get(ctx, originalURL, headers)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -454,7 +464,7 @@ func (h *Handler) serveMetadataSidecar(ctx context.Context, w http.ResponseWrite
|
||||
if credentials != "" {
|
||||
headers["Authorization"] = credentials
|
||||
}
|
||||
body, status, err := h.client.Get(ctx, upstreamURL, headers)
|
||||
body, status, err := h.get(ctx, upstreamURL, headers)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("url", upstreamURL).Msg("Failed to fetch PEP 658 metadata")
|
||||
http.Error(w, "Failed to fetch package metadata", http.StatusBadGateway)
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package pypi
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/lukaszraczylo/gohoarder/pkg/network"
|
||||
)
|
||||
|
||||
// A PEP 658 sidecar fetched from an allowed host must not follow a redirect off the allowlist.
|
||||
func TestSidecarRedirectOffAllowlistRefused(t *testing.T) {
|
||||
var hit atomic.Int32
|
||||
internal := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
hit.Add(1)
|
||||
_, _ = w.Write([]byte("internal-secret"))
|
||||
}))
|
||||
defer internal.Close()
|
||||
allowed := httptest.NewServer(http.RedirectHandler(strings.Replace(internal.URL, "127.0.0.1", "localhost", 1)+"/example-1.0.0-py3-none-any.whl.metadata", http.StatusFound))
|
||||
defer allowed.Close()
|
||||
|
||||
client := network.NewClient(network.Config{Timeout: 5 * time.Second, MaxRetries: 1, AllowPrivateRedirects: true})
|
||||
h := &Handler{client: client, allowedHosts: []string{"127.0.0.1"}}
|
||||
|
||||
const path = "/example/example-1.0.0-py3-none-any.whl.metadata"
|
||||
q := url.Values{"original_url": {allowed.URL + "/files/example-1.0.0-py3-none-any.whl"}}
|
||||
req := httptest.NewRequest(http.MethodGet, path+"?"+q.Encode(), nil)
|
||||
w := httptest.NewRecorder()
|
||||
h.handlePackageFile(req.Context(), w, req, path)
|
||||
|
||||
if w.Code != http.StatusBadGateway {
|
||||
t.Errorf("code=%d want 502 body=%q", w.Code, w.Body.String())
|
||||
}
|
||||
if hit.Load() != 0 || strings.Contains(w.Body.String(), "internal-secret") {
|
||||
t.Errorf("redirect target was fetched (hits=%d)", hit.Load())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user