Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 115 additions & 7 deletions .github/mutation/mutants.json
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@
"packages": [
"./fapitest"
],
"run": "^TestRevokeGrantBeforeTheCodeIsRedeemed$",
"run": "^(TestRevokeGrantBeforeTheCodeIsRedeemed|TestRevokeGrantBeforeAnOnlineCodeIsRedeemed)$",
"why": "a code whose grant was revoked before redemption is refused"
},
{
Expand Down Expand Up @@ -1563,8 +1563,8 @@
{
"id": "srv-issuance-rechecks-revocation",
"file": "server/grant_revocation.go",
"old": "if revErr := s.checkGrantNotRevoked(ctx, grant); revErr != nil {\n\t\treturn s.withdrawRefreshToken(",
"new": "if revErr := s.checkGrantNotRevoked(ctx, grant); revErr != nil && false {\n\t\treturn s.withdrawRefreshToken(",
"old": "if revErr == nil {\n\t\treturn nil\n\t}\n\tcause := fmt.Errorf(\"confirm the grant isn't revoked",
"new": "if revErr == nil || true {\n\t\treturn nil\n\t}\n\tcause := fmt.Errorf(\"confirm the grant isn't revoked",
"module": "root",
"packages": [
"./server"
Expand All @@ -1575,8 +1575,8 @@
{
"id": "srv-issuance-recheck-fails-closed",
"file": "server/grant_revocation.go",
"old": "if revErr := s.checkGrantNotRevoked(ctx, grant); revErr != nil {\n\t\treturn s.withdrawRefreshToken(",
"new": "if revErr := s.checkGrantNotRevoked(ctx, grant); revErr != nil && revErr.Code() == ErrorInvalidGrant {\n\t\treturn s.withdrawRefreshToken(",
"old": "if revErr == nil {\n\t\treturn nil\n\t}\n\tcause := fmt.Errorf(\"confirm the grant isn't revoked",
"new": "if revErr == nil || revErr.Code() != ErrorInvalidGrant {\n\t\treturn nil\n\t}\n\tcause := fmt.Errorf(\"confirm the grant isn't revoked",
"module": "root",
"packages": [
"./server"
Expand All @@ -1599,8 +1599,8 @@
{
"id": "srv-withdrawal-failure-keeps-entry",
"file": "server/grant_registry.go",
"old": "if err := s.deps.Grants.RevokeRefreshToken(ctx, refreshTokenHash); err != nil {\n\t\treturn errors.Join(cause, fmt.Errorf(\"withdraw the refresh token: %w\", err))",
"new": "if err := s.deps.Grants.RevokeRefreshToken(ctx, refreshTokenHash); err != nil {\n\t\t_ = s.forgetRevokedRefreshToken(ctx, refreshTokenHash)\n\t\treturn errors.Join(cause, fmt.Errorf(\"withdraw the refresh token: %w\", err))",
"old": "if err := s.deps.Grants.RevokeRefreshToken(ctx, refreshTokenHash); err != nil {\n\t\treturn errors.Join(cause, fmt.Errorf(\"%w: %w\", errRefreshTokenNotWithdrawn, err))",
"new": "if err := s.deps.Grants.RevokeRefreshToken(ctx, refreshTokenHash); err != nil {\n\t\t_ = s.forgetRevokedRefreshToken(ctx, refreshTokenHash)\n\t\treturn errors.Join(cause, fmt.Errorf(\"%w: %w\", errRefreshTokenNotWithdrawn, err))",
"module": "root",
"packages": [
"./server"
Expand Down Expand Up @@ -5070,5 +5070,113 @@
],
"run": "^(TestPermittedArrayStrings|TestAutomaticRegistrationRARTypesAbsentUnderEachOperator)$",
"why": "a one_of alone bounds an omitted array parameter to its listed values, not to nothing"
},
{
"id": "race-invalid-grant-mapping",
"file": "server/grant_registry.go",
"old": "if errors.Is(err, errGrantRevokedWhileIssued) && !errors.Is(err, errRefreshTokenNotWithdrawn) {",
"new": "if errors.Is(err, errGrantRevokedWhileIssued) && !errors.Is(err, errRefreshTokenNotWithdrawn) && false {",
"module": "root",
"packages": [
"./server"
],
"run": "^(TestGrantRevokedWhileIssuedIsInvalidGrant|TestGrantEntryGoneWhileIssuedIsInvalidGrant)$",
"why": "a grant revoked while its refresh token was issued is invalid_grant, not server_error"
},
{
"id": "race-invalid-grant-only-revoked",
"file": "server/grant_registry.go",
"old": "if errors.Is(err, errGrantRevokedWhileIssued) && !errors.Is(err, errRefreshTokenNotWithdrawn) {",
"new": "if (true || errors.Is(err, errGrantRevokedWhileIssued)) && !errors.Is(err, errRefreshTokenNotWithdrawn) {",
"module": "root",
"packages": [
"./server"
],
"run": "^(TestGrantRevocationUncheckableWhileIssuedIsServerError|TestGrantEntryGoneWhileIssuedIsInvalidGrant)$",
"why": "a store failure while issuing a refresh token stays server_error"
},
{
"id": "race-invalid-grant-withdraw-failure",
"file": "server/grant_registry.go",
"old": "if errors.Is(err, errGrantRevokedWhileIssued) && !errors.Is(err, errRefreshTokenNotWithdrawn) {",
"new": "if errors.Is(err, errGrantRevokedWhileIssued) && (true || !errors.Is(err, errRefreshTokenNotWithdrawn)) {",
"module": "root",
"packages": [
"./server"
],
"run": "^(TestGrantRevokedWhileIssuedWithdrawalFailsIsServerError|TestGrantEntryGoneWhileIssuedIsInvalidGrant)$",
"why": "a revoked grant whose refresh token can't be withdrawn is a store failure, server_error: a live token may remain"
},
{
"id": "race-invalid-grant-revocation-found",
"file": "server/grant_revocation.go",
"old": "if revErr.Code() == ErrorInvalidGrant {",
"new": "if revErr.Code() == ErrorInvalidGrant && false {",
"module": "root",
"packages": [
"./server"
],
"run": "^(TestGrantRevokedWhileIssuedIsInvalidGrant|TestCodeReuseWhileIssuedIsInvalidGrant|TestCIBAGrantRevokedWhileIssuedIsInvalidGrant)$",
"why": "the check that the grant isn't revoked marks a revocation found after storing the token as revoked-while-issued"
},
{
"id": "race-invalid-grant-revocation-uncheckable",
"file": "server/grant_revocation.go",
"old": "if revErr.Code() == ErrorInvalidGrant {",
"new": "if revErr.Code() == ErrorInvalidGrant || true {",
"module": "root",
"packages": [
"./server"
],
"run": "^TestGrantRevocationUncheckableWhileIssuedIsServerError$",
"why": "a revocation store that can't answer after the token is stored is server_error, not invalid_grant"
},
{
"id": "race-invalid-grant-ciba",
"file": "server/backchannel_token.go",
"old": "return refreshTokenIssueFailure(err)",
"new": "return newError(ErrorServerError, 500, \"failed to issue refresh token\", err)",
"module": "root",
"packages": [
"./server"
],
"run": "^TestCIBAGrantRevokedWhileIssuedIsInvalidGrant$",
"why": "the CIBA token request answers invalid_grant for a grant revoked while its refresh token was issued"
},
{
"id": "race-invalid-grant-embedder",
"file": "server/embedder_refresh.go",
"old": "err := refreshTokenIssueFailure(issueErr)",
"new": "err := newError(ErrorServerError, 500, \"failed to issue refresh token\", issueErr)",
"module": "root",
"packages": [
"./server"
],
"run": "^TestGrantRevokedWhileIssuedIsInvalidGrant$",
"why": "IssueRefreshToken answers invalid_grant for a grant revoked while its refresh token was issued"
},
{
"id": "race-invalid-grant-code-recorder",
"file": "server/token.go",
"old": "\t\treturn refreshTokenIssueFailure(err)\n\t}\n\taccessToken, accessKey, issueErr",
"new": "\t\treturn newError(ErrorServerError, 500, \"failed to issue refresh token\", err)\n\t}\n\taccessToken, accessKey, issueErr",
"module": "root",
"packages": [
"./server"
],
"run": "^TestCodeReuseWhileIssuedIsInvalidGrant$",
"why": "a code exchange recording its tokens together answers invalid_grant when a reuse revokes the grant mid-issuance"
},
{
"id": "race-invalid-grant-code-optional",
"file": "server/token.go",
"old": "\t\treturn refreshTokenIssueFailure(err)\n\t}\n\t// Recomputed",
"new": "\t\treturn newError(ErrorServerError, 500, \"failed to issue refresh token\", err)\n\t}\n\t// Recomputed",
"module": "root",
"packages": [
"./server"
],
"run": "^TestCodeReuseWhileIssuedIsInvalidGrant$",
"why": "a code exchange issuing its refresh token last answers invalid_grant when a reuse revokes the grant mid-issuance"
}
]
2 changes: 1 addition & 1 deletion GETTING_STARTED.md
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ stored before the store had it, only the record refuses them (one
issued while `RevokeGrant` runs isn't left to the record alone unless
the grant's others are: the issuance checks the grant's revocation
again once it has stored the token, and withdraws it instead if the
record is there by then): raising
record is there by then, answering the token request `invalid_grant`): raising
`Limits.RefreshTokenLifetime` later, or running instances with longer
`Limits`, can't bring a revoked grant back unless a refresh token
lifetime over 400 days applies. With sqlstore, refresh tokens stored
Expand Down
23 changes: 23 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,29 @@ federation relied on Relying Parties that leave the parameter out getting
every type, list those types in the Trust Anchor's or Intermediate's
`one_of`, or have the Relying Parties publish the types they use.

### A grant revoked while its refresh token is issued is `invalid_grant`, not `server_error` (server)

**Affects:** servers issuing refresh tokens (`offline_access`, CIBA,
`IssueRefreshToken`), and clients or monitoring that count their
token errors. No code changes.

**Why:** when the grant was revoked while its refresh token was being
stored, most often a reused authorization code racing the exchange
that redeemed it, the server withdrew the token and answered
`server_error` (HTTP 500) "failed to issue refresh token": nothing was
issued, but the client saw a store outage for what is a refused grant.

**What fails:** nothing is refused that wasn't before. That token
request now answers `invalid_grant` (HTTP 400) "the grant has been
revoked", from the authorization code exchange, the CIBA token
request and `IssueRefreshToken` alike, and its audit event records
`invalid_grant`. A genuine store failure there, including a failure to
withdraw the token, still answers `server_error`.

**What to change:** nothing, unless an alert or client keys on the
500: expect a 400 `invalid_grant` instead, and the client starts a new
authorization as for any other refused grant.

## v0.85.0

### Codes, refresh tokens and `auth_req_id`s are stored under digests covering the issuer (server, storage)
Expand Down
23 changes: 23 additions & 0 deletions fapitest/grant_revocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,29 @@ func TestRevokeGrantBeforeTheCodeIsRedeemed(t *testing.T) {
}
}

// TestRevokeGrantBeforeAnOnlineCodeIsRedeemed: a code whose grant was
// revoked before redemption is refused even when the exchange would
// issue no refresh token, so nothing after redemption re-checks the
// grant: the check before issuance is all that refuses it.
func TestRevokeGrantBeforeAnOnlineCodeIsRedeemed(t *testing.T) {
h := fapitest.New(t, fapitest.Config{Profile: server.ProfileFAPISecurity, GrantID: "grant-1"})
ctx := context.Background()
session, err := h.Client.BeginAuthorization(ctx, client.BeginAuthorizationRequest{Scope: []string{"openid", "accounts"}})
if err != nil {
t.Fatalf("BeginAuthorization: %v", err)
}
callback, err := h.CaptureCallback(ctx, session)
if err != nil {
t.Fatalf("CaptureCallback: %v", err)
}
if err := h.RevokeGrant(ctx, "grant-1"); err != nil {
t.Fatalf("RevokeGrant: %v", err)
}
if _, err := h.RunAuthorizationCodeFlowWithCallback(ctx, session.Handle(), callback); serverErrorCodeOf(err) != "invalid_grant" {
t.Fatalf("code exchange after revocation = %v, want invalid_grant", err)
}
}

// TestRevokeGrantLeavesOtherGrantsAlone covers tokens from a grant with
// no grant ID: revoking a grant ID doesn't touch them, and they carry no
// grant_id claim.
Expand Down
2 changes: 1 addition & 1 deletion server/backchannel_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func (s *Server) issueBackchannelOptionalTokens(ctx context.Context, client stor
if containsScope(grant.Scope, "offline_access") {
refreshToken, err := s.issueRefreshToken(ctx, client.ID(), grant, binding)
if err != nil {
return newError(ErrorServerError, 500, "failed to issue refresh token", err)
return refreshTokenIssueFailure(err)
}
result.RefreshToken = fapi.NewSecret(refreshToken)
result.HasRefreshToken = true
Expand Down
2 changes: 1 addition & 1 deletion server/embedder_refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (s *Server) IssueRefreshToken(ctx context.Context, req IssueRefreshTokenReq
}
raw, issueErr := s.issueRefreshToken(ctx, client.ID(), grant, refreshBinding{Thumbprint: req.Binding.Thumbprint, InstanceKey: req.Client.instanceKey})
if issueErr != nil {
err := newError(ErrorServerError, 500, "failed to issue refresh token", issueErr)
err := refreshTokenIssueFailure(issueErr)
s.audit(ctx, AuditEventIssueRefreshToken, client.ID(), AuditOutcomeFailure, string(err.Code()))
return fapi.Secret{}, err
}
Expand Down
26 changes: 24 additions & 2 deletions server/grant_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,31 @@ func (s *Server) recordGrant(ctx context.Context, clientID fapi.ClientID, grant
}

// errGrantRevokedWhileIssued is issueRefreshToken's failure when the
// grant was revoked while its refresh token was being issued.
// grant was revoked while its refresh token was being issued: either
// check after storing the token, confirmGrantRecorded's or
// confirmGrantNotRevoked's, found the revocation, and the token was
// withdrawn. refreshTokenIssueFailure answers it invalid_grant.
var errGrantRevokedWhileIssued = errors.New("the grant was revoked while its refresh token was being issued")

// errRefreshTokenNotWithdrawn marks withdrawRefreshToken's failure to
// revoke the token it withdraws: a store failure, which may leave a live
// token behind, whatever the cause it's joined to.
var errRefreshTokenNotWithdrawn = errors.New("withdraw the refresh token")

// refreshTokenIssueFailure is the token request's answer when
// issueRefreshToken fails with err: invalid_grant (RFC 6749 §5.2) when
// the grant was revoked while the token was being issued, the token
// withdrawn, as a reused authorization code's racing exchange is; and
// server_error for any other failure, a store's included, and for a
// revocation found when the token couldn't then be withdrawn, which is
// a store failure too.
func refreshTokenIssueFailure(err error) *Error {
if errors.Is(err, errGrantRevokedWhileIssued) && !errors.Is(err, errRefreshTokenNotWithdrawn) {
return newError(ErrorInvalidGrant, 400, "the grant has been revoked", err)
}
return newError(ErrorServerError, 500, "failed to issue refresh token", err)
}

// confirmGrantRecorded checks, after the refresh token stored under
// refreshTokenHash was created, that its grant is still in
// Dependencies.GrantRegistry, when one is set. A revocation that lists
Expand Down Expand Up @@ -161,7 +183,7 @@ func (s *Server) confirmGrantRecorded(ctx context.Context, subject string, refre
// RevokeListedGrant or RevokeSubjectGrants to revoke the token by.
func (s *Server) withdrawRefreshToken(ctx context.Context, refreshTokenHash [32]byte, cause error) error {
if err := s.deps.Grants.RevokeRefreshToken(ctx, refreshTokenHash); err != nil {
return errors.Join(cause, fmt.Errorf("withdraw the refresh token: %w", err))
return errors.Join(cause, fmt.Errorf("%w: %w", errRefreshTokenNotWithdrawn, err))
}
_ = s.forgetRevokedRefreshToken(ctx, refreshTokenHash)
return cause
Expand Down
16 changes: 12 additions & 4 deletions server/grant_revocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ func (s *Server) revokeGrantRefreshTokens(ctx context.Context, revocationKey str
// refreshTokenHash has been created, that grant, the grant it's issued
// from, still isn't revoked, and withdraws the token if it is, or if
// that can't be checked (withdrawRefreshToken), so the token isn't
// issued. The token request checked the grant before, but it may have
// issued: a revocation found is errGrantRevokedWhileIssued, which the
// token request answers invalid_grant (refreshTokenIssueFailure), a
// failure to check the revocation the server_error it is. The token
// request checked the grant before, but it may have
// been revoked since: RevokeGrant, or a reuse of the authorization code,
// records the grant as revoked, then revokes the grant's refresh tokens
// in a GrantStore that can find them (storage.GrantRefreshTokenRevoker),
Expand All @@ -210,10 +213,15 @@ func (s *Server) revokeGrantRefreshTokens(ctx context.Context, revocationKey str
// revocation store only for a grant with a GrantID or code grant ID, as
// checkGrantNotRevoked does.
func (s *Server) confirmGrantNotRevoked(ctx context.Context, grant grantRecord, refreshTokenHash [32]byte) error {
if revErr := s.checkGrantNotRevoked(ctx, grant); revErr != nil {
return s.withdrawRefreshToken(ctx, refreshTokenHash, fmt.Errorf("confirm the grant isn't revoked: %w", revErr))
revErr := s.checkGrantNotRevoked(ctx, grant)
if revErr == nil {
return nil
}
return nil
cause := fmt.Errorf("confirm the grant isn't revoked: %w", revErr)
if revErr.Code() == ErrorInvalidGrant {
cause = fmt.Errorf("%w: %w", errGrantRevokedWhileIssued, cause)
}
return s.withdrawRefreshToken(ctx, refreshTokenHash, cause)
}

// refreshTokenGrantKeys are the storage.NewRefreshToken.GrantKeys of a
Expand Down
Loading
Loading