From 4e99a57f2cc7deaa823d8604f8f3c6480f094956 Mon Sep 17 00:00:00 2001 From: "David M. Johnson" Date: Sat, 21 Nov 2026 17:31:43 -0500 Subject: [PATCH] Serve weblog pages, feeds, search and resources only through forwards Roller forwards a weblog's public URLs to the rendering servlets. Direct requests to the page, feed, search and resources servlets now answer 404, so a weblog's content is reached only at the weblog's own addresses. Comment posts, RSD and media file links keep their direct addresses. --- CHANGES.md | 6 + .../filters/RenderingForwardOnlyFilter.java | 58 ++++++++ app/src/main/webapp/WEB-INF/web.xml | 29 ++++ .../RenderingForwardOnlyFilterTest.java | 134 ++++++++++++++++++ 4 files changed, 227 insertions(+) create mode 100644 app/src/main/java/org/apache/roller/weblogger/ui/rendering/filters/RenderingForwardOnlyFilter.java create mode 100644 app/src/test/java/org/apache/roller/weblogger/ui/rendering/filters/RenderingForwardOnlyFilterTest.java diff --git a/CHANGES.md b/CHANGES.md index 6da5553f18..edc551d1b3 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -80,6 +80,12 @@ domains as before but look at most 63 characters between their parts; a copy of the old file in the uploads directory is still used if present. +- **Weblog content is served only at the weblog's own addresses.** Direct + requests to `/roller-ui/rendering/page/`, `feed/`, `search/` and + `resources/` now answer 404; Roller reaches those servlets by forwarding the + weblog's public URLs. Comment posts, RSD and + `/roller-ui/rendering/media-resources/` links in entries work as before. + ### Bug fixes - **Changing a password ends the account's other sessions.** Saving a new diff --git a/app/src/main/java/org/apache/roller/weblogger/ui/rendering/filters/RenderingForwardOnlyFilter.java b/app/src/main/java/org/apache/roller/weblogger/ui/rendering/filters/RenderingForwardOnlyFilter.java new file mode 100644 index 0000000000..3d5aded88a --- /dev/null +++ b/app/src/main/java/org/apache/roller/weblogger/ui/rendering/filters/RenderingForwardOnlyFilter.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. 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. For additional information regarding + * copyright in this work, please see the NOTICE file in the top level + * directory of this distribution. + */ +package org.apache.roller.weblogger.ui.rendering.filters; + +import java.io.IOException; +import javax.servlet.DispatcherType; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletResponse; + +/** + * Answers 404 to a browser request sent straight to a rendering servlet that + * serves weblog content. Roller forwards a weblog's public addresses to these + * servlets, so pages, feeds, search and theme resources are reached through + * the weblog's own URL and the site's rules for that URL. A forward passes; + * web.xml maps this filter for REQUEST dispatches only. + */ +public class RenderingForwardOnlyFilter implements Filter { + + @Override + public void init(FilterConfig filterConfig) { + // nothing to configure + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, + FilterChain chain) throws IOException, ServletException { + if (request.getDispatcherType() == DispatcherType.REQUEST) { + ((HttpServletResponse) response).sendError(HttpServletResponse.SC_NOT_FOUND); + return; + } + chain.doFilter(request, response); + } + + @Override + public void destroy() { + // nothing to release + } +} diff --git a/app/src/main/webapp/WEB-INF/web.xml b/app/src/main/webapp/WEB-INF/web.xml index a0ee02076b..d516b76fe0 100644 --- a/app/src/main/webapp/WEB-INF/web.xml +++ b/app/src/main/webapp/WEB-INF/web.xml @@ -54,6 +54,11 @@ org.apache.roller.weblogger.ui.core.filters.XmlRpcEnabledFilter + + RenderingForwardOnlyFilter + org.apache.roller.weblogger.ui.rendering.filters.RenderingForwardOnlyFilter + + RequestMappingFilter org.apache.roller.weblogger.ui.rendering.filters.RequestMappingFilter @@ -182,6 +187,30 @@ INCLUDE + + + RenderingForwardOnlyFilter + /roller-ui/rendering/page/* + REQUEST + + + RenderingForwardOnlyFilter + /roller-ui/rendering/feed/* + REQUEST + + + RenderingForwardOnlyFilter + /roller-ui/rendering/search/* + REQUEST + + + RenderingForwardOnlyFilter + /roller-ui/rendering/resources/* + REQUEST + + RequestMappingFilter /* diff --git a/app/src/test/java/org/apache/roller/weblogger/ui/rendering/filters/RenderingForwardOnlyFilterTest.java b/app/src/test/java/org/apache/roller/weblogger/ui/rendering/filters/RenderingForwardOnlyFilterTest.java new file mode 100644 index 0000000000..67c5e5e52b --- /dev/null +++ b/app/src/test/java/org/apache/roller/weblogger/ui/rendering/filters/RenderingForwardOnlyFilterTest.java @@ -0,0 +1,134 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. 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. For additional information regarding + * copyright in this work, please see the NOTICE file in the top level + * directory of this distribution. + */ +package org.apache.roller.weblogger.ui.rendering.filters; + +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.TreeSet; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import javax.servlet.DispatcherType; +import javax.servlet.FilterChain; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.when; + +/** + * Weblog pages, feeds, search and theme resources are served only through a + * forward from the weblog's own URL. Comment posts, RSD and media files keep + * their direct addresses. + */ +class RenderingForwardOnlyFilterTest { + + private static final String FILTER = "RenderingForwardOnlyFilter"; + private static final List FORWARD_ONLY = Arrays.asList( + "/roller-ui/rendering/feed/*", "/roller-ui/rendering/page/*", + "/roller-ui/rendering/resources/*", "/roller-ui/rendering/search/*"); + private static final List DIRECT = Arrays.asList( + "/roller-ui/rendering/comment/*", "/roller-ui/rendering/media-resources/*", + "/roller-ui/rendering/rsd/*"); + + @Test + void webXmlMapsTheFilterForDirectRequestsToWeblogContent() throws Exception { + List patterns = new ArrayList<>(); + for (String mapping : blocks(uncommentedWebXml(), "filter-mapping")) { + if (!values(mapping, "filter-name").contains(FILTER)) { + continue; + } + assertEquals(Arrays.asList("REQUEST"), values(mapping, "dispatcher"), mapping); + patterns.addAll(values(mapping, "url-pattern")); + } + assertEquals(new TreeSet<>(FORWARD_ONLY), new TreeSet<>(patterns)); + } + + @Test + void everyRenderingServletIsEitherForwardOnlyOrKeptDirect() throws Exception { + List rendering = new ArrayList<>(); + for (String mapping : blocks(uncommentedWebXml(), "servlet-mapping")) { + for (String pattern : values(mapping, "url-pattern")) { + if (pattern.startsWith("/roller-ui/rendering/")) { + rendering.add(pattern); + } + } + } + List known = new ArrayList<>(FORWARD_ONLY); + known.addAll(DIRECT); + assertEquals(new TreeSet<>(known), new TreeSet<>(rendering), + "a new rendering servlet needs a choice here"); + } + + @Test + void aDirectRequestGetsNotFound() throws Exception { + HttpServletRequest request = mock(HttpServletRequest.class); + when(request.getDispatcherType()).thenReturn(DispatcherType.REQUEST); + HttpServletResponse response = mock(HttpServletResponse.class); + FilterChain chain = mock(FilterChain.class); + + new RenderingForwardOnlyFilter().doFilter(request, response, chain); + + verify(response).sendError(HttpServletResponse.SC_NOT_FOUND); + verifyNoInteractions(chain); + } + + @Test + void aForwardIsServed() throws Exception { + HttpServletRequest request = mock(HttpServletRequest.class); + when(request.getDispatcherType()).thenReturn(DispatcherType.FORWARD); + HttpServletResponse response = mock(HttpServletResponse.class); + FilterChain chain = mock(FilterChain.class); + + new RenderingForwardOnlyFilter().doFilter(request, response, chain); + + verify(chain).doFilter(request, response); + verify(response, never()).sendError(HttpServletResponse.SC_NOT_FOUND); + } + + private static String uncommentedWebXml() throws Exception { + return new String(Files.readAllBytes(Paths.get("src/main/webapp/WEB-INF/web.xml")), + StandardCharsets.UTF_8).replaceAll("(?s)", ""); + } + + private static List blocks(String xml, String element) { + return values(xml, element, "(?s)<" + element + ">(.*?)"); + } + + private static List values(String xml, String element) { + return values(xml, element, "<" + element + ">\\s*([^<]+?)\\s*"); + } + + private static List values(String xml, String element, String regex) { + List found = new ArrayList<>(); + Matcher m = Pattern.compile(regex).matcher(xml); + while (m.find()) { + found.add(m.group(1)); + } + return found; + } +}