Skip to content

Commit e8f485a

Browse files
committed
cmd/go/internal/modfetch: Add GOINSECURE.
Enables insecure fetching of dependencies whos path matches those specified in the enironment variable GOINSECURE. Fixes golang#32966
1 parent 210e367 commit e8f485a

File tree

9 files changed

+80
-7
lines changed

9 files changed

+80
-7
lines changed

src/cmd/go/alldocs.go

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/go/internal/cfg/cfg.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,12 @@ var (
245245
GOPPC64 = envOr("GOPPC64", fmt.Sprintf("%s%d", "power", objabi.GOPPC64))
246246
GOWASM = envOr("GOWASM", fmt.Sprint(objabi.GOWASM))
247247

248-
GOPROXY = envOr("GOPROXY", "https://proxy.golang.org,direct")
249-
GOSUMDB = envOr("GOSUMDB", "sum.golang.org")
250-
GOPRIVATE = Getenv("GOPRIVATE")
251-
GONOPROXY = envOr("GONOPROXY", GOPRIVATE)
252-
GONOSUMDB = envOr("GONOSUMDB", GOPRIVATE)
248+
GOPROXY = envOr("GOPROXY", "https://proxy.golang.org,direct")
249+
GOSUMDB = envOr("GOSUMDB", "sum.golang.org")
250+
GOPRIVATE = Getenv("GOPRIVATE")
251+
GONOPROXY = envOr("GONOPROXY", GOPRIVATE)
252+
GONOSUMDB = envOr("GONOSUMDB", GOPRIVATE)
253+
GOINSECURE = Getenv("GOINSECURE")
253254
)
254255

255256
// GetArchEnv returns the name and setting of the

src/cmd/go/internal/envcmd/env.go

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func MkEnv() []cfg.EnvVar {
7575
{Name: "GOFLAGS", Value: cfg.Getenv("GOFLAGS")},
7676
{Name: "GOHOSTARCH", Value: runtime.GOARCH},
7777
{Name: "GOHOSTOS", Value: runtime.GOOS},
78+
{Name: "GOINSECURE", Value: cfg.GOINSECURE},
7879
{Name: "GONOPROXY", Value: cfg.GONOPROXY},
7980
{Name: "GONOSUMDB", Value: cfg.GONOSUMDB},
8081
{Name: "GOOS", Value: cfg.Goos},

src/cmd/go/internal/help/helpdoc.go

+4
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,10 @@ General-purpose environment variables:
506506
Because the entries are space-separated, flag values must
507507
not contain spaces. Flags listed on the command line
508508
are applied after this list and therefore override it.
509+
GOINSECURE
510+
Comma-separated list of glob patterns (in the syntax of Go's path.Match)
511+
of module path prefixes that should always be fetched in an insecure
512+
manner. Only applies to dependencies that are being fetched directly.
509513
GOOS
510514
The operating system for which to compile code.
511515
Examples are linux, darwin, windows, netbsd.
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package modfetch
6+
7+
import (
8+
"cmd/go/internal/cfg"
9+
"cmd/go/internal/get"
10+
"cmd/go/internal/str"
11+
)
12+
13+
// allowInsecure reports whether we are allowed to fetch this path in an insecure manner.
14+
func allowInsecure(path string) bool {
15+
return get.Insecure || str.GlobsMatchPath(cfg.GOINSECURE, path)
16+
}

src/cmd/go/internal/modfetch/repo.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ var (
257257

258258
func lookupDirect(path string) (Repo, error) {
259259
security := web.SecureOnly
260-
if get.Insecure {
260+
261+
if allowInsecure(path) {
261262
security = web.Insecure
262263
}
263264
rr, err := get.RepoRootForImportPath(path, get.PreferMod, security)
@@ -302,7 +303,7 @@ func ImportRepoRev(path, rev string) (Repo, *RevInfo, error) {
302303
// version control system, we ignore meta tags about modules
303304
// and use only direct source control entries (get.IgnoreMod).
304305
security := web.SecureOnly
305-
if get.Insecure {
306+
if allowInsecure(path) {
306307
security = web.Insecure
307308
}
308309
rr, err := get.RepoRootForImportPath(path, get.IgnoreMod, security)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
env GO111MODULE=on
2+
3+
# secure fetch should report insecure warning
4+
cd $WORK/test
5+
go mod init
6+
stderr 'redirected .* to insecure URL'
7+
8+
# insecure fetch should not
9+
env GOINSECURE=*.golang.org
10+
rm go.mod
11+
go mod init
12+
! stderr 'redirected .* to insecure URL'
13+
14+
# insecure fetch invalid path should report insecure warning
15+
env GOINSECURE=foo.golang.org
16+
rm go.mod
17+
go mod init
18+
stderr 'redirected .* to insecure URL'
19+
20+
-- $WORK/test/dependencies.tsv --
21+
vcs-test.golang.org/insecure/go/insecure git 6fecd21f7c0c 2019-09-04T18:39:48Z
22+
23+
-- $WORK/test/x.go --
24+
package x // import "m"

src/cmd/go/testdata/script/mod_get_insecure_redirect.txt

+21
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,24 @@ env GOSUMDB=off
1111
stderr 'redirected .* to insecure URL'
1212

1313
go get -d -insecure vcs-test.golang.org/insecure/go/insecure
14+
15+
# insecure host
16+
env GOINSECURE=vcs-test.golang.org
17+
go clean -modcache
18+
go get -d vcs-test.golang.org/insecure/go/insecure
19+
20+
# insecure glob host
21+
env GOINSECURE=*.golang.org
22+
go clean -modcache
23+
go get -d vcs-test.golang.org/insecure/go/insecure
24+
25+
# insecure multiple host
26+
env GOINSECURE=somewhere-else.com,*.golang.org
27+
go clean -modcache
28+
go get -d vcs-test.golang.org/insecure/go/insecure
29+
30+
# different insecure host does not fetch
31+
env GOINSECURE=somewhere-else.com
32+
go clean -modcache
33+
! go get -d vcs-test.golang.org/insecure/go/insecure
34+
stderr 'redirected .* to insecure URL'

src/internal/cfg/cfg.go

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const KnownEnv = `
4343
GOGCCFLAGS
4444
GOHOSTARCH
4545
GOHOSTOS
46+
GOINSECURE
4647
GOMIPS
4748
GOMIPS64
4849
GONOPROXY

0 commit comments

Comments
 (0)