481355 Nested Symlinks

This commit is contained in:
Greg Wilkins 2015-11-06 13:54:47 +11:00
parent 56afc2b0e5
commit 4bb63b9e03
2 changed files with 62 additions and 11 deletions

View File

@ -71,23 +71,39 @@ public class AllowSymLinkAliasChecker implements AliasCheck
}
// No, so let's check each element ourselves
Path d = path.getRoot();
for (Path e:path)
boolean linked=true;
Path target=path;
int loops=0;
while (linked)
{
d=d.resolve(e);
while (Files.exists(d) && Files.isSymbolicLink(d))
if (++loops>100)
{
Path link=Files.readSymbolicLink(d);
if (!link.isAbsolute())
link=d.resolve(link);
d=link;
if (LOG.isDebugEnabled())
LOG.debug("Too many symlinks {} --> {}",resource,target);
return false;
}
linked=false;
Path d = target.getRoot();
for (Path e:target)
{
d=d.resolve(e);
while (Files.exists(d) && Files.isSymbolicLink(d))
{
Path link=Files.readSymbolicLink(d);
if (!link.isAbsolute())
link=d.resolve(link);
d=link;
linked=true;
}
}
target=d;
}
if (pathResource.getAliasPath().equals(d))
if (pathResource.getAliasPath().equals(target))
{
if (LOG.isDebugEnabled())
LOG.debug("Allow path symlink {} --> {}",resource,d);
LOG.debug("Allow path symlink {} --> {}",resource,target);
return true;
}
}

View File

@ -85,6 +85,18 @@ public class ContextHandlerGetResourceTest
Files.createSymbolicLink(new File(docroot,"other").toPath(),new File("../transit").toPath());
Files.createSymbolicLink(transit.toPath(),otherroot.toPath());
// /web/logs -> /var/logs -> /media/internal/logs
// where /media/internal -> /media/internal-physical/
new File(docroot,"media/internal-physical/logs").mkdirs();
Files.createSymbolicLink(new File(docroot,"media/internal").toPath(),new File(docroot,"media/internal-physical").toPath());
new File(docroot,"var").mkdir();
Files.createSymbolicLink(new File(docroot,"var/logs").toPath(),new File(docroot,"media/internal/logs").toPath());
new File(docroot,"web").mkdir();
Files.createSymbolicLink(new File(docroot,"web/logs").toPath(),new File(docroot,"var/logs").toPath());
new File(docroot,"media/internal-physical/logs/file.log").createNewFile();
System.err.println("docroot="+docroot);
}
OS_ALIAS_SUPPORTED = new File(sub, "TEXTFI~1.TXT").exists();
@ -383,6 +395,29 @@ public class ContextHandlerGetResourceTest
}
}
@Test
public void testSymlinkNested() throws Exception
{
Assume.assumeTrue(OS.IS_UNIX);
try
{
allowSymlinks.set(true);
final String path="/web/logs/file.log";
Resource resource=context.getResource(path);
assertNotNull(resource);
assertEquals("file.log",resource.getFile().getName());
assertTrue(resource.exists());
}
finally
{
allowSymlinks.set(false);
}
}
@Test
public void testSymlinkUnknown() throws Exception