You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2025/03/15 17:16:51 UTC

(httpcomponents-client) branch disallow_auto_redirect_with_sensitive_headers created (now 49fa83c61)

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

olegk pushed a change to branch disallow_auto_redirect_with_sensitive_headers
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git


      at 49fa83c61 Disallow automatic request redirection if the original request contains poptentially sensitive headers

This branch includes the following new commits:

     new 49fa83c61 Disallow automatic request redirection if the original request contains poptentially sensitive headers

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(httpcomponents-client) 01/01: Disallow automatic request redirection if the original request contains poptentially sensitive headers

Posted by ol...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch disallow_auto_redirect_with_sensitive_headers
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git

commit 49fa83c6114e4c101dd4bc6c4409c203de7e560a
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Sat Mar 15 18:15:30 2025 +0100

    Disallow automatic request redirection if the original request contains poptentially sensitive headers
---
 .../client5/http/impl/DefaultRedirectStrategy.java | 21 ++++++++++++
 .../client5/http/impl/async/AsyncRedirectExec.java | 32 +++++++++++++-----
 .../hc/client5/http/impl/classic/RedirectExec.java | 38 +++++++++++++++-------
 .../hc/client5/http/protocol/RedirectStrategy.java | 19 +++++++++++
 4 files changed, 89 insertions(+), 21 deletions(-)

diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/DefaultRedirectStrategy.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/DefaultRedirectStrategy.java
index 895ec7397..dbfaed4c4 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/DefaultRedirectStrategy.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/DefaultRedirectStrategy.java
@@ -29,6 +29,7 @@
 
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.util.Iterator;
 import java.util.Locale;
 
 import org.apache.hc.client5.http.protocol.RedirectStrategy;
@@ -38,6 +39,7 @@
 import org.apache.hc.core5.http.Header;
 import org.apache.hc.core5.http.HttpException;
 import org.apache.hc.core5.http.HttpHeaders;
+import org.apache.hc.core5.http.HttpHost;
 import org.apache.hc.core5.http.HttpRequest;
 import org.apache.hc.core5.http.HttpResponse;
 import org.apache.hc.core5.http.HttpStatus;
@@ -59,6 +61,25 @@ public class DefaultRedirectStrategy implements RedirectStrategy {
      */
     public static final DefaultRedirectStrategy INSTANCE = new DefaultRedirectStrategy();
 
+    @Override
+    public boolean isRedirectAllowed(
+            final HttpHost currentTarget,
+            final HttpHost newTarget,
+            final HttpRequest redirect,
+            final HttpContext context) {
+        if (!currentTarget.equals(newTarget)) {
+            for (final Iterator<Header> it = redirect.headerIterator(); it.hasNext(); ) {
+                final Header header = it.next();
+                if (header.isSensitive()
+                        || header.getName().equalsIgnoreCase(HttpHeaders.AUTHORIZATION)
+                        || header.getName().equalsIgnoreCase(HttpHeaders.COOKIE)) {
+                    return false;
+                }
+            }
+        }
+        return true;
+    }
+
     @Override
     public boolean isRedirected(
             final HttpRequest request,
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncRedirectExec.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncRedirectExec.java
index 5d44d68cf..bb1384f6f 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncRedirectExec.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncRedirectExec.java
@@ -54,6 +54,7 @@
 import org.apache.hc.core5.http.HttpStatus;
 import org.apache.hc.core5.http.Method;
 import org.apache.hc.core5.http.ProtocolException;
+import org.apache.hc.core5.http.message.BasicHttpRequest;
 import org.apache.hc.core5.http.nio.AsyncDataConsumer;
 import org.apache.hc.core5.http.nio.AsyncEntityProducer;
 import org.apache.hc.core5.http.support.BasicRequestBuilder;
@@ -165,16 +166,28 @@ public AsyncDataConsumer handleResponse(
                             redirectBuilder = BasicRequestBuilder.copy(currentScope.originalRequest);
                     }
                     redirectBuilder.setUri(redirectUri);
+                    final BasicHttpRequest redirect = redirectBuilder.build();
+
+                    final HttpRoute currentRoute = currentScope.route;
+                    final HttpHost currentHost = currentRoute.getTargetHost();
+
+                    if (!redirectStrategy.isRedirectAllowed(currentHost, newTarget, redirect, clientContext)) {
+                        if (LOG.isDebugEnabled()) {
+                            LOG.debug("{} cannot redirect due to safety restrictions", exchangeId);
+                        }
+                        return asyncExecCallback.handleResponse(response, entityDetails);
+                    }
+
                     state.reroute = false;
                     state.redirectURI = redirectUri;
-                    state.currentRequest = redirectBuilder.build();
+                    state.currentRequest = redirect;
 
-                    HttpRoute currentRoute = currentScope.route;
-                    if (!Objects.equals(currentRoute.getTargetHost(), newTarget)) {
-                        final HttpRoute newRoute = routePlanner.determineRoute(newTarget, clientContext);
+                    final HttpRoute newRoute;
+                    if (!Objects.equals(currentHost, newTarget)) {
+                        newRoute = routePlanner.determineRoute(newTarget, clientContext);
                         if (!Objects.equals(currentRoute, newRoute)) {
                             state.reroute = true;
-                            final AuthExchange targetAuthExchange = clientContext.getAuthExchange(currentRoute.getTargetHost());
+                            final AuthExchange targetAuthExchange = clientContext.getAuthExchange(currentHost);
                             if (LOG.isDebugEnabled()) {
                                 LOG.debug("{} resetting target auth state", exchangeId);
                             }
@@ -188,20 +201,21 @@ public AsyncDataConsumer handleResponse(
                                     proxyAuthExchange.reset();
                                 }
                             }
-                            currentRoute = newRoute;
                         }
+                    } else {
+                        newRoute = currentRoute;
                     }
                     state.currentScope = new AsyncExecChain.Scope(
                             scope.exchangeId,
-                            currentRoute,
-                            redirectBuilder.build(),
+                            newRoute,
+                            BasicRequestBuilder.copy(redirect).build(),
                             scope.cancellableDependency,
                             scope.clientContext,
                             scope.execRuntime,
                             scope.scheduler,
                             scope.execCount);
                     if (LOG.isDebugEnabled()) {
-                        LOG.debug("{} redirecting to '{}' via {}", exchangeId, redirectUri, currentRoute);
+                        LOG.debug("{} redirecting to '{}' via {}", exchangeId, redirectUri, newRoute);
                     }
                     return null;
                 }
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/RedirectExec.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/RedirectExec.java
index e3d654769..892c37422 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/RedirectExec.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/RedirectExec.java
@@ -139,7 +139,6 @@ public ClassicHttpResponse execute(
                             throw new CircularRedirectException("Circular redirect to '" + redirectUri + "'");
                         }
                     }
-                    redirectLocations.add(redirectUri);
 
                     final int statusCode = response.getCode();
                     final ClassicRequestBuilder redirectBuilder;
@@ -163,21 +162,35 @@ public ClassicHttpResponse execute(
                             redirectBuilder = ClassicRequestBuilder.copy(currentScope.originalRequest);
                     }
                     redirectBuilder.setUri(redirectUri);
+                    final ClassicHttpRequest redirect = redirectBuilder.build();
+
+                    final HttpRoute currentRoute = currentScope.route;
+                    final HttpHost currentHost = currentRoute.getTargetHost();
+
+                    if (!redirectStrategy.isRedirectAllowed(currentHost, newTarget, redirect, context)) {
+                        if (LOG.isDebugEnabled()) {
+                            LOG.debug("{} cannot redirect due to safety restrictions", exchangeId);
+                        }
+                        return response;
+                    }
+
+                    redirectLocations.add(redirectUri);
 
-                    HttpRoute currentRoute = currentScope.route;
-                    if (!Objects.equals(currentRoute.getTargetHost(), newTarget)) {
-                        final HttpRoute newRoute = this.routePlanner.determineRoute(newTarget, context);
+                    final HttpRoute newRoute;
+                    if (!Objects.equals(currentHost, newTarget)) {
+                        newRoute = this.routePlanner.determineRoute(newTarget, context);
                         if (!Objects.equals(currentRoute, newRoute)) {
                             if (LOG.isDebugEnabled()) {
                                 LOG.debug("{} new route required", exchangeId);
                             }
-                            final AuthExchange targetAuthExchange = context.getAuthExchange(currentRoute.getTargetHost());
+                            final AuthExchange targetAuthExchange = context.getAuthExchange(currentHost);
                             if (LOG.isDebugEnabled()) {
                                 LOG.debug("{} resetting target auth state", exchangeId);
                             }
                             targetAuthExchange.reset();
-                            if (currentRoute.getProxyHost() != null) {
-                                final AuthExchange proxyAuthExchange = context.getAuthExchange(currentRoute.getProxyHost());
+                            final HttpHost proxyHost = currentRoute.getProxyHost();
+                            if (proxyHost != null) {
+                                final AuthExchange proxyAuthExchange = context.getAuthExchange(proxyHost);
                                 if (proxyAuthExchange.isConnectionBased()) {
                                     if (LOG.isDebugEnabled()) {
                                         LOG.debug("{} resetting proxy auth state", exchangeId);
@@ -185,20 +198,21 @@ public ClassicHttpResponse execute(
                                     proxyAuthExchange.reset();
                                 }
                             }
-                            currentRoute = newRoute;
                         }
+                    } else {
+                        newRoute = currentRoute;
                     }
 
                     if (LOG.isDebugEnabled()) {
-                        LOG.debug("{} redirecting to '{}' via {}", exchangeId, redirectUri, currentRoute);
+                        LOG.debug("{} redirecting to '{}' via {}", exchangeId, redirectUri, newRoute);
                     }
                     currentScope = new ExecChain.Scope(
                             scope.exchangeId,
-                            currentRoute,
-                            redirectBuilder.build(),
+                            newRoute,
+                            ClassicRequestBuilder.copy(redirect).build(),
                             scope.execRuntime,
                             scope.clientContext);
-                    currentRequest = redirectBuilder.build();
+                    currentRequest = redirect;
                     RequestEntityProxy.enhance(currentRequest);
 
                     EntityUtils.consume(response.getEntity());
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RedirectStrategy.java b/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RedirectStrategy.java
index 5fade4960..d8587a61d 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RedirectStrategy.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RedirectStrategy.java
@@ -32,6 +32,7 @@
 import org.apache.hc.core5.annotation.Contract;
 import org.apache.hc.core5.annotation.ThreadingBehavior;
 import org.apache.hc.core5.http.HttpException;
+import org.apache.hc.core5.http.HttpHost;
 import org.apache.hc.core5.http.HttpRequest;
 import org.apache.hc.core5.http.HttpResponse;
 import org.apache.hc.core5.http.protocol.HttpContext;
@@ -71,4 +72,22 @@ URI getLocationURI(
             final HttpResponse response,
             final HttpContext context) throws HttpException;
 
+    /**
+     * Determines if the given redirect should be executed or the redirect response
+     * should be returned to the caller without further processing.
+     * <p>
+     * It is legal for this method implementation to modify the redirect request
+     * in order to make it valid for execution.
+     * </p>
+     *
+     * @since 5.4
+     */
+    default boolean isRedirectAllowed(
+            HttpHost currentTarget,
+            HttpHost newTarget,
+            HttpRequest redirect,
+            HttpContext context) {
+        return true;
+    }
+
 }