Search in sources :

Example 1 with PathResource

use of org.eclipse.jetty.util.resource.PathResource in project jetty.project by eclipse.

the class AllowSymLinkAliasChecker method check.

@Override
public boolean check(String uri, Resource resource) {
    // Only support PathResource alias checking
    if (!(resource instanceof PathResource))
        return false;
    PathResource pathResource = (PathResource) resource;
    try {
        Path path = pathResource.getPath();
        Path alias = pathResource.getAliasPath();
        if (path.equals(alias))
            // Unknown why this is an alias
            return false;
        if (hasSymbolicLink(path) && Files.isSameFile(path, alias)) {
            if (LOG.isDebugEnabled())
                LOG.debug("Allow symlink {} --> {}", resource, pathResource.getAliasPath());
            return true;
        }
    } catch (Exception e) {
        LOG.ignore(e);
    }
    return false;
}
Also used : Path(java.nio.file.Path) PathResource(org.eclipse.jetty.util.resource.PathResource)

Example 2 with PathResource

use of org.eclipse.jetty.util.resource.PathResource in project opennms by OpenNMS.

the class ApproveAbsolutePathAliasesTest method canApproveDoubleSlash.

@Test
public void canApproveDoubleSlash() throws URISyntaxException, IOException {
    ApproveAbsolutePathAliases aliasCheck = new ApproveAbsolutePathAliases();
    // If the alias and file only differ by a double slash, it should be approved
    String path = "/WEB-INF/jsp//support/index.jsp";
    String uri = "file:///opt/opennms/jetty-webapps/opennms/WEB-INF/jsp//support/index.jsp";
    assertThat(aliasCheck.check(path, new PathResource(new URI(uri)) {

        @Override
        public URI getAlias() {
            try {
                return new URI("file:///opt/opennms/jetty-webapps/opennms/WEB-INF/jsp/support/index.jsp");
            } catch (URISyntaxException e) {
                throw new RuntimeException(e);
            }
        }
    }), equalTo(true));
    // Other differences should not be approved
    uri = "file:///opt/opennms/jetty-webapps/opennms/WEB-INF/jsp/support/index.jsp";
    assertThat(aliasCheck.check(path, new PathResource(new URI(uri)) {

        @Override
        public URI getAlias() {
            try {
                return new URI("file:///opt/opennms/jetty-webapps/opennms/WEB-INF/jsp/.support/index.jsp");
            } catch (URISyntaxException e) {
                throw new RuntimeException(e);
            }
        }
    }), equalTo(false));
}
Also used : PathResource(org.eclipse.jetty.util.resource.PathResource) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Test(org.junit.Test)

Example 3 with PathResource

use of org.eclipse.jetty.util.resource.PathResource in project jphp by jphp-compiler.

the class PHttpResourceHandler method __invoke.

@Signature
public void __invoke(PHttpServerRequest request, PHttpServerResponse response) throws Exception {
    final boolean[] once = { false };
    ResourceHandler resourceHandler = new ResourceHandler() {

        @Override
        public Resource getResource(String path) {
            if (!once[0]) {
                once[0] = true;
                Object attr = request.getRequest().getAttribute("**");
                File file;
                if (attr != null) {
                    file = new File(PHttpResourceHandler.this.file, "/" + attr);
                } else {
                    file = new File(PHttpResourceHandler.this.file);
                }
                if (file.exists()) {
                    return new PathResource(file.getAbsoluteFile());
                } else {
                    return null;
                }
            } else {
                return null;
            }
        }
    };
    resourceHandler.doStart();
    resourceHandler.setResourceBase(file());
    if (cacheControl() != null) {
        resourceHandler.setCacheControl(cacheControl());
    }
    resourceHandler.setRedirectWelcome(redirectWelcome());
    resourceHandler.setPathInfoOnly(pathInfoOnly());
    resourceHandler.setDirectoriesListed(directoriesListed());
    resourceHandler.setDirAllowed(dirAllowed());
    resourceHandler.setAcceptRanges(acceptRanges());
    resourceHandler.setEtags(etags());
    Request baseRequest = Request.getBaseRequest(request.getRequest());
    resourceHandler.handle(request.getRequest().getRequestURI(), baseRequest, request.getRequest(), response.getResponse());
}
Also used : PathResource(org.eclipse.jetty.util.resource.PathResource) Request(org.eclipse.jetty.server.Request) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) BaseObject(php.runtime.lang.BaseObject) File(java.io.File)

Example 4 with PathResource

use of org.eclipse.jetty.util.resource.PathResource in project gitiles by GerritCodeReview.

the class DevServer method staticHandler.

private Handler staticHandler() throws IOException {
    Path staticRoot = sourceRoot.resolve("resources/com/google/gitiles/static");
    ResourceHandler rh = new ResourceHandler();
    try {
        rh.setBaseResource(new PathResource(staticRoot.toUri().toURL()));
    } catch (URISyntaxException e) {
        throw new IOException(e);
    }
    rh.setWelcomeFiles(new String[] {});
    rh.setDirectoriesListed(false);
    ContextHandler handler = new ContextHandler("/+static");
    handler.setHandler(rh);
    return handler;
}
Also used : Path(java.nio.file.Path) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) PathResource(org.eclipse.jetty.util.resource.PathResource) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Example 5 with PathResource

use of org.eclipse.jetty.util.resource.PathResource in project jetty.project by eclipse.

the class AllowSymLinkAliasCheckerTest method setupServer.

private void setupServer() throws Exception {
    // Setup server
    server = new Server();
    localConnector = new LocalConnector(server);
    server.addConnector(localConnector);
    ResourceHandler fileResourceHandler = new ResourceHandler();
    fileResourceHandler.setDirectoriesListed(true);
    fileResourceHandler.setWelcomeFiles(new String[] { "index.html" });
    fileResourceHandler.setEtags(true);
    ContextHandler fileResourceContext = new ContextHandler();
    fileResourceContext.setContextPath("/");
    fileResourceContext.setAllowNullPathInfo(true);
    fileResourceContext.setHandler(fileResourceHandler);
    fileResourceContext.setBaseResource(new PathResource(rootPath));
    fileResourceContext.clearAliasChecks();
    fileResourceContext.addAliasCheck(new AllowSymLinkAliasChecker());
    server.setHandler(fileResourceContext);
    server.start();
}
Also used : Server(org.eclipse.jetty.server.Server) PathResource(org.eclipse.jetty.util.resource.PathResource) LocalConnector(org.eclipse.jetty.server.LocalConnector)

Aggregations

PathResource (org.eclipse.jetty.util.resource.PathResource)8 File (java.io.File)3 IOException (java.io.IOException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 Path (java.nio.file.Path)2 ResourceHandler (org.eclipse.jetty.server.handler.ResourceHandler)2 Resource (org.eclipse.jetty.util.resource.Resource)2 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 ZipFile (java.util.zip.ZipFile)1 LocalConnector (org.eclipse.jetty.server.LocalConnector)1 Request (org.eclipse.jetty.server.Request)1 Server (org.eclipse.jetty.server.Server)1 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)1 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)1 Before (org.junit.Before)1 Test (org.junit.Test)1 BaseObject (php.runtime.lang.BaseObject)1