GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add multipart filename validation · netty/netty@15c62b0

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-http/src
    • main/java/io/netty/handler/codec/http
      • HttpConstants.java
      • multipart
        • DiskFileUpload.java
        • FileUpload.java
        • FileUploadUtil.java
        • HttpPostMultipartRequestDecoder.java
        • MemoryFileUpload.java
    • test/java/io/netty/handler/codec/http/multipart
      • DiskFileUploadTest.java
      • HttpPostMultiPartRequestDecoderTest.java
      • MemoryFileUploadTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/http/HttpConstants.java‎

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 numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ public final class HttpConstants {
6666
*/
6767
public static final byte DOUBLE_QUOTE = '"';
6868

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+
6979
/**
7080
* Default character set (UTF-8)
7181
*/
Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/http/multipart/DiskFileUpload.java‎

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 numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ public String getFilename() {
7575

7676
@Override
7777
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;
7981
}
8082

8183
@Override
Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/http/multipart/FileUpload.java‎

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 numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ public interface FileUpload extends HttpData {
3131
String getFilename();
3232

3333
/**
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.
3538
*/
3639
void setFilename(String filename);
3740

Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/http/multipart/FileUploadUtil.java‎

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 numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package io.netty.handler.codec.http.multipart;
1717

18+
import io.netty.handler.codec.http.HttpConstants;
19+
1820
final class FileUploadUtil {
1921

2022
private FileUploadUtil() { }
@@ -30,4 +32,22 @@ static boolean equals(FileUpload upload1, FileUpload upload2) {
3032
static int compareTo(FileUpload upload1, FileUpload upload2) {
3133
return upload1.getName().compareToIgnoreCase(upload2.getName());
3234
}
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+
}
3353
}
Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostMultipartRequestDecoder.java‎

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 numberDiff line numberDiff line change
@@ -1283,10 +1283,15 @@ private static String cleanString(String field) {
12831283
sb.append(HttpConstants.SP_CHAR);
12841284
break;
12851285
case HttpConstants.DOUBLE_QUOTE:
1286-
// nothing added, just removes it
1286+
case HttpConstants.BACKSLASH:
1287+
// nothing added, just removes double quote and backslash
12871288
break;
12881289
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+
}
12901295
break;
12911296
}
12921297
}
Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/http/multipart/MemoryFileUpload.java‎

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 numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ public String getFilename() {
5757

5858
@Override
5959
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;
6163
}
6264

6365
@Override
Collapse file

‎codec-http/src/test/java/io/netty/handler/codec/http/multipart/DiskFileUploadTest.java‎

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 numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919
import io.netty.buffer.ByteBufInputStream;
2020
import io.netty.buffer.ByteBufUtil;
2121
import io.netty.buffer.Unpooled;
22+
import io.netty.handler.codec.http.HttpConstants;
2223
import io.netty.util.CharsetUtil;
2324
import io.netty.util.internal.PlatformDependent;
2425

2526
import org.junit.jupiter.api.Test;
27+
import org.junit.jupiter.params.ParameterizedTest;
28+
import org.junit.jupiter.params.provider.ValueSource;
2629

2730
import java.io.File;
2831
import java.io.FileInputStream;
@@ -32,6 +35,7 @@
3235
import java.util.UUID;
3336
import java.util.concurrent.ThreadLocalRandom;
3437

38+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3539
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
3640
import static org.junit.jupiter.api.Assertions.assertEquals;
3741
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -287,4 +291,25 @@ public void setSetContentFromFileExceptionally() throws Exception {
287291
f1.delete();
288292
}
289293
}
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+
}
290315
}
Collapse file

‎codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostMultiPartRequestDecoderTest.java‎

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 numberDiff line numberDiff line change
@@ -95,6 +95,50 @@ public void testDecodeFullHttpRequestWithInvalidPayloadReleaseBuffer() {
9595
}
9696
}
9797

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+
98142
@Test
99143
public void testDelimiterExceedLeftSpaceInCurrentBuffer() {
100144
String delimiter = "--861fbeab-cd20-470c-9609-d40a0f704466";
Collapse file

‎codec-http/src/test/java/io/netty/handler/codec/http/multipart/MemoryFileUploadTest.java‎

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 numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515
*/
1616
package io.netty.handler.codec.http.multipart;
1717

18+
import io.netty.handler.codec.http.HttpConstants;
1819
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.params.ParameterizedTest;
21+
import org.junit.jupiter.params.provider.ValueSource;
1922

23+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2024
import static org.junit.jupiter.api.Assertions.assertEquals;
2125

2226
public class MemoryFileUploadTest {
@@ -27,4 +31,25 @@ public final void testMemoryFileUploadEquals() {
2731
new MemoryFileUpload("m1", "m1", "application/json", null, null, 100);
2832
assertEquals(f1, f1);
2933
}
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+
}
3055
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.