|
| 1 | +/* |
| 2 | + * JBoss, Home of Professional Open Source. |
| 3 | + * Copyright 2022 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.security.constraint; |
| 20 | + |
| 21 | +import io.undertow.server.handlers.PathHandler; |
| 22 | +import io.undertow.servlet.api.DeploymentInfo; |
| 23 | +import io.undertow.servlet.api.DeploymentManager; |
| 24 | +import io.undertow.servlet.api.LoginConfig; |
| 25 | +import io.undertow.servlet.api.SecurityConstraint; |
| 26 | +import io.undertow.servlet.api.SecurityInfo; |
| 27 | +import io.undertow.servlet.api.ServletContainer; |
| 28 | +import io.undertow.servlet.api.ServletInfo; |
| 29 | +import io.undertow.servlet.api.WebResourceCollection; |
| 30 | +import io.undertow.servlet.test.SimpleServletTestCase; |
| 31 | +import io.undertow.servlet.test.util.MessageServlet; |
| 32 | +import io.undertow.servlet.test.util.TestClassIntrospector; |
| 33 | +import io.undertow.testutils.DefaultServer; |
| 34 | +import io.undertow.testutils.HttpClientUtils; |
| 35 | +import io.undertow.testutils.TestHttpClient; |
| 36 | +import io.undertow.util.StatusCodes; |
| 37 | +import org.apache.http.HttpResponse; |
| 38 | +import org.apache.http.client.methods.HttpGet; |
| 39 | +import org.junit.BeforeClass; |
| 40 | +import org.junit.Test; |
| 41 | +import org.junit.runner.RunWith; |
| 42 | + |
| 43 | +import javax.servlet.ServletException; |
| 44 | +import java.io.IOException; |
| 45 | + |
| 46 | +import static org.junit.Assert.assertEquals; |
| 47 | + |
| 48 | +/** |
| 49 | + * Do the same as SecurityConstraintURLMappingTestCase, with a small difference, all access to / are denied and public/* is no longer |
| 50 | + * covered by a SecurityConstraint. |
| 51 | + * Verify that all works as in the super class test case, except for public/*, that now is forbidden for all HTTP methods. |
| 52 | + * |
| 53 | + * @author Flavia Rainone |
| 54 | + */ |
| 55 | +@RunWith(DefaultServer.class) |
| 56 | +public class SecurityConstraintUrlMappingWithUnspecifiedForbiddenTestCase extends SecurityConstraintUrlMappingTestCase { |
| 57 | + |
| 58 | + @BeforeClass |
| 59 | + public static void setup() throws ServletException { |
| 60 | + |
| 61 | + final PathHandler root = new PathHandler(); |
| 62 | + final ServletContainer container = ServletContainer.Factory.newInstance(); |
| 63 | + |
| 64 | + ServletInfo s = new ServletInfo("servlet", AuthenticationMessageServlet.class) |
| 65 | + .addInitParam(MessageServlet.MESSAGE, HELLO_WORLD) |
| 66 | + .addMapping("/role1") |
| 67 | + .addMapping("/role2") |
| 68 | + .addMapping("/starstar") |
| 69 | + .addMapping("/secured/role2/*") |
| 70 | + .addMapping("/secured/1/2/*") |
| 71 | + .addMapping("/public/*") |
| 72 | + .addMapping("/extension/*"); |
| 73 | + |
| 74 | + ServletIdentityManager identityManager = new ServletIdentityManager(); |
| 75 | + identityManager.addUser("user1", "password1", "role1"); |
| 76 | + identityManager.addUser("user2", "password2", "role2", "**"); |
| 77 | + identityManager.addUser("user3", "password3", "role1", "role2"); |
| 78 | + identityManager.addUser("user4", "password4", "badRole"); |
| 79 | + |
| 80 | + DeploymentInfo builder = new DeploymentInfo() |
| 81 | + .setClassLoader(SimpleServletTestCase.class.getClassLoader()) |
| 82 | + .setContextPath("/servletContext") |
| 83 | + .setClassIntrospecter(TestClassIntrospector.INSTANCE) |
| 84 | + .setDeploymentName("servletContext.war") |
| 85 | + .setIdentityManager(identityManager) |
| 86 | + .setLoginConfig(new LoginConfig("BASIC", "Test Realm")) |
| 87 | + .addServlet(s); |
| 88 | + |
| 89 | + builder.addSecurityConstraint(new SecurityConstraint() |
| 90 | + .addWebResourceCollection(new WebResourceCollection() |
| 91 | + .addUrlPattern("/role1")) |
| 92 | + .addRoleAllowed("role1")); |
| 93 | + |
| 94 | + builder.addSecurityConstraint(new SecurityConstraint() |
| 95 | + .addWebResourceCollection(new WebResourceCollection() |
| 96 | + .addUrlPattern("/starstar")) |
| 97 | + .addRoleAllowed("**")); |
| 98 | + builder.addSecurityConstraint(new SecurityConstraint() |
| 99 | + .addWebResourceCollection(new WebResourceCollection() |
| 100 | + .addUrlPattern("/secured/*")) |
| 101 | + .addRoleAllowed("role2")); |
| 102 | + builder.addSecurityConstraint(new SecurityConstraint() |
| 103 | + .addWebResourceCollection(new WebResourceCollection() |
| 104 | + .addUrlPattern("/secured/*")) |
| 105 | + .addRoleAllowed("role2")); |
| 106 | + builder.addSecurityConstraint(new SecurityConstraint() |
| 107 | + .addWebResourceCollection(new WebResourceCollection() |
| 108 | + .addUrlPattern("/secured/1/*")) |
| 109 | + .addRoleAllowed("role1")); |
| 110 | + builder.addSecurityConstraint(new SecurityConstraint() |
| 111 | + .addWebResourceCollection(new WebResourceCollection() |
| 112 | + .addUrlPattern("/secured/1/2/*")) |
| 113 | + .addRoleAllowed("role2")); |
| 114 | + builder.addSecurityConstraint(new SecurityConstraint() |
| 115 | + .addWebResourceCollection(new WebResourceCollection() |
| 116 | + .addUrlPattern("*.html")) |
| 117 | + .addRoleAllowed("role2")); |
| 118 | + builder.addSecurityConstraint(new SecurityConstraint() |
| 119 | + .addWebResourceCollection(new WebResourceCollection() |
| 120 | + .addUrlPattern("/")).setEmptyRoleSemantic(SecurityInfo.EmptyRoleSemantic.DENY)); |
| 121 | + builder.addSecurityConstraint(new SecurityConstraint() |
| 122 | + .addWebResourceCollection(new WebResourceCollection() |
| 123 | + .addUrlPattern("/public/postSecured/*") |
| 124 | + .addHttpMethod("POST")) |
| 125 | + .addRoleAllowed("role1")); |
| 126 | + |
| 127 | + DeploymentManager manager = container.addDeployment(builder); |
| 128 | + manager.deploy(); |
| 129 | + root.addPrefixPath(builder.getContextPath(), manager.start()); |
| 130 | + |
| 131 | + builder = new DeploymentInfo() |
| 132 | + .setClassLoader(SimpleServletTestCase.class.getClassLoader()) |
| 133 | + .setContextPath("/star") |
| 134 | + .setClassIntrospecter(TestClassIntrospector.INSTANCE) |
| 135 | + .setDeploymentName("servletContext.war") |
| 136 | + .setIdentityManager(identityManager) |
| 137 | + .setLoginConfig(new LoginConfig("BASIC", "Test Realm")) |
| 138 | + .addSecurityRole("**") |
| 139 | + .addServlet(s); |
| 140 | + |
| 141 | + builder.addSecurityConstraint(new SecurityConstraint() |
| 142 | + .addWebResourceCollection(new WebResourceCollection() |
| 143 | + .addUrlPattern("/starstar")) |
| 144 | + .addRoleAllowed("**")); |
| 145 | + |
| 146 | + manager = container.addDeployment(builder); |
| 147 | + manager.deploy(); |
| 148 | + root.addPrefixPath(builder.getContextPath(), manager.start()); |
| 149 | + DefaultServer.setRootHandler(root); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + @Override |
| 154 | + public void testUnknown() throws IOException { |
| 155 | + TestHttpClient client = new TestHttpClient(); |
| 156 | + try { |
| 157 | + HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/unknown"); |
| 158 | + HttpResponse result = client.execute(get); |
| 159 | + assertEquals(StatusCodes.FORBIDDEN, result.getStatusLine().getStatusCode()); |
| 160 | + HttpClientUtils.readResponse(result); |
| 161 | + } finally { |
| 162 | + client.getConnectionManager().shutdown(); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + public void testPublic() throws IOException { |
| 168 | + TestHttpClient client = new TestHttpClient(); |
| 169 | + try { |
| 170 | + HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/public"); |
| 171 | + HttpResponse result = client.execute(get); |
| 172 | + assertEquals(StatusCodes.FORBIDDEN, result.getStatusLine().getStatusCode()); |
| 173 | + HttpClientUtils.readResponse(result); |
| 174 | + } finally { |
| 175 | + client.getConnectionManager().shutdown(); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + @Override |
| 181 | + public void testExtensionMatch() throws IOException { |
| 182 | + runSimpleUrlTest(DefaultServer.getDefaultServerURL() + "/servletContext/extension/a.html", "user1:password1", "user2:password2"); |
| 183 | + TestHttpClient client = new TestHttpClient(); |
| 184 | + try { |
| 185 | + HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/public/a.html"); |
| 186 | + get.addHeader("ExpectedMechanism", "None"); |
| 187 | + get.addHeader("ExpectedUser", "None"); |
| 188 | + HttpResponse result = client.execute(get); |
| 189 | + assertEquals(StatusCodes.UNAUTHORIZED, result.getStatusLine().getStatusCode()); |
| 190 | + HttpClientUtils.readResponse(result); |
| 191 | + } finally { |
| 192 | + client.getConnectionManager().shutdown(); |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments