You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kafka.apache.org by mi...@apache.org on 2020/11/26 15:00:31 UTC

[kafka] branch trunk updated: KAFKA-10713: Stricter protocol parsing in hostnames (#9593)

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

mimaison pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/kafka.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 8a59a22  KAFKA-10713: Stricter protocol parsing in hostnames (#9593)
8a59a22 is described below

commit 8a59a228817df221ac9acc7cce4f6c9dd702e9ef
Author: Tom Bentley <to...@users.noreply.github.com>
AuthorDate: Thu Nov 26 14:59:04 2020 +0000

    KAFKA-10713: Stricter protocol parsing in hostnames (#9593)
    
    
    Reviewers: Mickael Maison <mi...@gmail.com>
---
 clients/src/main/java/org/apache/kafka/common/utils/Utils.java      | 4 +++-
 clients/src/test/java/org/apache/kafka/clients/ClientUtilsTest.java | 3 +++
 clients/src/test/java/org/apache/kafka/common/utils/UtilsTest.java  | 1 +
 3 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/clients/src/main/java/org/apache/kafka/common/utils/Utils.java b/clients/src/main/java/org/apache/kafka/common/utils/Utils.java
index 9f84c4f..5a8aefe 100755
--- a/clients/src/main/java/org/apache/kafka/common/utils/Utils.java
+++ b/clients/src/main/java/org/apache/kafka/common/utils/Utils.java
@@ -82,7 +82,9 @@ public final class Utils {
 
     // This matches URIs of formats: host:port and protocol:\\host:port
     // IPv6 is supported with [ip] pattern
-    private static final Pattern HOST_PORT_PATTERN = Pattern.compile(".*?\\[?([0-9a-zA-Z\\-%._:]*)\\]?:([0-9]+)");
+    // Note the protocol (aka schema) part allows unicode letters where
+    // RFC 2396 does not because Kafka was historically lax about the protocol.
+    private static final Pattern HOST_PORT_PATTERN = Pattern.compile("(?:\\p{L}[\\p{L}\\p{Digit}+.-]*://)?\\[?([0-9a-zA-Z\\-%._:]*)\\]?:([0-9]+)");
 
     private static final Pattern VALID_HOST_CHARACTERS = Pattern.compile("([0-9a-zA-Z\\-%._:]*)");
 
diff --git a/clients/src/test/java/org/apache/kafka/clients/ClientUtilsTest.java b/clients/src/test/java/org/apache/kafka/clients/ClientUtilsTest.java
index 3a281f8..f7dc5d9 100644
--- a/clients/src/test/java/org/apache/kafka/clients/ClientUtilsTest.java
+++ b/clients/src/test/java/org/apache/kafka/clients/ClientUtilsTest.java
@@ -25,6 +25,7 @@ import java.util.stream.Collectors;
 import org.apache.kafka.common.config.ConfigException;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 import org.junit.Test;
 
@@ -42,6 +43,8 @@ public class ClientUtilsTest {
         InetSocketAddress onlyAddress = validatedAddresses.get(0);
         assertEquals("localhost", onlyAddress.getHostName());
         assertEquals(10000, onlyAddress.getPort());
+        assertThrows(ConfigException.class, () -> ClientUtils.parseAndValidateAddresses(
+                Arrays.asList("localhost:8000;localhost:9000"), ClientDnsLookup.USE_ALL_DNS_IPS));
     }
 
     @Test
diff --git a/clients/src/test/java/org/apache/kafka/common/utils/UtilsTest.java b/clients/src/test/java/org/apache/kafka/common/utils/UtilsTest.java
index 320c106..f7737c6 100755
--- a/clients/src/test/java/org/apache/kafka/common/utils/UtilsTest.java
+++ b/clients/src/test/java/org/apache/kafka/common/utils/UtilsTest.java
@@ -108,6 +108,7 @@ public class UtilsTest {
         assertEquals("mydomain.com", getHost("PLAINTEXT://mydomain.com:8080"));
         assertEquals("MyDomain.com", getHost("PLAINTEXT://MyDomain.com:8080"));
         assertEquals("My_Domain.com", getHost("PLAINTEXT://My_Domain.com:8080"));
+        assertEquals("My_Domain.com", getHost("\u021Dfoo\u021D+baz://My_Domain.com:8080"));
         assertEquals("::1", getHost("[::1]:1234"));
         assertEquals("2001:db8:85a3:8d3:1319:8a2e:370:7348", getHost("PLAINTEXT://[2001:db8:85a3:8d3:1319:8a2e:370:7348]:5678"));
         assertEquals("2001:DB8:85A3:8D3:1319:8A2E:370:7348", getHost("PLAINTEXT://[2001:DB8:85A3:8D3:1319:8A2E:370:7348]:5678"));