fix(core): encode filename (#5593)

close #5589
This commit is contained in:
YannC
2024-10-22 16:05:45 +02:00
committed by GitHub
parent 30dab4f027
commit b399907016
3 changed files with 10 additions and 7 deletions

View File

@@ -21,6 +21,8 @@ import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
@@ -128,6 +130,9 @@ public class Download extends AbstractHttp implements RunnableTask<Download.Outp
String contentDisposition = builder.headers.get("Content-Disposition").getFirst();
filename = filenameFromHeader(runContext, contentDisposition);
}
if (filename != null) {
filename = URLEncoder.encode(filename, StandardCharsets.UTF_8);
}
builder.uri(runContext.storage().putFile(tempFile, filename));
@@ -145,7 +150,7 @@ public class Download extends AbstractHttp implements RunnableTask<Download.Outp
String filename = null;
for (String part : parts) {
if (part.startsWith("filename")) {
filename = part.substring(part.lastIndexOf('=') + 2, part.length() - 1);
filename = part.substring(part.lastIndexOf('=') + 1);
}
if (part.startsWith("filename*")) {
// following https://datatracker.ietf.org/doc/html/rfc5987 the filename* should be <ENCODING>'(lang)'<filename>

View File

@@ -1,6 +1,7 @@
package io.kestra.plugin.core.http;
import com.google.common.collect.ImmutableMap;
import io.kestra.core.junit.annotations.KestraTest;
import io.kestra.core.runners.RunContext;
import io.kestra.core.runners.RunContextFactory;
import io.kestra.core.storages.StorageInterface;
@@ -12,19 +13,16 @@ import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.client.exceptions.HttpClientResponseException;
import io.micronaut.runtime.server.EmbeddedServer;
import io.kestra.core.junit.annotations.KestraTest;
import jakarta.inject.Inject;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -135,7 +133,7 @@ class DownloadTest {
Download.Output output = task.run(runContext);
assertThat(output.getUri().toString(), endsWith("filename.jpg"));
assertThat(output.getUri().toString(), containsString("filename.jpg"));
}
@Controller()

View File

@@ -161,7 +161,7 @@ public class LocalStorage implements StorageInterface {
}
}
return URI.create("kestra://" + uri.getPath());
return URI.create("kestra://" + uri.getRawPath());
}
@Override