|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2021 CloudBees, Inc. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | +package hudson.security; |
| 25 | + |
| 26 | +import com.gargoylesoftware.htmlunit.util.Cookie; |
| 27 | +import jenkins.model.Jenkins; |
| 28 | +import org.junit.Assert; |
| 29 | +import org.junit.Rule; |
| 30 | +import org.junit.Test; |
| 31 | +import org.junit.runner.RunWith; |
| 32 | +import org.junit.runners.Parameterized; |
| 33 | +import org.jvnet.hudson.test.JenkinsRule; |
| 34 | +import org.jvnet.hudson.test.MockAuthorizationStrategy; |
| 35 | + |
| 36 | +import java.util.Arrays; |
| 37 | +import java.util.List; |
| 38 | + |
| 39 | +/** |
| 40 | + * Split from {@link SecurityRealmTest} because this is parameterized. |
| 41 | + */ |
| 42 | +@RunWith(Parameterized.class) |
| 43 | +public class SecurityRealmSecurity2371Test { |
| 44 | + |
| 45 | + public static final String SESSION_COOKIE_NAME = "JSESSIONID"; |
| 46 | + public static final String USERNAME = "alice"; |
| 47 | + |
| 48 | + private final Integer mode; |
| 49 | + |
| 50 | + @Rule |
| 51 | + public JenkinsRule j = new JenkinsRule(); |
| 52 | + |
| 53 | + @Parameterized.Parameters |
| 54 | + public static List<Integer> modes() { |
| 55 | + return Arrays.asList(null, 1, 2); |
| 56 | + } |
| 57 | + |
| 58 | + public SecurityRealmSecurity2371Test(Integer mode) { |
| 59 | + this.mode = mode; |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testSessionChangeOnLogin() throws Exception { |
| 64 | + if (mode != null) { |
| 65 | + System.setProperty(SecurityRealm.class.getName() + ".sessionFixationProtectionMode", String.valueOf(mode)); |
| 66 | + } |
| 67 | + try { |
| 68 | + j.jenkins.setSecurityRealm(j.createDummySecurityRealm()); |
| 69 | + j.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy().grant(Jenkins.READ).everywhere().toEveryone().grant(Jenkins.ADMINISTER).everywhere().to(USERNAME)); |
| 70 | + final JenkinsRule.WebClient webClient = j.createWebClient(); |
| 71 | + webClient.goTo(""); |
| 72 | + try { |
| 73 | + webClient.goTo("manage"); |
| 74 | + Assert.fail("anonymous session should not be able to go to /manage"); |
| 75 | + } catch (Exception ex) { |
| 76 | + // OK |
| 77 | + } |
| 78 | + final Cookie anonymousCookie = webClient.getCookieManager().getCookie(SESSION_COOKIE_NAME); // dynamic cookie names are only set when run through Winstone |
| 79 | + webClient.login(USERNAME); |
| 80 | + webClient.goTo(""); |
| 81 | + final Cookie aliceCookie = webClient.getCookieManager().getCookie(SESSION_COOKIE_NAME); |
| 82 | + |
| 83 | + // Confirm the session cookie changed |
| 84 | + // We cannot just call #assertNotEquals(Cookie, Cookie) because it doesn't actually look at #getValue() |
| 85 | + Assert.assertNotEquals(anonymousCookie.getValue(), aliceCookie.getValue()); |
| 86 | + |
| 87 | + // Now ensure the old session was actually invalidated / is not associated with the new auth |
| 88 | + webClient.getCookieManager().clearCookies(); |
| 89 | + webClient.getCookieManager().addCookie(anonymousCookie); |
| 90 | + try { |
| 91 | + webClient.goTo("manage"); |
| 92 | + Assert.fail("anonymous session should not be able to go to /manage"); |
| 93 | + } catch (Exception ex) { |
| 94 | + // OK |
| 95 | + } |
| 96 | + } finally { |
| 97 | + System.clearProperty(SecurityRealm.class.getName() + ".sessionFixationProtectionMode"); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Explicitly disable |
| 103 | + */ |
| 104 | + @Test |
| 105 | + public void optOut() throws Exception { |
| 106 | + System.setProperty(SecurityRealm.class.getName() + ".sessionFixationProtectionMode", String.valueOf(0)); |
| 107 | + try { |
| 108 | + j.jenkins.setSecurityRealm(j.createDummySecurityRealm()); |
| 109 | + j.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy().grant(Jenkins.READ).everywhere().toEveryone().grant(Jenkins.ADMINISTER).everywhere().to(USERNAME)); |
| 110 | + final JenkinsRule.WebClient webClient = j.createWebClient(); |
| 111 | + webClient.goTo(""); |
| 112 | + |
| 113 | + final Cookie anonymousCookie = webClient.getCookieManager().getCookie(SESSION_COOKIE_NAME); // dynamic cookie names are only set when run through Winstone |
| 114 | + webClient.login(USERNAME); |
| 115 | + webClient.goTo(""); |
| 116 | + final Cookie aliceCookie = webClient.getCookieManager().getCookie(SESSION_COOKIE_NAME); |
| 117 | + |
| 118 | + // Confirm the session cookie did not change |
| 119 | + Assert.assertEquals(anonymousCookie.getValue(), aliceCookie.getValue()); |
| 120 | + } finally { |
| 121 | + System.clearProperty(SecurityRealm.class.getName() + ".sessionFixationProtectionMode"); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments