Skip to content

Commit 54e5859

Browse files
committed
grpclb: Be strict about only a single path segment in target URI
Guard this behavior change behind the RFC 3986 parser flag.
1 parent 1ea1a53 commit 54e5859

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

grpclb/src/main/java/io/grpc/grpclb/SecretGrpclbNameResolverProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public NameResolver newNameResolver(Uri targetUri, final NameResolver.Args args)
6464
if (SCHEME.equals(targetUri.getScheme())) {
6565
List<String> pathSegments = targetUri.getPathSegments();
6666
Preconditions.checkArgument(
67-
!pathSegments.isEmpty(),
67+
pathSegments.size() == 1,
6868
"expected 1 path segment in target %s but found %s",
6969
targetUri,
7070
pathSegments);

grpclb/src/test/java/io/grpc/grpclb/SecretGrpclbNameResolverProviderTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package io.grpc.grpclb;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20+
import static com.google.common.truth.TruthJUnit.assume;
21+
import static org.junit.Assert.assertThrows;
2022
import static org.junit.Assert.fail;
2123
import static org.mockito.Mockito.mock;
2224

@@ -103,6 +105,22 @@ public void validDnsNameWithPort() throws Exception {
103105
testValidUri("dns:/foo.googleapis.com:456");
104106
}
105107

108+
@Test
109+
public void newNameResolver_rejectsExtraPathSegments() {
110+
assume().that(enableRfc3986UrisParam).isTrue();
111+
IllegalArgumentException iae =
112+
assertThrows(
113+
IllegalArgumentException.class,
114+
() -> newNameResolver("dns:///localhost:443/extras", args));
115+
assertThat(iae).hasMessageThat().contains("expected 1 path segment in target");
116+
}
117+
118+
@Test
119+
public void newNameResolver_toleratesExtraPathSegments() {
120+
assume().that(enableRfc3986UrisParam).isFalse();
121+
newNameResolver("dns:///localhost:443/extras", args);
122+
}
123+
106124
private void testInvalidUri(String uri) {
107125
try {
108126
newNameResolver(uri, args);

0 commit comments

Comments
 (0)