Skip to content

Commit b7c944b

Browse files
GiteaBotKN4CK3R
andauthored
Prevent anonymous container access if RequireSignInView is enabled (#28877) (#28882)
Backport #28877 by @KN4CK3R Fixes #28875 If `RequireSignInView` is enabled, the ghost user has no access rights. Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
1 parent cf9a416 commit b7c944b

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

modules/context/package.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func packageAssignment(ctx *packageAssignmentCtx, errCb func(int, string, any))
9393
}
9494

9595
func determineAccessMode(ctx *Base, pkg *Package, doer *user_model.User) (perm.AccessMode, error) {
96-
if setting.Service.RequireSignInView && doer == nil {
96+
if setting.Service.RequireSignInView && (doer == nil || doer.IsGhost()) {
9797
return perm.AccessModeNone, nil
9898
}
9999

routers/api/packages/container/container.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,15 @@ func apiErrorDefined(ctx *context.Context, err *namedError) {
114114
})
115115
}
116116

117-
// ReqContainerAccess is a middleware which checks the current user valid (real user or ghost for anonymous access)
117+
func apiUnauthorizedError(ctx *context.Context) {
118+
ctx.Resp.Header().Add("WWW-Authenticate", `Bearer realm="`+setting.AppURL+`v2/token",service="container_registry",scope="*"`)
119+
apiErrorDefined(ctx, errUnauthorized)
120+
}
121+
122+
// ReqContainerAccess is a middleware which checks the current user valid (real user or ghost if anonymous access is enabled)
118123
func ReqContainerAccess(ctx *context.Context) {
119-
if ctx.Doer == nil {
120-
ctx.Resp.Header().Add("WWW-Authenticate", `Bearer realm="`+setting.AppURL+`v2/token",service="container_registry",scope="*"`)
121-
apiErrorDefined(ctx, errUnauthorized)
124+
if ctx.Doer == nil || (setting.Service.RequireSignInView && ctx.Doer.IsGhost()) {
125+
apiUnauthorizedError(ctx)
122126
}
123127
}
124128

@@ -138,10 +142,15 @@ func DetermineSupport(ctx *context.Context) {
138142
}
139143

140144
// Authenticate creates a token for the current user
141-
// If the current user is anonymous, the ghost user is used
145+
// If the current user is anonymous, the ghost user is used unless RequireSignInView is enabled.
142146
func Authenticate(ctx *context.Context) {
143147
u := ctx.Doer
144148
if u == nil {
149+
if setting.Service.RequireSignInView {
150+
apiUnauthorizedError(ctx)
151+
return
152+
}
153+
145154
u = user_model.NewGhostUser()
146155
}
147156

tests/integration/api_packages_container_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
container_module "code.gitea.io/gitea/modules/packages/container"
2222
"code.gitea.io/gitea/modules/setting"
2323
api "code.gitea.io/gitea/modules/structs"
24+
"code.gitea.io/gitea/modules/test"
2425
"code.gitea.io/gitea/tests"
2526

2627
"github.com/minio/sha256-simd"
@@ -106,6 +107,14 @@ func TestPackageContainer(t *testing.T) {
106107
req = NewRequest(t, "GET", fmt.Sprintf("%sv2", setting.AppURL))
107108
addTokenAuthHeader(req, anonymousToken)
108109
MakeRequest(t, req, http.StatusOK)
110+
111+
defer test.MockVariableValue(&setting.Service.RequireSignInView, true)()
112+
113+
req = NewRequest(t, "GET", fmt.Sprintf("%sv2", setting.AppURL))
114+
MakeRequest(t, req, http.StatusUnauthorized)
115+
116+
req = NewRequest(t, "GET", fmt.Sprintf("%sv2/token", setting.AppURL))
117+
MakeRequest(t, req, http.StatusUnauthorized)
109118
})
110119

111120
t.Run("User", func(t *testing.T) {

0 commit comments

Comments
 (0)