| index 0274fea8d..0a4377f28 100644 |
| --- a/cmd/mediaimage/mediaimage_test.go |
| +++ b/cmd/mediaimage/mediaimage_test.go |
| @@ -670,7 +670,7 @@ type fakeRegistry struct { |
| password string |
| tamper bool |
| failDigest bool // a manifest read by digest answers 500 |
| - redirect string // every request is sent here instead |
| + redirect string // every POST, PATCH and PUT is sent here instead |
| location func(stage string) string // replaces an upload's Location |
| srv *httptest.Server |
| } |
| @@ -695,7 +695,7 @@ func (r *fakeRegistry) ServeHTTP(w http.ResponseWriter, req *http.Request) { |
| return |
| } |
| } |
| - if r.redirect != "" { |
| + if r.redirect != "" && req.Method != http.MethodGet && req.Method != http.MethodHead { |
| w.Header().Set("Location", r.redirect) |
| w.WriteHeader(http.StatusTemporaryRedirect) |
| return |
| @@ -1968,7 +1968,7 @@ func TestPushKeepsCredentialsOnTheRegistry(t *testing.T) { |
| "no location": func(reg *fakeRegistry) { |
| reg.location = func(string) string { return "" } |
| }, |
| - "a redirect elsewhere": func(reg *fakeRegistry) { reg.redirect = trap.URL + "/stolen" }, |
| + "an upload redirected elsewhere": func(reg *fakeRegistry) { reg.redirect = trap.URL + "/stolen" }, |
| } { |
| t.Run(name, func(t *testing.T) { |
| reg := newFakeRegistry(t) |
| @@ -2017,6 +2017,198 @@ func TestUploadRefusesADowngradeToHTTP(t *testing.T) { |
| } |
| } |
| |
| +// hop is one fake HTTPS server in a redirect chain. It records what each |
| +// request brought and answers with next, or 200 when next is empty. |
| +type hop struct { |
| + srv *httptest.Server |
| + mu sync.Mutex |
| + auth []string |
| + refs []string |
| + seen []string |
| + next func(path string) string |
| +} |
| + |
| +func newHop(t *testing.T) *hop { |
| + t.Helper() |
| + h := &hop{} |
| + h.srv = httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| + h.mu.Lock() |
| + h.auth = append(h.auth, req.Header.Get("Authorization")) |
| + h.refs = append(h.refs, req.Referer()) |
| + h.seen = append(h.seen, req.Method+" "+req.URL.Path) |
| + next := h.next |
| + h.mu.Unlock() |
| + if next != nil { |
| + if target := next(req.URL.Path); target != "" { |
| + w.Header().Set("Location", target) |
| + w.WriteHeader(http.StatusTemporaryRedirect) |
| + return |
| + } |
| + } |
| + _, _ = w.Write([]byte("stored")) |
| + })) |
| + t.Cleanup(h.srv.Close) |
| + return h |
| +} |
| + |
| +// tlsRegistry is a registry client trusting the fake HTTPS servers. |
| +func tlsRegistry(t *testing.T, h *hop) *registry { |
| + t.Helper() |
| + saved := httpClient |
| + httpClient = h.srv.Client() |
| + t.Cleanup(func() { httpClient = saved }) |
| + return ®istry{base: h.srv.URL, repo: "engine", basic: "Basic secret"} |
| +} |
| + |
| +func TestReadFollowsStorageWithoutCredentials(t *testing.T) { |
| + for _, method := range []string{http.MethodGet, http.MethodHead} { |
| + t.Run(method, func(t *testing.T) { |
| + origin, first, second := newHop(t), newHop(t), newHop(t) |
| + origin.next = func(string) string { return first.srv.URL + "/presigned" } |
| + first.next = func(string) string { return second.srv.URL + "/presigned-again" } |
| + reg := tlsRegistry(t, origin) |
| + status := 0 |
| + err := send(t.Context(), reg, method, reg.url("blobs/sha256:abc"), "", nil, 0, func(resp *http.Response) error { |
| + status = resp.StatusCode |
| + return nil |
| + }) |
| + if err != nil || status != http.StatusOK { |
| + t.Fatalf("read = %d, %v", status, err) |
| + } |
| + if !slices.Equal(origin.auth, []string{"Basic secret"}) { |
| + t.Fatalf("the registry saw %q", origin.auth) |
| + } |
| + if !slices.Equal(first.auth, []string{""}) || !slices.Equal(second.auth, []string{""}) { |
| + t.Fatalf("storage saw authorization: %q and %q", first.auth, second.auth) |
| + } |
| + if !slices.Equal(second.refs, []string{""}) { |
| + t.Fatalf("the second hop was told the presigned URL before it: %q", second.refs) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// TestCredentialsNeverComeBack: Go's client would restore the header on a |
| +// hop that returns to the registry. |
| +func TestCredentialsNeverComeBack(t *testing.T) { |
| + origin, storage := newHop(t), newHop(t) |
| + origin.next = func(path string) string { |
| + switch path { |
| + case "/v2/engine/manifests/x": |
| + return origin.srv.URL + "/same-origin" |
| + case "/same-origin": |
| + return storage.srv.URL + "/presigned" |
| + } |
| + return "" |
| + } |
| + storage.next = func(string) string { return origin.srv.URL + "/back" } |
| + reg := tlsRegistry(t, origin) |
| + data, err := fetchManifest(t.Context(), reg, "x") |
| + if err != nil || string(data) != "stored" { |
| + t.Fatalf("fetchManifest = %q, %v", data, err) |
| + } |
| + if want := []string{"Basic secret", "Basic secret", ""}; !slices.Equal(origin.auth, want) { |
| + t.Fatalf("the registry saw %q on %q, want %q", origin.auth, origin.seen, want) |
| + } |
| + if !slices.Equal(storage.auth, []string{""}) { |
| + t.Fatalf("storage saw %q", storage.auth) |
| + } |
| +} |
| + |
| +func TestReadRefusesADowngradeToHTTP(t *testing.T) { |
| + trap, hits := credentialTrap(t) |
| + t.Run("from the registry", func(t *testing.T) { |
| + origin := newHop(t) |
| + origin.next = func(string) string { return trap.URL + "/plain" } |
| + reg := tlsRegistry(t, origin) |
| + if _, err := fetchManifest(t.Context(), reg, "x"); !errors.Is(err, errUnsafeLocation) { |
| + t.Fatalf("fetchManifest = %v, want errUnsafeLocation", err) |
| + } |
| + }) |
| + t.Run("from storage", func(t *testing.T) { |
| + origin, storage := newHop(t), newHop(t) |
| + origin.next = func(string) string { return storage.srv.URL + "/presigned" } |
| + storage.next = func(string) string { return trap.URL + "/plain" } |
| + reg := tlsRegistry(t, origin) |
| + if _, err := fetchManifest(t.Context(), reg, "x"); !errors.Is(err, errUnsafeLocation) { |
| + t.Fatalf("fetchManifest = %v, want errUnsafeLocation", err) |
| + } |
| + }) |
| + if got := hits.Load(); got != 0 { |
| + t.Fatalf("the plain http server got %d requests", got) |
| + } |
| +} |
| + |
| +func TestUploadsStayOnTheRegistry(t *testing.T) { |
| + for _, method := range []string{http.MethodPost, http.MethodPatch, http.MethodPut} { |
| + t.Run(method, func(t *testing.T) { |
| + origin, storage := newHop(t), newHop(t) |
| + origin.next = func(string) string { return storage.srv.URL + "/elsewhere" } |
| + reg := tlsRegistry(t, origin) |
| + body := []byte("manifest") |
| + err := send(t.Context(), reg, method, reg.url("manifests/v1"), mediaTypeOCIManifest, bytes.NewReader(body), int64(len(body)), expect("write", http.StatusCreated)) |
| + if !errors.Is(err, errUnsafeLocation) { |
| + t.Fatalf("send = %v, want errUnsafeLocation", err) |
| + } |
| + if len(storage.seen) != 0 { |
| + t.Fatalf("storage saw %q", storage.seen) |
| + } |
| + }) |
| + } |
| + t.Run("a POST that a 303 turns into a GET", func(t *testing.T) { |
| + reg := ®istry{base: "https://registry.example"} |
| + next, err := http.NewRequest(http.MethodGet, "https://storage.example/x", nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + first, err := http.NewRequest(http.MethodPost, "https://registry.example/v2/x", nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := reg.checkRedirect(next, []*http.Request{first}); !errors.Is(err, errUnsafeLocation) { |
| + t.Fatalf("checkRedirect = %v, want errUnsafeLocation", err) |
| + } |
| + }) |
| +} |
| + |
| +func TestCheckRedirectRules(t *testing.T) { |
| + request := func(method, target string) *http.Request { |
| + req, err := http.NewRequest(method, target, nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return req |
| + } |
| + first := request(http.MethodGet, "https://registry.example/v2/x") |
| + limit := slices.Repeat([]*http.Request{first}, maxRedirects) |
| + for name, c := range map[string]struct { |
| + base, target string |
| + via []*http.Request |
| + ok bool |
| + }{ |
| + "https storage": {"https://registry.example", "https://storage.example/x", []*http.Request{first}, true}, |
| + "the last hop within the limit": {"https://registry.example", "https://storage.example/x", limit[1:], true}, |
| + "past the limit": {"https://registry.example", "https://storage.example/x", limit, false}, |
| + "storage with userinfo": {"https://registry.example", "https://u:p@storage.example/x", []*http.Request{first}, false}, |
| + "http on the registry's host": {"https://registry.example", "http://registry.example/x", []*http.Request{first}, false}, |
| + "http loopback from https": {"https://registry.example", "http://127.0.0.1/x", []*http.Request{first}, false}, |
| + "http remote from loopback": {"http://127.0.0.1:5000", "http://storage.example/x", []*http.Request{request(http.MethodGet, "http://127.0.0.1:5000/v2/x")}, false}, |
| + "http loopback from loopback": {"http://127.0.0.1:5000", "http://127.0.0.1:6000/x", []*http.Request{request(http.MethodGet, "http://127.0.0.1:5000/v2/x")}, true}, |
| + "another scheme": {"https://registry.example", "ftp://storage.example/x", []*http.Request{first}, false}, |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + reg := ®istry{base: c.base} |
| + err := reg.checkRedirect(request(http.MethodGet, c.target), c.via) |
| + if c.ok && err != nil { |
| + t.Fatalf("refused: %v", err) |
| + } |
| + if !c.ok && !errors.Is(err, errUnsafeLocation) { |
| + t.Fatalf("checkRedirect = %v, want errUnsafeLocation", err) |
| + } |
| + }) |
| + } |
| +} |
| + |
| func TestCheckOrigin(t *testing.T) { |
| reg := ®istry{base: "https://registry.example"} |
| for target, ok := range map[string]bool{ |