|
| 1 | +package com.stablecoin.payments.platform.infrastructure.security; |
| 2 | + |
| 3 | +import jakarta.servlet.ServletException; |
| 4 | +import org.junit.jupiter.api.DisplayName; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.springframework.mock.web.MockFilterChain; |
| 7 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 8 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | + |
| 12 | +import static com.stablecoin.payments.platform.infrastructure.security.SecurityHeadersFilter.HEADER_CACHE_CONTROL; |
| 13 | +import static com.stablecoin.payments.platform.infrastructure.security.SecurityHeadersFilter.HEADER_CONTENT_SECURITY_POLICY; |
| 14 | +import static com.stablecoin.payments.platform.infrastructure.security.SecurityHeadersFilter.HEADER_PERMISSIONS_POLICY; |
| 15 | +import static com.stablecoin.payments.platform.infrastructure.security.SecurityHeadersFilter.HEADER_PRAGMA; |
| 16 | +import static com.stablecoin.payments.platform.infrastructure.security.SecurityHeadersFilter.HEADER_REFERRER_POLICY; |
| 17 | +import static com.stablecoin.payments.platform.infrastructure.security.SecurityHeadersFilter.HEADER_STRICT_TRANSPORT_SECURITY; |
| 18 | +import static com.stablecoin.payments.platform.infrastructure.security.SecurityHeadersFilter.HEADER_X_CONTENT_TYPE_OPTIONS; |
| 19 | +import static com.stablecoin.payments.platform.infrastructure.security.SecurityHeadersFilter.HEADER_X_FRAME_OPTIONS; |
| 20 | +import static com.stablecoin.payments.platform.infrastructure.security.SecurityHeadersFilter.HEADER_X_XSS_PROTECTION; |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | + |
| 23 | +@DisplayName("SecurityHeadersFilter") |
| 24 | +class SecurityHeadersFilterTest { |
| 25 | + |
| 26 | + @Test |
| 27 | + @DisplayName("should add all security headers when enabled with defaults") |
| 28 | + void shouldAddAllSecurityHeadersWhenEnabledWithDefaults() throws ServletException, IOException { |
| 29 | + // given |
| 30 | + var properties = new SecurityHeadersProperties(true, true, 31_536_000L); |
| 31 | + var filter = new SecurityHeadersFilter(properties); |
| 32 | + var request = new MockHttpServletRequest(); |
| 33 | + var response = new MockHttpServletResponse(); |
| 34 | + var filterChain = new MockFilterChain(); |
| 35 | + |
| 36 | + // when |
| 37 | + filter.doFilter(request, response, filterChain); |
| 38 | + |
| 39 | + // then |
| 40 | + assertThat(response.getHeaderNames()) |
| 41 | + .containsExactlyInAnyOrder( |
| 42 | + HEADER_X_CONTENT_TYPE_OPTIONS, |
| 43 | + HEADER_X_FRAME_OPTIONS, |
| 44 | + HEADER_CONTENT_SECURITY_POLICY, |
| 45 | + HEADER_X_XSS_PROTECTION, |
| 46 | + HEADER_CACHE_CONTROL, |
| 47 | + HEADER_PRAGMA, |
| 48 | + HEADER_REFERRER_POLICY, |
| 49 | + HEADER_PERMISSIONS_POLICY, |
| 50 | + HEADER_STRICT_TRANSPORT_SECURITY |
| 51 | + ); |
| 52 | + |
| 53 | + assertThat(response.getHeader(HEADER_X_CONTENT_TYPE_OPTIONS)).isEqualTo("nosniff"); |
| 54 | + assertThat(response.getHeader(HEADER_X_FRAME_OPTIONS)).isEqualTo("DENY"); |
| 55 | + assertThat(response.getHeader(HEADER_CONTENT_SECURITY_POLICY)).isEqualTo("default-src 'none'"); |
| 56 | + assertThat(response.getHeader(HEADER_X_XSS_PROTECTION)).isEqualTo("0"); |
| 57 | + assertThat(response.getHeader(HEADER_CACHE_CONTROL)).isEqualTo("no-store"); |
| 58 | + assertThat(response.getHeader(HEADER_PRAGMA)).isEqualTo("no-cache"); |
| 59 | + assertThat(response.getHeader(HEADER_REFERRER_POLICY)).isEqualTo("strict-origin-when-cross-origin"); |
| 60 | + assertThat(response.getHeader(HEADER_PERMISSIONS_POLICY)).isEqualTo("camera=(), microphone=(), geolocation=()"); |
| 61 | + assertThat(response.getHeader(HEADER_STRICT_TRANSPORT_SECURITY)).isEqualTo("max-age=31536000; includeSubDomains"); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + @DisplayName("should not add HSTS header when hsts-enabled is false") |
| 66 | + void shouldNotAddHstsHeaderWhenHstsDisabled() throws ServletException, IOException { |
| 67 | + // given |
| 68 | + var properties = new SecurityHeadersProperties(true, false, 31_536_000L); |
| 69 | + var filter = new SecurityHeadersFilter(properties); |
| 70 | + var request = new MockHttpServletRequest(); |
| 71 | + var response = new MockHttpServletResponse(); |
| 72 | + var filterChain = new MockFilterChain(); |
| 73 | + |
| 74 | + // when |
| 75 | + filter.doFilter(request, response, filterChain); |
| 76 | + |
| 77 | + // then |
| 78 | + assertThat(response.getHeaderNames()).doesNotContain(HEADER_STRICT_TRANSPORT_SECURITY); |
| 79 | + assertThat(response.getHeader(HEADER_X_CONTENT_TYPE_OPTIONS)).isEqualTo("nosniff"); |
| 80 | + assertThat(response.getHeader(HEADER_X_FRAME_OPTIONS)).isEqualTo("DENY"); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + @DisplayName("should use custom HSTS max-age when configured") |
| 85 | + void shouldUseCustomHstsMaxAge() throws ServletException, IOException { |
| 86 | + // given |
| 87 | + var customMaxAge = 86_400L; |
| 88 | + var properties = new SecurityHeadersProperties(true, true, customMaxAge); |
| 89 | + var filter = new SecurityHeadersFilter(properties); |
| 90 | + var request = new MockHttpServletRequest(); |
| 91 | + var response = new MockHttpServletResponse(); |
| 92 | + var filterChain = new MockFilterChain(); |
| 93 | + |
| 94 | + // when |
| 95 | + filter.doFilter(request, response, filterChain); |
| 96 | + |
| 97 | + // then |
| 98 | + assertThat(response.getHeader(HEADER_STRICT_TRANSPORT_SECURITY)).isEqualTo("max-age=86400; includeSubDomains"); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + @DisplayName("should continue filter chain after adding headers") |
| 103 | + void shouldContinueFilterChainAfterAddingHeaders() throws ServletException, IOException { |
| 104 | + // given |
| 105 | + var properties = new SecurityHeadersProperties(true, true, 31_536_000L); |
| 106 | + var filter = new SecurityHeadersFilter(properties); |
| 107 | + var request = new MockHttpServletRequest(); |
| 108 | + var response = new MockHttpServletResponse(); |
| 109 | + var filterChain = new MockFilterChain(); |
| 110 | + |
| 111 | + // when |
| 112 | + filter.doFilter(request, response, filterChain); |
| 113 | + |
| 114 | + // then |
| 115 | + assertThat(filterChain.getRequest()).isEqualTo(request); |
| 116 | + assertThat(filterChain.getResponse()).isEqualTo(response); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + @DisplayName("should apply default property values when nulls provided") |
| 121 | + void shouldApplyDefaultPropertyValuesWhenNullsProvided() throws ServletException, IOException { |
| 122 | + // given |
| 123 | + var properties = new SecurityHeadersProperties(null, null, null); |
| 124 | + var filter = new SecurityHeadersFilter(properties); |
| 125 | + var request = new MockHttpServletRequest(); |
| 126 | + var response = new MockHttpServletResponse(); |
| 127 | + var filterChain = new MockFilterChain(); |
| 128 | + |
| 129 | + // when |
| 130 | + filter.doFilter(request, response, filterChain); |
| 131 | + |
| 132 | + // then |
| 133 | + assertThat(response.getHeader(HEADER_STRICT_TRANSPORT_SECURITY)).isEqualTo("max-age=31536000; includeSubDomains"); |
| 134 | + assertThat(response.getHeader(HEADER_X_CONTENT_TYPE_OPTIONS)).isEqualTo("nosniff"); |
| 135 | + } |
| 136 | +} |
0 commit comments