Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isFile handles encoded URIs #250

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Calendar;
import java.net.URI;
import java.net.URISyntaxException;

/**
* Conditions must be met when the filter is processing a url.
Expand Down Expand Up @@ -267,7 +269,11 @@ public ConditionMatch getConditionMatch(final HttpServletRequest hsRequest) {
if (requestURI.startsWith(hsRequest.getContextPath() + "/")){
requestURI = requestURI.substring(hsRequest.getContextPath().length());
}
String fileName = rule.getServletContext().getRealPath(requestURI);
// Decode any encoded URI chars
try {
requestURI = new URI( requestURI ).getPath();
} catch( URISyntaxException URIe ) {}
String fileName = rule.getServletContext().getRealPath( requestURI );
if ( log.isDebugEnabled() ) log.debug("fileName found is " + fileName);
return evaluateStringCondition(fileName);
} else {
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/org/tuckey/web/filters/urlrewrite/RuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,19 @@ public void testFilename() throws InvocationTargetException, IOException, Servle
assertEquals("/found-the-file", rewrittenUrl.getTarget());
}

public void testEncodedFilename() throws InvocationTargetException, IOException, ServletException {
NormalRule rule = new NormalRule();
rule.setTo("/found-the-file");
Condition condition = new Condition();
condition.setType("request-filename");
condition.setOperator("isfile");
rule.addCondition(condition);
rule.initialise(new MockServletContext());
MockRequest request = new MockRequest("/conf%2Dtest1.xml");
NormalRewrittenUrl rewrittenUrl = (NormalRewrittenUrl) rule.matches(request.getRequestURI(), request, response);
assertEquals("/found-the-file", rewrittenUrl.getTarget());
}

public void testNotFilename() throws InvocationTargetException, IOException, ServletException {
NormalRule rule = new NormalRule();
rule.setTo("/not-found-the-file");
Expand Down