Skip to content

Commit 5db8cf3

Browse files
bkcsoftlunny
authored andcommitted
Always return valid go-get meta, even if unauthorized (#2010)
* Always return valid go-get meta, even if unauthorized * don't leak information
1 parent 49d397a commit 5db8cf3

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

modules/context/context.go

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"html/template"
1111
"io"
1212
"net/http"
13+
"path"
1314
"strings"
1415
"time"
1516

@@ -18,6 +19,7 @@ import (
1819
"code.gitea.io/gitea/modules/base"
1920
"code.gitea.io/gitea/modules/log"
2021
"code.gitea.io/gitea/modules/setting"
22+
"github.com/Unknwon/com"
2123
"github.com/go-macaron/cache"
2224
"github.com/go-macaron/csrf"
2325
"github.com/go-macaron/i18n"
@@ -33,6 +35,7 @@ type Context struct {
3335
Flash *session.Flash
3436
Session session.Store
3537

38+
Link string // current request URL
3639
User *models.User
3740
IsSigned bool
3841
IsBasicAuth bool
@@ -154,15 +157,50 @@ func Contexter() macaron.Handler {
154157
csrf: x,
155158
Flash: f,
156159
Session: sess,
160+
Link: setting.AppSubURL + strings.TrimSuffix(c.Req.URL.Path, "/"),
157161
Repo: &Repository{
158162
PullRequest: &PullRequest{},
159163
},
160164
Org: &Organization{},
161165
}
162-
// Compute current URL for real-time change language.
163-
ctx.Data["Link"] = setting.AppSubURL + strings.TrimSuffix(ctx.Req.URL.Path, "/")
164-
166+
c.Data["Link"] = ctx.Link
165167
ctx.Data["PageStartTime"] = time.Now()
168+
// Quick responses appropriate go-get meta with status 200
169+
// regardless of if user have access to the repository,
170+
// or the repository does not exist at all.
171+
// This is particular a workaround for "go get" command which does not respect
172+
// .netrc file.
173+
if ctx.Query("go-get") == "1" {
174+
ownerName := c.Params(":username")
175+
repoName := c.Params(":reponame")
176+
branchName := "master"
177+
178+
owner, err := models.GetUserByName(ownerName)
179+
if err == nil {
180+
repo, err := models.GetRepositoryByName(owner.ID, repoName)
181+
if err == nil && len(repo.DefaultBranch) > 0 {
182+
branchName = repo.DefaultBranch
183+
}
184+
}
185+
prefix := setting.AppURL + path.Join(ownerName, repoName, "src", branchName)
186+
c.PlainText(http.StatusOK, []byte(com.Expand(`
187+
<html>
188+
<head>
189+
<meta name="go-import" content="{GoGetImport} git {CloneLink}">
190+
<meta name="go-source" content="{GoGetImport} _ {GoDocDirectory} {GoDocFile}">
191+
</head>
192+
<body>
193+
go get {GoGetImport}
194+
</body>
195+
</html>
196+
`, map[string]string{
197+
"GoGetImport": path.Join(setting.Domain, setting.AppSubURL, ctx.Link),
198+
"CloneLink": models.ComposeHTTPSCloneURL(ownerName, repoName),
199+
"GoDocDirectory": prefix + "{/dir}",
200+
"GoDocFile": prefix + "{/dir}/{file}#L{line}",
201+
})))
202+
return
203+
}
166204

167205
// Get user from session if logged in.
168206
ctx.User, ctx.IsBasicAuth = auth.SignedInUser(ctx.Context, ctx.Session)

0 commit comments

Comments
 (0)