Skip to content

Commit

Permalink
Fix unzip task and add shaded relief workflow (#798)
Browse files Browse the repository at this point in the history
The unzip task implemented some checks against malicious zip files that failed with the shaded relief archive. They have now been removed.
  • Loading branch information
bchapuis authored Nov 4, 2023
1 parent 961e401 commit d71e324
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="naturalearth-create" type="Application" factoryName="Application">
<configuration default="false" name="naturalearth-workflow" type="Application" factoryName="Application">
<option name="MAIN_CLASS_NAME" value="org.apache.baremaps.cli.Baremaps" />
<module name="baremaps-cli" />
<option name="PROGRAM_PARAMETERS" value="workflow execute --file workflow.json" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/examples/naturalearth" />
<extension name="software.aws.toolkits.jetbrains.core.execution.JavaAwsConnectionExtension">
<option name="credential" />
<option name="region" />
<option name="useCurrentConnection" value="false" />
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,23 @@
import java.util.zip.ZipFile;
import org.apache.baremaps.workflow.Task;
import org.apache.baremaps.workflow.WorkflowContext;
import org.apache.baremaps.workflow.WorkflowException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public record UnzipFile(Path file, Path directory) implements Task {

private static final long THRESHOLD_ENTRIES = 10000;
private static final long THRESHOLD_SIZE = 10l << 30;
private static final double THRESHOLD_RATIO = 100;

private static final Logger logger = LoggerFactory.getLogger(UnzipFile.class);

@Override
public void execute(WorkflowContext context) throws Exception {
var filePath = file.toAbsolutePath();
var directoryPath = directory.toAbsolutePath();

try (var zipFile = new ZipFile(filePath.toFile())) {
var entries = zipFile.entries();
long totalSizeArchive = 0;
long totalEntryArchive = 0;

while (entries.hasMoreElements()) {
var ze = entries.nextElement();
if (ze.isDirectory()) {
continue;
}

var path = directoryPath.resolve(ze.getName());

var file = path.toFile().getCanonicalFile();
Expand All @@ -61,29 +54,10 @@ public void execute(WorkflowContext context) throws Exception {
try (var input = new BufferedInputStream(zipFile.getInputStream(ze));
var output = new BufferedOutputStream(new FileOutputStream(path.toFile()))) {

totalEntryArchive++;

int nBytes = -1;
int nBytes;
byte[] buffer = new byte[4096];
long totalSizeEntry = 0;

while ((nBytes = input.read(buffer)) > 0) {
output.write(buffer, 0, nBytes);
totalSizeEntry += nBytes;
totalSizeArchive += nBytes;

double compressionRatio = (double) totalSizeEntry / (double) ze.getCompressedSize();
if (compressionRatio > THRESHOLD_RATIO) {
throw new WorkflowException("Archive compression ratio is too high");
}
}

if (totalSizeArchive > THRESHOLD_SIZE) {
throw new IOException("Archive is too large");
}

if (totalEntryArchive > THRESHOLD_ENTRIES) {
throw new IOException("Archive contains too many entries");
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions examples/shadedrelief/workflow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"steps": [
{
"id": "natural-earth",
"needs": [],
"tasks": [
{
"type": "DownloadUrl",
"url": "http://www.shadedrelief.com/ne-draft/World-Base-Map-Shapefiles.zip",
"path": "shadedrelief.zip"
},
{
"type": "UnzipFile",
"file": "shadedrelief.zip",
"directory": "shadedrelief"
}
]
}
]
}

0 comments on commit d71e324

Please sign in to comment.