Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,8 @@ private Asset createAssetFromUpload(
try (BufferedUpload buffered =
ContextFileUploadSupport.bufferUpload(fileInputStream, maxFileSize)) {
String originalFileName =
fileDetail.getFileName() != null ? fileDetail.getFileName() : fileDetail.getName();
ContextFileUploadSupport.decodeMultipartFileName(
fileDetail.getFileName() != null ? fileDetail.getFileName() : fileDetail.getName());
String extension = "";
int dotIndex = originalFileName == null ? -1 : originalFileName.lastIndexOf('.');
if (dotIndex != -1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,8 @@ public Response uploadFile(
throws IOException {
String user = securityContext.getUserPrincipal().getName();
String originalFileName =
fileDetail.getFileName() != null ? fileDetail.getFileName() : fileDetail.getName();
ContextFileUploadSupport.decodeMultipartFileName(
fileDetail.getFileName() != null ? fileDetail.getFileName() : fileDetail.getName());
String contentType = URLConnection.guessContentTypeFromName(originalFileName);
if (contentType == null) {
contentType = "application/octet-stream";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -146,6 +150,48 @@ public static String sanitizeFileName(String fileName) {
return sanitized.isEmpty() ? "download" : sanitized;
}

/**
* Recover the original name of an uploaded file. Browsers send the multipart
* {@code filename} as raw UTF-8 bytes, but the multipart parser decodes part headers as
* ISO-8859-1, so {@code Отчёт.xlsx} arrives as {@code Отчёт.xlsx}. Re-decodes such a
* name as UTF-8. Names that are ASCII, contain characters outside ISO-8859-1 (already
* decoded), or whose bytes are not valid UTF-8 are returned unchanged.
*/
public static String decodeMultipartFileName(final String fileName) {
if (fileName == null || !isUtf8DecodingCandidate(fileName)) {
return fileName;
}
return decodeUtf8OrKeep(fileName);
}

/** Whether {@code fileName} has non-ASCII characters and all of them fit in ISO-8859-1. */
private static boolean isUtf8DecodingCandidate(final String fileName) {
boolean hasNonAscii = false;
for (int i = 0; i < fileName.length(); i++) {
final char character = fileName.charAt(i);
if (character > '\u00FF') {
return false;
}
hasNonAscii |= character > '\u007F';
}
return hasNonAscii;
}

/** Re-decode the ISO-8859-1 bytes of {@code fileName} as UTF-8, or keep it if invalid. */
private static String decodeUtf8OrKeep(final String fileName) {
final ByteBuffer input = ByteBuffer.wrap(fileName.getBytes(StandardCharsets.ISO_8859_1));
final CharBuffer output = CharBuffer.allocate(input.remaining());
final CharsetDecoder decoder =
StandardCharsets.UTF_8
.newDecoder()
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT);
if (decoder.decode(input, output, true).isError() || decoder.flush(output).isError()) {
return fileName;
}
return output.flip().toString();
}

/**
* Build a {@code Content-Disposition} header value that is safe for non-ASCII
* filenames. Emits both the legacy quoted {@code filename=} parameter (for older
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,30 @@

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.UUID;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.junit.jupiter.api.Test;
import org.jvnet.mimepull.MIMEConfig;
import org.jvnet.mimepull.MIMEMessage;
import org.openmetadata.schema.attachments.Asset;
import org.openmetadata.schema.entity.data.ContextFile;
import org.openmetadata.schema.entity.data.ContextFileContent;
import org.openmetadata.schema.entity.data.ContextFileType;

class ContextFileUploadSupportTest {
private static final String ASCII_FILE_NAME = "report-2026.pdf";
private static final String CYRILLIC_FILE_NAME = "Отчёт.xlsx";
private static final String CJK_FILE_NAME = "報告.pdf";
private static final String LATIN1_FILE_NAME = "Café.txt";

@Test
void detectFileTypeUsesMimeMappings() {
Expand Down Expand Up @@ -93,4 +103,39 @@ void bufferUploadRejectsOversizedFiles() {
assertEquals(bytes.length, ex.getActualSize());
assertEquals(3, ex.getMaxFileSize());
}

@Test
void decodeMultipartFileNameRecoversUtf8NameFromParsedUpload() throws Exception {
final String body =
"--XX\r\n"
+ "Content-Disposition: form-data; name=\"file\"; filename=\""
+ CYRILLIC_FILE_NAME
+ "\"\r\n"
+ "Content-Type: application/octet-stream\r\n\r\n"
+ "abc\r\n"
+ "--XX--\r\n";
final MIMEMessage message =
new MIMEMessage(
new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8)),
"XX",
new MIMEConfig());
final List<String> header =
message.getAttachments().getFirst().getHeader("Content-Disposition");
final String parsed = new FormDataContentDisposition(header.getFirst()).getFileName();

assertNotEquals(CYRILLIC_FILE_NAME, parsed);
assertEquals(CYRILLIC_FILE_NAME, ContextFileUploadSupport.decodeMultipartFileName(parsed));
}

@Test
void decodeMultipartFileNameLeavesOtherNamesUnchanged() {
assertNull(ContextFileUploadSupport.decodeMultipartFileName(null));
assertEquals(
ASCII_FILE_NAME, ContextFileUploadSupport.decodeMultipartFileName(ASCII_FILE_NAME));
assertEquals(
CYRILLIC_FILE_NAME, ContextFileUploadSupport.decodeMultipartFileName(CYRILLIC_FILE_NAME));
assertEquals(CJK_FILE_NAME, ContextFileUploadSupport.decodeMultipartFileName(CJK_FILE_NAME));
assertEquals(
LATIN1_FILE_NAME, ContextFileUploadSupport.decodeMultipartFileName(LATIN1_FILE_NAME));
}
}
Loading