|
4 | 4 | import static org.hamcrest.Matchers.allOf; |
5 | 5 | import static org.hamcrest.Matchers.endsWith; |
6 | 6 | import static org.hamcrest.Matchers.equalTo; |
| 7 | +import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 8 | +import static org.hamcrest.Matchers.is; |
7 | 9 | import static org.hamcrest.Matchers.nullValue; |
8 | 10 | import static org.hamcrest.Matchers.startsWith; |
9 | 11 | import static org.jvnet.hudson.test.LoggerRule.recorded; |
10 | 12 |
|
| 13 | +import hudson.ExtensionList; |
11 | 14 | import hudson.security.csrf.CrumbExclusion; |
12 | 15 | import jakarta.servlet.FilterChain; |
13 | 16 | import jakarta.servlet.ServletException; |
14 | 17 | import jakarta.servlet.http.HttpServletRequest; |
15 | 18 | import jakarta.servlet.http.HttpServletResponse; |
16 | 19 | import java.io.IOException; |
17 | 20 | import java.net.URL; |
| 21 | +import java.util.concurrent.atomic.AtomicInteger; |
18 | 22 | import java.util.logging.Level; |
19 | 23 | import jenkins.model.JenkinsLocationConfiguration; |
| 24 | +import jenkins.security.csp.Contributor; |
| 25 | +import jenkins.security.csp.CspBuilder; |
20 | 26 | import jenkins.util.HttpServletFilter; |
21 | 27 | import org.hamcrest.Matcher; |
22 | 28 | import org.htmlunit.HttpMethod; |
|
27 | 33 | import org.jvnet.hudson.test.LoggerRule; |
28 | 34 | import org.jvnet.hudson.test.TestExtension; |
29 | 35 | import org.jvnet.hudson.test.junit.jupiter.WithJenkins; |
| 36 | +import org.xml.sax.SAXException; |
30 | 37 |
|
31 | 38 | @WithJenkins |
32 | 39 | public class CspFilterTest { |
@@ -99,6 +106,44 @@ void testFilterWithoutCsp(JenkinsRule j) throws Exception { |
99 | 106 | endsWith(":YW5vbnltb3Vz::L3Rlc3QtZmlsdGVyLXdpdGhvdXQtY3NwL3NvbWUtcGF0aA==' but got 'null'")))); |
100 | 107 | } |
101 | 108 |
|
| 109 | + @Test |
| 110 | + void testCspBuilderNotCalledWithHeaderDisabled(JenkinsRule j) throws IOException, SAXException { |
| 111 | + try (JenkinsRule.WebClient webClient = j.createWebClient().withJavaScriptEnabled(false)) { |
| 112 | + final AtomicInteger counter = ExtensionList.lookupSingleton(CountingContributor.class).counter; |
| 113 | + { |
| 114 | + int start = counter.get(); |
| 115 | + webClient.goTo(""); |
| 116 | + int end = counter.get(); |
| 117 | + |
| 118 | + final int difference = end - start; |
| 119 | + assertThat(difference, greaterThanOrEqualTo(1)); |
| 120 | + } |
| 121 | + |
| 122 | + System.setProperty(SystemPropertyHeaderDecider.SYSTEM_PROPERTY_NAME, ""); |
| 123 | + try { |
| 124 | + int start = counter.get(); |
| 125 | + webClient.goTo(""); |
| 126 | + int end = counter.get(); |
| 127 | + |
| 128 | + // no calls expected |
| 129 | + final int difference = end - start; |
| 130 | + assertThat(difference, is(0)); |
| 131 | + } finally { |
| 132 | + System.clearProperty(SystemPropertyHeaderDecider.SYSTEM_PROPERTY_NAME); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @TestExtension("testCspBuilderNotCalledWithHeaderDisabled") |
| 138 | + public static class CountingContributor implements Contributor { |
| 139 | + AtomicInteger counter = new AtomicInteger(0); |
| 140 | + |
| 141 | + @Override |
| 142 | + public void apply(CspBuilder cspBuilder) { |
| 143 | + counter.incrementAndGet(); |
| 144 | + } |
| 145 | + } |
| 146 | + |
102 | 147 | @TestExtension |
103 | 148 | public static class TestFilter implements HttpServletFilter { |
104 | 149 | @Override |
|
0 commit comments