GeezerVison is showing github.com — Add multipart filename validation · netty/netty@15c62b0
File treeExpand file treeCollapse file tree Open diff view settings main/java/io/netty/handler/codec/http test/java/io/netty/handler/codec/http/multipart
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/HttpConstants.java +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -66,6 +66,16 @@ public final class HttpConstants { | 66 | 66 | */ | 67 | 67 | public static final byte DOUBLE_QUOTE = '"'; | 68 | 68 |
| | 69 | + /** | | 70 | + * Backslash '\' | | 71 | + */ | | 72 | + public static final byte BACKSLASH = '\\'; | | 73 | + | | 74 | + /** | | 75 | + * ASCII DEL character code | | 76 | + */ | | 77 | + public static final byte DEL = 0x7F; | | 78 | + | 69 | 79 | /** | 70 | 80 | * Default character set (UTF-8) | 71 | 81 | */ | |
Collapse file Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/multipart/DiskFileUpload.java +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -75,7 +75,9 @@ public String getFilename() { | 75 | 75 |
| 76 | 76 | @Override | 77 | 77 | public void setFilename(String filename) { | 78 | | - this.filename = ObjectUtil.checkNotNull(filename, "filename"); | | 78 | + ObjectUtil.checkNotNull(filename, "filename"); | | 79 | + FileUploadUtil.validateFileNameForMultiPart(filename); | | 80 | + this.filename = filename; | 79 | 81 | } | 80 | 82 |
| 81 | 83 | @Override | |
Collapse file Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/multipart/FileUpload.java +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,7 +31,10 @@ public interface FileUpload extends HttpData { | 31 | 31 | String getFilename(); | 32 | 32 |
| 33 | 33 | /** | 34 | | - * Set the original filename | | 34 | + * Set the original filename. | | 35 | + * <p> | | 36 | + * <strong>Note:</strong> This method validates that the filename is safe for including in an HTTP request, | | 37 | + * and will throw an exception if that's not the case. | 35 | 38 | */ | 36 | 39 | void setFilename(String filename); | 37 | 40 |
| |
Collapse file Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/multipart/FileUploadUtil.java +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 15 | 15 | */ | 16 | 16 | package io.netty.handler.codec.http.multipart; | 17 | 17 |
| | 18 | +import io.netty.handler.codec.http.HttpConstants; | | 19 | + | 18 | 20 | final class FileUploadUtil { | 19 | 21 |
| 20 | 22 | private FileUploadUtil() { } | @@ -30,4 +32,22 @@ static boolean equals(FileUpload upload1, FileUpload upload2) { | 30 | 32 | static int compareTo(FileUpload upload1, FileUpload upload2) { | 31 | 33 | return upload1.getName().compareToIgnoreCase(upload2.getName()); | 32 | 34 | } | | 35 | + | | 36 | + /** | | 37 | + * Control characters, the DEL character, double-quote, and backslash are either disallowed or strongly discouraged, | | 38 | + * depending on which {@code multipart/form-data} specification you read. | | 39 | + * This method conservatively rejects all of them, and is used for <em>outbound</em> (encoding) filenames. | | 40 | + * @param filename The filename to check. | | 41 | + */ | | 42 | + static void validateFileNameForMultiPart(String filename) { | | 43 | + int length = filename.length(); | | 44 | + for (int i = 0; i < length; i++) { | | 45 | + char c = filename.charAt(i); | | 46 | + if (c < HttpConstants.SP /*control character block*/ || c == HttpConstants.DEL || | | 47 | + c == HttpConstants.DOUBLE_QUOTE || c == HttpConstants.BACKSLASH) { | | 48 | + throw new IllegalArgumentException( | | 49 | + String.format("Illegal filename character 0x%02x at index %d", (int) c, i)); | | 50 | + } | | 51 | + } | | 52 | + } | 33 | 53 | } |
Collapse file Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostMultipartRequestDecoder.java +7-2Lines changed: 7 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1283,10 +1283,15 @@ private static String cleanString(String field) { | 1283 | 1283 | sb.append(HttpConstants.SP_CHAR); | 1284 | 1284 | break; | 1285 | 1285 | case HttpConstants.DOUBLE_QUOTE: | 1286 | | - // nothing added, just removes it | | 1286 | + case HttpConstants.BACKSLASH: | | 1287 | + // nothing added, just removes double quote and backslash | 1287 | 1288 | break; | 1288 | 1289 | default: | 1289 | | - sb.append(nextChar); | | 1290 | + if (nextChar < HttpConstants.SP || nextChar == HttpConstants.DEL) { | | 1291 | + sb.append(HttpConstants.SP_CHAR); | | 1292 | + } else { | | 1293 | + sb.append(nextChar); | | 1294 | + } | 1290 | 1295 | break; | 1291 | 1296 | } | 1292 | 1297 | } | |
Collapse file Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/multipart/MemoryFileUpload.java +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -57,7 +57,9 @@ public String getFilename() { | 57 | 57 |
| 58 | 58 | @Override | 59 | 59 | public void setFilename(String filename) { | 60 | | - this.filename = ObjectUtil.checkNotNull(filename, "filename"); | | 60 | + ObjectUtil.checkNotNull(filename, "filename"); | | 61 | + FileUploadUtil.validateFileNameForMultiPart(filename); | | 62 | + this.filename = filename; | 61 | 63 | } | 62 | 64 |
| 63 | 65 | @Override | |
Collapse file Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/multipart/DiskFileUploadTest.java +25Lines changed: 25 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 19 | 19 | import io.netty.buffer.ByteBufInputStream; | 20 | 20 | import io.netty.buffer.ByteBufUtil; | 21 | 21 | import io.netty.buffer.Unpooled; | | 22 | +import io.netty.handler.codec.http.HttpConstants; | 22 | 23 | import io.netty.util.CharsetUtil; | 23 | 24 | import io.netty.util.internal.PlatformDependent; | 24 | 25 |
| 25 | 26 | import org.junit.jupiter.api.Test; | | 27 | +import org.junit.jupiter.params.ParameterizedTest; | | 28 | +import org.junit.jupiter.params.provider.ValueSource; | 26 | 29 |
| 27 | 30 | import java.io.File; | 28 | 31 | import java.io.FileInputStream; | | 32 | 35 | import java.util.UUID; | 33 | 36 | import java.util.concurrent.ThreadLocalRandom; | 34 | 37 |
| | 38 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; | 35 | 39 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; | 36 | 40 | import static org.junit.jupiter.api.Assertions.assertEquals; | 37 | 41 | import static org.junit.jupiter.api.Assertions.assertFalse; | @@ -287,4 +291,25 @@ public void setSetContentFromFileExceptionally() throws Exception { | 287 | 291 | f1.delete(); | 288 | 292 | } | 289 | 293 | } | | 294 | + | | 295 | + @ParameterizedTest | | 296 | + @ValueSource(bytes = { | | 297 | + 0x00, | | 298 | + HttpConstants.CR, | | 299 | + HttpConstants.LF, | | 300 | + 0x19, | | 301 | + HttpConstants.DEL, | | 302 | + HttpConstants.DOUBLE_QUOTE, | | 303 | + HttpConstants.BACKSLASH}) | | 304 | + void filenameCannotContainIllegalCharacters(byte illegal) { | | 305 | + assertIllegalFilename(((char) illegal) + "f"); | | 306 | + assertIllegalFilename("f" + ((char) illegal) + "f"); | | 307 | + assertIllegalFilename("f" + ((char) illegal)); | | 308 | + } | | 309 | + | | 310 | + private static void assertIllegalFilename(String filename) { | | 311 | + assertThatThrownBy(() -> new DiskFileUpload("f", filename, "plain/text", null, null, 0)) | | 312 | + .isInstanceOf(IllegalArgumentException.class) | | 313 | + .hasMessageContaining("Illegal filename character"); | | 314 | + } | 290 | 315 | } |
Collapse file Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostMultiPartRequestDecoderTest.java +44Lines changed: 44 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -95,6 +95,50 @@ public void testDecodeFullHttpRequestWithInvalidPayloadReleaseBuffer() { | 95 | 95 | } | 96 | 96 | } | 97 | 97 |
| | 98 | + @Test | | 99 | + public void decodeMustCleanFilenameCharacters() { | | 100 | + String content = "\n--861fbeab-cd20-470c-9609-d40a0f704466\r\n" + | | 101 | + "content-disposition: form-data; " + | | 102 | + "name=\"file\"; filename=\" dir\\file\0\u007F÷.txt \"\r\n" + | | 103 | + "content-type: text/plain\r\n" + | | 104 | + "Content-Length: 1\r\n\r\n" + | | 105 | + "x\r\n--861fbeab-cd20-470c-9609-d40a0f704466--\r\n"; | | 106 | + FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/upload", | | 107 | + Unpooled.copiedBuffer(content, CharsetUtil.UTF_8)); | | 108 | + req.headers().set("content-type", "multipart/form-data; boundary=861fbeab-cd20-470c-9609-d40a0f704466"); | | 109 | + req.headers().set("content-length", content.length()); | | 110 | + | | 111 | + HttpPostMultipartRequestDecoder test = new HttpPostMultipartRequestDecoder(req); | | 112 | + FileUpload httpData = (FileUpload) test.getBodyHttpDatas("file").get(0); | | 113 | + try { | | 114 | + assertEquals("dirfile ÷.txt", httpData.getFilename()); | | 115 | + } finally { | | 116 | + test.destroy(); | | 117 | + } | | 118 | + } | | 119 | + | | 120 | + @Test | | 121 | + public void decodeMustCleanFilenamePercentEncodedCharacters() { | | 122 | + String content = "\n--861fbeab-cd20-470c-9609-d40a0f704466\r\n" + | | 123 | + "content-disposition: form-data; " + | | 124 | + "name=\"file\"; filename*=UTF-8''\"%20dir\\file%00%13%7F÷.txt%20\"\r\n" + | | 125 | + "content-type: text/plain\r\n" + | | 126 | + "Content-Length: 1\r\n\r\n" + | | 127 | + "x\r\n--861fbeab-cd20-470c-9609-d40a0f704466--\r\n"; | | 128 | + FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/upload", | | 129 | + Unpooled.copiedBuffer(content, CharsetUtil.UTF_8)); | | 130 | + req.headers().set("content-type", "multipart/form-data; boundary=861fbeab-cd20-470c-9609-d40a0f704466"); | | 131 | + req.headers().set("content-length", content.length()); | | 132 | + | | 133 | + HttpPostMultipartRequestDecoder test = new HttpPostMultipartRequestDecoder(req); | | 134 | + FileUpload httpData = (FileUpload) test.getBodyHttpDatas("file").get(0); | | 135 | + try { | | 136 | + assertEquals("dirfile ÷.txt", httpData.getFilename()); | | 137 | + } finally { | | 138 | + test.destroy(); | | 139 | + } | | 140 | + } | | 141 | + | 98 | 142 | @Test | 99 | 143 | public void testDelimiterExceedLeftSpaceInCurrentBuffer() { | 100 | 144 | String delimiter = "--861fbeab-cd20-470c-9609-d40a0f704466"; | |
Collapse file Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/multipart/MemoryFileUploadTest.java +25Lines changed: 25 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 15 | 15 | */ | 16 | 16 | package io.netty.handler.codec.http.multipart; | 17 | 17 |
| | 18 | +import io.netty.handler.codec.http.HttpConstants; | 18 | 19 | import org.junit.jupiter.api.Test; | | 20 | +import org.junit.jupiter.params.ParameterizedTest; | | 21 | +import org.junit.jupiter.params.provider.ValueSource; | 19 | 22 |
| | 23 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; | 20 | 24 | import static org.junit.jupiter.api.Assertions.assertEquals; | 21 | 25 |
| 22 | 26 | public class MemoryFileUploadTest { | @@ -27,4 +31,25 @@ public final void testMemoryFileUploadEquals() { | 27 | 31 | new MemoryFileUpload("m1", "m1", "application/json", null, null, 100); | 28 | 32 | assertEquals(f1, f1); | 29 | 33 | } | | 34 | + | | 35 | + @ParameterizedTest | | 36 | + @ValueSource(bytes = { | | 37 | + 0x00, | | 38 | + HttpConstants.CR, | | 39 | + HttpConstants.LF, | | 40 | + 0x19, | | 41 | + HttpConstants.DEL, | | 42 | + HttpConstants.DOUBLE_QUOTE, | | 43 | + HttpConstants.BACKSLASH}) | | 44 | + void filenameCannotContainIllegalCharacters(byte illegal) { | | 45 | + assertIllegalFilename(((char) illegal) + "f"); | | 46 | + assertIllegalFilename("f" + ((char) illegal) + "f"); | | 47 | + assertIllegalFilename("f" + ((char) illegal)); | | 48 | + } | | 49 | + | | 50 | + private static void assertIllegalFilename(String filename) { | | 51 | + assertThatThrownBy(() -> new MemoryFileUpload("f", filename, "plain/text", null, null, 0)) | | 52 | + .isInstanceOf(IllegalArgumentException.class) | | 53 | + .hasMessageContaining("Illegal filename character"); | | 54 | + } | 30 | 55 | } |
|