Skip to content

Commit 92e67ee

Browse files
committed
Fixes Issue ocpsoft#111 - Container based security broken when Rewrite is installed
1 parent 11b29fb commit 92e67ee

7 files changed

Lines changed: 160 additions & 3 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.servlet.container;
17+
18+
import junit.framework.Assert;
19+
20+
import org.apache.http.client.methods.HttpGet;
21+
import org.jboss.arquillian.container.test.api.Deployment;
22+
import org.jboss.arquillian.junit.Arquillian;
23+
import org.jboss.shrinkwrap.api.spec.WebArchive;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
import org.ocpsoft.rewrite.config.ConfigurationProvider;
27+
import org.ocpsoft.rewrite.servlet.ServletRoot;
28+
import org.ocpsoft.rewrite.test.HttpAction;
29+
import org.ocpsoft.rewrite.test.RewriteTest;
30+
31+
/**
32+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
33+
*/
34+
@RunWith(Arquillian.class)
35+
public class ContainerSecurityTest extends RewriteTest
36+
{
37+
@Deployment(testable = false)
38+
public static WebArchive getDeployment()
39+
{
40+
WebArchive deployment = RewriteTest.getDeployment()
41+
.addPackages(true, ServletRoot.class.getPackage())
42+
.addAsWebInfResource("org/ocpsoft/rewrite/servlet/container/web.xml")
43+
.addAsWebResource("org/ocpsoft/rewrite/servlet/container/login.html")
44+
.addAsWebResource("org/ocpsoft/rewrite/servlet/container/login-fail.html")
45+
.addAsWebResource("org/ocpsoft/rewrite/servlet/container/home.html", "/user/home.html")
46+
.addAsServiceProvider(ConfigurationProvider.class, ContainerSecurityTestProvider.class);
47+
48+
System.out.println(deployment.toString(true));
49+
50+
return deployment;
51+
}
52+
53+
@Test
54+
public void testContainerSecurityFunctionsWithRewriteJoin() throws Exception
55+
{
56+
HttpAction<HttpGet> action = get("/user/home");
57+
Assert.assertEquals(200, action.getResponse().getStatusLine().getStatusCode());
58+
Assert.assertTrue(action.getResponseContent().contains("LOGIN PAGE"));
59+
}
60+
61+
@Test
62+
public void testContainerSecurityFunctionsWithRewriteUnHandled() throws Exception
63+
{
64+
HttpAction<HttpGet> action = get("/user/x");
65+
Assert.assertEquals(200, action.getResponse().getStatusLine().getStatusCode());
66+
Assert.assertTrue(action.getResponseContent().contains("LOGIN PAGE"));
67+
}
68+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.servlet.container;
17+
18+
import javax.servlet.ServletContext;
19+
20+
import org.ocpsoft.rewrite.config.Configuration;
21+
import org.ocpsoft.rewrite.config.ConfigurationBuilder;
22+
import org.ocpsoft.rewrite.servlet.config.HttpConfigurationProvider;
23+
import org.ocpsoft.rewrite.servlet.config.rule.Join;
24+
25+
/**
26+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
27+
*
28+
*/
29+
public class ContainerSecurityTestProvider extends HttpConfigurationProvider
30+
{
31+
@Override
32+
public int priority()
33+
{
34+
return 0;
35+
}
36+
37+
@Override
38+
public Configuration getConfiguration(final ServletContext context)
39+
{
40+
Configuration config = ConfigurationBuilder.begin()
41+
.addRule(Join.path("/user/home").to("/user/home.html"));
42+
43+
return config;
44+
}
45+
46+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
USER HOME
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
LOGIN FAIL
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
LOGIN PAGE
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
5+
version="3.0">
6+
7+
<security-role>
8+
<description>Administrators</description>
9+
<role-name>admin</role-name>
10+
</security-role>
11+
<security-role>
12+
<description>Users</description>
13+
<role-name>user</role-name>
14+
</security-role>
15+
<security-constraint>
16+
<web-resource-collection>
17+
<web-resource-name>General</web-resource-name>
18+
<url-pattern>/user/*</url-pattern>
19+
<http-method>DELETE</http-method>
20+
<http-method>GET</http-method>
21+
<http-method>POST</http-method>
22+
<http-method>PUT</http-method>
23+
<http-method>HEAD</http-method>
24+
<http-method>TRACE</http-method>
25+
<http-method>OPTIONS</http-method>
26+
</web-resource-collection>
27+
<auth-constraint>
28+
<role-name>user</role-name>
29+
</auth-constraint>
30+
<user-data-constraint>
31+
<transport-guarantee>NONE</transport-guarantee>
32+
</user-data-constraint>
33+
</security-constraint>
34+
35+
<login-config>
36+
<auth-method>FORM</auth-method>
37+
<form-login-config>
38+
<form-login-page>/login.html</form-login-page>
39+
<form-error-page>/login-fail.html</form-error-page>
40+
</form-login-config>
41+
</login-config>
42+
</web-app>

‎impl-servlet/src/main/java/org/ocpsoft/rewrite/servlet/impl/HttpRewriteWrappedRequest.java‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.Map;
2222
import java.util.TreeMap;
2323

24-
import javax.servlet.ServletRequestWrapper;
2524
import javax.servlet.http.HttpServletRequest;
2625

2726
import org.ocpsoft.rewrite.servlet.RewriteWrappedRequest;
@@ -41,8 +40,7 @@ public class HttpRewriteWrappedRequest extends RewriteWrappedRequest
4140
*/
4241
public HttpRewriteWrappedRequest(final HttpServletRequest request, final Map<String, String[]> additionalParams)
4342
{
44-
super((HttpServletRequest) (request instanceof ServletRequestWrapper ? ((ServletRequestWrapper) request)
45-
.getRequest() : request));
43+
super(request);
4644

4745
modifiableParameters = new TreeMap<String, String[]>();
4846
modifiableParameters.putAll(additionalParams);

0 commit comments

Comments
 (0)