|
| 1 | +package io.github.wimdeblauwe.htmx.spring.boot.thymeleaf; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.springframework.beans.factory.annotation.Autowired; |
| 5 | +import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest; |
| 6 | +import org.springframework.context.annotation.Bean; |
| 7 | +import org.springframework.context.annotation.Configuration; |
| 8 | +import org.springframework.security.config.Customizer; |
| 9 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 10 | +import org.springframework.security.test.context.support.WithMockUser; |
| 11 | +import org.springframework.security.web.SecurityFilterChain; |
| 12 | +import org.springframework.security.web.csrf.CsrfToken; |
| 13 | +import org.springframework.stereotype.Controller; |
| 14 | +import org.springframework.test.context.ContextConfiguration; |
| 15 | +import org.springframework.test.web.servlet.MockMvc; |
| 16 | +import org.springframework.test.web.servlet.MvcResult; |
| 17 | +import org.springframework.web.bind.annotation.GetMapping; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 21 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 22 | + |
| 23 | +/** |
| 24 | + * Verifies automatic CSRF token injection into elements using unsafe HTTP methods |
| 25 | + * ({@code hx:post}, {@code hx:put}, {@code hx:patch}, {@code hx:delete}). |
| 26 | + * <p> |
| 27 | + * Tests cover the following scenarios: |
| 28 | + * <ul> |
| 29 | + * <li>The CSRF token is automatically injected into the {@code hx-headers} attribute.</li> |
| 30 | + * <li>Correct merging when {@code hx-headers} already contains additional entries.</li> |
| 31 | + * <li>Proper behavior when CSRF protection is disabled or no token is present.</li> |
| 32 | + * </ul> |
| 33 | + * |
| 34 | + * @author LC Nicolau |
| 35 | + * @since 5.1.0 |
| 36 | + */ |
| 37 | +@WebMvcTest(controllers = HtmxCsrfTest.TestController.class) |
| 38 | +@ContextConfiguration(classes = {HtmxCsrfTest.TestController.class, HtmxCsrfTest.SecurityConfig.class}) |
| 39 | +@WithMockUser |
| 40 | +class HtmxCsrfTest { |
| 41 | + |
| 42 | + @Autowired |
| 43 | + private MockMvc mockMvc; |
| 44 | + |
| 45 | + @Test |
| 46 | + void testHxPostCsrf() throws Exception { |
| 47 | + MvcResult result = mockMvc.perform(get("/htmx-csrf")) |
| 48 | + .andExpect(status().isOk()) |
| 49 | + .andReturn(); |
| 50 | + |
| 51 | + String html = result.getResponse().getContentAsString(); |
| 52 | + assertThat(html) |
| 53 | + .containsPattern("hx-post-div.*hx-post=\"/foo\"") |
| 54 | + .containsPattern("hx-post-headers.*hx-post=\"/foo\""); |
| 55 | + |
| 56 | + CsrfToken csrf = ((CsrfToken) result.getRequest().getAttribute("_csrf")); |
| 57 | + if (csrf == null) { |
| 58 | + assertThat(html) |
| 59 | + .doesNotContainPattern("hx-post-div.*hx-headers=") |
| 60 | + .doesNotContainPattern("hx-post-headers.*hx-headers=.*X-CSRF-TOKEN"); |
| 61 | + } else { |
| 62 | + String token = csrf.getToken(); |
| 63 | + assertThat(html) |
| 64 | + .containsPattern("hx-post-div.*hx-headers=\"\\{"X-CSRF-TOKEN":"" + token + ""}\"") |
| 65 | + .containsPattern("hx-post-headers.*hx-headers=\"\\{"someHeader":true,"X-CSRF-TOKEN":"" + token + ""}\""); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void testHxPutCsrf() throws Exception { |
| 71 | + MvcResult result = mockMvc.perform(get("/htmx-csrf")) |
| 72 | + .andExpect(status().isOk()) |
| 73 | + .andReturn(); |
| 74 | + |
| 75 | + String html = result.getResponse().getContentAsString(); |
| 76 | + assertThat(html) |
| 77 | + .containsPattern("hx-put-div.*hx-put=\"/foo\"") |
| 78 | + .containsPattern("hx-put-headers.*hx-put=\"/foo\""); |
| 79 | + |
| 80 | + CsrfToken csrf = ((CsrfToken) result.getRequest().getAttribute("_csrf")); |
| 81 | + if (csrf == null) { |
| 82 | + assertThat(html) |
| 83 | + .doesNotContainPattern("hx-put-div.*hx-headers=") |
| 84 | + .doesNotContainPattern("hx-put-headers.*hx-headers=.*X-CSRF-TOKEN"); |
| 85 | + } else { |
| 86 | + String token = csrf.getToken(); |
| 87 | + assertThat(html) |
| 88 | + .containsPattern("hx-put-div.*hx-headers=\"\\{"X-CSRF-TOKEN":"" + token + ""}\"") |
| 89 | + .containsPattern("hx-put-headers.*hx-headers=\"\\{"someHeader":true,"X-CSRF-TOKEN":"" + token + ""}\""); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void testHxPatchCsrf() throws Exception { |
| 95 | + MvcResult result = mockMvc.perform(get("/htmx-csrf")) |
| 96 | + .andExpect(status().isOk()) |
| 97 | + .andReturn(); |
| 98 | + |
| 99 | + String html = result.getResponse().getContentAsString(); |
| 100 | + assertThat(html) |
| 101 | + .containsPattern("hx-patch-div.*hx-patch=\"/foo\"") |
| 102 | + .containsPattern("hx-patch-headers.*hx-patch=\"/foo\""); |
| 103 | + |
| 104 | + CsrfToken csrf = ((CsrfToken) result.getRequest().getAttribute("_csrf")); |
| 105 | + if (csrf == null) { |
| 106 | + assertThat(html) |
| 107 | + .doesNotContainPattern("hx-patch-div.*hx-headers=") |
| 108 | + .doesNotContainPattern("hx-patch-headers.*hx-headers=.*X-CSRF-TOKEN"); |
| 109 | + } else { |
| 110 | + String token = csrf.getToken(); |
| 111 | + assertThat(html) |
| 112 | + .containsPattern("hx-patch-div.*hx-headers=\"\\{"X-CSRF-TOKEN":"" + token + ""}\"") |
| 113 | + .containsPattern("hx-patch-headers.*hx-headers=\"\\{"someHeader":true,"X-CSRF-TOKEN":"" + token + ""}\""); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void testHxDeleteCsrf() throws Exception { |
| 119 | + MvcResult result = mockMvc.perform(get("/htmx-csrf")) |
| 120 | + .andExpect(status().isOk()) |
| 121 | + .andReturn(); |
| 122 | + |
| 123 | + String html = result.getResponse().getContentAsString(); |
| 124 | + assertThat(html) |
| 125 | + .containsPattern("hx-delete-div.*hx-delete=\"/foo\"") |
| 126 | + .containsPattern("hx-delete-headers.*hx-delete=\"/foo\""); |
| 127 | + |
| 128 | + CsrfToken csrf = ((CsrfToken) result.getRequest().getAttribute("_csrf")); |
| 129 | + if (csrf == null) { |
| 130 | + assertThat(html) |
| 131 | + .doesNotContainPattern("hx-delete-div.*hx-headers=") |
| 132 | + .doesNotContainPattern("hx-delete-headers.*hx-headers=.*X-CSRF-TOKEN"); |
| 133 | + } else { |
| 134 | + String token = csrf.getToken(); |
| 135 | + assertThat(html) |
| 136 | + .containsPattern("hx-delete-div.*hx-headers=\"\\{"X-CSRF-TOKEN":"" + token + ""}\"") |
| 137 | + .containsPattern("hx-delete-headers.*hx-headers=\"\\{"someHeader":true,"X-CSRF-TOKEN":"" + token + ""}\""); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + @Controller |
| 142 | + static class TestController { |
| 143 | + |
| 144 | + @GetMapping("/htmx-csrf") |
| 145 | + public String csrf() { |
| 146 | + return "htmx-csrf"; |
| 147 | + } |
| 148 | + |
| 149 | + } |
| 150 | + |
| 151 | + @Configuration |
| 152 | + static class SecurityConfig { |
| 153 | + |
| 154 | + @Bean |
| 155 | + SecurityFilterChain securityFilterChain(HttpSecurity http) { |
| 156 | + return http.authorizeHttpRequests(config -> config |
| 157 | + .requestMatchers("/htmx-csrf").authenticated()) |
| 158 | + .csrf(Customizer.withDefaults()) |
| 159 | + .build(); |
| 160 | + } |
| 161 | + |
| 162 | + } |
| 163 | + |
| 164 | +} |
0 commit comments