|
| 1 | +/* |
| 2 | + * JBoss, Home of Professional Open Source. |
| 3 | + * Copyright 2019 Red Hat, Inc., and individual contributors |
| 4 | + * as indicated by the @author tags. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package io.undertow.servlet.test.defaultservlet; |
| 20 | + |
| 21 | +import static io.undertow.util.Headers.AUTHORIZATION; |
| 22 | +import static io.undertow.util.Headers.BASIC; |
| 23 | +import static io.undertow.util.Headers.LOCATION; |
| 24 | +import static io.undertow.util.Headers.WWW_AUTHENTICATE; |
| 25 | +import static org.junit.Assert.assertEquals; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | + |
| 29 | +import javax.servlet.ServletException; |
| 30 | + |
| 31 | +import org.apache.http.Header; |
| 32 | +import org.apache.http.HttpResponse; |
| 33 | +import org.apache.http.client.HttpClient; |
| 34 | +import org.apache.http.client.methods.HttpGet; |
| 35 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 36 | +import org.junit.Assert; |
| 37 | +import org.junit.BeforeClass; |
| 38 | +import org.junit.Test; |
| 39 | +import org.junit.runner.RunWith; |
| 40 | + |
| 41 | +import io.undertow.server.handlers.PathHandler; |
| 42 | +import io.undertow.servlet.api.DeploymentInfo; |
| 43 | +import io.undertow.servlet.api.DeploymentManager; |
| 44 | +import io.undertow.servlet.api.LoginConfig; |
| 45 | +import io.undertow.servlet.api.SecurityConstraint; |
| 46 | +import io.undertow.servlet.api.ServletContainer; |
| 47 | +import io.undertow.servlet.api.WebResourceCollection; |
| 48 | +import io.undertow.servlet.test.path.ServletPathMappingTestCase; |
| 49 | +import io.undertow.servlet.test.security.constraint.ServletIdentityManager; |
| 50 | +import io.undertow.servlet.test.util.TestClassIntrospector; |
| 51 | +import io.undertow.servlet.test.util.TestResourceLoader; |
| 52 | +import io.undertow.testutils.DefaultServer; |
| 53 | +import io.undertow.testutils.HttpClientUtils; |
| 54 | +import io.undertow.util.FlexBase64; |
| 55 | +import io.undertow.util.StatusCodes; |
| 56 | + |
| 57 | +/** |
| 58 | + * TestCase on redirect with or without trailing slash when requesting protected path. |
| 59 | + * |
| 60 | + * @author Lin Gao |
| 61 | + */ |
| 62 | +@RunWith(DefaultServer.class) |
| 63 | +public class SecurityRedirectTestCase { |
| 64 | + |
| 65 | + @BeforeClass |
| 66 | + public static void setup() throws ServletException { |
| 67 | + |
| 68 | + final PathHandler root = new PathHandler(); |
| 69 | + final ServletContainer container = ServletContainer.Factory.newInstance(); |
| 70 | + |
| 71 | + ServletIdentityManager identityManager = new ServletIdentityManager(); |
| 72 | + identityManager.addUser("user1", "password1", "role1"); |
| 73 | + |
| 74 | + DeploymentInfo builder = new DeploymentInfo() |
| 75 | + .setClassIntrospecter(TestClassIntrospector.INSTANCE) |
| 76 | + .setClassLoader(ServletPathMappingTestCase.class.getClassLoader()) |
| 77 | + .setContextPath("/servletContext") |
| 78 | + .setDeploymentName("servletContext.war") |
| 79 | + .setResourceManager(new TestResourceLoader(SecurityRedirectTestCase.class)) |
| 80 | + .addWelcomePages("index.html") |
| 81 | + .setIdentityManager(identityManager) |
| 82 | + .setLoginConfig(new LoginConfig("BASIC", "Test Realm")) |
| 83 | + .addSecurityConstraint(new SecurityConstraint() |
| 84 | + .addRoleAllowed("role1") |
| 85 | + .addWebResourceCollection(new WebResourceCollection() |
| 86 | + .addUrlPatterns("/index.html", "/filterpath/*"))); |
| 87 | + |
| 88 | + DeploymentManager manager = container.addDeployment(builder); |
| 89 | + manager.deploy(); |
| 90 | + root.addPrefixPath(builder.getContextPath(), manager.start()); |
| 91 | + DefaultServer.setRootHandler(root); |
| 92 | + } |
| 93 | + |
| 94 | + @SuppressWarnings("deprecation") |
| 95 | + @Test |
| 96 | + public void testSecurityWithWelcomeFileRedirect() throws IOException { |
| 97 | + // disable following redirect |
| 98 | + HttpClient client = HttpClientBuilder.create().disableRedirectHandling().build(); |
| 99 | + try { |
| 100 | + HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext"); |
| 101 | + HttpResponse result = client.execute(get); |
| 102 | + Assert.assertEquals(StatusCodes.FOUND, result.getStatusLine().getStatusCode()); |
| 103 | + Header[] values = result.getHeaders(LOCATION.toString()); |
| 104 | + assertEquals(1, values.length); |
| 105 | + assertEquals(DefaultServer.getDefaultServerURL() + "/servletContext/", values[0].getValue()); |
| 106 | + HttpClientUtils.readResponse(result); |
| 107 | + |
| 108 | + get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/"); |
| 109 | + result = client.execute(get); |
| 110 | + Assert.assertEquals(StatusCodes.UNAUTHORIZED, result.getStatusLine().getStatusCode()); |
| 111 | + |
| 112 | + values = result.getHeaders(WWW_AUTHENTICATE.toString()); |
| 113 | + assertEquals(1, values.length); |
| 114 | + assertEquals(BASIC + " realm=\"Test Realm\"", values[0].getValue()); |
| 115 | + HttpClientUtils.readResponse(result); |
| 116 | + |
| 117 | + get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/"); |
| 118 | + get.addHeader(AUTHORIZATION.toString(), BASIC + " " + FlexBase64.encodeString("user1:password1".getBytes(), false)); |
| 119 | + result = client.execute(get); |
| 120 | + String response = HttpClientUtils.readResponse(result); |
| 121 | + Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode()); |
| 122 | + Assert.assertTrue(response.contains("Redirected home page")); |
| 123 | + } finally { |
| 124 | + client.getConnectionManager().shutdown(); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @SuppressWarnings("deprecation") |
| 129 | + @Test |
| 130 | + public void testSecurityWithoutWelcomeFileRedirect() throws IOException { |
| 131 | + // disable following redirect |
| 132 | + HttpClient client = HttpClientBuilder.create().disableRedirectHandling().build(); |
| 133 | + try { |
| 134 | + HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/filterpath"); |
| 135 | + HttpResponse result = client.execute(get); |
| 136 | + Assert.assertEquals(StatusCodes.UNAUTHORIZED, result.getStatusLine().getStatusCode()); |
| 137 | + HttpClientUtils.readResponse(result); |
| 138 | + |
| 139 | + get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/filterpath/"); |
| 140 | + result = client.execute(get); |
| 141 | + Assert.assertEquals(StatusCodes.UNAUTHORIZED, result.getStatusLine().getStatusCode()); |
| 142 | + |
| 143 | + Header[] values = result.getHeaders(WWW_AUTHENTICATE.toString()); |
| 144 | + assertEquals(1, values.length); |
| 145 | + assertEquals(BASIC + " realm=\"Test Realm\"", values[0].getValue()); |
| 146 | + HttpClientUtils.readResponse(result); |
| 147 | + |
| 148 | + get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/filterpath"); |
| 149 | + get.addHeader(AUTHORIZATION.toString(), BASIC + " " + FlexBase64.encodeString("user1:password1".getBytes(), false)); |
| 150 | + result = client.execute(get); |
| 151 | + Assert.assertEquals(StatusCodes.FOUND, result.getStatusLine().getStatusCode()); |
| 152 | + values = result.getHeaders(LOCATION.toString()); |
| 153 | + assertEquals(1, values.length); |
| 154 | + assertEquals(DefaultServer.getDefaultServerURL() + "/servletContext/filterpath/", values[0].getValue()); |
| 155 | + HttpClientUtils.readResponse(result); |
| 156 | + |
| 157 | + get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/filterpath/filtered.txt"); |
| 158 | + get.addHeader(AUTHORIZATION.toString(), BASIC + " " + FlexBase64.encodeString("user1:password1".getBytes(), false)); |
| 159 | + result = client.execute(get); |
| 160 | + String response = HttpClientUtils.readResponse(result); |
| 161 | + Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode()); |
| 162 | + Assert.assertTrue(response.equals("Stuart")); |
| 163 | + } finally { |
| 164 | + client.getConnectionManager().shutdown(); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | +} |
0 commit comments