Skip to content

Commit 70b890c

Browse files
committed
cmd/go/internal/modfetch/codehost: disable fetch of server-resolved commit hash
We cannot rely on the server to filter out the refs we don't want (we only want refs/heads/* and refs/tags/*), so do not give it the full hash. Fixes #31191. Change-Id: If1208c35954228aa6e8734f8d5f1725d0ec79c87 Reviewed-on: https://go-review.googlesource.com/c/go/+/174517 Run-TryBot: Russ Cox <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 8bde43e commit 70b890c

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/cmd/go/internal/modfetch/codehost/git.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func LocalGitRepo(remote string) (Repo, error) {
3232
return newGitRepoCached(remote, true)
3333
}
3434

35-
const gitWorkDirType = "git2"
35+
const gitWorkDirType = "git3"
3636

3737
var gitRepoCache par.Cache
3838

@@ -339,8 +339,14 @@ func (r *gitRepo) stat(rev string) (*RevInfo, error) {
339339
}
340340
}
341341

342-
// If we know a specific commit we need, fetch it.
343-
if r.fetchLevel <= fetchSome && hash != "" && !r.local {
342+
// If we know a specific commit we need and its ref, fetch it.
343+
// We do NOT fetch arbitrary hashes (when we don't know the ref)
344+
// because we want to avoid ever importing a commit that isn't
345+
// reachable from refs/tags/* or refs/heads/* or HEAD.
346+
// Both Gerrit and GitHub expose every CL/PR as a named ref,
347+
// and we don't want those commits masquerading as being real
348+
// pseudo-versions in the main repo.
349+
if r.fetchLevel <= fetchSome && ref != "" && hash != "" && !r.local {
344350
r.fetchLevel = fetchSome
345351
var refspec string
346352
if ref != "" && ref != "HEAD" {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
env GO111MODULE=on
2+
env GOPROXY=direct
3+
env GOSUMDB=off
4+
[!net] skip
5+
6+
# fetch commit hash reachable from refs/heads/* and refs/tags/* is OK
7+
go list -m golang.org/x/time@8be79e1e0910c292df4e79c241bb7e8f7e725959 # on master branch
8+
9+
# fetch other commit hash, even with a non-standard ref, is not OK
10+
! go list -m golang.org/x/time@334d83c35137ac2b376c1dc3e4c7733791855a3a # refs/changes/24/41624/3
11+
stderr 'unknown revision'
12+
! go list -m golang.org/x/[email protected]
13+
stderr 'unknown revision'
14+
! go list -m golang.org/x/time@334d83c35137
15+
stderr 'unknown revision'
16+
17+
-- go.mod --
18+
module m
19+

0 commit comments

Comments
 (0)