Search in sources :

Example 11 with PathResourceManager

use of io.undertow.server.handlers.resource.PathResourceManager in project undertow by undertow-io.

the class PathResourceManagerTestCase method testCantEscapeRoot.

@Test
public void testCantEscapeRoot() throws Exception {
    final Path rootPath = Paths.get(getClass().getResource("page.html").toURI()).getParent().resolve("subdir");
    final PathResourceManager resourceManager = new PathResourceManager(rootPath, 1024 * 1024);
    Assert.assertNotNull(resourceManager.getResource("a.txt"));
    Assert.assertNull(resourceManager.getResource("../page.html"));
}
Also used : Path(java.nio.file.Path) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) UnitTest(io.undertow.testutils.category.UnitTest) Test(org.junit.Test)

Example 12 with PathResourceManager

use of io.undertow.server.handlers.resource.PathResourceManager in project undertow by undertow-io.

the class PathResourceManagerTestCase method testNonDefaultFileSystem.

@Test
public void testNonDefaultFileSystem() throws Exception {
    Path zipFile = Files.createTempFile("undertow", ".zip");
    try {
        String expectedText = "Hello, world!";
        byte[] expectedBytes = expectedText.getBytes(StandardCharsets.UTF_8);
        try (OutputStream os = Files.newOutputStream(zipFile);
            BufferedOutputStream bos = new BufferedOutputStream(os);
            ZipOutputStream zos = new ZipOutputStream(bos)) {
            zos.putNextEntry(new ZipEntry("dir/"));
            zos.closeEntry();
            zos.putNextEntry(new ZipEntry("dir/resource.txt"));
            zos.write(expectedBytes);
            zos.closeEntry();
            zos.putNextEntry(new ZipEntry("root_resource.txt"));
            zos.write(expectedBytes);
            zos.closeEntry();
        }
        try (FileSystem zipFileSystem = FileSystems.newFileSystem(zipFile, getClass().getClassLoader())) {
            PathResourceManager resourceManager = new PathResourceManager(zipFileSystem.getPath("/dir"));
            Resource resource = resourceManager.getResource("resource.txt");
            Assert.assertArrayEquals(expectedBytes, Files.readAllBytes(resource.getFilePath()));
            try {
                resourceManager.registerResourceChangeListener(changes -> {
                });
                Assert.fail("registerResourceChangeListener should have failed");
            } catch (IllegalStateException expected) {
            }
            try {
                resource.getFile();
                Assert.fail("getFile should have failed");
            } catch (UnsupportedOperationException expected) {
            }
            Resource dir = resourceManager.getResource(".");
            Assert.assertTrue(dir.isDirectory());
            List<Resource> list = dir.list();
            Assert.assertEquals(1, list.size());
            Assert.assertEquals(resource.getFilePath().normalize(), list.get(0).getFilePath().normalize());
            Resource outside = resourceManager.getResource("../root_resource.txt");
            Assert.assertNull(outside);
            Resource doesNotExist = resourceManager.getResource("does_not_exist.txt");
            Assert.assertNull(doesNotExist);
            resourceManager.setBase(Paths.get(getClass().getResource("page.html").toURI()).getParent());
            Assert.assertNotNull(resourceManager.getResource("page.html"));
            resourceManager.setBase(zipFileSystem.getPath("/"));
            Assert.assertNotNull(resourceManager.getResource("root_resource.txt"));
            resourceManager.setBase(new File(getClass().getResource("page.html").toURI()).getParentFile());
            Assert.assertNotNull(resourceManager.getResource("page.html"));
        }
    } finally {
        Files.deleteIfExists(zipFile);
    }
}
Also used : Path(java.nio.file.Path) OutputStream(java.io.OutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) ZipEntry(java.util.zip.ZipEntry) Resource(io.undertow.server.handlers.resource.Resource) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) ZipOutputStream(java.util.zip.ZipOutputStream) FileSystem(java.nio.file.FileSystem) BufferedOutputStream(java.io.BufferedOutputStream) File(java.io.File) UnitTest(io.undertow.testutils.category.UnitTest) Test(org.junit.Test)

Example 13 with PathResourceManager

use of io.undertow.server.handlers.resource.PathResourceManager in project undertow by undertow-io.

the class FileHandlerIndexTestCase method testDirectoryIndex.

@Test
public void testDirectoryIndex() throws IOException, URISyntaxException {
    TestHttpClient client = new TestHttpClient();
    Path rootPath = Paths.get(getClass().getResource("page.html").toURI()).getParent();
    Path badSymlink = null;
    try {
        DefaultServer.setRootHandler(new PathHandler().addPrefixPath("/path", new ResourceHandler(new PathResourceManager(rootPath, 10485760)).setDirectoryListingEnabled(true)));
        badSymlink = rootPath.resolve("tmp2");
        Path badSymlinkTarget = rootPath.resolve("/tmp2");
        Files.createSymbolicLink(badSymlink, badSymlinkTarget);
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        Header[] headers = result.getHeaders("Content-Type");
        Assert.assertEquals("text/html; charset=UTF-8", headers[0].getValue());
        Assert.assertTrue(response, response.contains("page.html"));
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/.");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        headers = result.getHeaders("Content-Type");
        Assert.assertEquals("text/html; charset=UTF-8", headers[0].getValue());
        Assert.assertTrue(response, response.contains("page.html"));
        Assert.assertTrue(response, response.contains("tmp2"));
        // All invalid symlinks have their date set to epoch
        SimpleDateFormat format = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss", Locale.US);
        Assert.assertTrue(response, response.contains(format.format((new Date(0L)))));
    } finally {
        client.getConnectionManager().shutdown();
        if (badSymlink != null) {
            Files.deleteIfExists(badSymlink);
        }
    }
}
Also used : Path(java.nio.file.Path) Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) PathHandler(io.undertow.server.handlers.PathHandler) HttpResponse(org.apache.http.HttpResponse) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) SimpleDateFormat(java.text.SimpleDateFormat) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) Date(java.util.Date) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 14 with PathResourceManager

use of io.undertow.server.handlers.resource.PathResourceManager in project undertow by undertow-io.

the class FileHandlerIndexTestCase method testWelcomeFile.

@Test
public void testWelcomeFile() throws IOException, URISyntaxException {
    TestHttpClient client = new TestHttpClient();
    Path rootPath = Paths.get(getClass().getResource("page.html").toURI()).getParent();
    try {
        DefaultServer.setRootHandler(new CanonicalPathHandler().setNext(new PathHandler().addPrefixPath("/path", new ResourceHandler(new PathResourceManager(rootPath, 10485760)).setDirectoryListingEnabled(true).addWelcomeFiles("page.html"))));
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        final String response = HttpClientUtils.readResponse(result);
        Header[] headers = result.getHeaders("Content-Type");
        Assert.assertEquals("text/html", headers[0].getValue());
        Assert.assertTrue(response, response.contains("A web page"));
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : Path(java.nio.file.Path) Header(org.apache.http.Header) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) HttpGet(org.apache.http.client.methods.HttpGet) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) PathHandler(io.undertow.server.handlers.PathHandler) HttpResponse(org.apache.http.HttpResponse) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 15 with PathResourceManager

use of io.undertow.server.handlers.resource.PathResourceManager in project undertow by undertow-io.

the class FileHandlerSymlinksTestCase method testExplicitAccessSymlinkDeniedForEmptySafePath.

@Test
public void testExplicitAccessSymlinkDeniedForEmptySafePath() throws IOException, URISyntaxException {
    TestHttpClient client = new TestHttpClient();
    Path rootPath = Paths.get(getClass().getResource("page.html").toURI()).getParent();
    Path newSymlink = rootPath.resolve("newSymlink");
    try {
        DefaultServer.setRootHandler(new CanonicalPathHandler().setNext(new PathHandler().addPrefixPath("/path", new ResourceHandler(new PathResourceManager(newSymlink, 10485760, true, "")).setDirectoryListingEnabled(false).addWelcomeFiles("page.html"))));
        /**
         * This request should return a 404 error, followLinks is true, but empty "" safePaths forbids all symbolics paths inside base path
         */
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/innerSymlink/");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.NOT_FOUND, result.getStatusLine().getStatusCode());
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : Path(java.nio.file.Path) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) HttpGet(org.apache.http.client.methods.HttpGet) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) PathHandler(io.undertow.server.handlers.PathHandler) HttpResponse(org.apache.http.HttpResponse) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Aggregations

PathResourceManager (io.undertow.server.handlers.resource.PathResourceManager)41 Path (java.nio.file.Path)34 Test (org.junit.Test)30 ResourceHandler (io.undertow.server.handlers.resource.ResourceHandler)28 PathHandler (io.undertow.server.handlers.PathHandler)27 CanonicalPathHandler (io.undertow.server.handlers.CanonicalPathHandler)25 TestHttpClient (io.undertow.testutils.TestHttpClient)24 HttpGet (org.apache.http.client.methods.HttpGet)23 HttpResponse (org.apache.http.HttpResponse)20 Header (org.apache.http.Header)12 HttpHandler (io.undertow.server.HttpHandler)5 UnitTest (io.undertow.testutils.category.UnitTest)5 Undertow (io.undertow.Undertow)4 CachingResourceManager (io.undertow.server.handlers.resource.CachingResourceManager)4 PreCompressedResourceSupplier (io.undertow.server.handlers.resource.PreCompressedResourceSupplier)4 Resource (io.undertow.server.handlers.resource.Resource)3 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)3 ServletInfo (io.undertow.servlet.api.ServletInfo)3 IOException (java.io.IOException)3 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)3