Skip to content

Commit

Permalink
fix: Return Unauthorized for /user endpoint APIs if session is anonym…
Browse files Browse the repository at this point in the history
…ous (#2102)
  • Loading branch information
atefehmohseni authored and Harness committed Jun 13, 2024
1 parent bcb8e7e commit b005db9
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion app/api/middleware/principal/principal.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/harness/gitness/app/api/render"
"github.com/harness/gitness/app/api/request"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum"

"github.com/rs/zerolog/log"
Expand All @@ -35,7 +36,20 @@ func RestrictTo(pType enum.PrincipalType) func(http.Handler) http.Handler {
ctx := r.Context()

p, ok := request.PrincipalFrom(ctx)
if !ok || p.Type != pType {
if !ok {
log.Ctx(ctx).Debug().Msgf("Failed to get principal from session")

render.Forbidden(ctx, w)
return
}
if p.UID == types.AnonymousPrincipalUID {
log.Ctx(ctx).Debug().Msgf("Valid principal is required, received an Anonymous.")

render.Unauthorized(ctx, w)
return
}

if p.Type != pType {
log.Ctx(ctx).Debug().Msgf("Principal of type '%s' required.", pType)

render.Forbidden(ctx, w)
Expand Down

0 comments on commit b005db9

Please sign in to comment.