Skip to content

[pat] Harden Personal Access Token name validation #14903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 24, 2022
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
25 changes: 22 additions & 3 deletions components/public-api-server/pkg/apiv1/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"errors"
"fmt"
"regexp"
"strings"

connect "github.com/bufbuild/connect-go"
Expand Down Expand Up @@ -45,9 +46,9 @@ type TokensService struct {
func (s *TokensService) CreatePersonalAccessToken(ctx context.Context, req *connect.Request[v1.CreatePersonalAccessTokenRequest]) (*connect.Response[v1.CreatePersonalAccessTokenResponse], error) {
tokenReq := req.Msg.GetToken()

name := strings.TrimSpace(tokenReq.GetName())
if name == "" {
return nil, connect.NewError(connect.CodeInvalidArgument, errors.New("Token Name is a required parameter."))
name, err := validatePersonalAccessTokenName(tokenReq.GetName())
if err != nil {
return nil, err
}

expiry := tokenReq.GetExpirationTime()
Expand Down Expand Up @@ -328,3 +329,21 @@ func personalAccessTokenToAPI(t db.PersonalAccessToken, value string) *v1.Person
CreatedAt: timestamppb.New(t.CreatedAt),
}
}

var (
// alpha-numeric characters, dashes, underscore, spaces, between 3 and 63 chars
personalAccessTokenNameRegex = regexp.MustCompile(`^[a-zA-Z0-9-_ ]{3,63}$`)
)

func validatePersonalAccessTokenName(name string) (string, error) {
trimmed := strings.TrimSpace(name)
if trimmed == "" {
return "", connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("Token Name is a required parameter, but got empty."))
}

if !personalAccessTokenNameRegex.MatchString(trimmed) {
return "", connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("Token Name is required to match regexp %s.", personalAccessTokenNameRegex.String()))
}

return trimmed, nil
}
16 changes: 16 additions & 0 deletions components/public-api-server/pkg/apiv1/tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -74,6 +75,21 @@ func TestTokensService_CreatePersonalAccessTokenWithoutFeatureFlag(t *testing.T)
require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))
})

t.Run("invalid argument when name does not match required regex", func(t *testing.T) {
_, _, client := setupTokensService(t, withTokenFeatureDisabled)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the token service configured with the token feature disabled? Not only this test but some others here too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably copy paste. It happens to work because the validation happens irrespective of whether the feature is enabled (we only check the feature enabled after we validate we got sensible arguments). I'll follow-up on this (in another PR) to reduce the confusion


names := []string{"a", "ab", strings.Repeat("a", 64), "!#$!%"}

for _, name := range names {
_, err := client.CreatePersonalAccessToken(context.Background(), connect.NewRequest(&v1.CreatePersonalAccessTokenRequest{
Token: &v1.PersonalAccessToken{
Name: name,
},
}))
require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))
}
})

t.Run("invalid argument when expiration time is unspecified", func(t *testing.T) {
_, _, client := setupTokensService(t, withTokenFeatureDisabled)

Expand Down
3 changes: 2 additions & 1 deletion components/public-api/gitpod/experimental/v1/tokens.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ message PersonalAccessToken {
// Read only.
string value = 2;

// name is the name of the token for humans, set by the user
// name is the name of the token for humans, set by the user.
// Must match regexp ^[a-zA-Z0-9-_ ]{3,63}$
string name = 3;

// expiration_time is the time when the token expires
Expand Down
3 changes: 2 additions & 1 deletion components/public-api/go/experimental/v1/tokens.pb.go

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

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