KAFKA-10713: Stricter protocol parsing in hostnames (#9593)

Reviewers: Mickael Maison <mickael.maison@gmail.com>
This commit is contained in:
Tom Bentley 2020-11-26 14:59:04 +00:00 committed by GitHub
parent 6b1c8f921d
commit 8a59a22881
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 1 deletions

View File

@ -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\\-%._:]*)");

View File

@ -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

View File

@ -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"));