Skip to content

Commit 6dad820

Browse files
authored
remote: validate foreign layer URLs to prevent SSRF (fixes #2259) (#2293)
* remote: validate foreign layer URLs to prevent SSRF Foreign layer descriptors in OCI/Docker manifests may carry arbitrary URLs in the descriptor's "urls" field. When the registry blob endpoint returns 404, the client fetches these URLs directly with no validation, allowing a malicious registry to point them at internal services (e.g. the cloud instance-metadata endpoint 169.254.169.254). Add validateForeignURL, which applies the same scheme and private-IP checks as transport.validateRealmURL to every foreign layer URL before making a network request. HTTP is only permitted when the registry itself was reached over HTTP (insecure=true). DNS-based SSRF remains out of scope, consistent with the design decision in validateRealmURL. Fixes #2259. * remote: block SSRF redirect bypass for foreign layer URLs The previous fix validates each foreign layer URL with validateForeignURL before adding it to the fetch list. However, http.Client follows redirects by default: an attacker can host a foreign layer URL on a public domain that passes the initial check, then redirect the client to http://169.254.169.254/... (AWS/GCP instance-metadata), leaking cloud credentials. Add fetchForeignBlobURL (on *fetcher) that reuses the existing transport but sets a CheckRedirect hook calling validateForeignURL on every redirect hop. Compressed() now routes foreign layer fetches through this method instead of the shared fetchBlobURL so that the validation happens at both the initial URL and each redirect destination. New tests: - TestPullingForeignLayerSSRFViaRedirect: attacker httptest server issues a 302 to a loopback victim; confirms Compressed() returns an error before any credentials are returned. * remote: move foreign layer SSRF helpers to fetcher.go per review Move validateForeignURL and fetchForeignBlobURL (plus their tests) next to the fetcher receiver, trim verbose function comments, and rework Compressed() to call fetchBlobURL on the single registry URL with foreign URLs collected and iterated separately. * remote: rename unused params to satisfy revive linter
1 parent 78bdf1b commit 6dad820

4 files changed

Lines changed: 276 additions & 12 deletions

File tree

‎pkg/v1/remote/fetcher.go‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"context"
2020
"fmt"
2121
"io"
22+
"net"
2223
"net/http"
2324
"net/url"
2425
"strings"
@@ -331,6 +332,57 @@ func (f *fetcher) headBlob(ctx context.Context, h v1.Hash) (*http.Response, erro
331332
return resp, nil
332333
}
333334

335+
// validateForeignURL rejects foreign layer URLs that use a disallowed scheme
336+
// or resolve to a private / link-local IP address (SSRF protection). DNS-based
337+
// SSRF is out of scope, matching transport.validateRealmURL.
338+
func validateForeignURL(rawURL string, insecure bool) error {
339+
u, err := url.Parse(rawURL)
340+
if err != nil {
341+
return fmt.Errorf("parsing foreign layer URL %q: %w", rawURL, err)
342+
}
343+
switch u.Scheme {
344+
case "https":
345+
case "http":
346+
if !insecure {
347+
return fmt.Errorf("foreign layer URL scheme %q not allowed for a secure registry; use https", u.Scheme)
348+
}
349+
default:
350+
return fmt.Errorf("foreign layer URL scheme %q not allowed; must be https (or http for insecure registries)", u.Scheme)
351+
}
352+
host := u.Hostname()
353+
if ip := net.ParseIP(host); ip != nil {
354+
if ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() || ip.IsPrivate() || ip.IsUnspecified() {
355+
return fmt.Errorf("foreign layer URL host %q is a private or link-local address", host)
356+
}
357+
}
358+
return nil
359+
}
360+
361+
// fetchForeignBlobURL fetches a foreign-layer blob, validating every redirect
362+
// destination through validateForeignURL (SSRF protection).
363+
func (f *fetcher) fetchForeignBlobURL(ctx context.Context, u url.URL, size int64, h v1.Hash, insecure bool) (io.ReadCloser, error) {
364+
safeClient := &http.Client{
365+
Transport: f.client.Transport,
366+
CheckRedirect: func(req *http.Request, _ []*http.Request) error {
367+
return validateForeignURL(req.URL.String(), insecure)
368+
},
369+
}
370+
371+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
372+
if err != nil {
373+
return nil, err
374+
}
375+
resp, err := safeClient.Do(req)
376+
if err != nil {
377+
return nil, err
378+
}
379+
if resp.StatusCode != http.StatusOK {
380+
resp.Body.Close()
381+
return nil, fmt.Errorf("GET %s: unexpected status %s", u.String(), resp.Status)
382+
}
383+
return verify.ReadCloser(resp.Body, size, h)
384+
}
385+
334386
func (f *fetcher) blobExists(ctx context.Context, h v1.Hash) (bool, error) {
335387
u := f.url("blobs", h.String())
336388
req, err := http.NewRequest(http.MethodHead, u.String(), nil)

‎pkg/v1/remote/fetcher_test.go‎

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
// Copyright 2023 Google LLC All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package remote
16+
17+
import (
18+
"fmt"
19+
"net/http"
20+
"net/http/httptest"
21+
"net/url"
22+
"strings"
23+
"testing"
24+
25+
"github.com/google/go-containerregistry/pkg/name"
26+
"github.com/google/go-containerregistry/pkg/v1/mutate"
27+
"github.com/google/go-containerregistry/pkg/v1/random"
28+
"github.com/google/go-containerregistry/pkg/v1/types"
29+
)
30+
31+
func TestValidateForeignURL(t *testing.T) {
32+
tests := []struct {
33+
url string
34+
insecure bool
35+
wantErr bool
36+
}{
37+
// public HTTPS — always allowed
38+
{"https://cdn.example.com/layer.tar.gz", false, false},
39+
{"https://8.8.8.8/layer.tar.gz", false, false},
40+
// public HTTP — only for insecure registries
41+
{"http://cdn.example.com/layer.tar.gz", true, false},
42+
{"http://cdn.example.com/layer.tar.gz", false, true},
43+
// loopback — rejected even for insecure
44+
{"http://127.0.0.1/layer.tar.gz", true, true},
45+
{"https://127.0.0.1/layer.tar.gz", false, true},
46+
{"https://[::1]/layer.tar.gz", false, true},
47+
// link-local
48+
{"https://169.254.169.254/latest/meta-data/", false, true},
49+
{"http://169.254.169.254/latest/meta-data/", true, true},
50+
// private RFC 1918
51+
{"https://10.0.0.1/layer.tar.gz", false, true},
52+
{"https://192.168.1.1/layer.tar.gz", false, true},
53+
// unspecified
54+
{"https://0.0.0.0/layer.tar.gz", false, true},
55+
// disallowed schemes
56+
{"ftp://cdn.example.com/layer.tar.gz", false, true},
57+
{"file:///etc/passwd", false, true},
58+
}
59+
for _, tt := range tests {
60+
err := validateForeignURL(tt.url, tt.insecure)
61+
if tt.wantErr && err == nil {
62+
t.Errorf("validateForeignURL(%q, insecure=%v) should have been rejected", tt.url, tt.insecure)
63+
}
64+
if !tt.wantErr && err != nil {
65+
t.Errorf("validateForeignURL(%q, insecure=%v) unexpected error: %v", tt.url, tt.insecure, err)
66+
}
67+
}
68+
}
69+
70+
// TestPullingForeignLayerSSRF verifies that a manifest whose foreign-layer URL
71+
// points to a private or loopback address is rejected.
72+
func TestPullingForeignLayerSSRF(t *testing.T) {
73+
img := randomImage(t)
74+
expectedRepo := "foo/bar"
75+
76+
foreignLayer, err := random.Layer(1024, types.DockerForeignLayer)
77+
if err != nil {
78+
t.Fatal(err)
79+
}
80+
81+
img, err = mutate.Append(img, mutate.Addendum{
82+
Layer: foreignLayer,
83+
URLs: []string{"http://169.254.169.254/latest/meta-data/iam/security-credentials/"},
84+
})
85+
if err != nil {
86+
t.Fatal(err)
87+
}
88+
89+
manifestPath := fmt.Sprintf("/v2/%s/manifests/latest", expectedRepo)
90+
configPath := fmt.Sprintf("/v2/%s/blobs/%s", expectedRepo, mustConfigName(t, img))
91+
92+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
93+
switch r.URL.Path {
94+
case "/v2/":
95+
w.WriteHeader(http.StatusOK)
96+
case configPath:
97+
w.Write(mustRawConfigFile(t, img))
98+
case manifestPath:
99+
w.Write(mustRawManifest(t, img))
100+
default:
101+
w.WriteHeader(http.StatusNotFound)
102+
}
103+
}))
104+
defer server.Close()
105+
u, err := url.Parse(server.URL)
106+
if err != nil {
107+
t.Fatal(err)
108+
}
109+
110+
tag := mustNewTag(t, fmt.Sprintf("%s/%s:latest", u.Host, expectedRepo))
111+
rmt, err := Image(tag, WithTransport(http.DefaultTransport))
112+
if err != nil {
113+
t.Fatal(err)
114+
}
115+
116+
layers, err := rmt.Layers()
117+
if err != nil {
118+
t.Fatal(err)
119+
}
120+
_, err = layers[1].Compressed()
121+
if err == nil {
122+
t.Error("Compressed() should have been rejected for a foreign layer URL pointing to a private address")
123+
}
124+
}
125+
126+
// TestPullingForeignLayerSSRFViaRedirect verifies the CheckRedirect hook in
127+
// fetchForeignBlobURL rejects redirects to private/loopback addresses.
128+
func TestPullingForeignLayerSSRFViaRedirect(t *testing.T) {
129+
img := randomImage(t)
130+
expectedRepo := "foo/bar"
131+
132+
foreignLayer, err := random.Layer(1024, types.DockerForeignLayer)
133+
if err != nil {
134+
t.Fatal(err)
135+
}
136+
137+
victim := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
138+
w.Header().Set("Content-Type", "application/json")
139+
w.Write([]byte(`{"AccessKeyId":"ASIA_LEAKED","SecretAccessKey":"LEAKED_SECRET"}`))
140+
}))
141+
defer victim.Close()
142+
143+
attacker := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
144+
http.Redirect(w, r, victim.URL+"/credentials", http.StatusFound)
145+
}))
146+
defer attacker.Close()
147+
148+
img, err = mutate.Append(img, mutate.Addendum{
149+
Layer: foreignLayer,
150+
URLs: []string{attacker.URL + "/layer.tar.gz"},
151+
})
152+
if err != nil {
153+
t.Fatal(err)
154+
}
155+
156+
manifestPath := fmt.Sprintf("/v2/%s/manifests/latest", expectedRepo)
157+
configPath := fmt.Sprintf("/v2/%s/blobs/%s", expectedRepo, mustConfigName(t, img))
158+
159+
registryServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
160+
switch r.URL.Path {
161+
case "/v2/":
162+
w.WriteHeader(http.StatusOK)
163+
case configPath:
164+
w.Write(mustRawConfigFile(t, img))
165+
case manifestPath:
166+
w.Write(mustRawManifest(t, img))
167+
default:
168+
w.WriteHeader(http.StatusNotFound)
169+
}
170+
}))
171+
defer registryServer.Close()
172+
173+
u, err := url.Parse(registryServer.URL)
174+
if err != nil {
175+
t.Fatal(err)
176+
}
177+
178+
ref, err := name.ParseReference(fmt.Sprintf("%s/%s:latest", u.Host, expectedRepo), name.Insecure)
179+
if err != nil {
180+
t.Fatal(err)
181+
}
182+
rmt, err := Image(ref, WithTransport(http.DefaultTransport))
183+
if err != nil {
184+
t.Fatal(err)
185+
}
186+
187+
layers, err := rmt.Layers()
188+
if err != nil {
189+
t.Fatal(err)
190+
}
191+
_, err = layers[1].Compressed()
192+
if err == nil {
193+
t.Error("Compressed() followed a redirect to a private address")
194+
}
195+
if err != nil && !strings.Contains(err.Error(), "private or link-local") {
196+
t.Logf("Compressed() returned error (expected): %v", err)
197+
}
198+
}

‎pkg/v1/remote/image.go‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func (rl *remoteImageLayer) Digest() (v1.Hash, error) {
170170

171171
// Compressed implements partial.CompressedLayer
172172
func (rl *remoteImageLayer) Compressed() (io.ReadCloser, error) {
173-
urls := []url.URL{rl.ri.fetcher.url("blobs", rl.digest.String())}
173+
u := rl.ri.fetcher.url("blobs", rl.digest.String())
174174

175175
// Add alternative layer sources from URLs (usually none).
176176
d, err := partial.BlobDescriptor(rl, rl.digest)
@@ -185,21 +185,33 @@ func (rl *remoteImageLayer) Compressed() (io.ReadCloser, error) {
185185
// We don't want to log binary layers -- this can break terminals.
186186
ctx := redact.NewContext(rl.ctx, "omitting binary blobs from logs")
187187

188-
for _, s := range d.URLs {
189-
u, err := url.Parse(s)
190-
if err != nil {
191-
return nil, err
192-
}
193-
urls = append(urls, *u)
194-
}
188+
insecure := rl.ri.fetcher.target.Scheme() == "http"
195189

196190
// The lastErr for most pulls will be the same (the first error), but for
197191
// foreign layers we'll want to surface the last one, since we try to pull
198192
// from the registry first, which would often fail.
199193
// TODO: Maybe we don't want to try pulling from the registry first?
200194
var lastErr error
201-
for _, u := range urls {
202-
rc, err := rl.ri.fetcher.fetchBlobURL(ctx, u, d.Size, rl.digest)
195+
rc, err := rl.ri.fetcher.fetchBlobURL(ctx, u, d.Size, rl.digest)
196+
if err == nil {
197+
return rc, nil
198+
}
199+
lastErr = err
200+
201+
foreignURLs := make([]url.URL, 0, len(d.URLs))
202+
for _, s := range d.URLs {
203+
if err := validateForeignURL(s, insecure); err != nil {
204+
return nil, err
205+
}
206+
fu, err := url.Parse(s)
207+
if err != nil {
208+
return nil, err
209+
}
210+
foreignURLs = append(foreignURLs, *fu)
211+
}
212+
213+
for _, fu := range foreignURLs {
214+
rc, err := rl.ri.fetcher.fetchForeignBlobURL(ctx, fu, d.Size, rl.digest, insecure)
203215
if err != nil {
204216
lastErr = err
205217
continue

‎pkg/v1/remote/image_test.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"net/http"
2424
"net/http/httptest"
2525
"net/url"
26-
"path"
2726
"strings"
2827
"testing"
2928

@@ -595,10 +594,13 @@ func TestPullingForeignLayer(t *testing.T) {
595594
t.Fatalf("url.Parse(%v) = %v", foreignServer.URL, err)
596595
}
597596

597+
// Use "localhost" (not "127.0.0.1") so the hostname is not an IP literal
598+
// and is not rejected by validateForeignURL's private-address check.
599+
// DNS-based SSRF is explicitly out of scope for this validation.
598600
img, err = mutate.Append(img, mutate.Addendum{
599601
Layer: foreignLayer,
600602
URLs: []string{
601-
"http://" + path.Join(fu.Host, foreignPath),
603+
"http://localhost:" + fu.Port() + foreignPath,
602604
},
603605
})
604606
if err != nil {

0 commit comments

Comments
 (0)