diff --git a/internal/frontend/fetch.go b/internal/frontend/fetch.go index 6f9a7a301..eecb73a4f 100644 --- a/internal/frontend/fetch.go +++ b/internal/frontend/fetch.go @@ -450,7 +450,6 @@ var vcsHostsWithThreeElementRepoName = map[string]bool{ "gitee.com": true, "github.com": true, "gitlab.com": true, - "golang.org": true, } // maxPathsToFetch is the number of modulePaths that are fetched from a single @@ -461,6 +460,12 @@ var maxPathsToFetch = 10 // candidateModulePaths returns the potential module paths that could contain // the fullPath. The paths are returned in reverse length order. func candidateModulePaths(fullPath string) (_ []string, err error) { + if !isValidPath(fullPath) { + return nil, &serverError{ + status: http.StatusBadRequest, + err: fmt.Errorf("isValidPath(%q): false", fullPath), + } + } var ( path string modulePaths []string diff --git a/internal/frontend/urlinfo.go b/internal/frontend/urlinfo.go index 0db14851c..0b31e8abb 100644 --- a/internal/frontend/urlinfo.go +++ b/internal/frontend/urlinfo.go @@ -144,6 +144,15 @@ func isValidPath(fullPath string) bool { return false } parts := strings.Split(fullPath, "/") + if parts[0] == "golang.org" { + if fullPath == "golang.org/dl" { + return true + } + if len(parts) >= 3 && parts[1] == "x" { + return true + } + return false + } if _, ok := vcsHostsWithThreeElementRepoName[parts[0]]; ok { if len(parts) < 3 { return false diff --git a/internal/frontend/urlinfo_test.go b/internal/frontend/urlinfo_test.go index 209af0b4a..11c886ada 100644 --- a/internal/frontend/urlinfo_test.go +++ b/internal/frontend/urlinfo_test.go @@ -179,6 +179,31 @@ func TestNewContextFromExps(t *testing.T) { } } +func TestIsValidPath(t *testing.T) { + tests := []struct { + path string + want bool + }{ + {"net/http", true}, + {"github.com/foo", false}, + {"github.com/foo", false}, + {"/github.com/foo/bar", false}, + {"github.com/foo/bar/", false}, + {"github.com/foo/bar", true}, + {"github.com/foo/bar/baz", true}, + {"golang.org/dl", true}, + {"golang.org/x", false}, + {"golang.org/x/tools", true}, + {"golang.org/x/tools/go/packages", true}, + } + for _, test := range tests { + got := isValidPath(test.path) + if got != test.want { + t.Errorf("isValidPath(ctx, ds, %q) = %t, want %t", test.path, got, test.want) + } + } +} + func TestIsSupportedVersion(t *testing.T) { tests := []struct { path, version string