Skip to content

Commit 23139aa

Browse files
GiteaBotlunny
andauthoredOct 1, 2023
Allow get release download files and lfs files with oauth2 token format (go-gitea#26430) (go-gitea#27378)
Backport go-gitea#26430 by @lunny Fix go-gitea#26165 Fix go-gitea#25257 Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
1 parent b6b71c7 commit 23139aa

File tree

8 files changed

+66
-6
lines changed

8 files changed

+66
-6
lines changed
 

‎models/fixtures/attachment.yml

+13
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,16 @@
140140
download_count: 0
141141
size: 0
142142
created_unix: 946684800
143+
144+
-
145+
id: 12
146+
uuid: a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a22
147+
repo_id: 2
148+
issue_id: 0
149+
release_id: 11
150+
uploader_id: 2
151+
comment_id: 0
152+
name: README.md
153+
download_count: 0
154+
size: 0
155+
created_unix: 946684800

‎models/fixtures/release.yml

+14
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,17 @@
136136
is_prerelease: false
137137
is_tag: false
138138
created_unix: 946684803
139+
140+
- id: 11
141+
repo_id: 2
142+
publisher_id: 2
143+
tag_name: "v1.1"
144+
lower_tag_name: "v1.1"
145+
target: ""
146+
title: "v1.1"
147+
sha1: "205ac761f3326a7ebe416e8673760016450b5cec"
148+
num_commits: 2
149+
is_draft: false
150+
is_prerelease: false
151+
is_tag: false
152+
created_unix: 946684803

‎routers/web/web.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -863,9 +863,6 @@ func registerRoutes(m *web.Route) {
863863
}, reqUnitAccess(unit.TypeCode, perm.AccessModeRead, false))
864864
}, ignSignIn, context_service.UserAssignmentWeb(), context.OrgAssignment()) // for "/{username}/-" (packages, projects, code)
865865

866-
// ***** Release Attachment Download without Signin
867-
m.Get("/{username}/{reponame}/releases/download/{vTag}/{fileName}", ignSignIn, context.RepoAssignment, repo.MustBeNotEmpty, repo.RedirectDownload)
868-
869866
m.Group("/{username}/{reponame}", func() {
870867
m.Group("/settings", func() {
871868
m.Group("", func() {
@@ -1118,8 +1115,9 @@ func registerRoutes(m *web.Route) {
11181115
m.Get(".rss", feedEnabled, repo.ReleasesFeedRSS)
11191116
m.Get(".atom", feedEnabled, repo.ReleasesFeedAtom)
11201117
}, ctxDataSet("EnableFeed", setting.Other.EnableFeed),
1121-
repo.MustBeNotEmpty, reqRepoReleaseReader, context.RepoRefByType(context.RepoRefTag, true))
1122-
m.Get("/releases/attachments/{uuid}", repo.MustBeNotEmpty, reqRepoReleaseReader, repo.GetAttachment)
1118+
repo.MustBeNotEmpty, context.RepoRefByType(context.RepoRefTag, true))
1119+
m.Get("/releases/attachments/{uuid}", repo.MustBeNotEmpty, repo.GetAttachment)
1120+
m.Get("/releases/download/{vTag}/{fileName}", repo.MustBeNotEmpty, repo.RedirectDownload)
11231121
m.Group("/releases", func() {
11241122
m.Get("/new", repo.NewRelease)
11251123
m.Post("/new", web.Bind(forms.NewReleaseForm{}), repo.NewReleasePost)

‎services/auth/oauth2.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ func (o *OAuth2) userIDFromToken(tokenSHA string, store DataStore) int64 {
126126
// If verification is successful returns an existing user object.
127127
// Returns nil if verification fails.
128128
func (o *OAuth2) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) (*user_model.User, error) {
129-
if !middleware.IsAPIPath(req) && !isAttachmentDownload(req) && !isAuthenticatedTokenRequest(req) {
129+
// These paths are not API paths, but we still want to check for tokens because they maybe in the API returned URLs
130+
if !middleware.IsAPIPath(req) && !isAttachmentDownload(req) && !isAuthenticatedTokenRequest(req) &&
131+
!gitRawReleasePathRe.MatchString(req.URL.Path) {
130132
return nil, nil
131133
}
132134

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1032bbf17fbc0d9c95bb5418dabe8f8c99278700

‎tests/integration/release_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,20 @@ func TestViewTagsList(t *testing.T) {
239239

240240
assert.EqualValues(t, []string{"v1.0", "delete-tag", "v1.1"}, tagNames)
241241
}
242+
243+
func TestDownloadReleaseAttachment(t *testing.T) {
244+
defer tests.PrepareTestEnv(t)()
245+
246+
tests.PrepareAttachmentsStorage(t)
247+
248+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
249+
250+
url := repo.Link() + "/releases/download/v1.1/README.md"
251+
252+
req := NewRequest(t, "GET", url)
253+
MakeRequest(t, req, http.StatusNotFound)
254+
255+
req = NewRequest(t, "GET", url)
256+
session := loginUser(t, "user2")
257+
session.MakeRequest(t, req, http.StatusOK)
258+
}

‎tests/test_utils.go

+14
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,20 @@ func InitTest(requireGitea bool) {
176176
routers.InitWebInstalled(graceful.GetManager().HammerContext())
177177
}
178178

179+
func PrepareAttachmentsStorage(t testing.TB) {
180+
// prepare attachments directory and files
181+
assert.NoError(t, storage.Clean(storage.Attachments))
182+
183+
s, err := storage.NewStorage(setting.LocalStorageType, &setting.Storage{
184+
Path: filepath.Join(filepath.Dir(setting.AppPath), "tests", "testdata", "data", "attachments"),
185+
})
186+
assert.NoError(t, err)
187+
assert.NoError(t, s.IterateObjects("", func(p string, obj storage.Object) error {
188+
_, err = storage.Copy(storage.Attachments, p, s, p)
189+
return err
190+
}))
191+
}
192+
179193
func PrepareTestEnv(t testing.TB, skip ...int) func() {
180194
t.Helper()
181195
ourSkip := 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This is a release README

0 commit comments

Comments
 (0)