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