Skip to content

Commit

Permalink
Register reserved routes so we do not register them by accident (elas…
Browse files Browse the repository at this point in the history
…tic#92893)

This commits adds four "reserved" paths that Elasticsearch should not register under any cases. By registering these and returning a bad request (we don't actually serve them), we can ensure that if we ever try to register them in the path, Elasticsearch will trip an assertion and fail to build.
  • Loading branch information
dakrone authored Jan 13, 2023
1 parent f9eb81b commit 4573c56
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public class RestController implements HttpServerTransport.Dispatcher {

static final String ELASTIC_PRODUCT_HTTP_HEADER = "X-elastic-product";
static final String ELASTIC_PRODUCT_HTTP_HEADER_VALUE = "Elasticsearch";
static final Set<String> RESERVED_PATHS = Set.of("/__elb_health__", "/__elb_health__/zk", "/_health", "/_health/zk");

private static final BytesReference FAVICON_RESPONSE;

Expand Down Expand Up @@ -264,6 +265,9 @@ private void registerHandlerNoWrap(RestRequest.Method method, String path, RestA
assert RestApiVersion.minimumSupported() == version || RestApiVersion.current() == version
: "REST API compatibility is only supported for version " + RestApiVersion.minimumSupported().major;

if (RESERVED_PATHS.contains(path)) {
throw new IllegalArgumentException("path [" + path + "] is a reserved path and may not be registered");
}
handlers.insertOrUpdate(
path,
new MethodHandlers(path).addMethod(method, version, handler),
Expand Down Expand Up @@ -659,7 +663,7 @@ private static void handleOptionsRequest(RestChannel channel, Set<RestRequest.Me
* Handle a requests with no candidate handlers (return a 400 Bad Request
* error).
*/
private static void handleBadRequest(String uri, RestRequest.Method method, RestChannel channel) throws IOException {
public static void handleBadRequest(String uri, RestRequest.Method method, RestChannel channel) throws IOException {
try (XContentBuilder builder = channel.newErrorBuilder()) {
builder.startObject();
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,21 @@ public void handleRequest(RestRequest request, RestChannel channel, NodeClient c
);
}

public void testRegisterWithReservedPath() {
final RestController restController = new RestController(Set.of(), null, client, circuitBreakerService, usageService, tracer);
for (String path : RestController.RESERVED_PATHS) {
IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> {
restController.registerHandler(
new Route(randomFrom(RestRequest.Method.values()), path),
(request, channel, client) -> channel.sendResponse(
new RestResponse(RestStatus.OK, RestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY)
)
);
});
assertThat(iae.getMessage(), containsString("path [" + path + "] is a reserved path and may not be registered"));
}
}

private static final class TestHttpServerTransport extends AbstractLifecycleComponent implements HttpServerTransport {

TestHttpServerTransport() {}
Expand Down

0 comments on commit 4573c56

Please sign in to comment.