Skip to content

Commit 13c6621

Browse files
committed
Prevent rewrite loops by avoiding nested rewrites for forwarded requests (#121)
1 parent ef143a2 commit 13c6621

5 files changed

Lines changed: 74 additions & 6 deletions

File tree

‎CHANGELOG.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Bugfixes:
1212
* Dynaview: Parameter injection now correctly happens before view determination (#120)
1313
* Fixed decoding of quotation mark character. Thanks to Bruno Aranda for the patch. (#122)
1414
* Use equals() instead of matches() to compare URL with view-id. Thanks to mbenson for bringing this up.
15-
15+
* Prevent rewrite loops by avoiding nested rewrites for forwarded requests (#121)
1616

1717
------------------------------------
1818
VERSION 3.3.0

‎core/src/main/java/com/ocpsoft/pretty/PrettyFilter.java‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,13 @@ private void rewrite(final HttpServletRequest req, final HttpServletResponse res
135135
if (!rewriteOccurred(req))
136136
{
137137
RewriteEngine rewriteEngine = new RewriteEngine();
138-
URL url = PrettyContext.getCurrentInstance(req).getRequestURL();
138+
139+
/*
140+
* Get the URL from a new detached instance of PrettyContext
141+
* so that we get the forwarded URL instead of the original one
142+
* if the filter is called for a forwarded request.
143+
*/
144+
URL url = PrettyContext.newDetachedInstance(req).getRequestURL();
139145

140146
try
141147
{
@@ -223,10 +229,6 @@ else if (StringUtils.isNotBlank(rule.getUrl()))
223229
{
224230
throw new PrettyException("Error occurred during canonicalization of request <[" + url + "]>", e);
225231
}
226-
finally
227-
{
228-
setRewriteOccurred(req);
229-
}
230232
}
231233
}
232234

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.ocpsoft.pretty.faces.test.rewrite;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import org.jboss.arquillian.api.Deployment;
6+
import org.jboss.arquillian.junit.Arquillian;
7+
import org.jboss.jsfunit.jsfsession.JSFClientSession;
8+
import org.jboss.jsfunit.jsfsession.JSFSession;
9+
import org.jboss.shrinkwrap.api.spec.WebArchive;
10+
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
13+
import com.ocpsoft.pretty.faces.test.PrettyFacesTestBase;
14+
15+
@RunWith(Arquillian.class)
16+
public class RewriteLoopTest extends PrettyFacesTestBase
17+
{
18+
19+
@Deployment
20+
public static WebArchive createDeployment()
21+
{
22+
return PrettyFacesTestBase.createDeployment()
23+
.addResource("rewrite/rewrite-loop.xhtml", "rewrite-loop.xhtml")
24+
.addWebResource("rewrite/rewrite-loop-config.xml", "pretty-config.xml");
25+
}
26+
27+
@Test
28+
public void testRewriteRuleSubstiuteMatchingRulePattern() throws Exception
29+
{
30+
31+
// Access the URL that is rewritten to the correct JSF page
32+
JSFSession jsfSession = new JSFSession("/rewrite-loop.html");
33+
JSFClientSession client = jsfSession.getJSFClientSession();
34+
35+
// the page should render fine without any rewrite loop
36+
assertTrue(client.getContentPage().getUrl().toString().endsWith("/rewrite-loop.html"));
37+
assertTrue(client.getPageAsText().contains("Rewrite loop test page rendered"));
38+
39+
}
40+
41+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<pretty-config xmlns="http://ocpsoft.com/prettyfaces/3.3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
2+
3+
<!-- matches "/rewrite-loop.html" AND "/rewrite-loop.jsf" -->
4+
<rewrite match="/rewrite-loop\..*" substitute="/rewrite-loop.jsf" redirect="chain" />
5+
6+
</pretty-config>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3+
<html xmlns="http://www.w3.org/1999/xhtml"
4+
xmlns:f="http://java.sun.com/jsf/core"
5+
xmlns:h="http://java.sun.com/jsf/html"
6+
xmlns:pretty="http://ocpsoft.com/prettyfaces"
7+
xmlns:ui="http://java.sun.com/jsf/facelets">
8+
9+
<head>
10+
<title>Rewrite Loop Test</title>
11+
</head>
12+
13+
<body>
14+
15+
<p>Rewrite loop test page rendered!</p>
16+
17+
</body>
18+
19+
</html>

0 commit comments

Comments
 (0)