Skip to content

Commit 9339661

Browse files
GiteaBotwxiaoguang
andauthored
Fix archive API (#34853) (#34857)
Backport #34853 by wxiaoguang Fix #34852 Co-authored-by: wxiaoguang <[email protected]>
1 parent 1e69f08 commit 9339661

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

models/perm/access/repo_permission.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func (p *Permission) IsAdmin() bool {
4242

4343
// HasAnyUnitAccess returns true if the user might have at least one access mode to any unit of this repository.
4444
// It doesn't count the "public(anonymous/everyone) access mode".
45+
// TODO: most calls to this function should be replaced with `HasAnyUnitAccessOrPublicAccess`
4546
func (p *Permission) HasAnyUnitAccess() bool {
4647
for _, v := range p.unitsMode {
4748
if v >= perm_model.AccessModeRead {

routers/api/v1/api.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func repoAssignment() func(ctx *context.APIContext) {
228228
}
229229
}
230230

231-
if !ctx.Repo.Permission.HasAnyUnitAccess() {
231+
if !ctx.Repo.Permission.HasAnyUnitAccessOrPublicAccess() {
232232
ctx.APIErrorNotFound()
233233
return
234234
}
@@ -1241,7 +1241,7 @@ func Routes() *web.Router {
12411241
}, reqToken())
12421242
m.Get("/raw/*", context.ReferencesGitRepo(), context.RepoRefForAPI, reqRepoReader(unit.TypeCode), repo.GetRawFile)
12431243
m.Get("/media/*", context.ReferencesGitRepo(), context.RepoRefForAPI, reqRepoReader(unit.TypeCode), repo.GetRawFileOrLFS)
1244-
m.Get("/archive/*", reqRepoReader(unit.TypeCode), repo.GetArchive)
1244+
m.Methods("HEAD,GET", "/archive/*", reqRepoReader(unit.TypeCode), repo.GetArchive)
12451245
m.Combo("/forks").Get(repo.ListForks).
12461246
Post(reqToken(), reqRepoReader(unit.TypeCode), bind(api.CreateForkOption{}), repo.CreateFork)
12471247
m.Post("/merge-upstream", reqToken(), mustNotBeArchived, reqRepoWriter(unit.TypeCode), bind(api.MergeUpstreamRequest{}), repo.MergeUpstream)
@@ -1445,7 +1445,7 @@ func Routes() *web.Router {
14451445
m.Delete("", repo.DeleteAvatar)
14461446
}, reqAdmin(), reqToken())
14471447

1448-
m.Get("/{ball_type:tarball|zipball|bundle}/*", reqRepoReader(unit.TypeCode), repo.DownloadArchive)
1448+
m.Methods("HEAD,GET", "/{ball_type:tarball|zipball|bundle}/*", reqRepoReader(unit.TypeCode), repo.DownloadArchive)
14491449
}, repoAssignment(), checkTokenPublicOnly())
14501450
}, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryRepository))
14511451

tests/integration/api_repo_archive_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import (
1212
"testing"
1313

1414
auth_model "code.gitea.io/gitea/models/auth"
15+
"code.gitea.io/gitea/models/perm"
1516
repo_model "code.gitea.io/gitea/models/repo"
17+
"code.gitea.io/gitea/models/unit"
1618
"code.gitea.io/gitea/models/unittest"
1719
user_model "code.gitea.io/gitea/models/user"
1820
"code.gitea.io/gitea/tests"
@@ -58,9 +60,12 @@ func TestAPIDownloadArchive(t *testing.T) {
5860

5961
link, _ = url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/archive/master", user2.Name, repo.Name))
6062
MakeRequest(t, NewRequest(t, "GET", link.String()).AddTokenAuth(token), http.StatusBadRequest)
63+
64+
t.Run("GitHubStyle", testAPIDownloadArchiveGitHubStyle)
65+
t.Run("PrivateRepo", testAPIDownloadArchivePrivateRepo)
6166
}
6267

63-
func TestAPIDownloadArchive2(t *testing.T) {
68+
func testAPIDownloadArchiveGitHubStyle(t *testing.T) {
6469
defer tests.PrepareTestEnv(t)()
6570

6671
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
@@ -95,7 +100,13 @@ func TestAPIDownloadArchive2(t *testing.T) {
95100
bs, err = io.ReadAll(resp.Body)
96101
assert.NoError(t, err)
97102
assert.Len(t, bs, 382)
103+
}
98104

99-
link, _ = url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/archive/master", user2.Name, repo.Name))
100-
MakeRequest(t, NewRequest(t, "GET", link.String()).AddTokenAuth(token), http.StatusBadRequest)
105+
func testAPIDownloadArchivePrivateRepo(t *testing.T) {
106+
_ = repo_model.UpdateRepositoryColsNoAutoTime(t.Context(), &repo_model.Repository{ID: 1, IsPrivate: true}, "is_private")
107+
MakeRequest(t, NewRequest(t, "HEAD", "/api/v1/repos/user2/repo1/archive/master.zip"), http.StatusNotFound)
108+
MakeRequest(t, NewRequest(t, "HEAD", "/api/v1/repos/user2/repo1/zipball/master"), http.StatusNotFound)
109+
_ = repo_model.UpdateRepoUnitPublicAccess(t.Context(), &repo_model.RepoUnit{RepoID: 1, Type: unit.TypeCode, AnonymousAccessMode: perm.AccessModeRead})
110+
MakeRequest(t, NewRequest(t, "HEAD", "/api/v1/repos/user2/repo1/archive/master.zip"), http.StatusOK)
111+
MakeRequest(t, NewRequest(t, "HEAD", "/api/v1/repos/user2/repo1/zipball/master"), http.StatusOK)
101112
}

0 commit comments

Comments
 (0)