diff --git a/changelog/unreleased/enhancement-graph-patch-me-password-change.md b/changelog/unreleased/enhancement-graph-patch-me-password-change.md new file mode 100644 index 00000000000..10be9e22d91 --- /dev/null +++ b/changelog/unreleased/enhancement-graph-patch-me-password-change.md @@ -0,0 +1,5 @@ +Security: Block password and account changes via PATCH /graph/v1.0/me + +`PATCH /graph/v1.0/me` no longer accepts `passwordProfile`, `accountEnabled`, or `onPremisesSamAccountName`. Use `POST /graph/v1.0/me/changePassword` to change your password. + +https://github.com/owncloud/ocis/pull/12493 \ No newline at end of file diff --git a/services/graph/pkg/service/v0/service.go b/services/graph/pkg/service/v0/service.go index 7e6b6b5691e..ac35aa0e347 100644 --- a/services/graph/pkg/service/v0/service.go +++ b/services/graph/pkg/service/v0/service.go @@ -52,6 +52,7 @@ type Service interface { //nolint:interfacebloat GetUser(w http.ResponseWriter, r *http.Request) PostUser(w http.ResponseWriter, r *http.Request) DeleteUser(w http.ResponseWriter, r *http.Request) + PatchMe(w http.ResponseWriter, r *http.Request) PatchUser(w http.ResponseWriter, r *http.Request) ChangeOwnPassword(w http.ResponseWriter, r *http.Request) diff --git a/services/graph/pkg/service/v0/users.go b/services/graph/pkg/service/v0/users.go index 80687320be6..e2e157ac658 100644 --- a/services/graph/pkg/service/v0/users.go +++ b/services/graph/pkg/service/v0/users.go @@ -863,6 +863,21 @@ func (g Graph) PatchMe(w http.ResponseWriter, r *http.Request) { errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "user is not allowed to change own mail") return } + if changes.HasPasswordProfile() { + logger.Info().Interface("user", changes).Msg("could not update user: user is not allowed to change own password via PATCH /me, use POST /me/changePassword") + errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "user is not allowed to change own password via PATCH /me, use POST /me/changePassword") + return + } + if changes.HasAccountEnabled() { + logger.Info().Interface("user", changes).Msg("could not update user: user is not allowed to change own accountEnabled") + errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "user is not allowed to change own accountEnabled") + return + } + if changes.HasOnPremisesSamAccountName() { + logger.Info().Interface("user", changes).Msg("could not update user: user is not allowed to change own onPremisesSamAccountName") + errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "user is not allowed to change own onPremisesSamAccountName") + return + } g.patchUser(w, r, userID, changes) } diff --git a/services/graph/pkg/service/v0/users_test.go b/services/graph/pkg/service/v0/users_test.go index f6d8990fdcf..fb2da0e32f0 100644 --- a/services/graph/pkg/service/v0/users_test.go +++ b/services/graph/pkg/service/v0/users_test.go @@ -1133,6 +1133,49 @@ var _ = Describe("Users", func() { }) }) + Describe("PatchMe", func() { + It("rejects passwordProfile changes (CWE-620)", func() { + update := libregraph.NewUserUpdate() + pp := libregraph.NewPasswordProfile() + pp.SetPassword("newpassword") + update.SetPasswordProfile(*pp) + body, err := json.Marshal(update) + Expect(err).ToNot(HaveOccurred()) + + r := httptest.NewRequest(http.MethodPatch, "/graph/v1.0/me", bytes.NewBuffer(body)) + r = r.WithContext(revactx.ContextSetUser(ctx, currentUser)) + svc.PatchMe(rr, r) + + Expect(rr.Code).To(Equal(http.StatusBadRequest)) + }) + + It("rejects accountEnabled changes", func() { + update := libregraph.NewUserUpdate() + update.SetAccountEnabled(false) + body, err := json.Marshal(update) + Expect(err).ToNot(HaveOccurred()) + + r := httptest.NewRequest(http.MethodPatch, "/graph/v1.0/me", bytes.NewBuffer(body)) + r = r.WithContext(revactx.ContextSetUser(ctx, currentUser)) + svc.PatchMe(rr, r) + + Expect(rr.Code).To(Equal(http.StatusBadRequest)) + }) + + It("rejects onPremisesSamAccountName changes", func() { + update := libregraph.NewUserUpdate() + update.SetOnPremisesSamAccountName("newusername") + body, err := json.Marshal(update) + Expect(err).ToNot(HaveOccurred()) + + r := httptest.NewRequest(http.MethodPatch, "/graph/v1.0/me", bytes.NewBuffer(body)) + r = r.WithContext(revactx.ContextSetUser(ctx, currentUser)) + svc.PatchMe(rr, r) + + Expect(rr.Code).To(Equal(http.StatusBadRequest)) + }) + }) + Describe("PatchUser", func() { var ( user *libregraph.User