From 666164c36938f6c2a4c6d5e9698125618639f7ae Mon Sep 17 00:00:00 2001 From: James Bardin Date: Tue, 24 Oct 2017 19:27:36 -0400 Subject: [PATCH] disallow github and bitbucket hosts These hosts have special usage in module source strings, and can't be valid registry hosts. --- registry/regsrc/module.go | 13 +++++++++++-- registry/regsrc/module_test.go | 10 ++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/registry/regsrc/module.go b/registry/regsrc/module.go index e26abcd7f..05a7bfa32 100644 --- a/registry/regsrc/module.go +++ b/registry/regsrc/module.go @@ -32,6 +32,13 @@ var ( moduleSourceRe = regexp.MustCompile( fmt.Sprintf("^(%s)\\/(%s)\\/(%s)(?:\\/\\/(.*))?$", nameSubRe, nameSubRe, providerSubRe)) + + // disallowed is a set of hostnames that have special usage in modules and + // can't be registry hosts + disallowed = map[string]bool{ + "github.com": true, + "bitbucket.org": true, + } ) // Module describes a Terraform Registry Module source. @@ -77,8 +84,10 @@ func NewModule(host, namespace, name, provider, submodule string) *Module { func ParseModuleSource(source string) (*Module, error) { // See if there is a friendly host prefix. host, rest := ParseFriendlyHost(source) - if host != nil && !host.Valid() { - return nil, ErrInvalidModuleSource + if host != nil { + if !host.Valid() || disallowed[host.Display()] { + return nil, ErrInvalidModuleSource + } } matches := moduleSourceRe.FindStringSubmatch(rest) diff --git a/registry/regsrc/module_test.go b/registry/regsrc/module_test.go index 19d9dfa19..bae502b0d 100644 --- a/registry/regsrc/module_test.go +++ b/registry/regsrc/module_test.go @@ -96,6 +96,16 @@ func TestModule(t *testing.T) { source: "foo.com/var/baz?otherthing", wantErr: true, }, + { + name: "disallow github", + source: "github.com/HashiCorp/Consul/aws", + wantErr: true, + }, + { + name: "disallow bitbucket", + source: "bitbucket.org/HashiCorp/Consul/aws", + wantErr: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {