-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Feature: Add Option to Strip Authorization Header on Redirect #2090
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
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
f768df2
Add isStripAuthorizationOnRedirect() config option to control Authori…
hyperxpro 00c2191
Update client/src/main/java/org/asynchttpclient/netty/handler/interce…
hyperxpro 42bcee2
Update client/src/main/java/org/asynchttpclient/DefaultAsyncHttpClien…
hyperxpro 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
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
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
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
30 changes: 30 additions & 0 deletions
30
client/src/test/java/org/asynchttpclient/DefaultAsyncHttpClientConfigTest.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,30 @@ | ||
package org.asynchttpclient; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class DefaultAsyncHttpClientConfigTest { | ||
@Test | ||
void testStripAuthorizationOnRedirect_DefaultIsFalse() { | ||
DefaultAsyncHttpClientConfig config = new DefaultAsyncHttpClientConfig.Builder().build(); | ||
assertFalse(config.isStripAuthorizationOnRedirect(), "Default should be false"); | ||
} | ||
|
||
@Test | ||
void testStripAuthorizationOnRedirect_SetTrue() { | ||
DefaultAsyncHttpClientConfig config = new DefaultAsyncHttpClientConfig.Builder() | ||
.setStripAuthorizationOnRedirect(true) | ||
.build(); | ||
assertTrue(config.isStripAuthorizationOnRedirect(), "Should be true when set"); | ||
} | ||
|
||
@Test | ||
void testStripAuthorizationOnRedirect_SetFalse() { | ||
DefaultAsyncHttpClientConfig config = new DefaultAsyncHttpClientConfig.Builder() | ||
.setStripAuthorizationOnRedirect(false) | ||
.build(); | ||
assertFalse(config.isStripAuthorizationOnRedirect(), "Should be false when set to false"); | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
client/src/test/java/org/asynchttpclient/StripAuthorizationOnRedirectHttpTest.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,95 @@ | ||
package org.asynchttpclient; | ||
|
||
import com.sun.net.httpserver.HttpExchange; | ||
import com.sun.net.httpserver.HttpHandler; | ||
import com.sun.net.httpserver.HttpServer; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
public class StripAuthorizationOnRedirectHttpTest { | ||
private static HttpServer server; | ||
private static int port; | ||
private static volatile String lastAuthHeader; | ||
|
||
@BeforeAll | ||
public static void startServer() throws Exception { | ||
server = HttpServer.create(new InetSocketAddress(0), 0); | ||
port = server.getAddress().getPort(); | ||
server.createContext("/redirect", new RedirectHandler()); | ||
server.createContext("/final", new FinalHandler()); | ||
server.start(); | ||
} | ||
|
||
@AfterAll | ||
public static void stopServer() { | ||
server.stop(0); | ||
} | ||
|
||
static class RedirectHandler implements HttpHandler { | ||
@Override | ||
public void handle(HttpExchange exchange) { | ||
String auth = exchange.getRequestHeaders().getFirst("Authorization"); | ||
lastAuthHeader = auth; | ||
exchange.getResponseHeaders().add("Location", "http://localhost:" + port + "/final"); | ||
try { | ||
exchange.sendResponseHeaders(302, -1); | ||
} catch (Exception ignored) { | ||
} | ||
exchange.close(); | ||
} | ||
} | ||
|
||
static class FinalHandler implements HttpHandler { | ||
@Override | ||
public void handle(HttpExchange exchange) { | ||
String auth = exchange.getRequestHeaders().getFirst("Authorization"); | ||
lastAuthHeader = auth; | ||
try { | ||
exchange.sendResponseHeaders(200, 0); | ||
exchange.getResponseBody().close(); | ||
} catch (Exception ignored) { | ||
} | ||
exchange.close(); | ||
} | ||
} | ||
|
||
@Test | ||
void testAuthHeaderPropagatedByDefault() throws Exception { | ||
DefaultAsyncHttpClientConfig config = new DefaultAsyncHttpClientConfig.Builder() | ||
.setFollowRedirect(true) | ||
.build(); | ||
try (DefaultAsyncHttpClient client = new DefaultAsyncHttpClient(config)) { | ||
lastAuthHeader = null; | ||
client.prepareGet("http://localhost:" + port + "/redirect") | ||
.setHeader("Authorization", "Bearer testtoken") | ||
.execute() | ||
.get(5, TimeUnit.SECONDS); | ||
// By default, Authorization header is propagated to /final | ||
assertEquals("Bearer testtoken", lastAuthHeader, "Authorization header should be present on redirect by default"); | ||
} | ||
} | ||
|
||
@Test | ||
void testAuthHeaderStrippedWhenEnabled() throws Exception { | ||
DefaultAsyncHttpClientConfig config = new DefaultAsyncHttpClientConfig.Builder() | ||
.setFollowRedirect(true) | ||
.setStripAuthorizationOnRedirect(true) | ||
.build(); | ||
try (DefaultAsyncHttpClient client = new DefaultAsyncHttpClient(config)) { | ||
lastAuthHeader = null; | ||
client.prepareGet("http://localhost:" + port + "/redirect") | ||
.setHeader("Authorization", "Bearer testtoken") | ||
.execute() | ||
.get(5, TimeUnit.SECONDS); | ||
// When enabled, Authorization header should be stripped on /final | ||
assertNull(lastAuthHeader, "Authorization header should be stripped on redirect when enabled"); | ||
} | ||
} | ||
} |
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.