From 89c57487cd99348ebe79ffbf136878a206c7eb50 Mon Sep 17 00:00:00 2001 From: Rutger Broekhoff Date: Fri, 6 Sep 2019 15:59:03 +0200 Subject: [PATCH] Fix Go 1.13 private repository go get issue (#8100) * Fix Go 1.13 invalid import path creation Signed-off-by: Rutger Broekhoff * Apply suggested changes from #8100 Signed-off-by: Rutger Broekhoff --- modules/context/context.go | 15 ++++++++++++++- modules/context/repo.go | 8 ++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/modules/context/context.go b/modules/context/context.go index b7c77ac460..067ef0b59c 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -249,6 +249,19 @@ func Contexter() macaron.Handler { if ctx.Query("go-get") == "1" { ownerName := c.Params(":username") repoName := c.Params(":reponame") + trimmedRepoName := strings.TrimSuffix(repoName, ".git") + + if ownerName == "" || trimmedRepoName == "" { + _, _ = c.Write([]byte(` + + + invalid import path + + +`)) + c.WriteHeader(400) + return + } branchName := "master" repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName) @@ -276,7 +289,7 @@ func Contexter() macaron.Handler { `, map[string]string{ - "GoGetImport": ComposeGoGetImport(ownerName, strings.TrimSuffix(repoName, ".git")), + "GoGetImport": ComposeGoGetImport(ownerName, trimmedRepoName), "CloneLink": models.ComposeHTTPSCloneURL(ownerName, repoName), "GoDocDirectory": prefix + "{/dir}", "GoDocFile": prefix + "{/dir}/{file}#L{line}", diff --git a/modules/context/repo.go b/modules/context/repo.go index 096f3c0a5d..8a056427e0 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -201,10 +201,14 @@ func ComposeGoGetImport(owner, repo string) string { // .netrc file. func EarlyResponseForGoGetMeta(ctx *Context) { username := ctx.Params(":username") - reponame := ctx.Params(":reponame") + reponame := strings.TrimSuffix(ctx.Params(":reponame"), ".git") + if username == "" || reponame == "" { + ctx.PlainText(400, []byte("invalid repository path")) + return + } ctx.PlainText(200, []byte(com.Expand(``, map[string]string{ - "GoGetImport": ComposeGoGetImport(username, strings.TrimSuffix(reponame, ".git")), + "GoGetImport": ComposeGoGetImport(username, reponame), "CloneLink": models.ComposeHTTPSCloneURL(username, reponame), }))) }