Skip to content

Commit

Permalink
git: ensure file-looking git refs aren't parsed as URLs
Browse files Browse the repository at this point in the history
URLs that look like `./path/to/file` and `../path/to/file` definitely
aren't git URLs - so we should bail out early.

This was causing a weird issue where if you copied `./.git` this would
be detected as a valid url parsing with `host = "."` and `path = "/git"`.
The fix for this is to make sure that for these explicit file-like
paths, we *never* parse them as url-refs.

Also some tests to make sure this doesn't break again!

Signed-off-by: Justin Chadwell <[email protected]>
  • Loading branch information
jedevc committed Mar 18, 2024
1 parent 6c0b576 commit 95ca25e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 3 additions & 1 deletion util/gitutil/git_ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ func ParseGitRef(ref string) (*GitRef, error) {
err error
)

if strings.HasPrefix(ref, "github.com/") {
if strings.HasPrefix(ref, "./") || strings.HasPrefix(ref, "../") {
return nil, errdefs.ErrInvalidArgument
} else if strings.HasPrefix(ref, "github.com/") {
res.IndistinguishableFromLocal = true // Deprecated
remote = fromURL(&url.URL{
Scheme: "https",
Expand Down
9 changes: 9 additions & 0 deletions util/gitutil/git_ref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,21 @@ func TestParseGitRef(t *testing.T) {
SubDir: "myfolder",
},
},
{
ref: "./.git",
expected: nil,
},
{
ref: ".git",
expected: nil,
},
}
for _, tt := range cases {
tt := tt
t.Run(tt.ref, func(t *testing.T) {
got, err := ParseGitRef(tt.ref)
if tt.expected == nil {
require.Nil(t, got)
require.Error(t, err)
} else {
require.NoError(t, err)
Expand Down

0 comments on commit 95ca25e

Please sign in to comment.