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

Commit 4437e02

Browse files
authored
Merge pull request #1509 from sttts/sttts-source-urls
Rearrange path validation to allow ports
2 parents 42d3398 + 249a930 commit 4437e02

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

gps/deduce.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ var (
6767
//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]*)*)$`)
6868
bbRegex = regexp.MustCompile(`^(?P<root>bitbucket\.org(?P<bitname>/[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+))((?:/[A-Za-z0-9_.\-]+)*)$`)
6969
//lpRegex = regexp.MustCompile(`^(?P<root>launchpad\.net/([A-Za-z0-9-._]+)(/[A-Za-z0-9-._]+)?)(/.+)?`)
70-
lpRegex = regexp.MustCompile(`^(?P<root>launchpad\.net(/[A-Za-z0-9-._]+))((?:/[A-Za-z0-9_.\-]+)*)?`)
70+
lpRegex = regexp.MustCompile(`^(?P<root>launchpad\.net(/[A-Za-z0-9-._]+))((?:/[A-Za-z0-9_.\-]+)*)?$`)
7171
//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_.\-]+)$`)
7272
glpRegex = regexp.MustCompile(`^(?P<root>git\.launchpad\.net(/[A-Za-z0-9_.\-]+))((?:/[A-Za-z0-9_.\-]+)*)$`)
7373
//gcRegex = regexp.MustCompile(`^(?P<root>code\.google\.com/[pr]/(?P<project>[a-z0-9\-]+)(\.(?P<subrepo>[a-z0-9\-]+))?)(/[A-Za-z0-9_.\-]+)*$`)
@@ -786,7 +786,12 @@ func (hmd *httpMetadataDeducer) deduce(ctx context.Context, path string) (pathDe
786786
return hmd.deduced, hmd.deduceErr
787787
}
788788

789-
func normalizeURI(p string) (u *url.URL, newpath string, err error) {
789+
// normalizeURI takes a path string - which can be a plain import path, or a
790+
// proper URI, or something SCP-shaped - performs basic validity checks, and
791+
// returns both a full URL and just the path portion.
792+
func normalizeURI(p string) (*url.URL, string, error) {
793+
var u *url.URL
794+
var newpath string
790795
if m := scpSyntaxRe.FindStringSubmatch(p); m != nil {
791796
// Match SCP-like syntax and convert it to a URL.
792797
// Eg, "[email protected]:user/repo" becomes
@@ -800,6 +805,7 @@ func normalizeURI(p string) (u *url.URL, newpath string, err error) {
800805
//RawPath: m[3],
801806
}
802807
} else {
808+
var err error
803809
u, err = url.Parse(p)
804810
if err != nil {
805811
return nil, "", errors.Errorf("%q is not a valid URI", p)
@@ -814,11 +820,7 @@ func normalizeURI(p string) (u *url.URL, newpath string, err error) {
814820
newpath = path.Join(u.Host, u.Path)
815821
}
816822

817-
if !pathvld.MatchString(newpath) {
818-
return nil, "", errors.Errorf("%q is not a valid import path", newpath)
819-
}
820-
821-
return
823+
return u, newpath, nil
822824
}
823825

824826
// fetchMetadata fetches the remote metadata for path.

gps/deduce_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ var pathDeductionFixtures = map[string][]pathDeductionFixture{
348348
},
349349
{
350350
in: "git.launchpad.net/repo root",
351-
rerr: errors.New("git.launchpad.net/repo root is not a valid path for a source on launchpad.net"),
351+
rerr: errors.New("git.launchpad.net/repo root is not a valid path for a source on git.launchpad.net"),
352352
},
353353
},
354354
"apache": {
@@ -658,6 +658,7 @@ func TestVanityDeductionSchemeMismatch(t *testing.T) {
658658
cm := newSupervisor(ctx)
659659
dc := newDeductionCoordinator(cm)
660660
_, err := dc.deduceRootPath(ctx, "ssh://golang.org/exp")
661+
// TODO(sdboyer) this is not actually the error that it should be
661662
if err == nil {
662663
t.Error("should have errored on scheme mismatch between input and go-get metadata")
663664
}

gps/source_manager.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,10 @@ func (sm *SourceMgr) DeduceProjectRoot(ip string) (ProjectRoot, error) {
539539
return "", ErrSourceManagerIsReleased
540540
}
541541

542+
if !pathvld.MatchString(ip) {
543+
return "", errors.Errorf("%q is not a valid import path", ip)
544+
}
545+
542546
pd, err := sm.deduceCoord.deduceRootPath(context.TODO(), ip)
543547
return ProjectRoot(pd.root), err
544548
}

0 commit comments

Comments
 (0)