Skip to content

Commit 5c9fbcc

Browse files
authored
Fix attachment download bug (go-gitea#27486)
1 parent 7ff1f25 commit 5c9fbcc

File tree

6 files changed

+21
-21
lines changed

6 files changed

+21
-21
lines changed

services/auth/auth.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,16 @@ func isContainerPath(req *http.Request) bool {
3636
}
3737

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

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

services/auth/auth_test.go

+9-5
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

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

services/auth/oauth2.go

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

services/auth/reverseproxy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (r *ReverseProxy) Verify(req *http.Request, w http.ResponseWriter, store Da
117117
}
118118

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

services/convert/attachment.go

+1-9
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)