Skip to content

remote: validate foreign layer URLs to prevent SSRF#2259

Closed
AlexJohnWilcox wants to merge 3 commits into
google:mainfrom
AlexJohnWilcox:fix-foreign-layer-ssrf
Closed

remote: validate foreign layer URLs to prevent SSRF#2259
AlexJohnWilcox wants to merge 3 commits into
google:mainfrom
AlexJohnWilcox:fix-foreign-layer-ssrf

Conversation

@AlexJohnWilcox

@AlexJohnWilcox AlexJohnWilcox commented Apr 9, 2026

Copy link
Copy Markdown

Summary

Foreign layer descriptors in OCI/Docker image manifests can contain arbitrary URLs in the urls field (OCI Image Spec). When the registry blob endpoint returns 404, the client fetches these foreign URLs with no validation. A malicious registry can serve manifests with foreign layer URLs pointing to internal services (e.g. cloud metadata at 169.254.169.254, RFC 1918 addresses), causing SSRF from any library consumer.

This PR adds two fixes:

1. Foreign layer URL validation (pkg/v1/remote/image.go)

Adds validateForeignURL() to reject foreign layer URLs that:

  • Use non-HTTP(S) schemes (ftp://, file://, etc.)
  • Reference private, loopback, link-local, or unspecified IP literals

This is consistent with the existing validateRealmURL() protection added in #2243 for Bearer auth realm URLs. DNS-based SSRF remains out of scope, matching the design decision in validateRealmURL().

2. IsUnspecified gap in validateRealmURL (pkg/v1/remote/transport/bearer.go)

validateRealmURL() checks IsLoopback, IsPrivate, IsLinkLocalUnicast, and IsLinkLocalMulticast but not IsUnspecified. The addresses 0.0.0.0 and [::] pass all four checks but connect to localhost on Linux/macOS. Adds ip.IsUnspecified() to close this gap.

Test plan

  • Added TestValidateForeignURL — table-driven unit test (12 cases covering public, private, loopback, link-local, unspecified IPs and disallowed schemes)
  • Added TestPullingForeignLayerSSRF — integration test that verifies pulling an image with a foreign layer URL pointing to 169.254.169.254 is rejected
  • Added TestValidateRealmURL — table-driven unit test (11 cases including 0.0.0.0 and [::])
  • Existing TestPullingForeignLayer updated and passing
  • Full go test ./pkg/v1/remote/ ./pkg/v1/remote/transport/ passes

@google-cla

google-cla Bot commented Apr 9, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Foreign layer descriptors in image manifests can contain arbitrary URLs
in the "urls" field. When the registry blob endpoint returns 404, the
client fetches these URLs with no validation, allowing a malicious
registry to trigger requests to internal services (SSRF).

Add validateForeignURL() to reject foreign layer URLs that use
non-HTTP(S) schemes or reference private, loopback, link-local, or
unspecified IP addresses. This is consistent with the existing
validateRealmURL() protection added in PR #2243 for Bearer auth
realm URLs.

DNS-based SSRF remains out of scope, matching the design decision
in validateRealmURL().
validateRealmURL blocks loopback, private, and link-local IP literals
but does not check for unspecified addresses (0.0.0.0, [::]).
These addresses pass all four existing checks but connect to localhost
on Linux and macOS.

Add ip.IsUnspecified() to close this gap.
@AlexJohnWilcox AlexJohnWilcox force-pushed the fix-foreign-layer-ssrf branch from ed3d202 to 9f6bd52 Compare April 9, 2026 22:51
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 53.05%. Comparing base (8b3c303) to head (9f6bd52).
⚠️ Report is 86 commits behind head on main.

❗ There is a different number of reports uploaded between BASE (8b3c303) and HEAD (9f6bd52). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (8b3c303) HEAD (9f6bd52)
2 1
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #2259       +/-   ##
===========================================
- Coverage   71.67%   53.05%   -18.63%     
===========================================
  Files         123      165       +42     
  Lines        9935    11219     +1284     
===========================================
- Hits         7121     5952     -1169     
- Misses       2115     4555     +2440     
- Partials      699      712       +13     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Subserial pushed a commit that referenced this pull request May 14, 2026
)

* 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
Subserial pushed a commit to Subserial/go-containerregistry that referenced this pull request May 15, 2026
… (google#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 google#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants