Skip to content

Commit

Permalink
[7.17] Register reserved routes so we do not register them by accident (
Browse files Browse the repository at this point in the history
#92893) (#92994)

* Register reserved routes so we do not register them by accident (#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.

(cherry picked from commit 4573c56)

# Conflicts:
#	server/src/main/java/org/elasticsearch/rest/RestController.java
#	server/src/test/java/org/elasticsearch/rest/RestControllerTests.java

* Fix compilation

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
dakrone and elasticmachine committed Jan 18, 2023
1 parent 80a36dc commit 7403dae
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public class RestController implements HttpServerTransport.Dispatcher {
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestController.class);
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 = org.elasticsearch.core.Set.of(
"/__elb_health__",
"/__elb_health__/zk",
"/_health",
"/_health/zk"
);

private static final BytesReference FAVICON_RESPONSE;

Expand Down Expand Up @@ -206,6 +212,9 @@ protected void registerHandler(RestRequest.Method method, String path, RestHandl
}

private void registerHandlerNoWrap(RestRequest.Method method, String path, RestHandler maybeWrappedHandler) {
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, maybeWrappedHandler),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,26 @@ public Exception getInboundException() {
assertThat(channel.getRestResponse().getHeaders().get("Allow"), hasItem(equalTo(GET.toString())));
}

public void testRegisterWithReservedPath() {
final RestController restController = new RestController(Collections.emptySet(), null, client, circuitBreakerService, usageService);
for (String path : RestController.RESERVED_PATHS) {
IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> {
restController.registerHandler(new Route(randomFrom(RestRequest.Method.values()), path), new RestHandler() {
@Override
public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) {
channel.sendResponse(new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY));
}

@Override
public boolean supportsContentStream() {
return true;
}
});
});
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 7403dae

Please sign in to comment.