Skip to content

Commit

Permalink
WICKET-7024 decode PackageResourceReference URL attributes only insid…
Browse files Browse the repository at this point in the history
…e requests for it
  • Loading branch information
pedrosans committed Oct 27, 2024
1 parent 01705aa commit f83f61b
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
*/
package org.apache.wicket.core.request.resource;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.StringContains.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -26,7 +30,10 @@
import java.util.Locale;

import org.apache.wicket.Application;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.ThreadContext;
import org.apache.wicket.markup.IMarkupResourceStreamProvider;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.protocol.http.mock.MockHttpServletRequest;
import org.apache.wicket.protocol.http.mock.MockHttpServletResponse;
import org.apache.wicket.request.Request;
Expand All @@ -43,6 +50,8 @@
import org.apache.wicket.request.resource.ResourceReference.UrlAttributes;
import org.apache.wicket.response.ByteArrayResponse;
import org.apache.wicket.util.io.IOUtils;
import org.apache.wicket.util.resource.IResourceStream;
import org.apache.wicket.util.resource.StringResourceStream;
import org.apache.wicket.util.tester.WicketTestCase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -396,4 +405,67 @@ void noRequestCycle()
assertEquals(variations[1], resource.getResourceStream().getVariation());
}

@Test
public void getResouceWithNoStyle()
{
tester.executeUrl(
"wicket/resource/org.apache.wicket.core.request.resource.PackageResourceReferenceTest/a.css");

assertThat(tester.getLastResponseAsString(), not(containsString("color")));
}

@Test
public void getStyleFromSession()
{
tester.getSession().setStyle("blue");
tester.executeUrl(
"wicket/resource/org.apache.wicket.core.request.resource.PackageResourceReferenceTest/a.css");

assertThat(tester.getLastResponseAsString(), containsString("blue"));
}

@Test
public void decodeStyleFromUrl()
{
tester.getSession().setStyle("blue");
tester.executeUrl(
"wicket/resource/org.apache.wicket.core.request.resource.PackageResourceReferenceTest/a.css?-orange");

assertThat(tester.getLastResponseAsString(), containsString("orange"));
}

/**
* @see https://issues.apache.org/jira/browse/WICKET-7024
*/
@Test
public void notDecodeStyleFromUrl()
{
tester.executeUrl(
"wicket/bookmarkable/org.apache.wicket.core.request.resource.PackageResourceReferenceTest$TestPage?0-1.0-resumeButton&_=1730041277224");

TestPage page = (TestPage)tester.getLastRenderedPage();

assertThat(page.resource.getStyle(), is(not("1.0")));
}

public static class TestPage extends WebPage implements IMarkupResourceStreamProvider
{
CssPackageResource resource;

@Override
protected void onConfigure()
{
super.onConfigure();
resource = (CssPackageResource)new PackageResourceReference(scope, "a.css")
.getResource();
}

@Override
public IResourceStream getMarkupResourceStream(MarkupContainer container,
Class<?> containerClass)
{
return new StringResourceStream("<html><head></head><body></body></html>");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.a{
color: blue;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.a{
color: orange;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.wicket.core.util.resource.locator.IResourceStreamLocator;
import org.apache.wicket.request.Url;
import org.apache.wicket.request.cycle.RequestCycle;
import org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler;
import org.apache.wicket.resource.ResourceUtil;
import org.apache.wicket.util.lang.Generics;
import org.apache.wicket.util.lang.Packages;
Expand Down Expand Up @@ -114,7 +115,8 @@ public PackageResource getResource()

RequestCycle requestCycle = RequestCycle.get();
UrlAttributes urlAttributes = null;
if (requestCycle != null)
if (requestCycle != null
&& requestCycle.getActiveRequestHandler() instanceof ResourceReferenceRequestHandler)
{
//resource attributes (locale, style, variation) might be encoded in the URL
final Url url = requestCycle.getRequest().getUrl();
Expand Down

0 comments on commit f83f61b

Please sign in to comment.