Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid unsafe split when validate hostname which might be ipv6 address #5713

Merged
merged 3 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.net.URI;
import java.util.List;
import java.util.stream.Collectors;

import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
Expand Down Expand Up @@ -134,21 +135,21 @@ public static ServiceURI create(URI uri) {
private static String validateHostName(String serviceName,
String[] serviceInfos,
String hostname) {
String[] parts = hostname.split(":");
if (parts.length >= 3) {
URI uri = null;
try {
uri = URI.create("dummyscheme://" + hostname);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Invalid hostname : " + hostname);
} else if (parts.length == 2) {
try {
Integer.parseUnsignedInt(parts[1]);
} catch (NumberFormatException nfe) {
throw new IllegalArgumentException("Invalid hostname : " + hostname);
}
return hostname;
} else if (parts.length == 1) {
return hostname + ":" + getServicePort(serviceName, serviceInfos);
} else {
return hostname;
}
String host = uri.getHost();
if (host == null) {
throw new IllegalArgumentException("Invalid hostname : " + hostname);
}
int port = uri.getPort();
if (port == -1) {
port = getServicePort(serviceName, serviceInfos);
}
return host + ":" + port;
}

private final String serviceName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
*/
package org.apache.pulsar.common.net;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;
import static org.testng.Assert.*;
import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;

import java.net.URI;
Expand Down Expand Up @@ -55,6 +54,7 @@ public void testInvalidServiceUris() {
"pulsar://localhost:6650:6651/", // invalid hostname pair
"pulsar://localhost:xyz/", // invalid port
"pulsar://localhost:-6650/", // negative port
"pulsar://fec0:0:0:ffff::1:6650", // missing brackets
};

for (String uri : uris) {
Expand Down Expand Up @@ -122,6 +122,18 @@ public void testUserInfo() {
"/path/to/namespace");
}

@Test
public void testIpv6Uri() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test case of using ipv6 address without port?

String serviceUri = "pulsar://pulsaruser@[fec0:0:0:ffff::1]:6650/path/to/namespace";
assertServiceUri(
serviceUri,
"pulsar",
new String[0],
"pulsaruser",
new String[] { "[fec0:0:0:ffff::1]:6650" },
"/path/to/namespace");
}

@Test
public void testMultipleHostsSemiColon() {
String serviceUri = "pulsar://host1:6650;host2:6650;host3:6650/path/to/namespace";
Expand Down