Skip to content

Commit 4c1a7ab

Browse files
committed
cmd/go: reject relative paths in GOMODCACHE environment
Go already rejects relative paths in a couple environment variables, It should reject relative paths in GOMODCACHE. Fixes #43715 Change-Id: Id1ceff839c7ab21c00cf4ace45ce48324733a526 Reviewed-on: https://go-review.googlesource.com/c/go/+/284432 Run-TryBot: Baokun Lee <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Jay Conrod <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]> Trust: Jay Conrod <[email protected]> Trust: Baokun Lee <[email protected]>
1 parent 580636a commit 4c1a7ab

File tree

5 files changed

+39
-17
lines changed

5 files changed

+39
-17
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ func checkEnvWrite(key, val string) error {
428428
return fmt.Errorf("GOPATH entry is relative; must be absolute path: %q", val)
429429
}
430430
// Make sure CC and CXX are absolute paths
431-
case "CC", "CXX":
431+
case "CC", "CXX", "GOMODCACHE":
432432
if !filepath.IsAbs(val) && val != "" && val != filepath.Base(val) {
433433
return fmt.Errorf("%s entry is relative; must be absolute path: %q", key, val)
434434
}

src/cmd/go/internal/modfetch/cache.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ import (
2828
)
2929

3030
func cacheDir(path string) (string, error) {
31-
if cfg.GOMODCACHE == "" {
32-
// modload.Init exits if GOPATH[0] is empty, and cfg.GOMODCACHE
33-
// is set to GOPATH[0]/pkg/mod if GOMODCACHE is empty, so this should never happen.
34-
return "", fmt.Errorf("internal error: cfg.GOMODCACHE not set")
31+
if err := checkCacheDir(); err != nil {
32+
return "", err
3533
}
3634
enc, err := module.EscapePath(path)
3735
if err != nil {
@@ -64,10 +62,8 @@ func CachePath(m module.Version, suffix string) (string, error) {
6462
// along with the directory if the directory does not exist or if the directory
6563
// is not completely populated.
6664
func DownloadDir(m module.Version) (string, error) {
67-
if cfg.GOMODCACHE == "" {
68-
// modload.Init exits if GOPATH[0] is empty, and cfg.GOMODCACHE
69-
// is set to GOPATH[0]/pkg/mod if GOMODCACHE is empty, so this should never happen.
70-
return "", fmt.Errorf("internal error: cfg.GOMODCACHE not set")
65+
if err := checkCacheDir(); err != nil {
66+
return "", err
7167
}
7268
enc, err := module.EscapePath(m.Path)
7369
if err != nil {
@@ -134,10 +130,8 @@ func lockVersion(mod module.Version) (unlock func(), err error) {
134130
// user's working directory.
135131
// If err is nil, the caller MUST eventually call the unlock function.
136132
func SideLock() (unlock func(), err error) {
137-
if cfg.GOMODCACHE == "" {
138-
// modload.Init exits if GOPATH[0] is empty, and cfg.GOMODCACHE
139-
// is set to GOPATH[0]/pkg/mod if GOMODCACHE is empty, so this should never happen.
140-
base.Fatalf("go: internal error: cfg.GOMODCACHE not set")
133+
if err := checkCacheDir(); err != nil {
134+
base.Fatalf("go: %v", err)
141135
}
142136

143137
path := filepath.Join(cfg.GOMODCACHE, "cache", "lock")
@@ -633,3 +627,16 @@ func rewriteVersionList(dir string) {
633627
base.Fatalf("go: failed to write version list: %v", err)
634628
}
635629
}
630+
631+
func checkCacheDir() error {
632+
if cfg.GOMODCACHE == "" {
633+
// modload.Init exits if GOPATH[0] is empty, and cfg.GOMODCACHE
634+
// is set to GOPATH[0]/pkg/mod if GOMODCACHE is empty, so this should never happen.
635+
return fmt.Errorf("internal error: cfg.GOMODCACHE not set")
636+
}
637+
638+
if !filepath.IsAbs(cfg.GOMODCACHE) {
639+
return fmt.Errorf("GOMODCACHE entry is relative; must be absolute path: %q.\n", cfg.GOMODCACHE)
640+
}
641+
return nil
642+
}

src/cmd/go/internal/modfetch/fetch.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@ var downloadCache par.Cache
3737
// local download cache and returns the name of the directory
3838
// corresponding to the root of the module's file tree.
3939
func Download(ctx context.Context, mod module.Version) (dir string, err error) {
40-
if cfg.GOMODCACHE == "" {
41-
// modload.Init exits if GOPATH[0] is empty, and cfg.GOMODCACHE
42-
// is set to GOPATH[0]/pkg/mod if GOMODCACHE is empty, so this should never happen.
43-
base.Fatalf("go: internal error: cfg.GOMODCACHE not set")
40+
if err := checkCacheDir(); err != nil {
41+
base.Fatalf("go: %v", err)
4442
}
4543

4644
// The par.Cache here avoids duplicate work.

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,9 @@ go env -w GOOS=linux GOARCH=mips
173173
env GOOS=windows
174174
! go env -u GOOS
175175
stderr 'unsupported GOOS/GOARCH.*windows/mips$'
176+
177+
# go env -w should reject relative paths in GOMODCACHE environment.
178+
! go env -w GOMODCACHE=~/test
179+
stderr 'go env -w: GOMODCACHE entry is relative; must be absolute path: "~/test"'
180+
! go env -w GOMODCACHE=./test
181+
stderr 'go env -w: GOMODCACHE entry is relative; must be absolute path: "./test"'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
env GO111MODULE=on
2+
3+
# Go should reject relative paths in GOMODCACHE environment.
4+
5+
env GOMODCACHE="~/test"
6+
! go get example.com/tools/cmd/hello
7+
stderr 'must be absolute path'
8+
9+
env GOMODCACHE="./test"
10+
! go get example.com/tools/cmd/hello
11+
stderr 'must be absolute path'

0 commit comments

Comments
 (0)