mirror of
https://tangled.org/evan.jarrett.net/at-container-registry
synced 2026-09-20 09:14:16 +00:00
Credential-less pulls of public images. /auth/token issues a pull-only
token with an empty subject when no Basic auth is present; the
destination hold still enforces captain.Public, and push or delete always
challenges.
- token.IsPullOnlyScope and AuthMethodAnonymous;
Handler.issueAnonymousToken skips the authorizer gate and the
service-auth pre-mint, since there is no identity to reconcile and no
AppView-to-hold service token to bind. The token is still stamped
with the resolved registry domain, so anonymous pull works on
secondary front doors whose access controller demands their own
audience.
- auth.allow_anonymous_pull (default true) turns it fully off, restoring
the previous always-challenge behavior. Mirrored into the deploy
template, since the default means existing deploys pick this up.
- RegistryContext.Anonymous is plumbed from the middleware.
- ProxyBlobStore sends no Authorization header when the service token is
empty, and returns 401 rather than 403 for anonymous denials so Docker
prompts for credentials, including when a stale captain cache lets the
request through and the hold says private.
- BearerChallenge wraps the /v2/ subtree so a 401 raised deep in the
stack via errcode.ServeJSON still carries WWW-Authenticate.
Distribution's own scoped challenges are left alone.
IsPullOnlyScope allowlists the pull action instead of denylisting push and
delete. Distribution's actionSet.contains treats "*" as *every* action, so
a scope of `repository:victim/img:*` names neither denied string and would
have handed an unauthenticated caller a token valid for push and delete on
someone else's repository — clearing the authgate entirely, since anonymous
tokens deliberately skip it. Writes would still have failed further down
(no PDS credential), but the gate itself was bypassable. Now every
requested action must be exactly "pull". Covered by new claims tests.
Unresolvable identities return NAME_UNKNOWN instead of a bare error that
distribution renders as 500. This path was previously unreachable without
credentials; anonymous pull opens it to the internet, and a 5xx on
arbitrary input both misreports a bad request as a server fault and sends
clients that retry 5xx into a retry loop. That loop was real: in the auth
matrix, regclient spent 83s on a single case before this fix, and the
suite now runs in 5s.
Stat preserves an authorization verdict from getPresignedURL rather than
flattening it to ErrBlobUnknown. Distribution calls Stat before ServeBlob
on GET and HEAD, so without this an anonymous pull from a private hold
answered 404 and BearerChallenge had no 401 to annotate — the 401 path
above could never actually reach a client.
The auth matrix is updated to match: anonymous pull of the seeded public
repo now succeeds, anonymous push is denied against a real identity's
namespace (rather than an unresolvable one, which was testing name
resolution rather than authorization), and a new case pins the
NAME_UNKNOWN behavior for an unknown identity.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
126 lines
3.5 KiB
Go
126 lines
3.5 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestBearerChallenge(t *testing.T) {
|
|
const realm = "https://atcr.io/auth/token"
|
|
services := []string{"atcr.io", "buoy.cr"}
|
|
wantChallenge := `Bearer realm="https://atcr.io/auth/token",service="atcr.io"`
|
|
|
|
tests := []struct {
|
|
name string
|
|
handler http.HandlerFunc
|
|
wantCode int
|
|
wantChallenge string
|
|
}{
|
|
{
|
|
name: "bare 401 gets a challenge injected",
|
|
handler: func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
},
|
|
wantCode: http.StatusUnauthorized,
|
|
wantChallenge: wantChallenge,
|
|
},
|
|
{
|
|
name: "401 with existing challenge is left untouched",
|
|
handler: func(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("WWW-Authenticate", `Bearer realm="x",service="y",scope="repository:a/b:pull"`)
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
},
|
|
wantCode: http.StatusUnauthorized,
|
|
wantChallenge: `Bearer realm="x",service="y",scope="repository:a/b:pull"`,
|
|
},
|
|
{
|
|
name: "200 is not given a challenge",
|
|
handler: func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
},
|
|
wantCode: http.StatusOK,
|
|
wantChallenge: "",
|
|
},
|
|
{
|
|
name: "implicit 200 via Write is not given a challenge",
|
|
handler: func(w http.ResponseWriter, _ *http.Request) {
|
|
_, _ = w.Write([]byte("ok"))
|
|
},
|
|
wantCode: http.StatusOK,
|
|
wantChallenge: "",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
wrapped := BearerChallenge(realm, services)(tt.handler)
|
|
req := httptest.NewRequest(http.MethodGet, "/v2/alice/app/blobs/sha256:abc", nil)
|
|
req.Host = "atcr.io"
|
|
w := httptest.NewRecorder()
|
|
|
|
wrapped.ServeHTTP(w, req)
|
|
|
|
if w.Code != tt.wantCode {
|
|
t.Errorf("status = %d, want %d", w.Code, tt.wantCode)
|
|
}
|
|
if got := w.Header().Get("WWW-Authenticate"); got != tt.wantChallenge {
|
|
t.Errorf("WWW-Authenticate = %q, want %q", got, tt.wantChallenge)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// The challenge has to name the domain the request arrived on: the atcr-token
|
|
// access controller demands that domain's own audience, so advertising another
|
|
// would send the client back with a token this host rejects.
|
|
func TestBearerChallengePerDomain(t *testing.T) {
|
|
const realm = "https://seamark.dev/auth/token"
|
|
services := []string{"atcr.io", "buoy.cr"}
|
|
|
|
tests := []struct {
|
|
name string
|
|
host string
|
|
want string
|
|
}{
|
|
{
|
|
name: "primary domain",
|
|
host: "atcr.io",
|
|
want: `Bearer realm="https://seamark.dev/auth/token",service="atcr.io"`,
|
|
},
|
|
{
|
|
name: "secondary domain gets its own service",
|
|
host: "buoy.cr",
|
|
want: `Bearer realm="https://seamark.dev/auth/token",service="buoy.cr"`,
|
|
},
|
|
{
|
|
name: "host with port still matches",
|
|
host: "buoy.cr:443",
|
|
want: `Bearer realm="https://seamark.dev/auth/token",service="buoy.cr"`,
|
|
},
|
|
{
|
|
name: "unconfigured host falls back to the primary",
|
|
host: "elsewhere.example",
|
|
want: `Bearer realm="https://seamark.dev/auth/token",service="atcr.io"`,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
wrapped := BearerChallenge(realm, services)(http.HandlerFunc(
|
|
func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
}))
|
|
req := httptest.NewRequest(http.MethodGet, "/v2/alice/app/blobs/sha256:abc", nil)
|
|
req.Host = tt.host
|
|
w := httptest.NewRecorder()
|
|
|
|
wrapped.ServeHTTP(w, req)
|
|
|
|
if got := w.Header().Get("WWW-Authenticate"); got != tt.want {
|
|
t.Errorf("WWW-Authenticate = %q, want %q", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|