-
Notifications
You must be signed in to change notification settings - Fork 8.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Require mfa code to change email #10354
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,9 +53,14 @@ describe('MeController', () => { | |
password: 'password', | ||
authIdentities: [], | ||
role: 'global:owner', | ||
mfaEnabled: false, | ||
}); | ||
const reqBody = { email: '[email protected]', firstName: 'John', lastName: 'Potato' }; | ||
const req = mock<MeRequest.UserUpdate>({ user, body: reqBody, browserId }); | ||
const req = mock<MeRequest.UserUpdate>({ user, browserId }); | ||
req.body = { | ||
email: '[email protected]', | ||
firstName: 'John', | ||
lastName: 'Potato', | ||
}; | ||
const res = mock<Response>(); | ||
userRepository.findOneByOrFail.mockResolvedValue(user); | ||
userRepository.findOneOrFail.mockResolvedValue(user); | ||
|
@@ -67,7 +72,7 @@ describe('MeController', () => { | |
expect(externalHooks.run).toHaveBeenCalledWith('user.profile.beforeUpdate', [ | ||
user.id, | ||
user.email, | ||
reqBody, | ||
req.body, | ||
]); | ||
|
||
expect(userService.update).toHaveBeenCalled(); | ||
|
@@ -98,25 +103,25 @@ describe('MeController', () => { | |
password: 'password', | ||
authIdentities: [], | ||
role: 'global:member', | ||
mfaEnabled: false, | ||
}); | ||
const reqBody = { email: '[email protected]', firstName: 'John', lastName: 'Potato' }; | ||
const req = mock<MeRequest.UserUpdate>({ user, browserId }); | ||
req.body = reqBody; | ||
req.body = { email: '[email protected]', firstName: 'John', lastName: 'Potato' }; | ||
const res = mock<Response>(); | ||
userRepository.findOneOrFail.mockResolvedValue(user); | ||
jest.spyOn(jwt, 'sign').mockImplementation(() => 'signed-token'); | ||
|
||
// Add invalid data to the request payload | ||
Object.assign(reqBody, { id: '0', role: 'global:owner' }); | ||
Object.assign(req.body, { id: '0', role: 'global:owner' }); | ||
|
||
await controller.updateCurrentUser(req, res); | ||
|
||
expect(userService.update).toHaveBeenCalled(); | ||
|
||
const updatePayload = userService.update.mock.calls[0][1]; | ||
expect(updatePayload.email).toBe(reqBody.email); | ||
expect(updatePayload.firstName).toBe(reqBody.firstName); | ||
expect(updatePayload.lastName).toBe(reqBody.lastName); | ||
expect(updatePayload.email).toBe(req.body.email); | ||
expect(updatePayload.firstName).toBe(req.body.firstName); | ||
expect(updatePayload.lastName).toBe(req.body.lastName); | ||
expect(updatePayload.id).toBeUndefined(); | ||
expect(updatePayload.role).toBeUndefined(); | ||
}); | ||
|
@@ -127,10 +132,11 @@ describe('MeController', () => { | |
password: 'password', | ||
authIdentities: [], | ||
role: 'global:owner', | ||
mfaEnabled: false, | ||
}); | ||
const reqBody = { email: '[email protected]', firstName: 'John', lastName: 'Potato' }; | ||
const req = mock<MeRequest.UserUpdate>({ user, body: reqBody }); | ||
// userService.findOneOrFail.mockResolvedValue(user); | ||
req.body = reqBody; // We don't want the body to be a mock object | ||
|
||
externalHooks.run.mockImplementationOnce(async (hookName) => { | ||
if (hookName === 'user.profile.beforeUpdate') { | ||
|
@@ -142,6 +148,76 @@ describe('MeController', () => { | |
new BadRequestError('Invalid email address'), | ||
); | ||
}); | ||
|
||
describe('when mfa is enabled', () => { | ||
it('should throw BadRequestError if mfa code is missing', async () => { | ||
const user = mock<User>({ | ||
id: '123', | ||
email: '[email protected]', | ||
password: 'password', | ||
authIdentities: [], | ||
role: 'global:owner', | ||
mfaEnabled: true, | ||
}); | ||
const req = mock<MeRequest.UserUpdate>({ user, browserId }); | ||
req.body = { email: '[email protected]', firstName: 'John', lastName: 'Potato' }; | ||
|
||
await expect(controller.updateCurrentUser(req, mock())).rejects.toThrowError( | ||
new BadRequestError('Two-factor code is required to change email'), | ||
); | ||
}); | ||
|
||
it('should throw InvalidMfaCodeError if mfa code is invalid', async () => { | ||
const user = mock<User>({ | ||
id: '123', | ||
email: '[email protected]', | ||
password: 'password', | ||
authIdentities: [], | ||
role: 'global:owner', | ||
mfaEnabled: true, | ||
}); | ||
const req = mock<MeRequest.UserUpdate>({ user, browserId }); | ||
req.body = { | ||
email: '[email protected]', | ||
firstName: 'John', | ||
lastName: 'Potato', | ||
mfaCode: 'invalid', | ||
}; | ||
mockMfaService.validateMfa.mockResolvedValue(false); | ||
|
||
await expect(controller.updateCurrentUser(req, mock())).rejects.toThrow( | ||
InvalidMfaCodeError, | ||
); | ||
}); | ||
|
||
it("should update the user's email if mfa code is valid", async () => { | ||
const user = mock<User>({ | ||
id: '123', | ||
email: '[email protected]', | ||
password: 'password', | ||
authIdentities: [], | ||
role: 'global:owner', | ||
mfaEnabled: true, | ||
}); | ||
const req = mock<MeRequest.UserUpdate>({ user, browserId }); | ||
req.body = { | ||
email: '[email protected]', | ||
firstName: 'John', | ||
lastName: 'Potato', | ||
mfaCode: '123456', | ||
}; | ||
const res = mock<Response>(); | ||
userRepository.findOneByOrFail.mockResolvedValue(user); | ||
userRepository.findOneOrFail.mockResolvedValue(user); | ||
jest.spyOn(jwt, 'sign').mockImplementation(() => 'signed-token'); | ||
userService.toPublic.mockResolvedValue({} as unknown as PublicUser); | ||
mockMfaService.validateMfa.mockResolvedValue(true); | ||
|
||
const result = await controller.updateCurrentUser(req, res); | ||
|
||
expect(result).toEqual({}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('updatePassword', () => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this instead be
close: MfaModalClosedEventPayload
, andmfaCode
instead be?: string
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to make a clear distinction between when the value is coming from the MFA modal and when from the "plumbing" (i.e. the
Modal
base).undefined
means the user closed the modal (handled byModal
), a value meansPromptMfaCodeModal
closed the modal.