Merge pull request #11433 from jetty/fix/jetty-12.0.x/11278-symlink-dir-listing

Issue #11278 - fix 500 response when trying to display symlinked directory
This commit is contained in:
Lachlan 2024-02-27 17:10:55 +11:00 committed by GitHub
commit 4f1401438a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 17 additions and 10 deletions

View File

@ -310,7 +310,7 @@ public class PathResource extends Resource
@Override @Override
public boolean isDirectory() public boolean isDirectory()
{ {
return Files.isDirectory(getPath(), LinkOption.NOFOLLOW_LINKS); return Files.isDirectory(getPath());
} }
@Override @Override

View File

@ -405,7 +405,16 @@ public abstract class Resource implements Iterable<Resource>
boolean noDepth = true; boolean noDepth = true;
for (Iterator<Resource> i = children.iterator(); noDepth && i.hasNext(); ) for (Iterator<Resource> i = children.iterator(); noDepth && i.hasNext(); )
noDepth = !i.next().isDirectory(); {
Resource resource = i.next();
if (resource.isDirectory())
{
// If the directory is a symlink we do not want to go any deeper.
Path resourcePath = resource.getPath();
if (resourcePath == null || !Files.isSymbolicLink(resourcePath))
noDepth = false;
}
}
if (noDepth) if (noDepth)
return children; return children;

View File

@ -27,7 +27,6 @@ import java.nio.file.InvalidPathException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.eclipse.jetty.toolchain.test.FS; import org.eclipse.jetty.toolchain.test.FS;
@ -728,11 +727,6 @@ public class PathResourceTest
resource.resolve("foo.txt") resource.resolve("foo.txt")
}; };
List<String> actual = allResources.stream()
.map(Resource::getURI)
.map(URI::toASCIIString)
.toList();
assertThat(allResources, containsInAnyOrder(expected)); assertThat(allResources, containsInAnyOrder(expected));
} }
} }

View File

@ -253,8 +253,12 @@ public class AliasCheckerSymlinkTest
Arguments.of(null, "/symlinkParentDir/webroot/file", HttpStatus.NOT_FOUND_404, null), Arguments.of(null, "/symlinkParentDir/webroot/file", HttpStatus.NOT_FOUND_404, null),
Arguments.of(null, "/symlinkParentDir/webroot/WEB-INF/web.xml", HttpStatus.NOT_FOUND_404, null), Arguments.of(null, "/symlinkParentDir/webroot/WEB-INF/web.xml", HttpStatus.NOT_FOUND_404, null),
Arguments.of(null, "/symlinkSiblingDir/file", HttpStatus.NOT_FOUND_404, null), Arguments.of(null, "/symlinkSiblingDir/file", HttpStatus.NOT_FOUND_404, null),
Arguments.of(null, "/webInfSymlink/web.xml", HttpStatus.NOT_FOUND_404, null) Arguments.of(null, "/webInfSymlink/web.xml", HttpStatus.NOT_FOUND_404, null),
);
// We should only be able to list contents of a symlinked directory if the alias checker is installed.
Arguments.of(null, "/symlinkDir", HttpStatus.NOT_FOUND_404, null),
Arguments.of(allowedResource, "/symlinkDir", HttpStatus.OK_200, null)
);
} }
public static Stream<Arguments> combinedResourceTestCases() public static Stream<Arguments> combinedResourceTestCases()