Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions server/__test__/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,4 +654,89 @@ describe("Account", () => {
expect(removeMock).toHaveBeenCalledTimes(1);
expect(res.sendStatus).toHaveBeenCalledWith(200);
});

it("lets a user update their own profile", async () => {
const res = mockResponse();
const req = mockRequest({
params: { userid: "123" },
user: { id: 123, email: "user@test.com", sub: "data_entry" },
body: {
firstName: "New",
lastName: "Name",
email: "user@test.com",
tenantId: "1",
},
});
const next = mockNext();
const updateUserProfileMock =
accountService.updateUserProfile as jest.MockedFunction<
typeof accountService.updateUserProfile
>;
updateUserProfileMock.mockResolvedValueOnce({
isSuccess: true,
code: "UPDATE_SUCCESS",
message: "User profile successfully updated",
} as any);

await accountController.updateUserProfile(req, res, next);
expect(updateUserProfileMock).toHaveBeenCalledWith(
"123",
"New",
"Name",
"user@test.com",
"1"
);
expect(res.status).toHaveBeenCalledWith(200);
});

it("blocks a user from updating another user's profile (IDOR)", async () => {
const res = mockResponse();
const req = mockRequest({
params: { userid: "999" },
user: { id: 123, email: "user@test.com", sub: "data_entry" },
body: {
firstName: "Attacker",
lastName: "Controlled",
email: "victim-new@test.com",
tenantId: "1",
},
});
const next = mockNext();
const updateUserProfileMock =
accountService.updateUserProfile as jest.MockedFunction<
typeof accountService.updateUserProfile
>;

await accountController.updateUserProfile(req, res, next);
expect(updateUserProfileMock).not.toHaveBeenCalled();
expect(res.status).toHaveBeenCalledWith(403);
});

it("lets an admin update another user's profile", async () => {
const res = mockResponse();
const req = mockRequest({
params: { userid: "999" },
user: { id: 123, email: "admin@test.com", sub: "admin,global_admin" },
body: {
firstName: "New",
lastName: "Name",
email: "someone@test.com",
tenantId: "1",
},
});
const next = mockNext();
const updateUserProfileMock =
accountService.updateUserProfile as jest.MockedFunction<
typeof accountService.updateUserProfile
>;
updateUserProfileMock.mockResolvedValueOnce({
isSuccess: true,
code: "UPDATE_SUCCESS",
message: "User profile successfully updated",
} as any);

await accountController.updateUserProfile(req, res, next);
expect(updateUserProfileMock).toHaveBeenCalledTimes(1);
expect(res.status).toHaveBeenCalledWith(200);
});
});
20 changes: 20 additions & 0 deletions server/app/controllers/account-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,26 @@ const updateUserProfile: RequestHandler<
never
> = async (req, res) => {
const userid = req.params.userid;

// Authorization: a user may only update their own profile, unless they hold
// an account-management admin role. Prevents the IDOR where any authenticated
// (or, previously, unauthenticated) caller could overwrite any user's profile
// -- including the email tied to their login (security audit finding #2).
const roles = new Set(
(req.user?.sub || req.user?.role || "").split(",").filter(Boolean)
);
const isAccountAdmin =
roles.has("admin") ||
roles.has("security_admin") ||
roles.has("global_admin");
if (String(req.user?.id) !== String(userid) && !isAccountAdmin) {
return res.status(403).json({
isSuccess: false,
code: "FORBIDDEN",
message: "You are not authorized to update this profile.",
});
}

const { firstName, lastName, email, tenantId } = req.body;
const response = await accountService.updateUserProfile(
userid,
Expand Down
6 changes: 5 additions & 1 deletion server/app/routes/account-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ router.get("/logout", (req, res) => {
res.sendStatus(200);
});

router.put("/:userid", accountController.updateUserProfile);
router.put(
"/:userid",
jwtSession.validateUser,
accountController.updateUserProfile
);

router.get("/:email", accountController.getByEmail);

Expand Down
Loading