Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Rearrange path validation to allow ports #1160

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions internal/gps/deduce.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var (
//gpinOldRegex = regexp.MustCompile(`^(?P<root>gopkg\.in/(?:([a-z0-9][-a-z0-9]+)/)?((?:v0|v[1-9][0-9]*)(?:\.0|\.[1-9][0-9]*){0,2}(-unstable)?)/([a-zA-Z][-a-zA-Z0-9]*)(?:\.git)?)((?:/[a-zA-Z][-a-zA-Z0-9]*)*)$`)
bbRegex = regexp.MustCompile(`^(?P<root>bitbucket\.org(?P<bitname>/[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+))((?:/[A-Za-z0-9_.\-]+)*)$`)
//lpRegex = regexp.MustCompile(`^(?P<root>launchpad\.net/([A-Za-z0-9-._]+)(/[A-Za-z0-9-._]+)?)(/.+)?`)
lpRegex = regexp.MustCompile(`^(?P<root>launchpad\.net(/[A-Za-z0-9-._]+))((?:/[A-Za-z0-9_.\-]+)*)?`)
lpRegex = regexp.MustCompile(`^(?P<root>launchpad\.net(/[A-Za-z0-9-._]+))((?:/[A-Za-z0-9_.\-]+)*)?$`)
//glpRegex = regexp.MustCompile(`^(?P<root>git\.launchpad\.net/([A-Za-z0-9_.\-]+)|~[A-Za-z0-9_.\-]+/(\+git|[A-Za-z0-9_.\-]+)/[A-Za-z0-9_.\-]+)$`)
glpRegex = regexp.MustCompile(`^(?P<root>git\.launchpad\.net(/[A-Za-z0-9_.\-]+))((?:/[A-Za-z0-9_.\-]+)*)$`)
//gcRegex = regexp.MustCompile(`^(?P<root>code\.google\.com/[pr]/(?P<project>[a-z0-9\-]+)(\.(?P<subrepo>[a-z0-9\-]+))?)(/[A-Za-z0-9_.\-]+)*$`)
Expand Down Expand Up @@ -782,7 +782,12 @@ func (hmd *httpMetadataDeducer) deduce(ctx context.Context, path string) (pathDe
return hmd.deduced, hmd.deduceErr
}

func normalizeURI(p string) (u *url.URL, newpath string, err error) {
// normalizeURI takes a path string - which can be a plain import path, or a
// proper URI, or something SCP-shaped - performs basic validity checks, and
// returns both a full URL and just the path portion.
func normalizeURI(p string) (*url.URL, string, error) {
var u *url.URL
var newpath string
if m := scpSyntaxRe.FindStringSubmatch(p); m != nil {
// Match SCP-like syntax and convert it to a URL.
// Eg, "git@github.com:user/repo" becomes
Expand All @@ -796,6 +801,7 @@ func normalizeURI(p string) (u *url.URL, newpath string, err error) {
//RawPath: m[3],
}
} else {
var err error
u, err = url.Parse(p)
if err != nil {
return nil, "", errors.Errorf("%q is not a valid URI", p)
Expand All @@ -810,11 +816,7 @@ func normalizeURI(p string) (u *url.URL, newpath string, err error) {
newpath = path.Join(u.Host, u.Path)
}

if !pathvld.MatchString(newpath) {
return nil, "", errors.Errorf("%q is not a valid import path", newpath)
}

return
return u, newpath, nil
}

// fetchMetadata fetches the remote metadata for path.
Expand Down
3 changes: 2 additions & 1 deletion internal/gps/deduce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ var pathDeductionFixtures = map[string][]pathDeductionFixture{
},
{
in: "git.launchpad.net/repo root",
rerr: errors.New("git.launchpad.net/repo root is not a valid path for a source on launchpad.net"),
rerr: errors.New("git.launchpad.net/repo root is not a valid path for a source on git.launchpad.net"),
},
},
"apache": {
Expand Down Expand Up @@ -652,6 +652,7 @@ func TestVanityDeductionSchemeMismatch(t *testing.T) {
cm := newSupervisor(ctx)
dc := newDeductionCoordinator(cm)
_, err := dc.deduceRootPath(ctx, "ssh://golang.org/exp")
// TODO(sdboyer) this is not actually the error that it should be
if err == nil {
t.Error("should have errored on scheme mismatch between input and go-get metadata")
}
Expand Down
4 changes: 4 additions & 0 deletions internal/gps/source_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,10 @@ func (sm *SourceMgr) DeduceProjectRoot(ip string) (ProjectRoot, error) {
return "", smIsReleased{}
}

if !pathvld.MatchString(ip) {
return "", errors.Errorf("%q is not a valid import path", ip)
}

pd, err := sm.deduceCoord.deduceRootPath(context.TODO(), ip)
return ProjectRoot(pd.root), err
}
Expand Down