Skip to content

Commit 075078e

Browse files
roycarserhub-cap
authored andcommitted
HLRC: fix uri encode bug when url path starts with '/' (#34436)
This commit sets the authority of a URI to blank such that it does not misinterpret slashes in the path as the authority. Closes #34433
1 parent 5347dec commit 075078e

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
@@ -1126,7 +1126,9 @@ private static String encodePart(String pathPart) {
11261126
//encode each part (e.g. index, type and id) separately before merging them into the path
11271127
//we prepend "/" to the path part to make this path absolute, otherwise there can be issues with
11281128
//paths that start with `-` or contain `:`
1129-
URI uri = new URI(null, null, null, -1, "/" + pathPart, null, null);
1129+
//the authority must be an empty string and not null, else paths that being with slashes could have them
1130+
//misinterpreted as part of the authority.
1131+
URI uri = new URI(null, "", "/" + pathPart, null, null);
11301132
//manually encode any slash that each part may contain
11311133
return uri.getRawPath().substring(1).replaceAll("/", "%2F");
11321134
} 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
@@ -1772,6 +1772,38 @@ public void testEndpointBuilderEncodeParts() {
17721772
.addPathPartAsIs("cache/clear");
17731773
assertEquals("/index1,index2/cache/clear", endpointBuilder.build());
17741774
}
1775+
{
1776+
EndpointBuilder endpointBuilder = new EndpointBuilder().addPathPart("/foo");
1777+
assertEquals("/%2Ffoo", endpointBuilder.build());
1778+
}
1779+
{
1780+
EndpointBuilder endpointBuilder = new EndpointBuilder().addPathPart("//foo");
1781+
assertEquals("/%2F%2Ffoo", endpointBuilder.build());
1782+
}
1783+
{
1784+
EndpointBuilder endpointBuilder = new EndpointBuilder().addPathPart("///foo");
1785+
assertEquals("/%2F%2F%2Ffoo", endpointBuilder.build());
1786+
}
1787+
{
1788+
EndpointBuilder endpointBuilder = new EndpointBuilder().addPathPart("/foo/bar");
1789+
assertEquals("/%2Ffoo%2Fbar", endpointBuilder.build());
1790+
}
1791+
{
1792+
EndpointBuilder endpointBuilder = new EndpointBuilder().addPathPart("//foo/bar");
1793+
assertEquals("/%2F%2Ffoo%2Fbar", endpointBuilder.build());
1794+
}
1795+
{
1796+
EndpointBuilder endpointBuilder = new EndpointBuilder().addPathPart("/foo@bar");
1797+
assertEquals("/%2Ffoo@bar", endpointBuilder.build());
1798+
}
1799+
{
1800+
EndpointBuilder endpointBuilder = new EndpointBuilder().addPathPart("//foo@bar");
1801+
assertEquals("/%2F%2Ffoo@bar", endpointBuilder.build());
1802+
}
1803+
{
1804+
EndpointBuilder endpointBuilder = new EndpointBuilder().addPathPart("/part1").addPathPart("//part2").addPathPart("///part3");
1805+
assertEquals("/%2Fpart1/%2F%2Fpart2/%2F%2F%2Fpart3", endpointBuilder.build());
1806+
}
17751807
}
17761808

17771809
public void testEndpoint() {

0 commit comments

Comments
 (0)