Skip to content

Commit

Permalink
UNDERTOW-576 Fix issue with resource manager path handling
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Nov 5, 2015
1 parent 0b11293 commit 587feae
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
Expand Up @@ -151,12 +151,13 @@ public Resource getResource(final String p) {
return null;
}
boolean followAll = this.followLinks && safePaths.isEmpty();
if (!followAll && isSymlinkPath(base, file)) {
Path symlinkBase = getSymlinkBase(base, file);
if (!followAll && symlinkBase != null) {
if (this.followLinks && isSymlinkSafe(file)) {
return getFileResource(file, path);
return getFileResource(file, path, symlinkBase);
}
} else {
return getFileResource(file, path);
return getFileResource(file, path, symlinkBase);
}
}
return null;
Expand Down Expand Up @@ -216,7 +217,7 @@ public synchronized void close() throws IOException {
/**
* Returns true is some element of path inside base path is a symlink.
*/
private boolean isSymlinkPath(final String base, final Path file) throws IOException {
private Path getSymlinkBase(final String base, final Path file) throws IOException {
int nameCount = file.getNameCount();
Path root = Paths.get(base);
int rootCount = root.getNameCount();
Expand All @@ -225,11 +226,11 @@ private boolean isSymlinkPath(final String base, final Path file) throws IOExcep
for (int i= rootCount; i<nameCount; i++) {
f = f.resolve(file.getName(i).toString());
if (Files.isSymbolicLink(f)) {
return true;
return f;
}
}
}
return false;
return null;
}

/**
Expand All @@ -244,11 +245,7 @@ private boolean isSymlinkPath(final String base, final Path file) throws IOExcep
*/
private boolean isFileSameCase(final Path file) throws IOException {
String canonicalName = file.toRealPath().getFileName().toString();
if (canonicalName.equals(file.getFileName().toString())) {
return true;
} else {
return !canonicalName.equalsIgnoreCase(file.getFileName().toString());
}
return canonicalName.equals(file.getFileName().toString());
}

/**
Expand Down Expand Up @@ -290,9 +287,28 @@ private boolean isSymlinkSafe(final Path file) throws IOException {
/**
* Apply security check for case insensitive file systems.
*/
protected PathResource getFileResource(final Path file, final String path) throws IOException {
protected PathResource getFileResource(final Path file, final String path, final Path symlinkBase) throws IOException {
if (this.caseSensitive) {
if (isFileSameCase(file)) {
if (symlinkBase != null) {
String relative = symlinkBase.relativize(file).toString();
String fileResolved = file.toRealPath().toAbsolutePath().toString();
String symlinkBaseResolved = symlinkBase.toRealPath().toAbsolutePath().toString();
if (!fileResolved.startsWith(symlinkBaseResolved)) {
return null;
}
String compare = fileResolved.substring(symlinkBaseResolved.length());
if(compare.startsWith("/")) {
compare = compare.substring(1);
}
if(relative.startsWith("/")) {
relative = relative.substring(1);
}
if (relative.equals(compare)) {
return new PathResource(file, this, path);
}

return null;
} else if (isFileSameCase(file)) {
return new PathResource(file, this, path);
} else {
return null;
Expand Down
Expand Up @@ -99,6 +99,24 @@ public void testHeadRequest() throws IOException, URISyntaxException {
}
}

@Test
public void testDotSuffix() throws IOException, URISyntaxException {
TestHttpClient client = new TestHttpClient();
Path rootPath = Paths.get(getClass().getResource("page.html").toURI()).getParent();
try {
DefaultServer.setRootHandler(new CanonicalPathHandler()
.setNext(new PathHandler()
.addPrefixPath("/path", new ResourceHandler(new PathResourceManager(rootPath, 1))
.setDirectoryListingEnabled(true))));

HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/page.html.");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.NOT_FOUND, result.getStatusLine().getStatusCode());
HttpClientUtils.readResponse(result);
} finally {
client.getConnectionManager().shutdown();
}
}
@Test
public void testFileTransfer() throws IOException, URISyntaxException {
TestHttpClient client = new TestHttpClient();
Expand Down

0 comments on commit 587feae

Please sign in to comment.