diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/AllowedResourceAliasChecker.java b/jetty-server/src/main/java/org/eclipse/jetty/server/AllowedResourceAliasChecker.java index a0debdcbc90d..41b3638558a7 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/AllowedResourceAliasChecker.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/AllowedResourceAliasChecker.java @@ -17,7 +17,6 @@ import java.nio.file.Files; import java.nio.file.LinkOption; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -64,10 +63,10 @@ protected void doStart() throws Exception { _base = getPath(_contextHandler.getBaseResource()); if (_base == null) - _base = Paths.get("/").toAbsolutePath(); + return; + if (Files.exists(_base, NO_FOLLOW_LINKS)) _base = _base.toRealPath(FOLLOW_LINKS); - String[] protectedTargets = _contextHandler.getProtectedTargets(); if (protectedTargets != null) { @@ -86,6 +85,9 @@ protected void doStop() throws Exception @Override public boolean check(String pathInContext, Resource resource) { + if (_base == null) + return false; + try { // The existence check resolves the symlinks. @@ -184,7 +186,7 @@ protected Path getPath(Resource resource) { if (resource instanceof PathResource) return ((PathResource)resource).getPath(); - return resource.getFile().toPath(); + return (resource == null) ? null : resource.getFile().toPath(); } catch (Throwable t) { diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/SymlinkAllowedResourceAliasChecker.java b/jetty-server/src/main/java/org/eclipse/jetty/server/SymlinkAllowedResourceAliasChecker.java index 8130195f9726..172eeedc4b6b 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/SymlinkAllowedResourceAliasChecker.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/SymlinkAllowedResourceAliasChecker.java @@ -40,6 +40,9 @@ public SymlinkAllowedResourceAliasChecker(ContextHandler contextHandler) @Override protected boolean check(String pathInContext, Path path) { + if (_base == null) + return false; + // do not allow any file separation characters in the URI, as we need to know exactly what are the segments if (File.separatorChar != '/' && pathInContext.indexOf(File.separatorChar) >= 0) return false; diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java index 3094af56edde..9f48cec007ab 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java @@ -1700,6 +1700,8 @@ public String getResourceBase() */ public void setBaseResource(Resource base) { + if (isStarting() || isStarted()) + throw new IllegalStateException("Cannot change base resource after starting"); _baseResource = base; }