From ad75bfcef78cda09f7ed8839e211d34faf6536de Mon Sep 17 00:00:00 2001 From: rgarcia <72655+rgarcia@users.noreply.github.com> Date: Fri, 27 Nov 2026 19:57:51 +0000 Subject: [PATCH] Hold access tokens to image scopes on the registry The registry under /v2 let every valid API token push and pull without looking at its permissions, so an access token an authorization server issued for, say, instance:read could push images. An access token now needs image:read to pull, image:read and image:write to push, and image:delete for a DELETE request, and is refused with 403 DENIED without the scope. Full access is unchanged: a token without a permissions claim and the wildcard hold all three. A token signed with jwt_secret keeps the registry access it has today, whatever its permissions. Scoped tokens in use push and pull, and they cannot be told apart from new ones. --- docs/oauth-access-tokens.md | 6 +++-- lib/jwtauth/verifier.go | 8 ++++++- lib/middleware/oapi_auth.go | 15 ++++++++++++ lib/middleware/oapi_auth_test.go | 39 +++++++++++++++++++++++++++++++- lib/scopes/README.md | 8 ++++--- 5 files changed, 69 insertions(+), 7 deletions(-) diff --git a/docs/oauth-access-tokens.md b/docs/oauth-access-tokens.md index bf7b90f96..3ddbbf6b8 100644 --- a/docs/oauth-access-tokens.md +++ b/docs/oauth-access-tokens.md @@ -43,8 +43,10 @@ Access tokens follow the JWT format of [RFC 9068](https://www.rfc-editor.org/rfc A token is at most 16 KB. Its lifetime is the authorization server's to choose. A token without a `scope` has no permissions. This differs from a token signed with `jwt_secret`, -where a missing `permissions` claim means full access. As for every API token, the registry under -`/v2` does not check permissions. +where a missing `permissions` claim means full access. The registry under `/v2` checks an access +token's permissions too: `image:read` to pull, `image:read` and `image:write` to push, and +`image:delete` for a DELETE request. A token signed with `jwt_secret` can do all of it whatever +its permissions. `revoked_token_hashes` applies to access tokens as it does to the others. Revoking a token signed with ES256, ES384 or ES512 also revokes the second token string that carries the same signature. diff --git a/lib/jwtauth/verifier.go b/lib/jwtauth/verifier.go index 6f395b050..eb246dd2c 100644 --- a/lib/jwtauth/verifier.go +++ b/lib/jwtauth/verifier.go @@ -56,7 +56,7 @@ func (v *Verifier) ParseWithClaims(ctx context.Context, tokenString string, clai // A token that declares itself an access token is never checked against // the HMAC keys, and no other token is checked against an authorization // server's keys. - if v.accessTokens.Enabled() && accesstoken.Typed(tokenString) { + if v.IsAccessToken(tokenString) { return v.parseAccessToken(ctx, tokenString, claims) } if len(v.keys) == 0 { @@ -86,6 +86,12 @@ func (v *Verifier) ParseWithClaims(ctx context.Context, tokenString string, clai return nil, err } +// IsAccessToken reports whether ParseWithClaims checks tokenString against an +// authorization server's keys rather than the HMAC keys. +func (v *Verifier) IsAccessToken(tokenString string) bool { + return v.accessTokens.Enabled() && accesstoken.Typed(tokenString) +} + // parseAccessToken verifies an authorization server's access token and hands // its claims to the caller in the shape of an HMAC token's: the scope becomes // the permissions list. The list is always present, so such a token is never diff --git a/lib/middleware/oapi_auth.go b/lib/middleware/oapi_auth.go index 5f5cfa363..f7af54e6c 100644 --- a/lib/middleware/oapi_auth.go +++ b/lib/middleware/oapi_auth.go @@ -439,6 +439,21 @@ func JwtAuth(verifier *jwtauth.Verifier, revoked *tokenrevocation.List) func(htt "auth_type", authType, "subject", userClaims["sub"]) ctx := contextWithUserClaims(r.Context(), userClaims) + // Tokens signed with the server's secret have always been able + // to push and pull whatever their permissions, and tokens in + // use rely on it, so only access tokens are held to theirs. + required := scopes.ImageRead + if r.Method == http.MethodDelete { + required = scopes.ImageDelete + } else if isWriteOperation(r.Method) { + required = scopes.ImageWrite + } + if verifier.IsAccessToken(token) && !scopes.HasScope(ctx, required) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusForbidden) + fmt.Fprintf(w, `{"errors":[{"code":"DENIED","message":"missing required scope: %s"}]}`, required) + return + } next.ServeHTTP(w, requestWithoutAuthorization(r, ctx)) return } diff --git a/lib/middleware/oapi_auth_test.go b/lib/middleware/oapi_auth_test.go index 2360eefa1..5b290f1c4 100644 --- a/lib/middleware/oapi_auth_test.go +++ b/lib/middleware/oapi_auth_test.go @@ -289,6 +289,43 @@ func TestJwtAuth_RegistryPathAcceptsUserTokens(t *testing.T) { }) } +// The registry holds an access token to its permissions. A token signed with +// the server's secret pushes and pulls whatever its permissions, as before. +func TestJwtAuth_RegistryPermissions(t *testing.T) { + server := accesstokentest.NewServer(t) + verifier := newTestVerifier(t, testJWTSecret).WithAccessTokens(server.Verifier(t)) + handler := JwtAuth(verifier, nil)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + })) + accessToken := func(scope string) string { + return server.Token(t, accesstokentest.Claims("federation:ci", scope), nil) + } + const ok, denied = http.StatusOK, http.StatusForbidden + for _, tt := range []struct { + name, token string + pull, push, drop int + }{ + {"secret token with full access", generateUserToken(t, "user"), ok, ok, ok}, + {"secret token scoped to other resources", generateScopedToken(t, "user", []string{"instance:read"}), ok, ok, ok}, + {"access token with image:read", accessToken("image:read"), ok, denied, denied}, + {"access token with image:write", accessToken("image:write"), denied, ok, denied}, + {"access token with image:delete", accessToken("image:delete"), denied, denied, ok}, + {"access token with the wildcard", accessToken("*"), ok, ok, ok}, + {"access token scoped to other resources", accessToken("instance:read"), denied, denied, denied}, + } { + for method, want := range map[string]int{http.MethodGet: tt.pull, http.MethodPut: tt.push, http.MethodDelete: tt.drop} { + req := httptest.NewRequest(method, "/v2/test-image/manifests/latest", nil) + req.Header.Set("Authorization", "Bearer "+tt.token) + rr := httptest.NewRecorder() + handler.ServeHTTP(rr, req) + assert.Equal(t, want, rr.Code, "%s: %s", tt.name, method) + if want == denied { + assert.Contains(t, rr.Body.String(), `"code":"DENIED"`) + } + } + } +} + func TestJwtAuth_RegistryUnauthorizedResponse(t *testing.T) { nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) @@ -670,7 +707,7 @@ func TestJwtAuth_AccessTokens(t *testing.T) { assert.Equal(t, "false", rr.Header().Get("X-Full-Access")) assert.Equal(t, "false", rr.Header().Get("X-Instance-Read")) - // The registry accepts it as it accepts any API token. + // The registry accepts it with the scope the request needs. rr = serve("/v2/test-image/manifests/latest", server.Token(t, accesstokentest.Claims("federation:ci", "image:read"), nil)) require.Equal(t, http.StatusOK, rr.Code) assert.Equal(t, "federation:ci", rr.Header().Get("X-User-ID")) diff --git a/lib/scopes/README.md b/lib/scopes/README.md index a38040129..470cd9b2f 100644 --- a/lib/scopes/README.md +++ b/lib/scopes/README.md @@ -24,9 +24,11 @@ Scopes follow the pattern `resource:action`. Each resource type supports `read`, | Scope | Grants access to | |---|---| -| `image:read` | List images, get image details | -| `image:write` | Pull/create images | -| `image:delete` | Delete images | +| `image:read` | List images, get image details; pull from the registry under `/v2` | +| `image:write` | Pull/create images; push to the registry under `/v2`, which needs `image:read` too | +| `image:delete` | Delete images; delete from the registry under `/v2` | + +The registry holds only [access tokens from an OAuth authorization server](../../docs/oauth-access-tokens.md) to these scopes. A token signed with `jwt_secret`, such as one `hypeman-token` mints, could always use the registry whatever its scopes, and keeps that access. ### Volumes