You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@baremaps.apache.org by bc...@apache.org on 2024/06/13 12:28:48 UTC
(incubator-baremaps) branch sonar created (now 03414eb5)
This is an automated email from the ASF dual-hosted git repository.
bchapuis pushed a change to branch sonar
in repository https://gitbox.apache.org/repos/asf/incubator-baremaps.git
at 03414eb5 Prevent zip slip and path injection
This branch includes the following new commits:
new 03414eb5 Prevent zip slip and path injection
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(incubator-baremaps) 01/01: Prevent zip slip and path injection
Posted by bc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
bchapuis pushed a commit to branch sonar
in repository https://gitbox.apache.org/repos/asf/incubator-baremaps.git
commit 03414eb5ef46bdbabb4aa32edb54222c8abcacdc
Author: Bertil Chapuis <bc...@gmail.com>
AuthorDate: Thu Jun 13 14:28:35 2024 +0200
Prevent zip slip and path injection
---
.../baremaps/workflow/tasks/DecompressFile.java | 46 ++++++++++++----------
1 file changed, 26 insertions(+), 20 deletions(-)
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/workflow/tasks/DecompressFile.java b/baremaps-core/src/main/java/org/apache/baremaps/workflow/tasks/DecompressFile.java
index 6fca8f46..87c48a85 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/workflow/tasks/DecompressFile.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/workflow/tasks/DecompressFile.java
@@ -159,17 +159,21 @@ public class DecompressFile implements Task {
TarArchiveEntry entry;
while ((entry = tarInputStream.getNextEntry()) != null) {
var path = target.resolve(entry.getName());
- if (entry.isDirectory()) {
- Files.createDirectories(path);
- } else {
- Files.createDirectories(path.getParent());
- Files.write(path, new byte[] {},
- StandardOpenOption.CREATE,
- StandardOpenOption.TRUNCATE_EXISTING);
- try (BufferedOutputStream outputStream =
- new BufferedOutputStream(Files.newOutputStream(path))) {
- tarInputStream.transferTo(outputStream);
+ if (path.toFile().getCanonicalPath().startsWith(target.toFile().getCanonicalPath())) {
+ if (entry.isDirectory()) {
+ Files.createDirectories(path);
+ } else {
+ Files.createDirectories(path.getParent());
+ Files.write(path, new byte[] {},
+ StandardOpenOption.CREATE,
+ StandardOpenOption.TRUNCATE_EXISTING);
+ try (BufferedOutputStream outputStream =
+ new BufferedOutputStream(Files.newOutputStream(path))) {
+ tarInputStream.transferTo(outputStream);
+ }
}
+ } else {
+ throw new IOException("Entry is outside of the target directory");
}
}
}
@@ -189,16 +193,18 @@ public class DecompressFile implements Task {
while (entries.hasMoreElements()) {
var entry = entries.nextElement();
var path = target.resolve(entry.getName());
- if (entry.isDirectory()) {
- Files.createDirectories(path);
- } else {
- Files.createDirectories(path.getParent());
- Files.write(path, new byte[] {},
- StandardOpenOption.CREATE,
- StandardOpenOption.TRUNCATE_EXISTING);
- try (var input = new BufferedInputStream(zipFile.getInputStream(entry));
- var output = new BufferedOutputStream(new FileOutputStream(path.toFile()))) {
- input.transferTo(output);
+ if (path.toFile().getCanonicalPath().startsWith(target.toFile().getCanonicalPath())) {
+ if (entry.isDirectory()) {
+ Files.createDirectories(path);
+ } else {
+ Files.createDirectories(path.getParent());
+ Files.write(path, new byte[] {},
+ StandardOpenOption.CREATE,
+ StandardOpenOption.TRUNCATE_EXISTING);
+ try (var input = new BufferedInputStream(zipFile.getInputStream(entry));
+ var output = new BufferedOutputStream(new FileOutputStream(path.toFile()))) {
+ input.transferTo(output);
+ }
}
}
}