Repository navigation
fix: reject newlines in redirect locations #19812
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
Merged
FrankChen021
merged 6 commits into
apache:master
from
FrankChen021:agent/codeql-redirect-response-splitting
Sep 10, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2fff253
Reject newlines in redirect locations
FrankChen021 634a9fd
fix: make redirect sanitization explicit
FrankChen021 2fbcf8b
fix: satisfy strict test compilation
FrankChen021 449a99c
test: strengthen redirect validation coverage
FrankChen021 8bfaa0f
fix: preserve redirect sanitization
FrankChen021 c4e98d8
test: migrate redirect filter test to JUnit 5
FrankChen021 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
server/src/test/java/org/apache/druid/server/http/RedirectFilterTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| /* | ||
| * 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.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.Mock; | ||
| import org.mockito.Mockito; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| 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; | ||
|
|
||
| @ExtendWith(MockitoExtension.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; | ||
|
|
||
| @BeforeEach | ||
| 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); | ||
| Mockito.verifyNoInteractions(filterChain); | ||
| } | ||
|
|
||
| @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()); | ||
| Mockito.verifyNoInteractions(filterChain); | ||
| } | ||
|
|
||
| private static URL urlWithExternalForm(final String externalForm) throws Exception | ||
| { | ||
| return URL.of( | ||
| URI.create("https://leader.example"), | ||
| new URLStreamHandler() | ||
| { | ||
| @Override | ||
| protected URLConnection openConnection(final URL url) | ||
| { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| @Override | ||
| protected String toExternalForm(final URL url) | ||
| { | ||
| return externalForm; | ||
| } | ||
| } | ||
| ); | ||
|
FrankChen021 marked this conversation as resolved.
|
||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.