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