You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by ad...@apache.org on 2026/08/17 21:35:14 UTC

(wicket) branch master updated: restrict SourcesPage to resources of the page's own package (#1523)

This is an automated email from the ASF dual-hosted git repository.

bitstorm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/wicket.git


The following commit(s) were added to refs/heads/master by this push:
     new 225f119052 restrict SourcesPage to resources of the page's own package (#1523)
225f119052 is described below

commit 225f119052bf8e734859f4750882436d70417a71
Author: Farkhalit Rida <75...@users.noreply.github.com>
AuthorDate: Tue Aug 18 03:05:08 2026 +0530

    restrict SourcesPage to resources of the page's own package (#1523)
---
 .../apache/wicket/examples/source/SourcesPage.java | 16 ++++-
 .../wicket/examples/source/SourcesPageTest.java    | 68 ++++++++++++++++++++++
 2 files changed, 82 insertions(+), 2 deletions(-)

diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/source/SourcesPage.java b/wicket-examples/src/main/java/org/apache/wicket/examples/source/SourcesPage.java
index 69f754bc3b..a8dc03e902 100644
--- a/wicket-examples/src/main/java/org/apache/wicket/examples/source/SourcesPage.java
+++ b/wicket-examples/src/main/java/org/apache/wicket/examples/source/SourcesPage.java
@@ -101,6 +101,12 @@ public class SourcesPage extends WebPage
 			try
 			{
 				source = (name != null) ? name : sourceParam.toString();
+				if (!packagedResources.getObject().contains(source))
+				{
+					log.error("user is trying to access resource: {} which is not part of the package of {}",
+						source, getPageTargetClass().getName());
+					return "Unable to read the source for " + source;
+				}
 				resourceAsStream = getPageTargetClass().getResourceAsStream(source);
 				if (resourceAsStream == null)
 				{
@@ -198,7 +204,7 @@ public class SourcesPage extends WebPage
 					}
 					else
 					{
-						String absolutePath = scope.getResource("").toExternalForm();
+						String absolutePath = resource.toExternalForm();
 						File basedir;
 						URI uri;
 						try
@@ -290,7 +296,7 @@ public class SourcesPage extends WebPage
 		private FilesBrowser(String id)
 		{
 			super(id);
-			ListView<String> lv = new ListView<String>("file", new PackagedResourcesModel())
+			ListView<String> lv = new ListView<String>("file", packagedResources)
 			{
 				@Override
 				protected void populateItem(final ListItem<String> item)
@@ -377,6 +383,12 @@ public class SourcesPage extends WebPage
 	 */
 	private String name;
 
+	/**
+	 * The resources of the package of the selected page. Doubles as the white list of what may be
+	 * displayed.
+	 */
+	private final PackagedResourcesModel packagedResources = new PackagedResourcesModel();
+
 	private transient Class<? extends Page> page;
 
 	/**
diff --git a/wicket-examples/src/test/java/org/apache/wicket/examples/source/SourcesPageTest.java b/wicket-examples/src/test/java/org/apache/wicket/examples/source/SourcesPageTest.java
new file mode 100644
index 0000000000..bc505a5540
--- /dev/null
+++ b/wicket-examples/src/test/java/org/apache/wicket/examples/source/SourcesPageTest.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.examples.source;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.apache.wicket.examples.helloworld.HelloWorld;
+import org.apache.wicket.request.mapper.parameter.PageParameters;
+import org.apache.wicket.util.tester.WicketTestCase;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests for {@link SourcesPage}.
+ */
+public class SourcesPageTest extends WicketTestCase
+{
+	private String render(String source)
+	{
+		PageParameters parameters = SourcesPage.generatePageParameters(HelloWorld.class, source);
+		tester.startPage(SourcesPage.class, parameters);
+		return tester.getLastResponseAsString();
+	}
+
+	/**
+	 * A resource of the package of the requested page is displayed.
+	 */
+	@Test
+	public void sourceOfThePackageIsDisplayed()
+	{
+		assertFalse(render("HelloWorld.html").contains("Unable to read the source for"));
+	}
+
+	/**
+	 * An absolute class path resource outside of the package of the requested page is refused.
+	 */
+	@Test
+	public void absoluteClassPathResourceIsRefused()
+	{
+		String response = render("/META-INF/NOTICE");
+		assertFalse(response.contains("Apache Software Foundation (http"));
+		assertTrue(response.contains("Unable to read the source for"));
+	}
+
+	/**
+	 * A parent directory reference is refused.
+	 */
+	@Test
+	public void parentDirectoryResourceIsRefused()
+	{
+		String response = render("../style.css");
+		assertTrue(response.contains("Unable to read the source for"));
+	}
+}