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