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"));
}
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);
}
}
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);
}
}
}
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();
}
}
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();
}
}
Aggregations