FrankChen021 commented on code in PR #19812:
URL: https://github.com/apache/druid/pull/19812#discussion_r3686788459


##########
server/src/test/java/org/apache/druid/server/http/RedirectFilterTest.java:
##########
@@ -0,0 +1,131 @@
+/*
+ * 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.druid.server.http;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import javax.servlet.FilterChain;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.net.URI;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+
+@RunWith(MockitoJUnitRunner.class)
+public class RedirectFilterTest
+{
+  private static final String REQUEST_URI = "/druid/coordinator/v1/loadstatus";
+  private static final String QUERY_STRING = "simple=true";
+
+  @Mock
+  private RedirectInfo redirectInfo;
+  @Mock
+  private HttpServletRequest request;
+  @Mock
+  private HttpServletResponse response;
+  @Mock
+  private FilterChain filterChain;
+
+  private RedirectFilter redirectFilter;
+
+  @Before
+  public void setUp()
+  {
+    redirectFilter = new RedirectFilter(redirectInfo);
+    Mockito.when(request.getRequestURI()).thenReturn(REQUEST_URI);
+    Mockito.when(request.getQueryString()).thenReturn(QUERY_STRING);
+    Mockito.when(redirectInfo.doLocal(REQUEST_URI)).thenReturn(false);
+  }
+
+  @Test
+  public void testRedirect() throws Exception
+  {
+    final String location = "https://leader.example:8081"; + REQUEST_URI + "?" 
+ QUERY_STRING + "#result";
+    Mockito.when(redirectInfo.getRedirectURL(QUERY_STRING, 
REQUEST_URI)).thenReturn(URI.create(location).toURL());
+
+    redirectFilter.doFilter(request, response, filterChain);
+
+    
Mockito.verify(response).setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
+    Mockito.verify(response).setHeader("Location", location);
+    Mockito.verifyNoInteractions(filterChain);
+  }
+
+  @Test
+  public void testRedirectPreservesEncodedNewlines() throws Exception
+  {
+    final String location = "https://leader.example/path%0D%0Avalue?query=%0a";;
+    Mockito.when(redirectInfo.getRedirectURL(QUERY_STRING, 
REQUEST_URI)).thenReturn(URI.create(location).toURL());
+
+    redirectFilter.doFilter(request, response, filterChain);
+
+    
Mockito.verify(response).setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
+    Mockito.verify(response).setHeader("Location", location);
+  }

Review Comment:
   Addressed in commit 6e192e4173: the encoded-newline redirect test now 
verifies that the filter chain is not invoked. RedirectFilterTest passes 4/4; 
server checkstyle and strict test compilation also pass.



##########
server/src/test/java/org/apache/druid/server/http/RedirectFilterTest.java:
##########
@@ -0,0 +1,131 @@
+/*
+ * 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.druid.server.http;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import javax.servlet.FilterChain;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.net.URI;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+
+@RunWith(MockitoJUnitRunner.class)
+public class RedirectFilterTest
+{
+  private static final String REQUEST_URI = "/druid/coordinator/v1/loadstatus";
+  private static final String QUERY_STRING = "simple=true";
+
+  @Mock
+  private RedirectInfo redirectInfo;
+  @Mock
+  private HttpServletRequest request;
+  @Mock
+  private HttpServletResponse response;
+  @Mock
+  private FilterChain filterChain;
+
+  private RedirectFilter redirectFilter;
+
+  @Before
+  public void setUp()
+  {
+    redirectFilter = new RedirectFilter(redirectInfo);
+    Mockito.when(request.getRequestURI()).thenReturn(REQUEST_URI);
+    Mockito.when(request.getQueryString()).thenReturn(QUERY_STRING);
+    Mockito.when(redirectInfo.doLocal(REQUEST_URI)).thenReturn(false);
+  }
+
+  @Test
+  public void testRedirect() throws Exception
+  {
+    final String location = "https://leader.example:8081"; + REQUEST_URI + "?" 
+ QUERY_STRING + "#result";
+    Mockito.when(redirectInfo.getRedirectURL(QUERY_STRING, 
REQUEST_URI)).thenReturn(URI.create(location).toURL());
+
+    redirectFilter.doFilter(request, response, filterChain);
+
+    
Mockito.verify(response).setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
+    Mockito.verify(response).setHeader("Location", location);
+    Mockito.verifyNoInteractions(filterChain);
+  }
+
+  @Test
+  public void testRedirectPreservesEncodedNewlines() throws Exception
+  {
+    final String location = "https://leader.example/path%0D%0Avalue?query=%0a";;
+    Mockito.when(redirectInfo.getRedirectURL(QUERY_STRING, 
REQUEST_URI)).thenReturn(URI.create(location).toURL());
+
+    redirectFilter.doFilter(request, response, filterChain);
+
+    
Mockito.verify(response).setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
+    Mockito.verify(response).setHeader("Location", location);
+  }

Review Comment:
   Addressed in 6e192e4173. The encoded-newline redirect test now verifies that 
the filter chain is not invoked.



##########
server/src/main/java/org/apache/druid/server/http/RedirectFilter.java:
##########
@@ -72,17 +72,23 @@ public void doFilter(ServletRequest req, ServletResponse 
res, FilterChain chain)
     if (redirectInfo.doLocal(request.getRequestURI())) {
       chain.doFilter(request, response);
     } else {
-      URL url = redirectInfo.getRedirectURL(request.getQueryString(), 
request.getRequestURI());
-      log.debug("Forwarding request to [%s]", url);
-
+      final URL url = redirectInfo.getRedirectURL(request.getQueryString(), 
request.getRequestURI());
       if (url == null) {
         // We apparently have nothing to redirect to, so let's do a Service 
Unavailable
         response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
         return;
       }
 
+      final String location = url.toString();
+      final String sanitizedLocation = location.replace('\r', ' 
').replace('\n', ' ');
+      if (!location.equals(sanitizedLocation)) {
+        response.sendError(HttpServletResponse.SC_BAD_REQUEST);

Review Comment:
   Addressed in 6e192e4173. Redirect validation now checks CR and LF directly 
and sets Location from the original validated string.



##########
server/src/test/java/org/apache/druid/server/http/RedirectFilterTest.java:
##########
@@ -0,0 +1,131 @@
+/*
+ * 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.druid.server.http;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import javax.servlet.FilterChain;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.net.URI;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+
+@RunWith(MockitoJUnitRunner.class)
+public class RedirectFilterTest
+{
+  private static final String REQUEST_URI = "/druid/coordinator/v1/loadstatus";
+  private static final String QUERY_STRING = "simple=true";
+
+  @Mock
+  private RedirectInfo redirectInfo;
+  @Mock
+  private HttpServletRequest request;
+  @Mock
+  private HttpServletResponse response;
+  @Mock
+  private FilterChain filterChain;
+
+  private RedirectFilter redirectFilter;
+
+  @Before
+  public void setUp()
+  {
+    redirectFilter = new RedirectFilter(redirectInfo);
+    Mockito.when(request.getRequestURI()).thenReturn(REQUEST_URI);
+    Mockito.when(request.getQueryString()).thenReturn(QUERY_STRING);
+    Mockito.when(redirectInfo.doLocal(REQUEST_URI)).thenReturn(false);
+  }
+
+  @Test
+  public void testRedirect() throws Exception
+  {
+    final String location = "https://leader.example:8081"; + REQUEST_URI + "?" 
+ QUERY_STRING + "#result";
+    Mockito.when(redirectInfo.getRedirectURL(QUERY_STRING, 
REQUEST_URI)).thenReturn(URI.create(location).toURL());
+
+    redirectFilter.doFilter(request, response, filterChain);
+
+    
Mockito.verify(response).setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
+    Mockito.verify(response).setHeader("Location", location);
+    Mockito.verifyNoInteractions(filterChain);
+  }
+
+  @Test
+  public void testRedirectPreservesEncodedNewlines() throws Exception
+  {
+    final String location = "https://leader.example/path%0D%0Avalue?query=%0a";;
+    Mockito.when(redirectInfo.getRedirectURL(QUERY_STRING, 
REQUEST_URI)).thenReturn(URI.create(location).toURL());
+
+    redirectFilter.doFilter(request, response, filterChain);
+
+    
Mockito.verify(response).setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
+    Mockito.verify(response).setHeader("Location", location);
+  }
+
+  @Test
+  public void testRedirectRejectsCarriageReturn() throws Exception
+  {
+    assertInvalidRedirect("https://leader.example/path\rInjected: value");
+  }
+
+  @Test
+  public void testRedirectRejectsLineFeed() throws Exception
+  {
+    assertInvalidRedirect("https://leader.example/path\nInjected: value");
+  }
+
+  private void assertInvalidRedirect(String location) throws Exception
+  {
+    Mockito.when(redirectInfo.getRedirectURL(QUERY_STRING, 
REQUEST_URI)).thenReturn(urlWithExternalForm(location));
+
+    redirectFilter.doFilter(request, response, filterChain);
+
+    Mockito.verify(response).sendError(HttpServletResponse.SC_BAD_REQUEST);
+    Mockito.verify(response, Mockito.never()).setStatus(Mockito.anyInt());
+    Mockito.verify(response, Mockito.never()).setHeader(Mockito.anyString(), 
Mockito.anyString());
+  }

Review Comment:
   Addressed in commit 6e192e4173: invalid redirect targets now verify that the 
filter chain is not invoked after the 400 response. RedirectFilterTest passes 
4/4; server checkstyle and strict test compilation also pass.



##########
server/src/test/java/org/apache/druid/server/http/RedirectFilterTest.java:
##########
@@ -0,0 +1,131 @@
+/*
+ * 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.druid.server.http;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import javax.servlet.FilterChain;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.net.URI;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+
+@RunWith(MockitoJUnitRunner.class)
+public class RedirectFilterTest
+{
+  private static final String REQUEST_URI = "/druid/coordinator/v1/loadstatus";
+  private static final String QUERY_STRING = "simple=true";
+
+  @Mock
+  private RedirectInfo redirectInfo;
+  @Mock
+  private HttpServletRequest request;
+  @Mock
+  private HttpServletResponse response;
+  @Mock
+  private FilterChain filterChain;
+
+  private RedirectFilter redirectFilter;
+
+  @Before
+  public void setUp()
+  {
+    redirectFilter = new RedirectFilter(redirectInfo);
+    Mockito.when(request.getRequestURI()).thenReturn(REQUEST_URI);
+    Mockito.when(request.getQueryString()).thenReturn(QUERY_STRING);
+    Mockito.when(redirectInfo.doLocal(REQUEST_URI)).thenReturn(false);
+  }
+
+  @Test
+  public void testRedirect() throws Exception
+  {
+    final String location = "https://leader.example:8081"; + REQUEST_URI + "?" 
+ QUERY_STRING + "#result";
+    Mockito.when(redirectInfo.getRedirectURL(QUERY_STRING, 
REQUEST_URI)).thenReturn(URI.create(location).toURL());
+
+    redirectFilter.doFilter(request, response, filterChain);
+
+    
Mockito.verify(response).setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
+    Mockito.verify(response).setHeader("Location", location);
+    Mockito.verifyNoInteractions(filterChain);
+  }
+
+  @Test
+  public void testRedirectPreservesEncodedNewlines() throws Exception
+  {
+    final String location = "https://leader.example/path%0D%0Avalue?query=%0a";;
+    Mockito.when(redirectInfo.getRedirectURL(QUERY_STRING, 
REQUEST_URI)).thenReturn(URI.create(location).toURL());
+
+    redirectFilter.doFilter(request, response, filterChain);
+
+    
Mockito.verify(response).setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
+    Mockito.verify(response).setHeader("Location", location);
+  }
+
+  @Test
+  public void testRedirectRejectsCarriageReturn() throws Exception
+  {
+    assertInvalidRedirect("https://leader.example/path\rInjected: value");
+  }
+
+  @Test
+  public void testRedirectRejectsLineFeed() throws Exception
+  {
+    assertInvalidRedirect("https://leader.example/path\nInjected: value");
+  }
+
+  private void assertInvalidRedirect(String location) throws Exception
+  {
+    Mockito.when(redirectInfo.getRedirectURL(QUERY_STRING, 
REQUEST_URI)).thenReturn(urlWithExternalForm(location));
+
+    redirectFilter.doFilter(request, response, filterChain);
+
+    Mockito.verify(response).sendError(HttpServletResponse.SC_BAD_REQUEST);
+    Mockito.verify(response, Mockito.never()).setStatus(Mockito.anyInt());
+    Mockito.verify(response, Mockito.never()).setHeader(Mockito.anyString(), 
Mockito.anyString());
+  }

Review Comment:
   Addressed in 6e192e4173. The invalid-redirect helper now verifies that the 
filter chain is not invoked after the 400 response.



##########
server/src/main/java/org/apache/druid/server/http/RedirectFilter.java:
##########
@@ -72,17 +72,23 @@ public void doFilter(ServletRequest req, ServletResponse 
res, FilterChain chain)
     if (redirectInfo.doLocal(request.getRequestURI())) {
       chain.doFilter(request, response);
     } else {
-      URL url = redirectInfo.getRedirectURL(request.getQueryString(), 
request.getRequestURI());
-      log.debug("Forwarding request to [%s]", url);
-
+      final URL url = redirectInfo.getRedirectURL(request.getQueryString(), 
request.getRequestURI());
       if (url == null) {
         // We apparently have nothing to redirect to, so let's do a Service 
Unavailable
         response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
         return;
       }
 
+      final String location = url.toString();
+      final String sanitizedLocation = location.replace('\r', ' 
').replace('\n', ' ');
+      if (!location.equals(sanitizedLocation)) {
+        response.sendError(HttpServletResponse.SC_BAD_REQUEST);

Review Comment:
   Addressed in commit 6e192e4173: CR and LF are checked directly and the 
original Location value is used on the success path, avoiding the extra 
sanitized string allocation. RedirectFilterTest passes 4/4; server checkstyle 
and strict test compilation also pass.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to