Skip to content

Commit e973049

Browse files
roycarserw00464553
authored andcommitted
Rest-High-Level-Client:fix uri encode bug when url path start with '/' . Relates to issues #34433
1 parent 429c29e commit e973049

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,9 @@ private static String encodePart(String pathPart) {
996996
//encode each part (e.g. index, type and id) separately before merging them into the path
997997
//we prepend "/" to the path part to make this path absolute, otherwise there can be issues with
998998
//paths that start with `-` or contain `:`
999-
URI uri = new URI(null, null, null, -1, "/" + pathPart, null, null);
999+
//the authority must be an empty string and not null, else paths that being with slashes could have them
1000+
//misinterpreted as part of the authority.
1001+
URI uri = new URI(null, "", "/" + pathPart, null, null);
10001002
//manually encode any slash that each part may contain
10011003
return uri.getRawPath().substring(1).replaceAll("/", "%2F");
10021004
} catch (URISyntaxException e) {

client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,6 +1411,38 @@ public void testEndpointBuilderEncodeParts() {
14111411
.addPathPartAsIs("cache/clear");
14121412
assertEquals("/index1,index2/cache/clear", endpointBuilder.build());
14131413
}
1414+
{
1415+
EndpointBuilder endpointBuilder = new EndpointBuilder().addPathPart("/foo");
1416+
assertEquals("/%2Ffoo", endpointBuilder.build());
1417+
}
1418+
{
1419+
EndpointBuilder endpointBuilder = new EndpointBuilder().addPathPart("//foo");
1420+
assertEquals("/%2F%2Ffoo", endpointBuilder.build());
1421+
}
1422+
{
1423+
EndpointBuilder endpointBuilder = new EndpointBuilder().addPathPart("///foo");
1424+
assertEquals("/%2F%2F%2Ffoo", endpointBuilder.build());
1425+
}
1426+
{
1427+
EndpointBuilder endpointBuilder = new EndpointBuilder().addPathPart("/foo/bar");
1428+
assertEquals("/%2Ffoo%2Fbar", endpointBuilder.build());
1429+
}
1430+
{
1431+
EndpointBuilder endpointBuilder = new EndpointBuilder().addPathPart("//foo/bar");
1432+
assertEquals("/%2F%2Ffoo%2Fbar", endpointBuilder.build());
1433+
}
1434+
{
1435+
EndpointBuilder endpointBuilder = new EndpointBuilder().addPathPart("/foo@bar");
1436+
assertEquals("/%2Ffoo@bar", endpointBuilder.build());
1437+
}
1438+
{
1439+
EndpointBuilder endpointBuilder = new EndpointBuilder().addPathPart("//foo@bar");
1440+
assertEquals("/%2F%2Ffoo@bar", endpointBuilder.build());
1441+
}
1442+
{
1443+
EndpointBuilder endpointBuilder = new EndpointBuilder().addPathPart("/part1").addPathPart("//part2").addPathPart("///part3");
1444+
assertEquals("/%2Fpart1/%2F%2Fpart2/%2F%2F%2Fpart3", endpointBuilder.build());
1445+
}
14141446
}
14151447

14161448
public void testEndpoint() {

0 commit comments

Comments
 (0)