Skip to content

Commit

Permalink
vcsurl: detect bad URLs without a hostname (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
FelicianoTech authored Feb 7, 2023
1 parent 77fc773 commit 45c36f3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion warden/cmd/repos_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var (
// ensure the repository URL is valid
_, err := vcsurl.Parse(repository)
if err != nil {
return fmt.Errorf("The repository URL %s isn't valid.", repository)
return fmt.Errorf("The repository URL %s is invalid: %s", repository, err)
}

repositoriesFile, _, err := loadRepositoriesFile(repositoriesFileFl)
Expand Down
40 changes: 21 additions & 19 deletions warden/vcsurl/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ package vcsurl

import (
"fmt"
"net/url"
"strings"

"golang.org/x/exp/slices"
)

var hosts = []string{
"github.com",
}

var protocols = [...]string{
"git",
"http",
Expand All @@ -28,36 +35,31 @@ func (this *Repository) ToSSH() string {

func Parse(input string) (*Repository, error) {

var repo Repository

if strings.HasSuffix(input, ".git") {
input = input[:len(input)-4]
}

if strings.HasPrefix(input, "git@") {

input = input[4:]
repo.Host = strings.Split(input, ":")[0]
input = input[len(repo.Host)+1:]
input = strings.Replace(input, ":", "/", 1)
input = strings.Replace(input, "git@", "https://", 1)
}

if strings.HasPrefix(input, "https://") {

input = input[8:]
repo.Host = strings.Split(input, "/")[0]
input = input[len(repo.Host)+1:]
} else if strings.HasPrefix(input, "http://") {
repoURL, err := url.Parse(input)
if err != nil {
return nil, err
}

input = input[7:]
repo.Host = strings.Split(input, "/")[0]
input = input[len(repo.Host)+1:]
if !slices.Contains(hosts, repoURL.Host) {
return nil, fmt.Errorf("%s is not a valid hostname.", repoURL.Host)
}

repoParts := strings.Split(input, "/")
repo.Owner = repoParts[0]
repo.Name = repoParts[1]
repoParts := strings.Split(repoURL.Path, "/")

return &repo, nil
return &Repository{
Host: repoURL.Host,
Owner: repoParts[1],
Name: repoParts[2],
}, nil
}

func Validate(url string) bool {
Expand Down

0 comments on commit 45c36f3

Please sign in to comment.