1b0c51
From 0ac5caeaed1fa0354e02e0609f2c726b1b72eb8c Mon Sep 17 00:00:00 2001
1b0c51
From: Oleg Kalnichevski <olegk@apache.org>
1b0c51
Date: Tue, 29 Sep 2020 09:37:38 +0200
1b0c51
Subject: [PATCH 3/3] Incorrect handling of malformed authority component by
1b0c51
 URIUtils#extractHost
1b0c51
1b0c51
---
1b0c51
 .../apache/http/client/utils/URIUtils.java    | 69 ++++++++-----------
1b0c51
 .../http/client/utils/TestURIUtils.java       |  6 +-
1b0c51
 2 files changed, 32 insertions(+), 43 deletions(-)
1b0c51
1b0c51
diff --git a/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java b/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java
1b0c51
index 8eb7667e3..aa3431f6f 100644
1b0c51
--- a/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java
1b0c51
+++ b/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java
1b0c51
@@ -419,56 +419,43 @@ public class URIUtils {
1b0c51
         if (uri == null) {
1b0c51
             return null;
1b0c51
         }
1b0c51
-        HttpHost target = null;
1b0c51
         if (uri.isAbsolute()) {
1b0c51
-            int port = uri.getPort(); // may be overridden later
1b0c51
-            String host = uri.getHost();
1b0c51
-            if (host == null) { // normal parse failed; let's do it ourselves
1b0c51
+            if (uri.getHost() == null) { // normal parse failed; let's do it ourselves
1b0c51
                 // authority does not seem to care about the valid character-set for host names
1b0c51
-                host = uri.getAuthority();
1b0c51
-                if (host != null) {
1b0c51
+                if (uri.getAuthority() != null) {
1b0c51
+                    String content = uri.getAuthority();
1b0c51
                     // Strip off any leading user credentials
1b0c51
-                    final int at = host.indexOf('@');
1b0c51
-                    if (at >= 0) {
1b0c51
-                        if (host.length() > at+1 ) {
1b0c51
-                            host = host.substring(at+1);
1b0c51
-                        } else {
1b0c51
-                            host = null; // @ on its own
1b0c51
-                        }
1b0c51
+                    int at = content.indexOf('@');
1b0c51
+                    if (at != -1) {
1b0c51
+                        content = content.substring(at + 1);
1b0c51
                     }
1b0c51
-                    // Extract the port suffix, if present
1b0c51
-                    if (host != null) {
1b0c51
-                        final int colon = host.indexOf(':');
1b0c51
-                        if (colon >= 0) {
1b0c51
-                            final int pos = colon + 1;
1b0c51
-                            int len = 0;
1b0c51
-                            for (int i = pos; i < host.length(); i++) {
1b0c51
-                                if (Character.isDigit(host.charAt(i))) {
1b0c51
-                                    len++;
1b0c51
-                                } else {
1b0c51
-                                    break;
1b0c51
-                                }
1b0c51
-                            }
1b0c51
-                            if (len > 0) {
1b0c51
-                                try {
1b0c51
-                                    port = Integer.parseInt(host.substring(pos, pos + len));
1b0c51
-                                } catch (final NumberFormatException ex) {
1b0c51
-                                }
1b0c51
-                            }
1b0c51
-                            host = host.substring(0, colon);
1b0c51
+                    final String scheme = uri.getScheme();
1b0c51
+                    final String hostname;
1b0c51
+                    final int port;
1b0c51
+                    at = content.indexOf(":");
1b0c51
+                    if (at != -1) {
1b0c51
+                        hostname = content.substring(0, at);
1b0c51
+                        try {
1b0c51
+                            final String portText = content.substring(at + 1);
1b0c51
+                            port = !TextUtils.isEmpty(portText) ? Integer.parseInt(portText) : -1;
1b0c51
+                        } catch (final NumberFormatException ex) {
1b0c51
+                            return null;
1b0c51
                         }
1b0c51
+                    } else {
1b0c51
+                        hostname = content;
1b0c51
+                        port = -1;
1b0c51
+                    }
1b0c51
+                    try {
1b0c51
+                        return new HttpHost(hostname, port, scheme);
1b0c51
+                    } catch (final IllegalArgumentException ex) {
1b0c51
+                        return null;
1b0c51
                     }
1b0c51
                 }
1b0c51
-            }
1b0c51
-            final String scheme = uri.getScheme();
1b0c51
-            if (!TextUtils.isBlank(host)) {
1b0c51
-                try {
1b0c51
-                    target = new HttpHost(host, port, scheme);
1b0c51
-                } catch (final IllegalArgumentException ignore) {
1b0c51
-                }
1b0c51
+            } else {
1b0c51
+                return new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
1b0c51
             }
1b0c51
         }
1b0c51
-        return target;
1b0c51
+        return null;
1b0c51
     }
1b0c51
 
1b0c51
     /**
1b0c51
diff --git a/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java b/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java
1b0c51
index 189966635..98a44bc1c 100644
1b0c51
--- a/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java
1b0c51
+++ b/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java
1b0c51
@@ -273,14 +273,16 @@ public class TestURIUtils {
1b0c51
 
1b0c51
         Assert.assertEquals(new HttpHost("localhost",8080),
1b0c51
                 URIUtils.extractHost(new URI("http://localhost:8080/;sessionid=stuff/abcd")));
1b0c51
-        Assert.assertEquals(new HttpHost("localhost",8080),
1b0c51
+        Assert.assertEquals(null,
1b0c51
                 URIUtils.extractHost(new URI("http://localhost:8080;sessionid=stuff/abcd")));
1b0c51
-        Assert.assertEquals(new HttpHost("localhost",-1),
1b0c51
+        Assert.assertEquals(null,
1b0c51
                 URIUtils.extractHost(new URI("http://localhost:;sessionid=stuff/abcd")));
1b0c51
         Assert.assertEquals(null,
1b0c51
                 URIUtils.extractHost(new URI("http://:80/robots.txt")));
1b0c51
         Assert.assertEquals(null,
1b0c51
                 URIUtils.extractHost(new URI("http://some%20domain:80/robots.txt")));
1b0c51
+        Assert.assertEquals(null,
1b0c51
+                URIUtils.extractHost(new URI("http://blah@goggle.com:80@google.com/")));
1b0c51
     }
1b0c51
 
1b0c51
     @Test
1b0c51
-- 
1b0c51
2.31.1
1b0c51