Skip to content

Commit

Permalink
Skip reset password requests with HEAD method
Browse files Browse the repository at this point in the history
  • Loading branch information
jbiland-nt committed Jun 26, 2023
1 parent 862eb92 commit 3800fc7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ public String emailSentPage(@ModelAttribute("code") String code,
return "email_sent";
}

@RequestMapping(value = "/reset_password", method = RequestMethod.HEAD)
public void resetPassword() {
// Some mail providers initially send a HEAD request to check the validity of the link before redirecting users.
}

@RequestMapping(value = "/reset_password", method = RequestMethod.GET, params = {"code"})
public String resetPasswordPage(Model model,
HttpServletResponse response,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import static org.mockito.Mockito.*;
import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.head;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
Expand Down Expand Up @@ -282,6 +283,21 @@ void testResetPasswordPage() throws Exception {
.andExpect(content().string(containsString("<input type=\"hidden\" name=\"username\" value=\"username\"/>")));
}

@Test
void testResetPasswordPageWithPriorHeadRequest() throws Exception {
ExpiringCode code = codeStore.generateCode("{\"user_id\" : \"some-user-id\"}", new Timestamp(System.currentTimeMillis() + 1000000), null, IdentityZoneHolder.get().getId());
mockMvc.perform(head("/reset_password").param("email", "[email protected]").param("code", code.getCode()))
.andExpect(status().isOk());
mockMvc.perform(get("/reset_password").param("email", "[email protected]").param("code", code.getCode()))
.andExpect(status().isOk())
.andDo(print())
.andExpect(view().name("reset_password"))
.andExpect(model().attribute("email", "email"))
.andExpect(model().attribute("username", "username"))
.andExpect(content().string(containsString("<div class=\"email-display\">Username: username</div>")))
.andExpect(content().string(containsString("<input type=\"hidden\" name=\"username\" value=\"username\"/>")));
}

@Test
void testResetPasswordPageDuplicate() throws Exception {
ExpiringCode code = codeStore.generateCode("{\"user_id\" : \"some-user-id\"}", new Timestamp(System.currentTimeMillis() + 1000000), null, IdentityZoneHolder.get().getId());
Expand Down

0 comments on commit 3800fc7

Please sign in to comment.