Skip to content
112 changes: 110 additions & 2 deletions .github/mutation/mutants.json
Original file line number Diff line number Diff line change
Expand Up @@ -1215,8 +1215,8 @@
{
"id": "srv-code-reuse-audit-access-token-failure",
"file": "server/token.go",
"old": "failures = append(failures, s.deps.Revocation.Revoke(ctx, reused.IssuedAccessTokenKey, s.deps.Clock.Now().Add(timeutil.SaturatingAdd(s.cfg.Limits.AccessTokenLifetime, s.cfg.Limits.MaxClockSkew, s.cfg.Limits.MaxClockSkew))))",
"new": "_ = s.deps.Revocation.Revoke(ctx, reused.IssuedAccessTokenKey, s.deps.Clock.Now().Add(timeutil.SaturatingAdd(s.cfg.Limits.AccessTokenLifetime, s.cfg.Limits.MaxClockSkew, s.cfg.Limits.MaxClockSkew)))",
"old": "failures = append(failures, s.deps.Revocation.Revoke(ctx, key, until))",
"new": "_ = s.deps.Revocation.Revoke(ctx, key, until)",
"module": "root",
"packages": [
"./server"
Expand Down Expand Up @@ -4910,5 +4910,113 @@
],
"run": "^TestBackchannelDecisionsAndExpiry$",
"why": "sqlstore wraps ErrBackchannelAuthenticationAlreadyDecided for a second decision"
},
{
"id": "server-code-digest-covers-issuer",
"file": "server/token.go",
"old": "codeHashPrefix + lengthPrefixed(issuer) +",
"new": "codeHashPrefix + lengthPrefixed(\"\") +",
"module": "root",
"packages": [
"./server"
],
"run": "^(TestAnotherIssuersCodeIsLeftUnspent|TestAuthorizationCodeHash)$",
"why": "an authorization code's digest covers the issuer, so another issuer's server sharing the store neither finds nor spends it"
},
{
"id": "server-refresh-digest-covers-issuer",
"file": "server/token.go",
"old": "refreshHashPrefix + lengthPrefixed(issuer) + token",
"new": "refreshHashPrefix + lengthPrefixed(\"\") + token",
"module": "root",
"packages": [
"./server"
],
"run": "^TestAnotherIssuersRefreshTokenIsNotFound$",
"why": "a refresh token's digest covers the issuer, so another issuer's server sharing the store doesn't find it"
},
{
"id": "server-auth-req-id-digest-covers-issuer",
"file": "server/backchannel_token.go",
"old": "backchannelAuthReqIDHashPrefix + lengthPrefixed(issuer) +",
"new": "backchannelAuthReqIDHashPrefix + lengthPrefixed(\"\") +",
"module": "root",
"packages": [
"./server"
],
"run": "^(TestAnotherIssuersAuthReqIDIsLeftUnspent|TestBackchannelAuthReqIDHash)$",
"why": "an auth_req_id's digest covers the issuer, so another issuer's server sharing the store neither polls nor spends it"
},
{
"id": "server-reuse-revokes-namespaced-access-token-key",
"file": "server/token.go",
"old": "range grantrevocation.AccessTokenKeys(s.cfg.Issuer.String(), reused.IssuedAccessTokenKey)",
"new": "range []string{reused.IssuedAccessTokenKey}",
"module": "root",
"packages": [
"./server"
],
"run": "^(TestExchangeAuthorizationCodeReuseRevokesAccessToken|TestExchangeAuthorizationCodeReuseRevokesOpaqueAccessToken)$",
"why": "a reused code's access token is revoked under its issuer-namespaced key"
},
{
"id": "server-reuse-still-revokes-raw-access-token-key",
"file": "server/token.go",
"old": "range grantrevocation.AccessTokenKeys(s.cfg.Issuer.String(), reused.IssuedAccessTokenKey)",
"new": "range grantrevocation.AccessTokenKeys(s.cfg.Issuer.String(), reused.IssuedAccessTokenKey)[:1]",
"module": "root",
"packages": [
"./server"
],
"run": "^(TestExchangeAuthorizationCodeReuseRevokesAccessToken|TestExchangeAuthorizationCodeReuseRevokesOpaqueAccessToken)$",
"why": "during the transition a reused code's access token is still revoked under its raw key, for resource servers not yet upgraded"
},
{
"id": "resource-checks-namespaced-token-key",
"file": "resource/verify.go",
"old": "\treturn grantrevocation.AccessTokenKeys(resolved.Issuer, resolved.Key)\n",
"new": "\treturn []string{resolved.Key}\n",
"module": "root",
"packages": [
"./resource"
],
"run": "^TestVerifyChecksTheTokenRevocationKeys$",
"why": "Verify refuses a token revoked under its issuer-namespaced key"
},
{
"id": "resource-still-checks-raw-token-key",
"file": "resource/verify.go",
"old": "\treturn grantrevocation.AccessTokenKeys(resolved.Issuer, resolved.Key)\n",
"new": "\treturn grantrevocation.AccessTokenKeys(resolved.Issuer, resolved.Key)[:1]\n",
"module": "root",
"packages": [
"./resource"
],
"run": "^TestVerifyChecksTheTokenRevocationKeys$",
"why": "Verify still refuses a token an older server revoked under its raw key"
},
{
"id": "resource-batch-checks-namespaced-token-key",
"file": "resource/verify.go",
"old": "batch.IsAnyRevoked(ctx, append(tokenRevocationKeys(resolved), keys...))",
"new": "batch.IsAnyRevoked(ctx, append([]string{resolved.Key}, keys...))",
"module": "root",
"packages": [
"./resource"
],
"run": "^(TestVerifyChecksTheTokenRevocationKeys|TestVerifyChecksRevocationInOneBatch)$",
"why": "a batch revocation check covers the token's issuer-namespaced key, so its 'none revoked' answer can't pass a revoked token"
},
{
"id": "grantrevocation-access-token-key-length-prefix",
"file": "internal/grantrevocation/grantrevocation.go",
"old": "\"access-token:\" + strconv.Itoa(len(issuer)) + \":\" + issuer + key",
"new": "\"access-token:\" + strconv.Itoa(0*len(issuer)) + \":\" + issuer + key",
"module": "root",
"packages": [
"./internal/grantrevocation"
],
"run": "^TestAccessTokenKeysAreNamespacedByIssuer$",
"why": "the issuer's length prefix splits an access token revocation key into issuer and token key one way only"
}
]
85 changes: 85 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,91 @@ Most production-assurance changes only affect `Config.Assurance =
AssuranceProduction`. A development-assurance setup built on `memstore`
and `keys/ephemeral` needs only the steps not marked *production only*.

## v0.85.0

### Codes, refresh tokens and `auth_req_id`s are stored under digests covering the issuer (server, storage)

**Affects:** every server, once, at deploy. Most visibly a server
issuing refresh tokens (`offline_access`). No code changes.

**Why:** the digest an authorization code, a refresh token or a CIBA
`auth_req_id` is stored under (`NewAuthorizationCode.CodeHash`,
`NewRefreshToken.TokenHash`, `NewBackchannelAuthentication.AuthReqIDHash`)
didn't include the server's issuer. Two servers of different issuers
sharing one store found each other's records: the store spent the code
or the CIBA approval (or reported a code reused, revoking what it had
issued) before the server could refuse the record as another issuer's.
So one issuer's client could burn a code or `auth_req_id` the other
issued. Each digest is now taken over the issuer as well, behind a new
domain-separation prefix: a code, refresh token or `auth_req_id`
presented to a server of another issuer is unknown to the store, and
neither found nor consumed.

**What fails:** every value stored before the upgrade is under the old
digest, which the upgraded server no longer computes, so:

- **Every refresh token issued before the upgrade stops working.**
`RefreshAccessToken` answers `invalid_grant` ("refresh_token is
invalid, expired, or already used"), as for a token that never
existed, and `RevokeToken` treats it as unknown too. Clients must get a new one through a new
authorization: the end user signs in (and consents) again. Access
tokens already issued are unaffected and run out their lifetime.
- Authorization codes in flight at deploy (issued, not yet redeemed)
fail once: the exchange answers `invalid_grant` and the client starts
the authorization again. Codes live `Limits.AuthorizationCodeLifetime`
(a minute at most). A pre-upgrade code presented again after the
upgrade is unknown rather than reused, so it no longer revokes the
tokens issued from it; those tokens were issued at most a code
lifetime before the upgrade, and expire as usual.
- CIBA requests in flight at deploy fail once: a poll answers
`invalid_grant` and the client starts a new request.
- Rows a `GrantStore`, `BackchannelAuthenticationStore` or
`GrantRegistry` holds for the old digests are no longer reachable
through the server and expire as usual. `ListGrants` may still list
a pre-upgrade grant until its entry expires, and `RevokeListedGrant`
still revokes it.

**What to change:** nothing in code, and no store migration: `memstore`,
`sqlstore` and any custom store key records by whatever digest the
server hands them. Plan the deploy: expect every refresh-token holder
to re-authorize, and tell client operators (or their users) that a
one-time sign-in is coming. A store shared by servers of different
issuers needs nothing more; each server now sees only its own records.

### A reused code's access token is revoked under an issuer-namespaced key too (server, resource)

**Affects:** a deployment with its own `server.RevocationSink` or
`resource.RevocationChecker` that does more than store and compare keys
exactly, such as one that only accepts keys shaped like a jti or an
opaque token's hex hash, or limits a key's length.

**Why:** the server revoked a reused code's access token under the
token's own key alone (a JWT's `jti`, an opaque token's hex SHA-256).
With servers of different issuers sharing one revocation store, one
issuer's revoked `jti` would refuse another issuer's token with the same
`jti`. The server now also revokes it under that key namespaced by its
issuer: `access-token:`, the issuer's length in bytes, `:`, the issuer,
then the token's key. It still writes the raw key as well, so a resource
server that hasn't been upgraded, or a checker that knows only the raw
form, keeps refusing the token. `resource.Verifier` asks about the
namespaced key (when the resolver reports the token's issuer) and then
the raw one, so a record an older server wrote still refuses the token.
A later release will stop writing and checking the raw key.

**What fails:** nothing for `memstore`, `sqlstore` or any store that
keeps keys as given and compares them exactly; such a store holds one
more record per reused code. A sink that refuses the new key makes a
code reuse's revocation report a failure
(`AuditEventRevokeReusedAuthorizationCode` with
`AuditOutcomeFailure`), though the raw key is still written.

**What to change:** make a custom sink accept any key (an opaque
token's namespaced key runs about 30 bytes longer than the
`code-grant:` keys it already stores for the same issuer), and a custom
checker answer the namespaced key from what the sink stored, as it
answers every other key. Don't rely on the raw key alone: it will be
dropped.

## v0.84.0

### A JWT access token's `client_sub` claim no longer sets `ClientSubject` (resource)
Expand Down
34 changes: 31 additions & 3 deletions internal/grantrevocation/grantrevocation.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
// Package grantrevocation is how a revoked grant is recorded and found:
// shared by server, which revokes grants and refuses their codes and
// refresh tokens, and resource, which refuses their access tokens.
// Package grantrevocation is how a revoked grant, or a revoked access
// token, is recorded and found: shared by server, which revokes grants
// and access tokens and refuses the grants' codes and refresh tokens,
// and resource, which refuses their access tokens.
package grantrevocation

import (
"crypto/sha256"
"encoding/base64"
"strconv"
)

// AccessTokenKey is the revocation-store key recording one access token
// issuer issued, whose own revocation key (a JWT's jti, an opaque
// token's hex SHA-256: resource.ResolvedAccessToken.Key) is key, as
// revoked: "access-token:", the issuer's length in bytes, ":", the
// issuer, then key. The prefix keeps it apart from Key's and CodeKey's,
// and the issuer keeps one issuer's tokens apart from another's when
// servers with different issuers share the store: one issuer's revoked
// jti no longer refuses another issuer's token with the same jti. The
// length prefix splits the key into issuer and token key one way only,
// whatever either holds.
func AccessTokenKey(issuer, key string) string {
return "access-token:" + strconv.Itoa(len(issuer)) + ":" + issuer + key
}

// AccessTokenKeys are the keys a server writes, and IsRevoked checks,
// for the access token whose own revocation key is key: AccessTokenKey,
// then key itself, the only form servers before v0.84.0 wrote and that
// a custom RevocationChecker may still look for. Both are written so a
// resource server not yet upgraded, or a checker that only knows the
// raw form, still refuses a token a new server revoked; both are checked
// so a record an older server wrote still refuses the token. The raw
// form will be dropped in a later release.
func AccessTokenKeys(issuer, key string) []string {
return []string{AccessTokenKey(issuer, key), key}
}

// Claim is the access token claim carrying the grant ID — the name the
// OAuth Grant Management draft (FAPI working group) uses for the same
// concept.
Expand Down
23 changes: 23 additions & 0 deletions internal/grantrevocation/grantrevocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,26 @@ func TestKeysAreNamespacedByIssuer(t *testing.T) {
})
}
}

func TestAccessTokenKeysAreNamespacedByIssuer(t *testing.T) {
const a, b = "https://a.example.com", "https://b.example.com"
if got, want := AccessTokenKey(a, "jti-1"), "access-token:21:"+a+"jti-1"; got != want {
t.Errorf("AccessTokenKey = %q, want %q", got, want)
}
if AccessTokenKey(a, "jti-1") == AccessTokenKey(b, "jti-1") {
t.Error("two issuers' tokens of one key share a revocation key")
}
// The length prefix splits issuer and key one way only, whatever
// either holds.
if AccessTokenKey("https://a.example.com/x", "y") == AccessTokenKey("https://a.example.com/", "xy") {
t.Error("two splits of the same text between issuer and key share a revocation key")
}
for _, other := range []string{Key(a, "jti-1"), CodeKey(a, "jti-1"), "jti-1"} {
if AccessTokenKey(a, "jti-1") == other {
t.Errorf("AccessTokenKey equals %q", other)
}
}
if got := AccessTokenKeys(a, "jti-1"); len(got) != 2 || got[0] != AccessTokenKey(a, "jti-1") || got[1] != "jti-1" {
t.Errorf("AccessTokenKeys = %q, want [the namespaced key, the raw key]", got)
}
}
3 changes: 2 additions & 1 deletion resource/accesstoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ type ResolvedAccessToken struct {
// Key is this token's revocation-lookup identifier — a JWT's jti
// claim for JWTAccessTokens, an opaque token's own hash for
// OpaqueAccessTokens — passed to Dependencies.Revocation.IsRevoked
// exactly as returned; its shape is an implementation detail
// exactly as returned, and also namespaced by Issuer when Issuer is
// set (see RevocationChecker); its shape is an implementation detail
// specific to each AccessTokenResolver. Required: Verify answers a
// resolved token with no Key as 500 server_error, since revocation
// couldn't apply to it.
Expand Down
22 changes: 18 additions & 4 deletions resource/revocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,28 @@ import (
type RevocationChecker interface {
// IsRevoked reports whether key has been revoked. Verify asks about
// two kinds of key, which must both be answered from what the
// server recorded: the token's own key (ResolvedAccessToken.Key —
// a JWT's jti claim for JWTAccessTokens, an opaque token's own hash
// for OpaqueAccessTokens), and, for a token carrying a grant_id
// server recorded: the token's own key, and, for a token carrying a grant_id
// claim, the key server.Server.RevokeGrant records that grant under,
// and for one carrying a code_grant_id claim, the key the server
// records when the authorization code the token's grant came from
// is reused. Run storage.TestRevocationStoreContract against an
// implementation. An implementation that is also a
// implementation.
//
// The token's own key comes in two forms, and Verify asks about
// both. ResolvedAccessToken.Key is the raw form: a JWT's jti claim
// for JWTAccessTokens, an opaque token's own hash (hex SHA-256) for
// OpaqueAccessTokens. A server since v0.84.0 also records it
// namespaced by its issuer, as "access-token:", the length of the
// issuer in bytes, ":", the issuer, then the raw key (for issuer
// "https://as.example" and jti "j1",
// "access-token:18:https://as.example" followed by "j1"), so a jti
// one issuer revoked doesn't refuse another issuer's token sharing
// the store. Verify asks about the namespaced form first, when the
// resolver reports the token's Issuer, then the raw one. The server
// writes both for now, so a checker that only knows the raw form
// keeps working; the raw form will be dropped in a later release,
// so a checker that derives or filters keys itself should accept
// the namespaced one too. An implementation that is also a
// storage.RevocationBatchChecker is asked about all of a token's
// keys in one IsAnyRevoked call instead.
IsRevoked(ctx context.Context, key string) (bool, error)
Expand Down
7 changes: 4 additions & 3 deletions resource/revocation_batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@ type batchRevocationCase struct {
// batchRevocationClaims names, in the order Verify asks about them.
var (
batchRevocationClaims = map[string]json.RawMessage{"grant_id": json.RawMessage(`"g-1"`), "code_grant_id": json.RawMessage(`"cg-1"`)}
batchRevocationKeys = []string{"token-1", "grant:g-1@" + testIssuer, "grant:g-1", "code-grant:cg-1@" + testIssuer, "code-grant:cg-1"}
batchRevocationKeys = []string{"access-token:22:" + testIssuer + "token-1", "token-1", "grant:g-1@" + testIssuer, "grant:g-1", "code-grant:cg-1@" + testIssuer, "code-grant:cg-1"}
)

// TestVerifyChecksRevocationInOneBatch: with a
// storage.RevocationBatchChecker, a token carrying grant_id and
// code_grant_id is checked in one IsAnyRevoked call covering its own key
// and every grant key, current and pre-v0.61.0; anything but "none
// code_grant_id is checked in one IsAnyRevoked call covering its own
// keys, namespaced by issuer and raw, and every grant key, current and
// pre-v0.61.0; anything but "none
// revoked" falls back to the IsRevoked calls, so each outcome keeps the
// response it has without a batch checker, and a batch answer of
// revoked is never overturned.
Expand Down
Loading
Loading