Skip to content

Commit

Permalink
add validation
Browse files Browse the repository at this point in the history
Signed-off-by: bryans-go <[email protected]>
  • Loading branch information
bryans-go committed Sep 26, 2024
1 parent 27db7e6 commit 377c0b4
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions pkg/views/gitprovider/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"log"
"regexp"
"slices"

"github.com/charmbracelet/huh"
Expand Down Expand Up @@ -146,6 +147,16 @@ func GitProviderSelectionView(gitProviderAddView *apiclient.SetGitProviderConfig
if *gitProviderAddView.SigningMethod != "none" && str == "" {
return errors.New("signing key cannot be blank when a signing method is selected")
}
if *gitProviderAddView.SigningMethod == "gpg" {
if err := isValidGPGKey(str); err != nil {
return err
}
}
if *gitProviderAddView.SigningMethod == "ssh" {
if err := isValidSSHKey(str); err != nil {
return err
}
}
return nil
}),
).WithHeight(5).WithHideFunc(func() bool {
Expand All @@ -163,6 +174,22 @@ func GitProviderSelectionView(gitProviderAddView *apiclient.SetGitProviderConfig
}
}

func isValidGPGKey(key string) error {
gpgKeyPattern := regexp.MustCompile(`^[A-Fa-f0-9]{16,40}$`)
if !gpgKeyPattern.MatchString(key) {
return errors.New("invalid GPG key: must be 16-40 hexadecimal characters")
}
return nil
}

func isValidSSHKey(key string) error {
sshKeyPattern := regexp.MustCompile(`^(ssh-(rsa|ed25519|dss)|ecdsa-sha2-nistp(256|384|521))\s+[A-Za-z0-9+/=]+$`)
if !sshKeyPattern.MatchString(key) {
return errors.New("invalid SSH key: must start with valid SSH key type (e.g., ssh-rsa, ssh-ed25519)")
}
return nil
}

func providerRequiresUsername(gitProviderId string) bool {
return gitProviderId == "bitbucket" || gitProviderId == "bitbucket-server" || gitProviderId == "aws-codecommit"
}
Expand Down

0 comments on commit 377c0b4

Please sign in to comment.