**TL;DR** Middle of the stack, split out of #28045, that makes an OAuth2 app's scope allowlist actually restrict what a client can be granted. The authorize endpoint has always parsed `scope` and then thrown it away. | PR | What it does | |---|---| | #28007 | Schema: `codes.scope` and `tokens.scope`, so a negotiated scope has somewhere to live. Every code is still stamped unrestricted. | | #28167 | `ScopesCover` compares an allowlist against a request by permission coverage rather than by name, and fails closed on any shape it does not model. Added with no production callers. | | **#28178 (this)** | The authorize endpoint checks the request against the scope catalog, grants it only if the app's filtered allowlist covers it, and persists the result on the code. Both GET and POST negotiate, so a doomed request fails before the consent page renders. | | #28179 | The consent page states what was negotiated, and `invalid_scope` is delivered to the client's own callback per RFC 6749 §4.1.2.1 instead of a Coder error page. | - Each earlier PR is inert alone: the columns carry a placeholder, the coverage rules have no caller. - Visible on upgrade, unlike the rest of the stack. A scope name outside the catalog, OIDC vocabulary included, now gets `invalid_scope`, on admin-created apps as well as DCR ones. - Tokens are unaffected. This changes which authorization requests succeed, not what a token can do. **Where in the flow** - Authorize endpoint only, both legs: GET before the consent page renders, POST before the code is minted. The token exchange, refresh, and revocation are untouched. - The result is persisted on the authorization code. The exchange copies it onto the token row, but the API key it mints still carries no scope, so nothing is enforced at request time yet. - Registration and discovery are unchanged. The catalog checked against is the `scopes_supported` both discovery documents already advertise. **What it satisfies** - OAuth 2.1 §1.4.1 — the server "MAY fully or partially ignore the scope requested by the client, based on the authorization server policy". The app's allowlist is that policy; until now the server ignored scope in every direction. - OAuth 2.1 §1.4.1 — on an omitted scope it "MUST either process the request using a pre-defined default value or fail the request". The filtered allowlist is that default; an allowlist filtering to nothing takes the second branch. Same rule as RFC 6749 §3.3. - OAuth 2.1 §1.4.1 — scope values "are defined by the authorization server", and OIDC is named there as an extension that defines its own. Hence `invalid_scope` for openid/profile/email rather than silent acceptance. - OAuth 2.1 §4.1.2.1 — `invalid_scope` is "invalid, unknown, or malformed", which now covers an uncatalogued name and an over-broad one alike. Delivery is interim here (static page on GET, error body on POST); §4.1.2.1 wants it redirected to the client's callback, which is #28179. - OAuth 2.1 §4.1.1 — `scope` stays OPTIONAL. Omitting it selects a default, never an error. - RFC 7591 §2 — a registered `scope` is the list "the client can use when requesting access tokens", a ceiling rather than a menu. Enforced here for the first time; a client that registered an unsupportable one repairs it via RFC 7592. - RFC 8414 §2 — `scopes_supported` is that catalog, advertised since before this stack, so a conformant client had no grounds to send a name outside it. - Not yet: §1.4.1 also requires `scope` in the token response when the grant differs from the request. Tokens are untouched here, so that lands with enforcement. --- Split out of #28045 ([PLAT-479](https://linear.app/codercom/issue/PLAT-479)). #28167 has merged, so this diff is against main and reviews on its own. The authorize endpoint parsed scope and threw it away, so an app's allowlist never restricted anything and a client asking for too much was never told no. Phase 1 added the columns that carry a negotiated scope from a code to the token, but nothing wrote one. **What changes** - Requested names must be in the external scope catalog. The app's stored allowlist is filtered through that same catalog, which only ever narrows. - The allowlist bounds authority, not spelling. A request is granted when every permission it grants is also granted by the allowlist, named or not, so an app allowed coder:workspaces.access can approve a client asking only for workspace:ssh. - Omitting scope grants the filtered allowlist (RFC 6749 §3.3). - Both handlers negotiate, so a request that cannot succeed fails before the consent page renders rather than after the user clicks Allow. - Two paths produce an empty result and stay distinct. No allowlist and no request keeps the previous unrestricted grant, written as an explicit sentinel because the column is NOT NULL with a non-empty CHECK. An allowlist filtering to nothing is rejected, since falling back would grant more than it ever permitted. **Accepted compatibility break, and it is wider than DCR.** The catalog check runs before the allowlist is consulted, so it covers admin-created apps too. - Admin-created apps store no allowlist and had scope discarded outright. A client sending OIDC vocabulary (openid, profile, email) now gets invalid_scope on upgrade. - DCR apps registered with openid, read or admin also hold allowlists this server cannot grant from, and fail in both directions. - scopes_supported has always been advertised on both discovery documents, predating this stack, so a conformant client had no grounds to send an uncatalogued name. RFC 6749 §4.1.2.1 makes invalid_scope the answer for one that does. - Grandfathering would seed the enforcement path with values it cannot evaluate: a visible error now, a silent hole later. - The rejection names the registered scopes and the remedy. Pinned in TestOAuth2AuthorizeDCRScopeCompatibility. **Interim error delivery, replaced in PR 3.** Rejections go out however each handler already reports errors: a static error page on GET, an OAuth2 error body on POST. #28179 delivers them to the client's own callback (RFC 6749 §4.1.2.1) and rewrites the requireInvalidScope helper. About 25 lines here are interim on purpose and flagged as such in that helper. **Not changed.** Issued tokens are still unrestricted. The exchange copies the negotiated scope onto the token record, but the API key it mints carries no scope. This changes which authorization requests succeed, not what a token can do. Stack: #28167 (merged), this PR, then #28179 for the consent page and the invalid_scope redirect. --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: Steven Masley <Emyrk@users.noreply.github.com>
775 lines
20 KiB
Go
775 lines
20 KiB
Go
package oauth2provider_test
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/coderd/coderdtest"
|
|
"github.com/coder/coder/v2/coderd/oauth2provider/oauth2providertest"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
"github.com/coder/coder/v2/testutil"
|
|
)
|
|
|
|
// TestOAuth2ClientMetadataValidation tests enhanced metadata validation per RFC 7591
|
|
func TestOAuth2ClientMetadataValidation(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// Single instance shared across all sub-tests. Each registers independent OAuth2 apps with unique client names.
|
|
client := coderdtest.New(t, nil)
|
|
_ = coderdtest.CreateFirstUser(t, client)
|
|
oauth2providertest.EnableDCR(t, client)
|
|
|
|
t.Run("RedirectURIValidation", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
redirectURIs []string
|
|
expectError bool
|
|
errorContains string
|
|
}{
|
|
{
|
|
name: "ValidHTTPS",
|
|
redirectURIs: []string{"https://example.com/callback"},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "ValidLocalhost",
|
|
redirectURIs: []string{"http://localhost:8080/callback"},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "ValidLocalhostIP",
|
|
redirectURIs: []string{"http://127.0.0.1:8080/callback"},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "ValidCustomScheme",
|
|
redirectURIs: []string{"com.example.myapp://auth/callback"},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "InvalidHTTPNonLocalhost",
|
|
redirectURIs: []string{"http://example.com/callback"},
|
|
expectError: true,
|
|
errorContains: "redirect_uri",
|
|
},
|
|
{
|
|
name: "InvalidWithFragment",
|
|
redirectURIs: []string{"https://example.com/callback#fragment"},
|
|
expectError: true,
|
|
errorContains: "fragment",
|
|
},
|
|
{
|
|
name: "InvalidJavaScriptScheme",
|
|
redirectURIs: []string{"javascript:alert('xss')"},
|
|
expectError: true,
|
|
errorContains: "dangerous scheme",
|
|
},
|
|
{
|
|
name: "InvalidDataScheme",
|
|
redirectURIs: []string{"data:text/html,<script>alert('xss')</script>"},
|
|
expectError: true,
|
|
errorContains: "dangerous scheme",
|
|
},
|
|
{
|
|
name: "InvalidFileScheme",
|
|
redirectURIs: []string{"file:///etc/passwd"},
|
|
expectError: true,
|
|
errorContains: "dangerous scheme",
|
|
},
|
|
{
|
|
name: "EmptyString",
|
|
redirectURIs: []string{""},
|
|
expectError: true,
|
|
errorContains: "redirect_uri",
|
|
},
|
|
{
|
|
name: "RelativeURL",
|
|
redirectURIs: []string{"/callback"},
|
|
expectError: true,
|
|
errorContains: "redirect_uri",
|
|
},
|
|
{
|
|
name: "MultipleValid",
|
|
redirectURIs: []string{"https://example.com/callback", "com.example.app://auth"},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "MixedValidInvalid",
|
|
redirectURIs: []string{"https://example.com/callback", "http://example.com/callback"},
|
|
expectError: true,
|
|
errorContains: "redirect_uri",
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := testutil.Context(t, testutil.WaitLong)
|
|
|
|
req := codersdk.OAuth2ClientRegistrationRequest{
|
|
RedirectURIs: test.redirectURIs,
|
|
ClientName: fmt.Sprintf("test-client-%d", time.Now().UnixNano()),
|
|
}
|
|
|
|
_, err := client.PostOAuth2ClientRegistration(ctx, req)
|
|
|
|
if test.expectError {
|
|
require.Error(t, err)
|
|
if test.errorContains != "" {
|
|
require.Contains(t, strings.ToLower(err.Error()), strings.ToLower(test.errorContains))
|
|
}
|
|
} else {
|
|
require.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
|
|
t.Run("ClientURIValidation", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
clientURI string
|
|
expectError bool
|
|
}{
|
|
{
|
|
name: "ValidHTTPS",
|
|
clientURI: "https://example.com",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "ValidHTTPLocalhost",
|
|
clientURI: "http://localhost:8080",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "ValidWithPath",
|
|
clientURI: "https://example.com/app",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "ValidWithQuery",
|
|
clientURI: "https://example.com/app?param=value",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "InvalidNotURL",
|
|
clientURI: "not-a-url",
|
|
expectError: true,
|
|
},
|
|
{
|
|
name: "ValidWithFragment",
|
|
clientURI: "https://example.com#fragment",
|
|
expectError: false, // Fragments are allowed in client_uri, unlike redirect_uri
|
|
},
|
|
{
|
|
name: "InvalidJavaScript",
|
|
clientURI: "javascript:alert('xss')",
|
|
expectError: true, // Only http/https allowed for client_uri
|
|
},
|
|
{
|
|
name: "InvalidFTP",
|
|
clientURI: "ftp://example.com",
|
|
expectError: true, // Only http/https allowed for client_uri
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := testutil.Context(t, testutil.WaitLong)
|
|
|
|
req := codersdk.OAuth2ClientRegistrationRequest{
|
|
RedirectURIs: []string{"https://example.com/callback"},
|
|
ClientName: fmt.Sprintf("test-client-%d", time.Now().UnixNano()),
|
|
ClientURI: test.clientURI,
|
|
}
|
|
|
|
_, err := client.PostOAuth2ClientRegistration(ctx, req)
|
|
|
|
if test.expectError {
|
|
require.Error(t, err)
|
|
} else {
|
|
require.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
|
|
t.Run("LogoURIValidation", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
logoURI string
|
|
expectError bool
|
|
}{
|
|
{
|
|
name: "ValidHTTPS",
|
|
logoURI: "https://example.com/logo.png",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "ValidHTTPLocalhost",
|
|
logoURI: "http://localhost:8080/logo.png",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "ValidWithQuery",
|
|
logoURI: "https://example.com/logo.png?size=large",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "InvalidNotURL",
|
|
logoURI: "not-a-url",
|
|
expectError: true,
|
|
},
|
|
{
|
|
name: "ValidWithFragment",
|
|
logoURI: "https://example.com/logo.png#fragment",
|
|
expectError: false, // Fragments are allowed in logo_uri
|
|
},
|
|
{
|
|
name: "InvalidJavaScript",
|
|
logoURI: "javascript:alert('xss')",
|
|
expectError: true, // Only http/https allowed for logo_uri
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := testutil.Context(t, testutil.WaitLong)
|
|
|
|
req := codersdk.OAuth2ClientRegistrationRequest{
|
|
RedirectURIs: []string{"https://example.com/callback"},
|
|
ClientName: fmt.Sprintf("test-client-%d", time.Now().UnixNano()),
|
|
LogoURI: test.logoURI,
|
|
}
|
|
|
|
_, err := client.PostOAuth2ClientRegistration(ctx, req)
|
|
|
|
if test.expectError {
|
|
require.Error(t, err)
|
|
} else {
|
|
require.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
|
|
t.Run("GrantTypeValidation", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
grantTypes []codersdk.OAuth2ProviderGrantType
|
|
expectError bool
|
|
}{
|
|
{
|
|
name: "DefaultEmpty",
|
|
grantTypes: []codersdk.OAuth2ProviderGrantType{},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "ValidAuthorizationCode",
|
|
grantTypes: []codersdk.OAuth2ProviderGrantType{codersdk.OAuth2ProviderGrantTypeAuthorizationCode},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "InvalidRefreshTokenAlone",
|
|
grantTypes: []codersdk.OAuth2ProviderGrantType{codersdk.OAuth2ProviderGrantTypeRefreshToken},
|
|
expectError: true, // refresh_token requires authorization_code to be present
|
|
},
|
|
{
|
|
name: "ValidMultiple",
|
|
grantTypes: []codersdk.OAuth2ProviderGrantType{codersdk.OAuth2ProviderGrantTypeAuthorizationCode, codersdk.OAuth2ProviderGrantTypeRefreshToken},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "InvalidUnsupported",
|
|
grantTypes: []codersdk.OAuth2ProviderGrantType{codersdk.OAuth2ProviderGrantTypeClientCredentials},
|
|
expectError: true,
|
|
},
|
|
{
|
|
name: "InvalidPassword",
|
|
grantTypes: []codersdk.OAuth2ProviderGrantType{codersdk.OAuth2ProviderGrantTypePassword},
|
|
expectError: true,
|
|
},
|
|
{
|
|
name: "InvalidImplicit",
|
|
grantTypes: []codersdk.OAuth2ProviderGrantType{codersdk.OAuth2ProviderGrantTypeImplicit},
|
|
expectError: true,
|
|
},
|
|
{
|
|
name: "MixedValidInvalid",
|
|
grantTypes: []codersdk.OAuth2ProviderGrantType{codersdk.OAuth2ProviderGrantTypeAuthorizationCode, codersdk.OAuth2ProviderGrantTypeClientCredentials},
|
|
expectError: true,
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := testutil.Context(t, testutil.WaitLong)
|
|
|
|
req := codersdk.OAuth2ClientRegistrationRequest{
|
|
RedirectURIs: []string{"https://example.com/callback"},
|
|
ClientName: fmt.Sprintf("test-client-%d", time.Now().UnixNano()),
|
|
GrantTypes: test.grantTypes,
|
|
}
|
|
|
|
_, err := client.PostOAuth2ClientRegistration(ctx, req)
|
|
|
|
if test.expectError {
|
|
require.Error(t, err)
|
|
} else {
|
|
require.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
|
|
t.Run("ResponseTypeValidation", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
responseTypes []codersdk.OAuth2ProviderResponseType
|
|
expectError bool
|
|
}{
|
|
{
|
|
name: "DefaultEmpty",
|
|
responseTypes: []codersdk.OAuth2ProviderResponseType{},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "ValidCode",
|
|
responseTypes: []codersdk.OAuth2ProviderResponseType{codersdk.OAuth2ProviderResponseTypeCode},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "InvalidToken",
|
|
responseTypes: []codersdk.OAuth2ProviderResponseType{codersdk.OAuth2ProviderResponseTypeToken},
|
|
expectError: true,
|
|
},
|
|
{
|
|
name: "InvalidIDToken",
|
|
responseTypes: []codersdk.OAuth2ProviderResponseType{"id_token"}, // OIDC-specific, no constant
|
|
expectError: true,
|
|
},
|
|
{
|
|
name: "InvalidMultiple",
|
|
responseTypes: []codersdk.OAuth2ProviderResponseType{codersdk.OAuth2ProviderResponseTypeCode, codersdk.OAuth2ProviderResponseTypeToken},
|
|
expectError: true,
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := testutil.Context(t, testutil.WaitLong)
|
|
|
|
req := codersdk.OAuth2ClientRegistrationRequest{
|
|
RedirectURIs: []string{"https://example.com/callback"},
|
|
ClientName: fmt.Sprintf("test-client-%d", time.Now().UnixNano()),
|
|
ResponseTypes: test.responseTypes,
|
|
}
|
|
|
|
_, err := client.PostOAuth2ClientRegistration(ctx, req)
|
|
|
|
if test.expectError {
|
|
require.Error(t, err)
|
|
} else {
|
|
require.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
|
|
t.Run("TokenEndpointAuthMethodValidation", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
authMethod codersdk.OAuth2TokenEndpointAuthMethod
|
|
expectError bool
|
|
}{
|
|
{
|
|
name: "DefaultEmpty",
|
|
authMethod: "",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "ValidClientSecretBasic",
|
|
authMethod: codersdk.OAuth2TokenEndpointAuthMethodClientSecretBasic,
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "ValidClientSecretPost",
|
|
authMethod: codersdk.OAuth2TokenEndpointAuthMethodClientSecretPost,
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "ValidNone",
|
|
authMethod: codersdk.OAuth2TokenEndpointAuthMethodNone,
|
|
expectError: false, // "none" is valid for public clients per RFC 7591
|
|
},
|
|
{
|
|
name: "InvalidPrivateKeyJWT",
|
|
authMethod: "private_key_jwt", // OIDC-specific, no constant defined
|
|
expectError: true,
|
|
},
|
|
{
|
|
name: "InvalidClientSecretJWT",
|
|
authMethod: "client_secret_jwt", // OIDC-specific, no constant defined
|
|
expectError: true,
|
|
},
|
|
{
|
|
name: "InvalidCustom",
|
|
authMethod: "custom_method",
|
|
expectError: true,
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := testutil.Context(t, testutil.WaitLong)
|
|
|
|
req := codersdk.OAuth2ClientRegistrationRequest{
|
|
RedirectURIs: []string{"https://example.com/callback"},
|
|
ClientName: fmt.Sprintf("test-client-%d", time.Now().UnixNano()),
|
|
TokenEndpointAuthMethod: test.authMethod,
|
|
}
|
|
|
|
_, err := client.PostOAuth2ClientRegistration(ctx, req)
|
|
|
|
if test.expectError {
|
|
require.Error(t, err)
|
|
} else {
|
|
require.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
// TestOAuth2ClientNameValidation tests client name validation requirements
|
|
func TestOAuth2ClientNameValidation(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// Single instance shared across all sub-tests. Each registers independent OAuth2 apps.
|
|
client := coderdtest.New(t, nil)
|
|
_ = coderdtest.CreateFirstUser(t, client)
|
|
oauth2providertest.EnableDCR(t, client)
|
|
|
|
tests := []struct {
|
|
name string
|
|
clientName string
|
|
expectError bool
|
|
}{
|
|
{
|
|
name: "ValidBasic",
|
|
clientName: "My App",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "ValidWithNumbers",
|
|
clientName: "My App 2.0",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "ValidWithSpecialChars",
|
|
clientName: "My-App_v1.0",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "ValidUnicode",
|
|
clientName: "My App 🚀",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "ValidLong",
|
|
clientName: strings.Repeat("A", 100),
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "ValidEmpty",
|
|
clientName: "",
|
|
expectError: false, // Empty names are allowed, defaults are applied
|
|
},
|
|
{
|
|
name: "ValidWhitespaceOnly",
|
|
clientName: " ",
|
|
expectError: false, // Whitespace-only names are allowed
|
|
},
|
|
{
|
|
name: "ValidTooLong",
|
|
clientName: strings.Repeat("A", 1000),
|
|
expectError: false, // Very long names are allowed
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := testutil.Context(t, testutil.WaitLong)
|
|
|
|
req := codersdk.OAuth2ClientRegistrationRequest{
|
|
RedirectURIs: []string{"https://example.com/callback"},
|
|
ClientName: test.clientName,
|
|
}
|
|
|
|
_, err := client.PostOAuth2ClientRegistration(ctx, req)
|
|
|
|
if test.expectError {
|
|
require.Error(t, err)
|
|
} else {
|
|
require.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Registration stores the scope verbatim, so every value below is accepted.
|
|
// The catalog is enforced at authorization: see
|
|
// TestOAuth2AuthorizeDCRScopeCompatibility.
|
|
func TestOAuth2ClientScopeValidation(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// Single instance shared across all sub-tests. Each registers independent OAuth2 apps.
|
|
client := coderdtest.New(t, nil)
|
|
_ = coderdtest.CreateFirstUser(t, client)
|
|
oauth2providertest.EnableDCR(t, client)
|
|
|
|
tests := []struct {
|
|
name string
|
|
scope string
|
|
expectError bool
|
|
}{
|
|
{
|
|
name: "DefaultEmpty",
|
|
scope: "",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "ValidRead",
|
|
scope: "read",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "ValidWrite",
|
|
scope: "write",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "ValidMultiple",
|
|
scope: "read write",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "ValidOpenID",
|
|
scope: "openid",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "ValidProfile",
|
|
scope: "profile",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "ValidEmail",
|
|
scope: "email",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "ValidCombined",
|
|
scope: "openid profile email read write",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "InvalidAdmin",
|
|
scope: "admin",
|
|
expectError: false, // Rejected at authorization, not registration.
|
|
},
|
|
{
|
|
name: "ValidCustom",
|
|
scope: "custom:scope",
|
|
expectError: false,
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := testutil.Context(t, testutil.WaitLong)
|
|
|
|
req := codersdk.OAuth2ClientRegistrationRequest{
|
|
RedirectURIs: []string{"https://example.com/callback"},
|
|
ClientName: fmt.Sprintf("test-client-%d", time.Now().UnixNano()),
|
|
Scope: test.scope,
|
|
}
|
|
|
|
_, err := client.PostOAuth2ClientRegistration(ctx, req)
|
|
|
|
if test.expectError {
|
|
require.Error(t, err)
|
|
} else {
|
|
require.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestOAuth2ClientMetadataDefaults tests that default values are properly applied
|
|
func TestOAuth2ClientMetadataDefaults(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
client := coderdtest.New(t, nil)
|
|
_ = coderdtest.CreateFirstUser(t, client)
|
|
oauth2providertest.EnableDCR(t, client)
|
|
|
|
ctx := testutil.Context(t, testutil.WaitLong)
|
|
|
|
// Register a minimal client to test defaults
|
|
req := codersdk.OAuth2ClientRegistrationRequest{
|
|
RedirectURIs: []string{"https://example.com/callback"},
|
|
ClientName: fmt.Sprintf("test-client-%d", time.Now().UnixNano()),
|
|
}
|
|
|
|
resp, err := client.PostOAuth2ClientRegistration(ctx, req)
|
|
require.NoError(t, err)
|
|
|
|
// Get the configuration to check defaults
|
|
config, err := client.GetOAuth2ClientConfiguration(ctx, resp.ClientID, resp.RegistrationAccessToken)
|
|
require.NoError(t, err)
|
|
|
|
// Should default to authorization_code
|
|
require.Contains(t, config.GrantTypes, codersdk.OAuth2ProviderGrantTypeAuthorizationCode)
|
|
|
|
// Should default to code
|
|
require.Contains(t, config.ResponseTypes, codersdk.OAuth2ProviderResponseTypeCode)
|
|
|
|
// Should default to client_secret_basic or client_secret_post
|
|
require.True(t, config.TokenEndpointAuthMethod == codersdk.OAuth2TokenEndpointAuthMethodClientSecretBasic ||
|
|
config.TokenEndpointAuthMethod == codersdk.OAuth2TokenEndpointAuthMethodClientSecretPost ||
|
|
config.TokenEndpointAuthMethod == "")
|
|
|
|
// Client secret should be generated
|
|
require.NotEmpty(t, resp.ClientSecret)
|
|
require.Greater(t, len(resp.ClientSecret), 20)
|
|
|
|
// Registration access token should be generated
|
|
require.NotEmpty(t, resp.RegistrationAccessToken)
|
|
require.Greater(t, len(resp.RegistrationAccessToken), 20)
|
|
}
|
|
|
|
// TestOAuth2ClientMetadataEdgeCases tests edge cases and boundary conditions
|
|
func TestOAuth2ClientMetadataEdgeCases(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// Single instance shared across all sub-tests. Each registers independent OAuth2 apps with unique client names.
|
|
client := coderdtest.New(t, nil)
|
|
_ = coderdtest.CreateFirstUser(t, client)
|
|
oauth2providertest.EnableDCR(t, client)
|
|
|
|
t.Run("ExtremelyLongRedirectURI", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := testutil.Context(t, testutil.WaitLong)
|
|
|
|
// Create a very long but valid HTTPS URI
|
|
longPath := strings.Repeat("a", 2000)
|
|
longURI := "https://example.com/" + longPath
|
|
|
|
req := codersdk.OAuth2ClientRegistrationRequest{
|
|
RedirectURIs: []string{longURI},
|
|
ClientName: fmt.Sprintf("test-client-%d", time.Now().UnixNano()),
|
|
}
|
|
|
|
_, err := client.PostOAuth2ClientRegistration(ctx, req)
|
|
// This might be accepted or rejected depending on URI length limits
|
|
// The test verifies the behavior is consistent
|
|
if err != nil {
|
|
require.Contains(t, strings.ToLower(err.Error()), "uri")
|
|
}
|
|
})
|
|
|
|
t.Run("ManyRedirectURIs", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := testutil.Context(t, testutil.WaitLong)
|
|
|
|
// Test with many redirect URIs
|
|
redirectURIs := make([]string, 20)
|
|
for i := 0; i < 20; i++ {
|
|
redirectURIs[i] = fmt.Sprintf("https://example%d.com/callback", i)
|
|
}
|
|
|
|
req := codersdk.OAuth2ClientRegistrationRequest{
|
|
RedirectURIs: redirectURIs,
|
|
ClientName: fmt.Sprintf("test-client-%d", time.Now().UnixNano()),
|
|
}
|
|
|
|
_, err := client.PostOAuth2ClientRegistration(ctx, req)
|
|
// Should handle multiple redirect URIs gracefully
|
|
require.NoError(t, err)
|
|
})
|
|
|
|
t.Run("URIWithUnusualPort", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := testutil.Context(t, testutil.WaitLong)
|
|
|
|
req := codersdk.OAuth2ClientRegistrationRequest{
|
|
RedirectURIs: []string{"https://example.com:8443/callback"},
|
|
ClientName: fmt.Sprintf("test-client-%d", time.Now().UnixNano()),
|
|
}
|
|
|
|
_, err := client.PostOAuth2ClientRegistration(ctx, req)
|
|
require.NoError(t, err)
|
|
})
|
|
|
|
t.Run("URIWithComplexPath", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := testutil.Context(t, testutil.WaitLong)
|
|
|
|
req := codersdk.OAuth2ClientRegistrationRequest{
|
|
RedirectURIs: []string{"https://example.com/path/to/callback?param=value&other=123"},
|
|
ClientName: fmt.Sprintf("test-client-%d", time.Now().UnixNano()),
|
|
}
|
|
|
|
_, err := client.PostOAuth2ClientRegistration(ctx, req)
|
|
require.NoError(t, err)
|
|
})
|
|
|
|
t.Run("URIWithEncodedCharacters", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := testutil.Context(t, testutil.WaitLong)
|
|
|
|
// Test with URL-encoded characters
|
|
encodedURI := "https://example.com/callback?param=" + url.QueryEscape("value with spaces")
|
|
|
|
req := codersdk.OAuth2ClientRegistrationRequest{
|
|
RedirectURIs: []string{encodedURI},
|
|
ClientName: fmt.Sprintf("test-client-%d", time.Now().UnixNano()),
|
|
}
|
|
|
|
_, err := client.PostOAuth2ClientRegistration(ctx, req)
|
|
require.NoError(t, err)
|
|
})
|
|
}
|