Skip to content

Commit 23c943e

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
[release-branch.go1.21] cmd/go/internal/vcs: error out if the requested repo does not support a secure protocol
Updates #63845. Fixes #63973. Change-Id: If86d6b13d3b55877b35c087112bd76388c9404b8 Reviewed-on: https://go-review.googlesource.com/c/go/+/539321 Reviewed-by: Michael Matloob <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Roland Shoemaker <[email protected]> Auto-Submit: Bryan Mills <[email protected]> (cherry picked from commit be26ae1) Reviewed-on: https://go-review.googlesource.com/c/go/+/540257 Auto-Submit: Dmitri Shuralyov <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 4952f41 commit 23c943e

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

src/cmd/go/internal/vcs/vcs.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,18 +1204,31 @@ func repoRootFromVCSPaths(importPath string, security web.SecurityMode, vcsPaths
12041204
var ok bool
12051205
repoURL, ok = interceptVCSTest(repo, vcs, security)
12061206
if !ok {
1207-
scheme := vcs.Scheme[0] // default to first scheme
1208-
if vcs.PingCmd != "" {
1209-
// If we know how to test schemes, scan to find one.
1207+
scheme, err := func() (string, error) {
12101208
for _, s := range vcs.Scheme {
12111209
if security == web.SecureOnly && !vcs.isSecureScheme(s) {
12121210
continue
12131211
}
1214-
if vcs.Ping(s, repo) == nil {
1215-
scheme = s
1216-
break
1212+
1213+
// If we know how to ping URL schemes for this VCS,
1214+
// check that this repo works.
1215+
// Otherwise, default to the first scheme
1216+
// that meets the requested security level.
1217+
if vcs.PingCmd == "" {
1218+
return s, nil
1219+
}
1220+
if err := vcs.Ping(s, repo); err == nil {
1221+
return s, nil
12171222
}
12181223
}
1224+
securityFrag := ""
1225+
if security == web.SecureOnly {
1226+
securityFrag = "secure "
1227+
}
1228+
return "", fmt.Errorf("no %sprotocol found for repository", securityFrag)
1229+
}()
1230+
if err != nil {
1231+
return nil, err
12191232
}
12201233
repoURL = scheme + "://" + repo
12211234
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Regression test for https://go.dev/issue/63845:
2+
# If 'git ls-remote' fails for all secure protocols,
3+
# we should fail instead of falling back to an arbitrary protocol.
4+
#
5+
# Note that this test does not use the local vcweb test server
6+
# (vcs-test.golang.org), because the hook for redirecting to that
7+
# server bypasses the "ping to determine protocol" logic
8+
# in cmd/go/internal/vcs.
9+
10+
[!net:golang.org] skip
11+
[!git] skip
12+
[short] skip 'tries to access a nonexistent external Git repo'
13+
14+
env GOPRIVATE=golang.org
15+
env CURLOPT_TIMEOUT_MS=100
16+
env GIT_SSH_COMMAND=false
17+
18+
! go get -x golang.org/nonexist.git@latest
19+
stderr '^git ls-remote https://golang.org/nonexist$'
20+
stderr '^git ls-remote git\+ssh://golang.org/nonexist'
21+
stderr '^git ls-remote ssh://golang.org/nonexist$'
22+
! stderr 'git://'
23+
stderr '^go: golang.org/nonexist.git@latest: no secure protocol found for repository$'
24+
25+
-- go.mod --
26+
module example
27+
28+
go 1.19

0 commit comments

Comments
 (0)