Files
coder/coderd/oauth2provider/apps_internal_test.go
T
Bobby Ho 631fe09e11 fix: narrow OAuth2 client registration scope to the catalog (#29952)
fix: filter unsupported scopes in OAuth2 dynamic client registration

- Filter scopes against the external catalog during registration and RFC 7592 updates, storing and returning only supported names.
- Reject non-empty lists with no supported scopes using `400 invalid_client_metadata` to avoid creating unrestricted allowlists.
- Preserve legacy scope lists when resent unchanged; leave existing records and the admin app API unchanged.
- Add filtering tests and verify stored scopes in registration tests.

Not a security fix: authorization already rejects unsupported scopes.

Closes PLAT-674.
2026-09-28 18:46:14 +00:00

177 lines
4.7 KiB
Go

package oauth2provider
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/codersdk"
)
func TestResolveRedirectURIs(t *testing.T) {
t.Parallel()
const (
a = "https://a.example.com/callback"
b = "https://b.example.com/callback"
c = "https://c.example.com/callback"
)
tests := []struct {
name string
callbackURL string
redirectURIs []string
stored []string
want []string
}{
{
name: "ListOnly",
redirectURIs: []string{a, b, a},
want: []string{a, b},
},
{
name: "ListReplacesStored",
redirectURIs: []string{c},
stored: []string{a, b},
want: []string{c},
},
{
name: "ListWithMatchingCallback",
callbackURL: a,
redirectURIs: []string{a, b},
stored: []string{c},
want: []string{a, b},
},
{
name: "CallbackOnlyCreate",
callbackURL: c,
want: []string{c},
},
{
name: "CallbackOnlyReplacesPrimary",
callbackURL: c,
stored: []string{a, b},
want: []string{c, b},
},
{
name: "CallbackOnlyPromotesAlternate",
callbackURL: b,
stored: []string{a, b},
want: []string{b},
},
{
name: "CallbackOnlyUnchanged",
callbackURL: a,
stored: []string{a, b},
want: []string{a, b},
},
{
name: "NeitherKeepsStored",
stored: []string{a, b},
want: []string{a, b},
},
{
name: "NeitherOnCreate",
want: nil,
},
{
name: "ExplicitEmptyListIgnoresStored",
redirectURIs: []string{},
stored: []string{a, b},
want: []string{},
},
{
name: "ExplicitEmptyListIgnoresCallback",
callbackURL: c,
redirectURIs: []string{},
stored: []string{a, b},
want: []string{},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got := resolveRedirectURIs(tc.callbackURL, tc.redirectURIs, tc.stored)
require.Equal(t, tc.want, got)
})
}
}
func TestValidateRedirectURIFieldsAgree(t *testing.T) {
t.Parallel()
const (
a = "https://a.example.com/callback"
b = "https://b.example.com/callback"
)
tests := []struct {
name string
callbackURL string
redirectURIs []string
wantErr bool
}{
{name: "ListOnly", redirectURIs: []string{a, b}},
{name: "CallbackOnly", callbackURL: a},
{name: "CallbackWithEmptyList", callbackURL: a, redirectURIs: []string{}},
{name: "CallbackMatchesFirst", callbackURL: a, redirectURIs: []string{a, b}},
{name: "CallbackMatchesLater", callbackURL: b, redirectURIs: []string{a, b}, wantErr: true},
{name: "CallbackNotInList", callbackURL: "https://c.example.com/callback", redirectURIs: []string{a, b}, wantErr: true},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
errs := validateRedirectURIFieldsAgree(tc.callbackURL, tc.redirectURIs)
if !tc.wantErr {
require.Nil(t, errs)
return
}
require.Len(t, errs, 1)
require.Equal(t, "callback_url", errs[0].Field)
})
}
}
func TestRegisteredScopeAllowlist(t *testing.T) {
t.Parallel()
tests := []struct {
name string
raw string
want string
wantErr string
}{
{name: "EmptyIsNoAllowlist", raw: "", want: ""},
{name: "WhitespaceRejected", raw: " ", wantErr: "scope is blank"},
{name: "CatalogNamesKept", raw: "workspace:read template:read", want: "workspace:read template:read"},
{name: "AliasesCanonicalized", raw: "all application_connect", want: "coder:all coder:application_connect"},
{name: "DuplicatesDropped", raw: "workspace:read all coder:all", want: "workspace:read coder:all"},
{name: "UnknownNamesDropped", raw: "openid workspace:read offline_access", want: "workspace:read"},
{name: "OnlyUnknownRejected", raw: "openid profile", wantErr: "'openid profile': unknown or unsupported scope"},
{name: "InternalOnlyRejected", raw: "debug_info:read", wantErr: "'debug_info:read': unknown or unsupported scope"},
{name: "NonASCIIReplaced", raw: "wörkspace:read", wantErr: "'w rkspace:read': unknown or unsupported scope"},
{name: "LongUnknownListTruncated", raw: strings.TrimSpace(strings.Repeat(strings.Repeat("x", 300)+" ", 10)), wantErr: "(truncated)"},
{name: "TooManyNames", raw: strings.Repeat("workspace:read ", codersdk.OAuth2ScopeListMaxNames+1), wantErr: "must list at most"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got, err := registeredScopeAllowlist(tc.raw)
if tc.wantErr != "" {
require.ErrorContains(t, err, tc.wantErr)
return
}
require.NoError(t, err)
require.True(t, got.Valid)
require.Equal(t, tc.want, got.String)
})
}
}