Skip to content

Commit b294fe9

Browse files
Bryan C. Millsrsc
Bryan C. Mills
authored andcommitted
cmd/go: prohibit modules from importing vendored golang_org packages
Expand mod_internal tests to cover vendoring, replacements, and failure messages. Packages beginning with "golang_org/" resolve to $GOROOT/src/vendor, and should therefore not be visible within module code. Fixes #23970. Change-Id: I706e9c4a1d1e025883e84b897972678d0fa3f2bd Reviewed-on: https://go-review.googlesource.com/125836 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Russ Cox <[email protected]>
1 parent 68170aa commit b294fe9

File tree

2 files changed

+72
-9
lines changed

2 files changed

+72
-9
lines changed

src/cmd/go/internal/load/pkg.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,8 @@ func disallowInternal(srcDir string, p *Package, stk *ImportStack) *Package {
961961
if i > 0 {
962962
i-- // rewind over slash in ".../internal"
963963
}
964+
965+
var where string
964966
if p.Module == nil {
965967
parent := p.Dir[:i+len(p.Dir)-len(p.ImportPath)]
966968

@@ -978,19 +980,18 @@ func disallowInternal(srcDir string, p *Package, stk *ImportStack) *Package {
978980
// p is in a module, so make it available based on the import path instead
979981
// of the file path (https://golang.org/issue/23970).
980982
parent := p.ImportPath[:i]
981-
// TODO(bcmills): In case of replacements, use the module path declared by
982-
// the replacement module, not the path seen by the user.
983-
importerPath := (*stk)[len(*stk)-2]
984-
if strings.HasPrefix(importerPath, parent) {
983+
importer := (*stk)[len(*stk)-2]
984+
if str.HasPathPrefix(importer, parent) {
985985
return p
986986
}
987+
where = " in " + importer
987988
}
988989

989990
// Internal is present, and srcDir is outside parent's tree. Not allowed.
990991
perr := *p
991992
perr.Error = &PackageError{
992993
ImportStack: stk.Copy(),
993-
Err: "use of internal package " + p.ImportPath + " not allowed",
994+
Err: "use of internal package " + p.ImportPath + " not allowed" + where,
994995
}
995996
perr.Incomplete = true
996997
return &perr
@@ -1027,6 +1028,29 @@ func disallowVendor(srcDir, path string, p *Package, stk *ImportStack) *Package
10271028
return p
10281029
}
10291030

1031+
if p.Standard && ModPackageModuleInfo != nil {
1032+
// Modules must not import vendor packages in the standard library,
1033+
// but the usual vendor visibility check will not catch them
1034+
// because the module loader presents them with an ImportPath starting
1035+
// with "golang_org/" instead of "vendor/".
1036+
importer := (*stk)[len(*stk)-2]
1037+
if mod := ModPackageModuleInfo(importer); mod != nil {
1038+
dir := p.Dir
1039+
if relDir, err := filepath.Rel(p.Root, p.Dir); err == nil {
1040+
dir = relDir
1041+
}
1042+
if _, ok := FindVendor(filepath.ToSlash(dir)); ok {
1043+
perr := *p
1044+
perr.Error = &PackageError{
1045+
ImportStack: stk.Copy(),
1046+
Err: "use of vendored package " + path + " not allowed",
1047+
}
1048+
perr.Incomplete = true
1049+
return &perr
1050+
}
1051+
}
1052+
}
1053+
10301054
if perr := disallowVendorVisibility(srcDir, p, stk); perr != p {
10311055
return perr
10321056
}

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

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
env GO111MODULE=on
22

33
# golang.org/x/internal should be importable from other golang.org/x modules.
4+
rm go.mod
45
go mod -init -module golang.org/x/anything
56
go build .
67

78
# ...but that should not leak into other modules.
89
! go build ./baddep
9-
stderr 'use of internal package'
10+
stderr 'use of internal package golang.org/x/.* not allowed in golang.org/notx/useinternal$'
1011

1112
# Internal packages in the standard library should not leak into modules.
1213
! go build ./fromstd
13-
stderr 'use of internal package'
14+
stderr 'use of internal package internal/testenv not allowed$'
15+
16+
# Packages found via standard-library vendoring should not leak.
17+
! go build ./fromstdvendor
18+
stderr 'use of vendored package golang_org/x/net/http/httpguts not allowed$'
1419

1520

1621
# Dependencies should be able to use their own internal modules...
@@ -20,11 +25,27 @@ go build ./throughdep
2025

2126
# ... but other modules should not, even if they have transitive dependencies.
2227
! go build .
23-
stderr 'use of internal package'
28+
stderr 'use of internal package golang.org/x/.* not allowed in golang.org/notx$'
2429

2530
# And transitive dependencies still should not leak.
2631
! go build ./baddep
27-
stderr 'use of internal package'
32+
stderr 'use of internal package golang.org/x/.* not allowed in golang.org/notx/useinternal$'
33+
34+
35+
# Replacing an internal module should keep it internal to the same paths.
36+
rm go.mod
37+
go mod -init -module golang.org/notx
38+
go mod -replace golang.org/x/internal=./replace/golang.org/notx/internal
39+
go build ./throughdep
40+
41+
! go build ./baddep
42+
stderr 'use of internal package golang.org/x/.* not allowed in golang.org/notx/useinternal$'
43+
44+
go mod -replace golang.org/x/internal=./vendor/golang.org/x/internal
45+
go build ./throughdep
46+
47+
! go build ./baddep
48+
stderr 'use of internal package golang.org/x/.* not allowed in golang.org/notx/useinternal$'
2849

2950

3051
-- useinternal.go --
@@ -42,3 +63,21 @@ import _ "golang.org/notx/useinternal"
4263
-- fromstd/useinternal.go --
4364
package fromstd
4465
import _ "internal/testenv"
66+
67+
-- fromstdvendor/useinternal.go --
68+
package fromstdvendor
69+
import _ "golang_org/x/net/http/httpguts"
70+
71+
-- replace/golang.org/notx/internal/go.mod --
72+
module golang.org/x/internal
73+
74+
-- replace/golang.org/notx/internal/subtle/subtle.go --
75+
package subtle
76+
// Ha ha! Nothing here!
77+
78+
-- vendor/golang.org/x/internal/go.mod --
79+
module golang.org/x/internal
80+
81+
-- vendor/golang.org/x/internal/subtle/subtle.go --
82+
package subtle
83+
// Ha ha! Nothing here!

0 commit comments

Comments
 (0)