internal/frontend: improve validation of candidate module paths for fetch

Additional source hosts are added to pre-filter invalid module paths.
Each path is also validated using module.CheckImportPath.

For golang/go#37002

Change-Id: I4090a915c023b4d84b9b139e74256c61ca183176
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/245897
Reviewed-by: Jonathan Amsterdam <jba@google.com>
diff --git a/internal/frontend/details.go b/internal/frontend/details.go
index 7125d28..7a8bbd0 100644
--- a/internal/frontend/details.go
+++ b/internal/frontend/details.go
@@ -460,7 +460,7 @@
 		if !ok {
 			return pathNotFoundError(ctx, pathType, fullPath, requestedVersion)
 		}
-		modulePaths, err := modulePathsToFetch(ctx, db, fullPath, modulePath)
+		modulePaths, err := candidateModulePaths(fullPath)
 		if err != nil {
 			return pathNotFoundError(ctx, pathType, fullPath, requestedVersion)
 		}
diff --git a/internal/frontend/fetch.go b/internal/frontend/fetch.go
index 37e8f92..a684ada 100644
--- a/internal/frontend/fetch.go
+++ b/internal/frontend/fetch.go
@@ -18,6 +18,7 @@
 	"go.opencensus.io/stats"
 	"go.opencensus.io/stats/view"
 	"go.opencensus.io/tag"
+	"golang.org/x/mod/module"
 	"golang.org/x/mod/semver"
 	"golang.org/x/pkgsite/internal"
 	"golang.org/x/pkgsite/internal/derrors"
@@ -438,8 +439,11 @@
 
 var vcsHostsWithThreeElementRepoName = map[string]bool{
 	"bitbucket.org": true,
+	"gitea.com":     true,
+	"gitee.com":     true,
 	"github.com":    true,
 	"gitlab.com":    true,
+	"golang.org":    true,
 }
 
 // candidateModulePaths returns the potential module paths that could contain
@@ -462,6 +466,9 @@
 	}
 	for _, part := range parts {
 		path += part
+		if err := module.CheckImportPath(path); err != nil {
+			continue
+		}
 		modulePaths = append([]string{path}, modulePaths...)
 		path += "/"
 	}