mirror of
https://github.com/go-gitea/gitea.git
synced 2026-05-23 05:42:33 +09:00
This PR closes remaining `public-only` token gaps in the API by making the restriction apply consistently across repository, organization, activity, notification, and authenticated `/api/v1/user/...` routes. Previously, `public-only` tokens were still able to: - receive private results from some list/search/self endpoints, - access repository data through ID-based lookups, - and reach several authenticated self routes that should remain unavailable for public-only access. This change treats `public-only` as a cross-cutting visibility boundary: - list/search endpoints now filter private resources consistently, - repository lookups enforce the same restriction even when addressed indirectly, - and self routes that inherently expose or mutate private account state now reject `public-only` tokens. --- Generated by a coding agent with Codex 5.2 --------- Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: Claude (Opus 4.7) <noreply@anthropic.com>
108 lines
3.3 KiB
Go
108 lines
3.3 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package integration
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
|
|
auth_model "code.gitea.io/gitea/models/auth"
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
"code.gitea.io/gitea/tests"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestAPIUserReposPublicOnly(t *testing.T) {
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
token := getUserToken(t, "user2", auth_model.AccessTokenScopeReadUser, auth_model.AccessTokenScopeReadRepository, auth_model.AccessTokenScopePublicOnly)
|
|
req := NewRequest(t, "GET", "/api/v1/user/repos").
|
|
AddTokenAuth(token)
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
var repos []api.Repository
|
|
DecodeJSON(t, resp, &repos)
|
|
assert.NotEmpty(t, repos)
|
|
for _, repo := range repos {
|
|
assert.False(t, repo.Private)
|
|
}
|
|
assert.NotContains(t, repoNames(repos), "user2/repo2")
|
|
|
|
req = NewRequest(t, "GET", "/api/v1/users/user2/repos").
|
|
AddTokenAuth(token)
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
|
DecodeJSON(t, resp, &repos)
|
|
assert.NotEmpty(t, repos)
|
|
for _, repo := range repos {
|
|
assert.False(t, repo.Private)
|
|
}
|
|
assert.NotContains(t, repoNames(repos), "user2/repo2")
|
|
}
|
|
|
|
func repoNames(repos []api.Repository) []string {
|
|
names := make([]string, 0, len(repos))
|
|
for _, repo := range repos {
|
|
names = append(names, repo.FullName)
|
|
}
|
|
return names
|
|
}
|
|
|
|
func TestAPIRepoByIDPublicOnly(t *testing.T) {
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
token := getUserToken(t, "user2", auth_model.AccessTokenScopeReadRepository, auth_model.AccessTokenScopePublicOnly)
|
|
req := NewRequest(t, "GET", "/api/v1/repositories/1").
|
|
AddTokenAuth(token)
|
|
MakeRequest(t, req, http.StatusOK)
|
|
|
|
req = NewRequest(t, "GET", "/api/v1/repositories/2").
|
|
AddTokenAuth(token)
|
|
MakeRequest(t, req, http.StatusNotFound)
|
|
}
|
|
|
|
func TestAPIActivityFeedsPublicOnly(t *testing.T) {
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
token := getUserToken(t, "user2", auth_model.AccessTokenScopeReadUser)
|
|
req := NewRequest(t, "GET", "/api/v1/users/user2/activities/feeds").
|
|
AddTokenAuth(token)
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
var activities []api.Activity
|
|
DecodeJSON(t, resp, &activities)
|
|
assert.NotEmpty(t, activities)
|
|
|
|
publicToken := getUserToken(t, "user2", auth_model.AccessTokenScopeReadUser, auth_model.AccessTokenScopePublicOnly)
|
|
req = NewRequest(t, "GET", "/api/v1/users/user2/activities/feeds").
|
|
AddTokenAuth(publicToken)
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
|
DecodeJSON(t, resp, &activities)
|
|
assertPublicActivitiesOnly(t, activities)
|
|
|
|
orgToken := getUserToken(t, "user2", auth_model.AccessTokenScopeReadOrganization)
|
|
req = NewRequest(t, "GET", "/api/v1/orgs/org3/activities/feeds").
|
|
AddTokenAuth(orgToken)
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
|
DecodeJSON(t, resp, &activities)
|
|
assert.NotEmpty(t, activities)
|
|
|
|
publicOrgToken := getUserToken(t, "user2", auth_model.AccessTokenScopeReadOrganization, auth_model.AccessTokenScopePublicOnly)
|
|
req = NewRequest(t, "GET", "/api/v1/orgs/org3/activities/feeds").
|
|
AddTokenAuth(publicOrgToken)
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
|
DecodeJSON(t, resp, &activities)
|
|
assertPublicActivitiesOnly(t, activities)
|
|
}
|
|
|
|
func assertPublicActivitiesOnly(t *testing.T, activities []api.Activity) {
|
|
t.Helper()
|
|
|
|
for _, activity := range activities {
|
|
assert.False(t, activity.IsPrivate)
|
|
if activity.Repo != nil {
|
|
assert.False(t, activity.Repo.Private)
|
|
}
|
|
}
|
|
}
|