diff --git a/chaoscenter/authentication/api/handlers/rest/user_handlers.go b/chaoscenter/authentication/api/handlers/rest/user_handlers.go index 723fc66908..4f702a8360 100644 --- a/chaoscenter/authentication/api/handlers/rest/user_handlers.go +++ b/chaoscenter/authentication/api/handlers/rest/user_handlers.go @@ -152,6 +152,18 @@ func UpdateUser(service services.ApplicationService) gin.HandlerFunc { func GetUser(service services.ApplicationService) gin.HandlerFunc { return func(c *gin.Context) { uid := c.Param("uid") + + // Validating logged in user + // Must be either requesting info from the logged in user + // or any user if it has the admin role + role := c.MustGet("role").(string) + if c.MustGet("uid").(string) != uid && role != string(entities.RoleAdmin) { + log.Error("auth error: unauthorized") + c.JSON(utils.ErrorStatusCodes[utils.ErrUnauthorized], + presenter.CreateErrorResponse(utils.ErrUnauthorized)) + return + } + user, err := service.GetUser(uid) if err != nil { log.Error(err) @@ -559,6 +571,15 @@ func CreateApiToken(service services.ApplicationService) gin.HandlerFunc { return } + // Validating logged in user + // Requesting info must be from the logged in user + if c.MustGet("uid").(string) != apiTokenRequest.UserID { + log.Error("auth error: unauthorized") + c.JSON(utils.ErrorStatusCodes[utils.ErrUnauthorized], + presenter.CreateErrorResponse(utils.ErrUnauthorized)) + return + } + // Checking if user exists user, err := service.GetUser(apiTokenRequest.UserID) if err != nil { @@ -594,6 +615,16 @@ func CreateApiToken(service services.ApplicationService) gin.HandlerFunc { func GetApiTokens(service services.ApplicationService) gin.HandlerFunc { return func(c *gin.Context) { uid := c.Param("uid") + + // Validating logged in user + // Requesting info must be from the logged in user + if c.MustGet("uid").(string) != uid { + log.Error("auth error: unauthorized") + c.JSON(utils.ErrorStatusCodes[utils.ErrUnauthorized], + presenter.CreateErrorResponse(utils.ErrUnauthorized)) + return + } + apiTokens, err := service.GetApiTokensByUserID(uid) if err != nil { log.Error(err) diff --git a/chaoscenter/authentication/api/handlers/rest/user_handlers_test.go b/chaoscenter/authentication/api/handlers/rest/user_handlers_test.go index e06e4b2964..8a909ceeeb 100644 --- a/chaoscenter/authentication/api/handlers/rest/user_handlers_test.go +++ b/chaoscenter/authentication/api/handlers/rest/user_handlers_test.go @@ -142,12 +142,14 @@ func TestGetUser(t *testing.T) { tests := []struct { name string uid string + role string given func() expectedCode int }{ { name: "Successfully retrieve user", uid: "testUID", + role: "user", given: func() { user := &entities.User{ ID: "testUID", @@ -167,7 +169,8 @@ func TestGetUser(t *testing.T) { c.Params = gin.Params{ {"uid", tt.uid}, } - + c.Set("uid", tt.uid) + c.Set("role", tt.role) tt.given() rest.GetUser(service)(c) @@ -637,7 +640,7 @@ func TestCreateApiToken(t *testing.T) { bodyBytes, _ := json.Marshal(tt.inputBody) c.Request = httptest.NewRequest(http.MethodPost, "/api/token", bytes.NewReader(bodyBytes)) c.Request.Header.Set("Content-Type", "application/json") - + c.Set("uid", tt.inputBody.UserID) tt.given() rest.CreateApiToken(service)(c) @@ -682,7 +685,7 @@ func TestGetApiTokens(t *testing.T) { w := httptest.NewRecorder() c, _ := gin.CreateTestContext(w) c.Params = []gin.Param{{Key: "uid", Value: tt.uid}} - + c.Set("uid", tt.uid) tt.given() rest.GetApiTokens(service)(c)