Skip to content

Commit

Permalink
fix: cover the case of filename contain raise the directory traversal…
Browse files Browse the repository at this point in the history
… vulnerability

refs #1028
  • Loading branch information
Jacksgong committed May 18, 2018
1 parent a37b329 commit e8dd772
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -648,10 +648,10 @@ public static String findFilename(FileDownloadConnection connection, String url)

if (TextUtils.isEmpty(filename)) {
filename = FileDownloadUtils.generateFileName(url);
} else if (filename.startsWith("../")) {
} else if (filename.contains("../")) {
throw new FileDownloadSecurityException(FileDownloadUtils.formatString(
"The filename [%s] from the response is not allowable, because it start"
+ " with '../', which can raise the directory traversal vulnerability",
"The filename [%s] from the response is not allowable, because it is " +
"contains '../', which can raise the directory traversal vulnerability",
filename));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import com.liulishuo.filedownloader.connection.FileDownloadConnection;
import com.liulishuo.filedownloader.exception.FileDownloadSecurityException;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

Expand Down Expand Up @@ -68,13 +70,25 @@ public void parseContentLengthFromContentRange_withUnavailableContentRange() {
assertThat(length).isEqualTo(-1);
}

@Test(expected = FileDownloadSecurityException.class)
@Rule public ExpectedException thrown = ExpectedException.none();

@Test
public void findFilename_securityIssue() throws FileDownloadSecurityException {
final FileDownloadConnection connection = mock(FileDownloadConnection.class);
when(connection.getResponseHeaderField("Content-Disposition"))
.thenReturn("attachment; filename=\"../abc\"");

thrown.expect(FileDownloadSecurityException.class);
FileDownloadUtils.findFilename(connection, "url");

thrown.expect(FileDownloadSecurityException.class);
when(connection.getResponseHeaderField("Content-Disposition"))
.thenReturn("attachment; filename=\"a/b/../abc\"");
FileDownloadUtils.findFilename(connection, "url");

when(connection.getResponseHeaderField("Content-Disposition"))
.thenReturn("attachment; filename=\"/abc/adb\"");
assertThat(FileDownloadUtils.findFilename(connection, "url")).isEqualTo("/abc/adb");
}

}

0 comments on commit e8dd772

Please sign in to comment.