Skip to content

Commit fd6546b

Browse files
easyCZroboquat
authored andcommitted
[pat] HHarden retrieval of PATs and check for nil UUIDs
1 parent 7b7bb5f commit fd6546b

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

components/gitpod-db/go/personal_access_token.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,25 @@ func (d *PersonalAccessToken) TableName() string {
3838
func GetPersonalAccessTokenForUser(ctx context.Context, conn *gorm.DB, tokenID uuid.UUID, userID uuid.UUID) (PersonalAccessToken, error) {
3939
var token PersonalAccessToken
4040

41-
db := conn.WithContext(ctx)
41+
if tokenID == uuid.Nil {
42+
return PersonalAccessToken{}, fmt.Errorf("Token ID is a required argument to get personal access token for user")
43+
}
4244

43-
db = db.Where("id = ?", tokenID).Where("userId = ?", userID).Where("deleted = ?", 0).First(&token)
44-
if db.Error != nil {
45-
if errors.Is(db.Error, gorm.ErrRecordNotFound) {
45+
if userID == uuid.Nil {
46+
return PersonalAccessToken{}, fmt.Errorf("User ID is a required argument to get personal access token for user")
47+
}
48+
49+
tx := conn.
50+
WithContext(ctx).
51+
Where("id = ?", tokenID).
52+
Where("userId = ?", userID).
53+
Where("deleted = ?", 0).
54+
First(&token)
55+
if tx.Error != nil {
56+
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
4657
return PersonalAccessToken{}, fmt.Errorf("Token with ID %s does not exist: %w", tokenID, ErrorNotFound)
4758
}
48-
return PersonalAccessToken{}, fmt.Errorf("Failed to retrieve token: %v", db.Error)
59+
return PersonalAccessToken{}, fmt.Errorf("Failed to retrieve token: %v", tx.Error)
4960
}
5061

5162
return token, nil

components/gitpod-db/go/personal_access_token_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ func TestPersonalAccessToken_Get(t *testing.T) {
2929

3030
dbtest.CreatePersonalAccessTokenRecords(t, conn, tokenEntries...)
3131

32+
t.Run("nil token ID is rejected", func(t *testing.T) {
33+
_, err := db.GetPersonalAccessTokenForUser(context.Background(), conn, uuid.Nil, token.UserID)
34+
require.Error(t, err)
35+
})
36+
37+
t.Run("nil user ID is rejected", func(t *testing.T) {
38+
_, err := db.GetPersonalAccessTokenForUser(context.Background(), conn, token.ID, uuid.Nil)
39+
require.Error(t, err)
40+
})
41+
3242
t.Run("not matching user", func(t *testing.T) {
3343
_, err := db.GetPersonalAccessTokenForUser(context.Background(), conn, token.ID, token2.UserID)
3444
require.Error(t, err, db.ErrorNotFound)

0 commit comments

Comments
 (0)