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