Skip to content

Commit 801020b

Browse files
committed
fixed partial path travesal bypass, bumped version, fixed bug in readme
1 parent 7c8e93e commit 801020b

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,22 @@ In Maven:
3535
<dependency>
3636
<groupId>io.github.pixee</groupId>
3737
<artifactId>java-security-toolkit</artifactId>
38-
<version>1.1.1</version>
38+
<version>1.1.2</version>
3939
</dependency>
4040
```
4141
In Gradle:
4242
```kotlin
43-
implementation("io.github.pixee:java-security-toolkit:1.1.1")
43+
implementation("io.github.pixee:java-security-toolkit:1.1.2")
4444
```
4545

4646
## Contributing
4747
We'd love to get contributions! See [CONTRIBUTING.md](CONTRIBUTING.md).
4848

4949
### Building
50-
Building is meant for Java 11 and Maven 3:
50+
Building is meant for Java 11:
5151

5252
```
53-
mvn clean package
53+
./gradlew check
5454
```
5555

5656
## FAQ

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ tasks.named(java11SourceSet.jarTaskName) {
9191
}
9292

9393
group = "io.github.pixee"
94-
version = "1.1.1"
94+
version = "1.1.2"
9595
description = "java-security-toolkit"
9696

9797

src/main/java/io/github/pixee/security/ZipSecurity.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.IOException;
55
import java.io.InputStream;
66
import java.nio.charset.Charset;
7+
import java.nio.file.Path;
78
import java.util.zip.ZipEntry;
89
import java.util.zip.ZipInputStream;
910

@@ -69,7 +70,7 @@ private boolean containsEscapesAndTargetsBelowRoot(final String name) {
6970
if (name.contains("../") || name.contains("..\\")) {
7071
final File fileWithEscapes = new File(name);
7172
try {
72-
if (isBelowCurrentDirectory(fileWithEscapes)) {
73+
if (isBelowOrSisterToCurrentDirectory(fileWithEscapes)) {
7374
return true;
7475
}
7576
} catch (IOException e) {
@@ -79,11 +80,11 @@ private boolean containsEscapesAndTargetsBelowRoot(final String name) {
7980
return false;
8081
}
8182

82-
boolean isBelowCurrentDirectory(final File fileWithEscapes) throws IOException {
83+
private boolean isBelowOrSisterToCurrentDirectory(final File fileWithEscapes) throws IOException {
8384
final File currentDirectory = new File("");
84-
String canonicalizedTargetPath = fileWithEscapes.getCanonicalPath();
85-
String canonicalizedCurrentPath = currentDirectory.getCanonicalPath();
86-
return !canonicalizedTargetPath.startsWith(canonicalizedCurrentPath);
85+
Path currentPathRoot = currentDirectory.getCanonicalFile().toPath();
86+
Path pathWithEscapes = fileWithEscapes.getCanonicalFile().toPath();
87+
return pathWithEscapes.startsWith(currentPathRoot) || pathWithEscapes.getParent().equals(currentPathRoot.getParent());
8788
}
8889

8990
private boolean isRootFileEntry(final String name) {

src/test/java/io/github/pixee/security/ZipSecurityTest.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
import static org.hamcrest.Matchers.equalTo;
55
import static org.junit.jupiter.api.Assertions.assertThrows;
66

7-
import java.io.ByteArrayInputStream;
8-
import java.io.ByteArrayOutputStream;
9-
import java.io.IOException;
10-
import java.io.InputStream;
7+
import java.io.*;
118
import java.nio.charset.StandardCharsets;
129
import java.util.zip.ZipEntry;
1310
import java.util.zip.ZipInputStream;
@@ -50,6 +47,19 @@ void it_prevents_escapes(String path) throws IOException {
5047
assertThrows(SecurityException.class, hardenedStream::getNextEntry);
5148
}
5249

50+
/**
51+
*
52+
*/
53+
@Test
54+
void it_prevents_sister_directory_escape() throws IOException {
55+
String currentDir = new File("").getCanonicalFile().getName();
56+
ZipEntry entry = new ZipEntry("foo/../../" + currentDir + "-other-sister-dir");
57+
InputStream is = createZipFrom(entry);
58+
59+
ZipInputStream hardenedStream = ZipSecurity.createHardenedInputStream(is);
60+
assertThrows(SecurityException.class, hardenedStream::getNextEntry);
61+
}
62+
5363
@Test
5464
void it_prevents_absolute_paths_in_zip_entries() throws IOException {
5565
ZipEntry entry = new ZipEntry("/foo.txt");

0 commit comments

Comments
 (0)