diff --git a/src/main/java/org/elasticsearch/http/HttpServer.java b/src/main/java/org/elasticsearch/http/HttpServer.java index fa190bca9ad..25527aebcde 100644 --- a/src/main/java/org/elasticsearch/http/HttpServer.java +++ b/src/main/java/org/elasticsearch/http/HttpServer.java @@ -177,8 +177,17 @@ public class HttpServer extends AbstractLifecycleComponent { return; } if (!file.isFile()) { - channel.sendResponse(new StringRestResponse(FORBIDDEN)); - return; + // If it's not a dir, we send a 403 + if (!file.isDirectory()) { + channel.sendResponse(new StringRestResponse(FORBIDDEN)); + return; + } + // We don't serve dir but if index.html exists in dir we should serve it + file = new File(file, "index.html"); + if (!file.exists() || file.isHidden() || !file.isFile()) { + channel.sendResponse(new StringRestResponse(FORBIDDEN)); + return; + } } if (!file.getAbsolutePath().startsWith(siteFile.getAbsolutePath())) { channel.sendResponse(new StringRestResponse(FORBIDDEN)); diff --git a/src/test/java/org/elasticsearch/plugin/SitePluginTests.java b/src/test/java/org/elasticsearch/plugin/SitePluginTests.java index 56765c7e03f..465a355e0c5 100644 --- a/src/test/java/org/elasticsearch/plugin/SitePluginTests.java +++ b/src/test/java/org/elasticsearch/plugin/SitePluginTests.java @@ -73,4 +73,32 @@ public class SitePluginTests extends ElasticsearchIntegrationTest { assertThat(response.errorCode(), equalTo(RestStatus.OK.getStatus())); assertThat(response.response(), containsString("Dummy Site Plugin")); } + + /** + * Test direct access to an existing file (index.html) + */ + @Test + public void testAnyPage() throws Exception { + HttpClientResponse response = httpClient("test").request("/_plugin/dummy/index.html"); + assertThat(response.errorCode(), equalTo(RestStatus.OK.getStatus())); + assertThat(response.response(), containsString("Dummy Site Plugin")); + } + + /** + * Test case for #4845: https://github.com/elasticsearch/elasticsearch/issues/4845 + * Serving _site plugins do not pick up on index.html for sub directories + */ + @Test + public void testWelcomePageInSubDirs() throws Exception { + HttpClientResponse response = httpClient("test").request("/_plugin/subdir/dir/"); + assertThat(response.errorCode(), equalTo(RestStatus.OK.getStatus())); + assertThat(response.response(), containsString("Dummy Site Plugin (subdir)")); + + response = httpClient("test").request("/_plugin/subdir/dir_without_index/"); + assertThat(response.errorCode(), equalTo(RestStatus.FORBIDDEN.getStatus())); + + response = httpClient("test").request("/_plugin/subdir/dir_without_index/page.html"); + assertThat(response.errorCode(), equalTo(RestStatus.OK.getStatus())); + assertThat(response.response(), containsString("Dummy Site Plugin (page)")); + } } diff --git a/src/test/resources/org/elasticsearch/plugin/subdir/_site/dir/index.html b/src/test/resources/org/elasticsearch/plugin/subdir/_site/dir/index.html new file mode 100644 index 00000000000..f18ae8070e4 --- /dev/null +++ b/src/test/resources/org/elasticsearch/plugin/subdir/_site/dir/index.html @@ -0,0 +1,9 @@ + + + + Dummy Site Plugin (subdir) + + +

Welcome to this dummy elasticsearch plugin

+ + diff --git a/src/test/resources/org/elasticsearch/plugin/subdir/_site/dir_without_index/page.html b/src/test/resources/org/elasticsearch/plugin/subdir/_site/dir_without_index/page.html new file mode 100644 index 00000000000..407ecddfd40 --- /dev/null +++ b/src/test/resources/org/elasticsearch/plugin/subdir/_site/dir_without_index/page.html @@ -0,0 +1,9 @@ + + + + Dummy Site Plugin (page) + + +

Welcome to this dummy elasticsearch plugin

+ +