You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2023/12/13 08:23:42 UTC

(httpcomponents-core) 15/37: HTTPCORE-756 - Enforced non-empty host identifier for http/https URIs. (#423)

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git

commit f8721abbc8cf6bb3681ee77dc0e2439568d25393
Author: Arturo Bernal <ab...@apache.org>
AuthorDate: Sun Sep 3 18:35:03 2023 +0200

    HTTPCORE-756 - Enforced non-empty host identifier for http/https URIs. (#423)
    
    - Modified URIBuilder's build() method to throw an exception for http and https URIs with an empty host identifier, aligning with RFC 9110, Sections 4.2.2 and 4.2.1
---
 .../java/org/apache/hc/core5/net/URIBuilder.java   |  4 +++
 .../hc/core5/concurrent/TestBasicFuture.java       |  1 -
 .../org/apache/hc/core5/net/TestURIBuilder.java    | 33 +++++++++++++++++++---
 3 files changed, 33 insertions(+), 5 deletions(-)

diff --git a/httpcore5/src/main/java/org/apache/hc/core5/net/URIBuilder.java b/httpcore5/src/main/java/org/apache/hc/core5/net/URIBuilder.java
index 5331ec8df..5f3c0fac9 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/net/URIBuilder.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/net/URIBuilder.java
@@ -41,6 +41,7 @@ import java.util.List;
 
 import org.apache.hc.core5.http.HttpHost;
 import org.apache.hc.core5.http.NameValuePair;
+import org.apache.hc.core5.http.URIScheme;
 import org.apache.hc.core5.http.message.BasicNameValuePair;
 import org.apache.hc.core5.http.message.ParserCursor;
 import org.apache.hc.core5.util.Args;
@@ -306,6 +307,9 @@ public class URIBuilder {
      * Builds a {@link URI} instance.
      */
     public URI build() throws URISyntaxException {
+        if ((URIScheme.HTTPS.same(scheme) || URIScheme.HTTP.same(scheme))  && (TextUtils.isBlank(host))) {
+            throw new URISyntaxException(scheme, "http/https URI cannot have an empty host identifier");
+        }
         return new URI(buildString());
     }
 
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/concurrent/TestBasicFuture.java b/httpcore5/src/test/java/org/apache/hc/core5/concurrent/TestBasicFuture.java
index 3d34a2394..790fc08dd 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/concurrent/TestBasicFuture.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/concurrent/TestBasicFuture.java
@@ -296,5 +296,4 @@ public class TestBasicFuture {
             }
         });
     }
-
 }
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIBuilder.java b/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIBuilder.java
index 5886712a2..4531d7290 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIBuilder.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIBuilder.java
@@ -178,9 +178,9 @@ public class TestURIBuilder {
     @Test
     public void testMutationToRelativeUri() throws Exception {
         final URI uri = new URI("http://stuff@localhost:80/stuff?param=stuff#fragment");
-        final URIBuilder uribuilder = new URIBuilder(uri).setHost((String) null);
+        final URIBuilder uribuilder = new URIBuilder(uri).setHost("localhost");
         final URI result = uribuilder.build();
-        Assertions.assertEquals(new URI("http:///stuff?param=stuff#fragment"), result);
+        Assertions.assertEquals(new URI("http://localhost:80/stuff?param=stuff#fragment"), result);
     }
 
     @Test
@@ -911,8 +911,7 @@ public class TestURIBuilder {
                 new URIBuilder("file:../../.././").optimize().build().toASCIIString());
         Assertions.assertEquals("http://host/",
                 new URIBuilder("http://host/../../.././").optimize().build().toASCIIString());
-        Assertions.assertEquals("http:/",
-                new URIBuilder("http:///../../.././").optimize().build().toASCIIString());
+        Assertions.assertThrows(URISyntaxException.class, () -> new URIBuilder("http:///../../.././").optimize().build().toASCIIString());
     }
 
     @Test
@@ -958,4 +957,30 @@ public class TestURIBuilder {
         Assertions.assertEquals("/path", builder.getPath());
         Assertions.assertEquals("/path", uri.getPath());
     }
+
+    @Test
+    public void testHttpsUriWithEmptyHost() {
+        final URIBuilder uribuilder = new URIBuilder()
+                .setScheme("https")
+                .setUserInfo("stuff")
+                .setHost("")
+                .setPort(80)
+                .setPath("/some stuff")
+                .setParameter("param", "stuff")
+                .setFragment("fragment");
+        Assertions.assertThrows(URISyntaxException.class, uribuilder::build);
+    }
+
+    @Test
+    public void testHttpUriWithEmptyHost() {
+        final URIBuilder uribuilder = new URIBuilder()
+                .setScheme("http")
+                .setUserInfo("stuff")
+                .setHost("")
+                .setPort(80)
+                .setPath("/some stuff")
+                .setParameter("param", "stuff")
+                .setFragment("fragment");
+        Assertions.assertThrows(URISyntaxException.class, uribuilder::build);
+    }
 }