mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-21 19:10:45 +00:00
Backport #39006 by @bircni Enforce public-only token scope for repositories resolved as compare heads. _Assisted-by: Codex:GPT-5_ Co-authored-by: bircni <bircni@icloud.com>
This commit is contained in:
@@ -1099,6 +1099,10 @@ func parseCompareInfo(ctx *context.APIContext, compareParam string) (result *git
|
||||
ctx.APIErrorInternal(err)
|
||||
return nil, nil
|
||||
}
|
||||
if !ctx.TokenCanAccessRepo(headRepo) {
|
||||
ctx.APIErrorNotFound()
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
isSameRepo := baseRepo.ID == headRepo.ID
|
||||
|
||||
|
||||
@@ -98,6 +98,26 @@ func TestAPICompareBranches(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestAPIComparePublicOnlyToken(t *testing.T) {
|
||||
onGiteaRun(t, func(t *testing.T, _ *url.URL) {
|
||||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||||
org26 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 26})
|
||||
pr := createOutdatedPR(t, user, org26)
|
||||
require.NoError(t, pr.LoadBaseRepo(t.Context()))
|
||||
require.NoError(t, pr.LoadHeadRepo(t.Context()))
|
||||
|
||||
require.NoError(t, repo_model.UpdateRepositoryColsNoAutoTime(t.Context(),
|
||||
&repo_model.Repository{ID: pr.HeadRepo.ID, IsPrivate: true}, "is_private"))
|
||||
|
||||
compareURL := "/api/v1/repos/" + pr.BaseRepo.FullName() + "/compare/" + pr.BaseBranch + "..." + pr.HeadRepo.OwnerName + ":" + pr.HeadBranch
|
||||
fullToken := getUserToken(t, user.Name, auth_model.AccessTokenScopeReadRepository)
|
||||
MakeRequest(t, NewRequest(t, "GET", compareURL).AddTokenAuth(fullToken), http.StatusOK)
|
||||
|
||||
publicOnlyToken := getUserToken(t, user.Name, auth_model.AccessTokenScopeReadRepository, auth_model.AccessTokenScopePublicOnly)
|
||||
MakeRequest(t, NewRequest(t, "GET", compareURL).AddTokenAuth(publicOnlyToken), http.StatusNotFound)
|
||||
})
|
||||
}
|
||||
|
||||
func TestAPIDownloadCompareDiffOrPatch(t *testing.T) {
|
||||
onGiteaRun(t, func(t *testing.T, _ *url.URL) {
|
||||
session := loginUser(t, "user2")
|
||||
|
||||
Reference in New Issue
Block a user