Skip to content

Commit

Permalink
Issue #7059 - prevent an internal NPE in AllowedResourceAliasChecker …
Browse files Browse the repository at this point in the history
…doStart

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
  • Loading branch information
lachlan-roberts committed Nov 3, 2021
1 parent b139cbf commit 59b9d4f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand All @@ -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.
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 59b9d4f

Please sign in to comment.