Skip to content

Commit 7b96f71

Browse files
GiteaBotlunny
andauthored
Fix attachment download bug (go-gitea#27486) (go-gitea#27570)
Backport go-gitea#27486 by @lunny Fix go-gitea#27204 This PR allows `/<username>/<reponame>/attachments/<uuid>` access with personal access token and also changed attachments API download url to it so it can be download correctly. Co-authored-by: Lunny Xiao <[email protected]>
1 parent e6d1afa commit 7b96f71

File tree

6 files changed

+21
-21
lines changed

6 files changed

+21
-21
lines changed

services/auth/auth.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ func isContainerPath(req *http.Request) bool {
3737
}
3838

3939
var (
40-
gitRawReleasePathRe = regexp.MustCompile(`^/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+/(?:(?:git-(?:(?:upload)|(?:receive))-pack$)|(?:info/refs$)|(?:HEAD$)|(?:objects/)|(?:raw/)|(?:releases/download/))`)
41-
lfsPathRe = regexp.MustCompile(`^/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+/info/lfs/`)
40+
gitRawOrAttachPathRe = regexp.MustCompile(`^/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+/(?:(?:git-(?:(?:upload)|(?:receive))-pack$)|(?:info/refs$)|(?:HEAD$)|(?:objects/)|(?:raw/)|(?:releases/download/)|(?:attachments/))`)
41+
lfsPathRe = regexp.MustCompile(`^/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+/info/lfs/`)
4242
)
4343

44-
func isGitRawReleaseOrLFSPath(req *http.Request) bool {
45-
if gitRawReleasePathRe.MatchString(req.URL.Path) {
44+
func isGitRawOrAttachPath(req *http.Request) bool {
45+
return gitRawOrAttachPathRe.MatchString(req.URL.Path)
46+
}
47+
48+
func isGitRawOrAttachOrLFSPath(req *http.Request) bool {
49+
if isGitRawOrAttachPath(req) {
4650
return true
4751
}
4852
if setting.LFS.StartServer {

services/auth/auth_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ func Test_isGitRawOrLFSPath(t *testing.T) {
8585
"/owner/repo/releases/download/tag/repo.tar.gz",
8686
true,
8787
},
88+
{
89+
"/owner/repo/attachments/6d92a9ee-5d8b-4993-97c9-6181bdaa8955",
90+
true,
91+
},
8892
}
8993
lfsTests := []string{
9094
"/owner/repo/info/lfs/",
@@ -104,11 +108,11 @@ func Test_isGitRawOrLFSPath(t *testing.T) {
104108
t.Run(tt.path, func(t *testing.T) {
105109
req, _ := http.NewRequest("POST", "http://localhost"+tt.path, nil)
106110
setting.LFS.StartServer = false
107-
if got := isGitRawReleaseOrLFSPath(req); got != tt.want {
111+
if got := isGitRawOrAttachOrLFSPath(req); got != tt.want {
108112
t.Errorf("isGitOrLFSPath() = %v, want %v", got, tt.want)
109113
}
110114
setting.LFS.StartServer = true
111-
if got := isGitRawReleaseOrLFSPath(req); got != tt.want {
115+
if got := isGitRawOrAttachOrLFSPath(req); got != tt.want {
112116
t.Errorf("isGitOrLFSPath() = %v, want %v", got, tt.want)
113117
}
114118
})
@@ -117,11 +121,11 @@ func Test_isGitRawOrLFSPath(t *testing.T) {
117121
t.Run(tt, func(t *testing.T) {
118122
req, _ := http.NewRequest("POST", tt, nil)
119123
setting.LFS.StartServer = false
120-
if got := isGitRawReleaseOrLFSPath(req); got != setting.LFS.StartServer {
121-
t.Errorf("isGitOrLFSPath(%q) = %v, want %v, %v", tt, got, setting.LFS.StartServer, gitRawReleasePathRe.MatchString(tt))
124+
if got := isGitRawOrAttachOrLFSPath(req); got != setting.LFS.StartServer {
125+
t.Errorf("isGitOrLFSPath(%q) = %v, want %v, %v", tt, got, setting.LFS.StartServer, gitRawOrAttachPathRe.MatchString(tt))
122126
}
123127
setting.LFS.StartServer = true
124-
if got := isGitRawReleaseOrLFSPath(req); got != setting.LFS.StartServer {
128+
if got := isGitRawOrAttachOrLFSPath(req); got != setting.LFS.StartServer {
125129
t.Errorf("isGitOrLFSPath(%q) = %v, want %v", tt, got, setting.LFS.StartServer)
126130
}
127131
})

services/auth/basic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (b *Basic) Name() string {
4343
// Returns nil if header is empty or validation fails.
4444
func (b *Basic) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) (*user_model.User, error) {
4545
// Basic authentication should only fire on API, Download or on Git or LFSPaths
46-
if !middleware.IsAPIPath(req) && !isContainerPath(req) && !isAttachmentDownload(req) && !isGitRawReleaseOrLFSPath(req) {
46+
if !middleware.IsAPIPath(req) && !isContainerPath(req) && !isAttachmentDownload(req) && !isGitRawOrAttachOrLFSPath(req) {
4747
return nil, nil
4848
}
4949

services/auth/oauth2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func (o *OAuth2) userIDFromToken(tokenSHA string, store DataStore) int64 {
128128
func (o *OAuth2) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) (*user_model.User, error) {
129129
// These paths are not API paths, but we still want to check for tokens because they maybe in the API returned URLs
130130
if !middleware.IsAPIPath(req) && !isAttachmentDownload(req) && !isAuthenticatedTokenRequest(req) &&
131-
!gitRawReleasePathRe.MatchString(req.URL.Path) {
131+
!isGitRawOrAttachPath(req) {
132132
return nil, nil
133133
}
134134

services/auth/reverseproxy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (r *ReverseProxy) Verify(req *http.Request, w http.ResponseWriter, store Da
118118
}
119119

120120
// Make sure requests to API paths, attachment downloads, git and LFS do not create a new session
121-
if !middleware.IsAPIPath(req) && !isAttachmentDownload(req) && !isGitRawReleaseOrLFSPath(req) {
121+
if !middleware.IsAPIPath(req) && !isAttachmentDownload(req) && !isGitRawOrAttachOrLFSPath(req) {
122122
if sess != nil && (sess.Get("uid") == nil || sess.Get("uid").(int64) != user.ID) {
123123
handleSignIn(w, req, sess, user)
124124
}

services/convert/attachment.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
package convert
55

66
import (
7-
"strconv"
8-
97
repo_model "code.gitea.io/gitea/models/repo"
10-
"code.gitea.io/gitea/modules/setting"
118
api "code.gitea.io/gitea/modules/structs"
129
)
1310

@@ -16,12 +13,7 @@ func WebAssetDownloadURL(repo *repo_model.Repository, attach *repo_model.Attachm
1613
}
1714

1815
func APIAssetDownloadURL(repo *repo_model.Repository, attach *repo_model.Attachment) string {
19-
if attach.CustomDownloadURL != "" {
20-
return attach.CustomDownloadURL
21-
}
22-
23-
// /repos/{owner}/{repo}/releases/{id}/assets/{attachment_id}
24-
return setting.AppURL + "api/repos/" + repo.FullName() + "/releases/" + strconv.FormatInt(attach.ReleaseID, 10) + "/assets/" + strconv.FormatInt(attach.ID, 10)
16+
return attach.DownloadURL()
2517
}
2618

2719
// ToAttachment converts models.Attachment to api.Attachment for API usage

0 commit comments

Comments
 (0)