823 Corrigir retorno de status ao realizar login com usuário inexistente#851
Hidden character warning
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
| void shouldReturnUnauthorizedWhenCredentialsAreInvalid() throws Exception { | ||
| SignInDTO requestDto = new SignInDTO( | ||
| "[email protected]", | ||
| "senha-incorreta" |
There was a problem hiding this comment.
Reverta todas as alterações nesse arquivo AuthControllerTest.
| when(authService.signIn(requestDto)) | ||
| .thenThrow(new UserNotFoundException()); | ||
| when(authService.signIn(any(SignInDTO.class))) | ||
| .thenThrow(new InvalidPasswordException("E-mail ou senha incorretos")); |
There was a problem hiding this comment.
Retire a string de dentro de todos os InvalidPasswordException (as edições de retorno sempre devem ser feitas dentro do exception em si), e volte com o requestDto dentro dos signIn
| .thenThrow(new InvalidPasswordException("E-mail ou senha incorretos")); | ||
|
|
||
| mockMvc.perform(post(BASE_URL + "/signin") | ||
| mockMvc.perform(post("/auth/signin") |
There was a problem hiding this comment.
reverta todos esses /auth para BASE_URL + "/signin"
| @@ -35,136 +35,136 @@ | |||
|
|
|||
There was a problem hiding this comment.
Reverta todas as alterações nesse arquivo AuthApplicationService
JoaoGabrielCosta2004
left a comment
There was a problem hiding this comment.
Resolver todos os problemas comentados
Qual é o objetivo desta PR?
Esta Pull Request resolve uma vulnerabilidade de segurança conhecida como "Enumeração de Usuários" mapeada nas Issues 822 e 823. O sistema estava retornando 404 Not Found quando um e-mail não existia e 400 Bad Request quando a senha estava incorreta. Esse comportamento expunha indiretamente quais e-mails estavam ou não na base de dados do sistema. O objetivo foi padronizar o retorno para 401 Unauthorized com uma mensagem genérica, fortalecendo a segurança da API.
O que foi feito?
Unificação no Serviço: A classe AuthApplicationServiceImpl foi alterada para capturar as exceções UserNotFoundException e InvalidPasswordException simultaneamente, mascarando o erro real e lançando sempre um erro genérico de "E-mail ou senha incorretos".
Ajuste no Manipulador de Exceções: A classe AuthExceptionHandler foi atualizada para mapear a InvalidPasswordException diretamente para o status HTTP 401 Unauthorized.
Ajustes nos Testes Unitários: Os testes shouldReturnUnauthorizedWhenCredentialsAreInvalid e shouldReturnUnauthorizedWhenUserDoesNotExist da classe AuthControllerTest foram atualizados para simular o novo fluxo genérico do serviço, validando perfeitamente o retorno 401.