Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
a672066
feat(coderd/rbac): compare scopes by permission coverage
BobbyHo Aug 14, 2026
4315706
Merge branch 'main' into plat479-1-rbac-scope-coverage
BobbyHo Aug 17, 2026
787c461
docs(coderd/rbac): shorten the ScopesCover doc comment
BobbyHo Aug 17, 2026
2d39e04
Merge branch 'main' into plat479-1-rbac-scope-coverage
BobbyHo Aug 18, 2026
2f6c44e
fix(coderd/rbac): guard allowed-side org and user permissions
BobbyHo Aug 18, 2026
bd40270
refactor(coderd/rbac): drop the unreachable negative skip in coverage
BobbyHo Aug 18, 2026
3139c54
test(coderd/rbac): pin the scope coverage table's weak assertions
BobbyHo Aug 18, 2026
9276bb8
refactor(coderd/rbac): make the coverage guards reachable from tests
BobbyHo Aug 18, 2026
865eb9a
refactor(coderd/rbac): share the alias table and name the canonical c…
BobbyHo Aug 18, 2026
26a6bed
docs(coderd/rbac): document the expansion invariant where it can be b…
BobbyHo Aug 18, 2026
7cca7b3
Merge branch 'main' into plat479-1-rbac-scope-coverage
BobbyHo Aug 19, 2026
1678a77
test(coderd/rbac): pin the coverage guards at one strength
BobbyHo Aug 19, 2026
aba3c6c
fix(coderd/rbac): name the scope once in expansion errors
BobbyHo Aug 19, 2026
9a08105
docs(coderd/rbac): correct the external scope list contract
BobbyHo Aug 19, 2026
2fcce8b
docs(coderd/rbac): trim the restated coverage invariant
BobbyHo Aug 19, 2026
a775a48
docs(coderd/rbac): correct the negative permission cross-reference
BobbyHo Aug 19, 2026
e0c0d4a
docs(coderd/rbac): name every category IsExternalScope admits
BobbyHo Aug 19, 2026
189740d
test(coderd/rbac): pin the alias list invariants on the alias table
BobbyHo Aug 19, 2026
7d08e49
Merge branch 'main' into plat479-1-rbac-scope-coverage
BobbyHo Aug 19, 2026
ca4dc52
feat(coderd/oauth2provider): negotiate and persist authorization scope
BobbyHo Aug 14, 2026
f402e1a
feat(site): state the negotiated scope on the OAuth2 consent page
BobbyHo Aug 14, 2026
28ef5ba
fix(coderd/oauth2provider): return invalid_scope to the client's call…
BobbyHo Aug 14, 2026
62950d3
test(coderd/rbac): close two mutation gaps in scope coverage tests
BobbyHo Aug 20, 2026
554a703
Merge branch 'main' into plat479-2-negotiate-scope
BobbyHo Aug 20, 2026
09bc1b4
refactor(coderd/oauth2provider): rename validateRequestedScope to neg…
BobbyHo Aug 21, 2026
6fe6c71
test(coderd/oauth2provider): bind the wire-level scope reasons to the…
BobbyHo Aug 21, 2026
e296dda
refactor(coderd): log undecidable oauth2 scope coverage instead of re…
BobbyHo Aug 21, 2026
2760db5
fix(coderd/oauth2provider): name the stored allowlist when none of it…
BobbyHo Aug 21, 2026
5b2ca57
refactor(coderd/oauth2provider): reword the oauth2 scope rejection re…
BobbyHo Aug 21, 2026
90a6f83
docs: correct what the oauth2 authorize scope parameter promises
BobbyHo Aug 21, 2026
77654a1
test(coderd/oauth2provider): fold the no-allowlist guarantee into one…
BobbyHo Aug 21, 2026
dfdd33f
Merge branch 'plat479-2-negotiate-scope' into plat479-3-report-negoti…
BobbyHo Aug 22, 2026
ba8a123
Merge branch 'main' into plat479-2-negotiate-scope
BobbyHo Aug 22, 2026
bc40908
test(coderd): close the consent-page and state-echo test gaps
BobbyHo Aug 23, 2026
cb7df2e
fix(site/static): left-align the consent permission list
BobbyHo Aug 23, 2026
41e9f40
fix(coderd/oauth2provider): validate the callback scheme before any r…
BobbyHo Aug 23, 2026
a56cdac
fix(coderd/oauth2provider): replace callback query params instead of …
BobbyHo Aug 23, 2026
009962c
feat: say that the consent permission names are technical
BobbyHo Aug 23, 2026
f983335
fix: state unrestricted OAuth2 consent in its own field
BobbyHo Aug 23, 2026
81bbfb0
docs: trim OAuth2 scope negotiation comments
BobbyHo Aug 23, 2026
b7f88f0
Merge branch 'plat479-2-negotiate-scope' into plat479-3-report-negoti…
BobbyHo Aug 23, 2026
2a2e006
Merge branch 'main' into plat479-2-negotiate-scope
BobbyHo Aug 23, 2026
a11d42c
test(coderd/oauth2provider): pin the consent list semantics and the s…
BobbyHo Aug 23, 2026
eb37c2f
test(coderd/oauth2provider): cover the error redirect in the query-pa…
BobbyHo Aug 23, 2026
a5e3f94
fix(coderd/oauth2provider): answer 500 and log on an unusable callbac…
BobbyHo Aug 23, 2026
60cbe59
fix: refuse to render a consent page for a grant with no permissions
BobbyHo Aug 23, 2026
fdc9532
docs: document invalid_scope rejections on the authorize endpoint
BobbyHo Aug 23, 2026
e63ff3a
docs(coderd): trim the scope test comments
BobbyHo Aug 25, 2026
a9ade4a
Merge branch 'plat479-2-negotiate-scope' into plat479-3-report-negoti…
BobbyHo Aug 25, 2026
5589bf6
docs: trim the consent page and error redirect comments
BobbyHo Aug 25, 2026
24b4f46
docs(coderd): drop the godoc-style comments from the scope tests
BobbyHo Aug 25, 2026
b92b7f9
docs(coderd/oauth2provider): trim the authorize.go comments
BobbyHo Aug 25, 2026
e4cc41d
Merge branch 'plat479-2-negotiate-scope' into plat479-3-report-negoti…
BobbyHo Aug 25, 2026
20caa9c
docs(coderd): trim the scope reporting test comments
BobbyHo Aug 25, 2026
c7d3d53
docs(site): trim the consent page scope comments
BobbyHo Aug 25, 2026
98545a6
feat(coderd): mint OAuth2 tokens with the negotiated scope
BobbyHo Aug 15, 2026
641e45e
test(coderd/oauth2provider): cover the negotiated scope end to end
BobbyHo Aug 18, 2026
1a30a9c
feat(coderd/oauth2provider): report the granted scope in token responses
BobbyHo Aug 18, 2026
8b46823
refactor(coderd/oauth2provider): clarify the unmintable scope sentinel
BobbyHo Aug 18, 2026
da5b590
test(coderd/oauth2provider): derive the exchange query from authorize…
BobbyHo Aug 18, 2026
fc6eb77
docs: scope the OAuth2 refresh limitation to narrowing
BobbyHo Aug 18, 2026
589b8b8
docs(coderd): trim the negotiated scope comments
BobbyHo Aug 25, 2026
78fbab1
Merge branch 'main' into plat479-2-negotiate-scope
BobbyHo Aug 26, 2026
f675cf1
fix(site/static): make the whole consent page reachable by scrolling
BobbyHo Aug 27, 2026
6b5f01d
Merge branch 'plat479-3-report-negotiated-scope' into plat480-1-apply…
BobbyHo Aug 27, 2026
90821d8
docs: say the negotiated scope now bounds the issued token
BobbyHo Aug 27, 2026
72b8cb5
docs: annotate the token endpoint with the response it returns
BobbyHo Aug 27, 2026
ceb3997
Update coderd/oauth2provider/authorize.go
BobbyHo Aug 31, 2026
2cd9b60
Update coderd/oauth2provider/authorize.go
BobbyHo Aug 31, 2026
d160bda
docs(coderd/oauth2provider): trim the errNoGrantableScope comment
BobbyHo Aug 31, 2026
b60eee6
refactor(coderd/oauth2provider): canonicalize scopes before the catal…
BobbyHo Aug 31, 2026
4732d1f
refactor(coderd/oauth2provider): canonicalize the allowlist in one pass
BobbyHo Aug 31, 2026
03df6a9
test(coderd/rbac): pin the field sets scope coverage reads
BobbyHo Aug 31, 2026
a2d2856
docs(coderd/rbac): trim the TestCoverageModelFields comment
BobbyHo Aug 31, 2026
d83636b
Merge branch 'plat479-2-negotiate-scope' into plat479-3-report-negoti…
BobbyHo Aug 31, 2026
037e744
Merge remote-tracking branch 'origin/main' into plat479-3-report-nego…
BobbyHo Aug 31, 2026
cd35b94
Merge branch 'plat479-3-report-negotiated-scope' into plat480-1-apply…
BobbyHo Aug 31, 2026
433e449
docs(coderd/oauth2provider): trim scope negotiation comments
BobbyHo Aug 31, 2026
5dffb74
Merge branch 'plat479-3-report-negotiated-scope' into plat480-1-apply…
BobbyHo Aug 31, 2026
6aa7510
docs(coderd/oauth2provider): trim scope negotiation comments
BobbyHo Aug 31, 2026
35ce008
Merge branch 'main' into plat479-3-report-negotiated-scope
BobbyHo Aug 31, 2026
48d9c7f
fix(coderd/oauth2provider): return server_error for undecidable scope…
BobbyHo Sep 1, 2026
c8a3754
Merge branch 'plat479-3-report-negotiated-scope' into plat480-1-apply…
BobbyHo Sep 1, 2026
6b29b13
Merge branch 'main' into plat480-1-apply-negotiated-scope
BobbyHo Sep 1, 2026
976335d
fix: answer invalid_grant for an unmintable stored scope
BobbyHo Sep 1, 2026
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
63 changes: 37 additions & 26 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 31 additions & 26 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions coderd/database/modelmethods.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,10 @@ func (s APIKeyScopes) Has(target APIKeyScope) bool {
}

// expandRBACScope merges the permissions of all scopes in the list into a
// single RBAC scope. If the list is empty, it defaults to rbac.ScopeAll for
// backward compatibility. This method is internal; use ScopeSet() to combine
// scopes with the API key's allow list for authorization.
// single RBAC scope. An empty list is an error rather than rbac.ScopeAll, which
// would widen a key rather than fail it. This method is internal; use
// ScopeSet() to combine scopes with the API key's allow list for authorization.
func (s APIKeyScopes) expandRBACScope() (rbac.Scope, error) {
// Default to ScopeAll for backward compatibility when no scopes provided.
if len(s) == 0 {
return rbac.Scope{}, xerrors.New("no scopes provided")
}
Expand Down
2 changes: 1 addition & 1 deletion coderd/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (api *API) postOAuth2ProviderAppAuthorize() http.HandlerFunc {
// @Param code_verifier formData string false "PKCE code verifier, required if grant_type=authorization_code. 43-128 characters per RFC 7636."
// @Param refresh_token formData string false "Refresh token, required if grant_type=refresh_token"
// @Param grant_type formData codersdk.OAuth2ProviderGrantType true "Grant type"
// @Success 200 {object} oauth2.Token
// @Success 200 {object} codersdk.OAuth2TokenResponse
// @Router /oauth2/tokens [post]
func (api *API) postOAuth2ProviderAppToken() http.HandlerFunc {
return oauth2provider.Tokens(api.Database, api.DeploymentValues.Sessions)
Expand Down
6 changes: 3 additions & 3 deletions coderd/oauth2provider/authorize.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func noScopeAllowlist(appScope sql.NullString) bool {
// allowlist. A rejection is an RFC 6749 §4.1.2.1 invalid_scope.
//
// allowlist request result
// absent absent ApiKeyScopeCoderAll, the pre-enforcement grant
// absent absent ApiKeyScopeCoderAll, an unrestricted grant
// absent present the request
// present absent the allowlist, catalog-filtered (RFC 6749 §3.3 default)
// present present the request, once shown to be within the allowlist
Expand Down Expand Up @@ -486,8 +486,8 @@ func ProcessAuthorize(db database.Store, logger slog.Logger) http.HandlerFunc {
StateHash: hashOAuth2State(params.state),
RedirectUri: sql.NullString{String: params.redirectURL.String(), Valid: params.redirectURIProvided},
// The negotiated scope, not the requested one. The exchange
// copies it onto the token row but not yet onto the API key it
// mints, so this records what was agreed, not what is enforced.
// copies it onto the token row and the API key it mints, so
// this bounds the issued token.
Scope: grantedScope,
})
if err != nil {
Expand Down
56 changes: 53 additions & 3 deletions coderd/oauth2provider/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"net/url"
"slices"
"strings"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -39,8 +40,32 @@ var (
// errConflictingClientAuth means the client provided credentials in both the
// request body and HTTP Basic, but they did not match.
errConflictingClientAuth = xerrors.New("conflicting client authentication")
// errUnmintableScope means the scope stored on a grant names something no
// API key can be minted from.
errUnmintableScope = xerrors.New("scope is not a valid API key scope")
)

// scopeStringToAPIKeyScopes converts a grant's stored scope into the scope list
// an API key is minted with. Names are checked here, not in apikey.Generate,
// whose error would surface as a 500. An empty list is an error rather than an
// unrestricted key.
func scopeStringToAPIKeyScopes(scope string) (database.APIKeyScopes, error) {
names := strings.Fields(scope)
if len(names) == 0 {
return nil, xerrors.Errorf("'%s': %w", scope, errUnmintableScope)
Comment thread
BobbyHo marked this conversation as resolved.
}

scopes := make(database.APIKeyScopes, 0, len(names))
for _, name := range names {
s := database.APIKeyScope(name)
if !s.Valid() {
return nil, xerrors.Errorf("'%s': %w", name, errUnmintableScope)
}
scopes = append(scopes, s)
}
return scopes, nil
}

// extractTokenRequest parses and validates the /oauth2/tokens form. It takes
// the app because whether client_secret is required depends on the client
// type.
Expand Down Expand Up @@ -227,6 +252,14 @@ func Tokens(db database.Store, lifetimes codersdk.SessionLifetime) http.HandlerF
httpapi.WriteOAuth2Error(ctx, rw, http.StatusBadRequest, codersdk.OAuth2ErrorCodeInvalidGrant, "The refresh token is invalid or expired")
return
}
if errors.Is(err, errUnmintableScope) {
// Not invalid_scope: RFC 6749 §5.2 scopes that to what the client
// requested, and this value is stored state the client cannot
// change by asking differently. The grant is what is unusable, and
// re-authorizing is the only way out, so invalid_grant.
httpapi.WriteOAuth2Error(ctx, rw, http.StatusBadRequest, codersdk.OAuth2ErrorCodeInvalidGrant, err.Error())
return
}
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to exchange token",
Expand Down Expand Up @@ -375,21 +408,29 @@ func authorizationCodeGrant(ctx context.Context, db database.Store, app database
return codersdk.OAuth2TokenResponse{}, err
}

// Without this the key defaults to coder:all, discarding the negotiation.
scopes, err := scopeStringToAPIKeyScopes(dbCode.Scope)
if err != nil {
return codersdk.OAuth2TokenResponse{}, err
}

// Generate the API key we will swap for the code.
// TODO: We are ignoring scopes for now.
tokenName := fmt.Sprintf("%s_%s_oauth_session_token", dbCode.UserID, app.ID)
key, sessionToken, err := apikey.Generate(apikey.CreateParams{
UserID: dbCode.UserID,
LoginType: database.LoginTypeOAuth2ProviderApp,
DefaultLifetime: lifetimes.DefaultDuration.Value(),
Scopes: scopes,
// For now, we allow only one token per app and user at a time.
TokenName: tokenName,
})
if err != nil {
return codersdk.OAuth2TokenResponse{}, err
}

// Grab the user roles so we can perform the exchange as the user.
// Grab the user roles so we can perform the exchange as the user. ScopeAll
// because this actor writes the key: narrowing it to the granted scope would
// deny api_key:create. The issued token is bounded by api_keys.scopes.
actor, _, err := httpmw.UserRBACSubject(ctx, db, dbCode.UserID, rbac.ScopeAll)
Comment thread
BobbyHo marked this conversation as resolved.
if err != nil {
return codersdk.OAuth2TokenResponse{}, xerrors.Errorf("fetch user actor: %w", err)
Expand Down Expand Up @@ -454,6 +495,7 @@ func authorizationCodeGrant(ctx context.Context, db database.Store, app database
TokenType: codersdk.OAuth2TokenTypeBearer,
RefreshToken: refreshToken.Formatted,
ExpiresIn: int64(time.Until(key.ExpiresAt).Seconds()),
Scope: dbCode.Scope,
Expiry: &key.ExpiresAt,
}, nil
}
Expand Down Expand Up @@ -507,6 +549,7 @@ func refreshTokenGrant(ctx context.Context, db database.Store, app database.OAut
return codersdk.OAuth2TokenResponse{}, err
}

// ScopeAll for the same reason as in authorizationCodeGrant.
actor, _, err := httpmw.UserRBACSubject(ctx, db, prevKey.UserID, rbac.ScopeAll)
if err != nil {
return codersdk.OAuth2TokenResponse{}, xerrors.Errorf("fetch user actor: %w", err)
Comment thread
BobbyHo marked this conversation as resolved.
Expand All @@ -518,13 +561,19 @@ func refreshTokenGrant(ctx context.Context, db database.Store, app database.OAut
return codersdk.OAuth2TokenResponse{}, err
}

// A refresh neither widens nor narrows the original grant.
scopes, err := scopeStringToAPIKeyScopes(dbToken.Scope)
if err != nil {
return codersdk.OAuth2TokenResponse{}, err
}

// Generate the new API key.
// TODO: We are ignoring scopes for now.
tokenName := fmt.Sprintf("%s_%s_oauth_session_token", prevKey.UserID, app.ID)
key, sessionToken, err := apikey.Generate(apikey.CreateParams{
UserID: prevKey.UserID,
LoginType: database.LoginTypeOAuth2ProviderApp,
DefaultLifetime: lifetimes.DefaultDuration.Value(),
Scopes: scopes,
// For now, we allow only one token per app and user at a time.
TokenName: tokenName,
})
Expand Down Expand Up @@ -582,6 +631,7 @@ func refreshTokenGrant(ctx context.Context, db database.Store, app database.OAut
TokenType: codersdk.OAuth2TokenTypeBearer,
RefreshToken: refreshToken.Formatted,
ExpiresIn: int64(time.Until(key.ExpiresAt).Seconds()),
Scope: dbToken.Scope,
Expiry: &key.ExpiresAt,
}, nil
}
Expand Down
Loading
Loading