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
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
2 changes: 2 additions & 0 deletions coderd/database/check_constraint.go

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

2 changes: 2 additions & 0 deletions coderd/database/dbauthz/dbauthz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6013,6 +6013,7 @@ func (s *MethodTestSuite) TestOAuth2ProviderAppCodes() {
check.Args(database.InsertOAuth2ProviderAppCodeParams{
AppID: app.ID,
UserID: user.ID,
Scope: string(database.ApiKeyScopeCoderAll),
}).Asserts(rbac.ResourceOauth2AppCodeToken.WithOwner(user.ID.String()), policy.ActionCreate)
}))
s.Run("DeleteOAuth2ProviderAppCodeByID", s.Subtest(func(db database.Store, check *expects) {
Expand Down Expand Up @@ -6057,6 +6058,7 @@ func (s *MethodTestSuite) TestOAuth2ProviderAppTokens() {
AppSecretID: uuid.NullUUID{UUID: secret.ID, Valid: true},
APIKeyID: key.ID,
UserID: user.ID,
Scope: string(database.ApiKeyScopeCoderAll),
}).Asserts(rbac.ResourceOauth2AppCodeToken.WithOwner(user.ID.String()), policy.ActionCreate)
}))
s.Run("GetOAuth2ProviderAppTokenByPrefix", s.Subtest(func(db database.Store, check *expects) {
Expand Down
2 changes: 2 additions & 0 deletions coderd/database/dbgen/dbgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -1784,6 +1784,7 @@ func OAuth2ProviderAppCode(t testing.TB, db database.Store, seed database.OAuth2
CodeChallengeMethod: seed.CodeChallengeMethod,
StateHash: seed.StateHash,
RedirectUri: seed.RedirectUri,
Scope: takeFirst(seed.Scope, string(database.ApiKeyScopeCoderAll)),
})
require.NoError(t, err, "insert oauth2 app code")
return code
Expand All @@ -1805,6 +1806,7 @@ func OAuth2ProviderAppToken(t testing.TB, db database.Store, seed database.OAuth
APIKeyID: takeFirst(seed.APIKeyID, uuid.New().String()),
UserID: takeFirst(seed.UserID, uuid.New()),
Audience: seed.Audience,
Scope: takeFirst(seed.Scope, string(database.ApiKeyScopeCoderAll)),
})
require.NoError(t, err, "insert oauth2 app token")
return token
Expand Down
12 changes: 10 additions & 2 deletions coderd/database/dump.sql

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE oauth2_provider_app_codes DROP COLUMN scope;

ALTER TABLE oauth2_provider_app_tokens DROP COLUMN scope;
30 changes: 30 additions & 0 deletions coderd/database/migrations/000569_oauth2_scope_columns.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- The scope negotiated at /oauth2/authorize travels with the grant itself:
Comment thread
BobbyHo marked this conversation as resolved.
-- recorded on the code when it is issued, then carried onto the token it is
-- exchanged for, so a refresh can be narrowed against what was actually
-- granted rather than against the app's current allowlist.
--
-- Existing rows are unrestricted in fact rather than by omission, since
-- apikey.Generate mints every OAuth2 access key with the coder:all scope.
-- The backfill writes that down. Both columns are then NOT NULL with no
-- default, so a grant's authority is always stated explicitly and a caller
-- that omits the column fails instead of silently issuing full access.

ALTER TABLE oauth2_provider_app_codes ADD COLUMN scope text;
Comment thread
BobbyHo marked this conversation as resolved.
Comment thread
BobbyHo marked this conversation as resolved.

ALTER TABLE oauth2_provider_app_tokens ADD COLUMN scope text;
Comment thread
BobbyHo marked this conversation as resolved.

UPDATE oauth2_provider_app_codes SET scope = 'coder:all' WHERE scope IS NULL;

UPDATE oauth2_provider_app_tokens SET scope = 'coder:all' WHERE scope IS NULL;

ALTER TABLE oauth2_provider_app_codes
ALTER COLUMN scope SET NOT NULL,
ADD CONSTRAINT oauth2_provider_app_codes_scope_not_empty CHECK (scope <> '');

ALTER TABLE oauth2_provider_app_tokens
ALTER COLUMN scope SET NOT NULL,
ADD CONSTRAINT oauth2_provider_app_tokens_scope_not_empty CHECK (scope <> '');

COMMENT ON COLUMN oauth2_provider_app_codes.scope IS 'Space-separated scope negotiated at authorization time, drawn from the api_key_scope vocabulary. Always set; coder:all records an unrestricted grant.';

COMMENT ON COLUMN oauth2_provider_app_tokens.scope IS 'Space-separated scope granted to this token, drawn from the api_key_scope vocabulary. Always set; coder:all records an unrestricted grant. Later phases will narrow this on refresh and never widen it.';
4 changes: 4 additions & 0 deletions coderd/database/models.go

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

64 changes: 64 additions & 0 deletions coderd/database/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18882,6 +18882,70 @@ func TestGetActiveUsersAuthorizationRolesParity(t *testing.T) {
}
}

func TestOAuth2ProviderScopeNotEmpty(t *testing.T) {
t.Parallel()
if testing.Short() {
t.SkipNow()
}

// An unrestricted grant is recorded as an explicit sentinel rather than as
// an absent value, so an insert that fails to carry the negotiated scope
// forward is rejected instead of silently issuing full access.
t.Run("Code", func(t *testing.T) {
t.Parallel()
db, _ := dbtestutil.NewDB(t)
ctx := testutil.Context(t, testutil.WaitLong)

user := dbgen.User(t, db, database.User{})
app := dbgen.OAuth2ProviderApp(t, db, database.OAuth2ProviderApp{})

_, err := db.InsertOAuth2ProviderAppCode(ctx, database.InsertOAuth2ProviderAppCodeParams{
ID: uuid.New(),
CreatedAt: dbtime.Now(),
ExpiresAt: dbtime.Now().Add(time.Minute),
SecretPrefix: []byte("prefix"),
HashedSecret: []byte("hashed-secret"),
AppID: app.ID,
UserID: user.ID,
ResourceUri: sql.NullString{},
CodeChallenge: sql.NullString{},
CodeChallengeMethod: sql.NullString{},
StateHash: sql.NullString{},
RedirectUri: sql.NullString{},
Scope: "",
})
require.True(t, database.IsCheckViolation(err, database.CheckOauth2ProviderAppCodesScopeNotEmpty),
"empty scope must be rejected, got %v", err)
})

t.Run("Token", func(t *testing.T) {
t.Parallel()
db, _ := dbtestutil.NewDB(t)
ctx := testutil.Context(t, testutil.WaitLong)

user := dbgen.User(t, db, database.User{})
app := dbgen.OAuth2ProviderApp(t, db, database.OAuth2ProviderApp{})
secret := dbgen.OAuth2ProviderAppSecret(t, db, database.OAuth2ProviderAppSecret{AppID: app.ID})
key, _ := dbgen.APIKey(t, db, database.APIKey{UserID: user.ID})

_, err := db.InsertOAuth2ProviderAppToken(ctx, database.InsertOAuth2ProviderAppTokenParams{
ID: uuid.New(),
CreatedAt: dbtime.Now(),
ExpiresAt: dbtime.Now().Add(time.Minute),
HashPrefix: []byte("prefix"),
RefreshHash: []byte("hashed-secret"),
AppID: app.ID,
AppSecretID: uuid.NullUUID{UUID: secret.ID, Valid: true},
APIKeyID: key.ID,
UserID: user.ID,
Audience: sql.NullString{},
Scope: "",
})
require.True(t, database.IsCheckViolation(err, database.CheckOauth2ProviderAppTokensScopeNotEmpty),
"empty scope must be rejected, got %v", err)
})
}

func TestGetAIModelPrices(t *testing.T) {
t.Parallel()

Expand Down
34 changes: 24 additions & 10 deletions coderd/database/queries.sql.go

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

Loading
Loading