Skip to content

Commit dfe31ce

Browse files
committed
test(appsec): add unit tests for readContent truncation in CommonsFileUploadAppSecModule
1 parent 0817b8f commit dfe31ce

File tree

2 files changed

+81
-14
lines changed

2 files changed

+81
-14
lines changed

dd-java-agent/instrumentation/commons-fileupload-1.5/src/main/java/datadog/trace/instrumentation/commons/fileupload/CommonsFileUploadAppSecModule.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,7 @@ static void after(
114114
if (name == null || name.isEmpty()) {
115115
continue;
116116
}
117-
String content = "";
118-
try {
119-
InputStream is = fileItem.getInputStream();
120-
byte[] buf = new byte[MAX_FILE_CONTENT_BYTES];
121-
int total = 0;
122-
int n;
123-
while (total < MAX_FILE_CONTENT_BYTES
124-
&& (n = is.read(buf, total, MAX_FILE_CONTENT_BYTES - total)) != -1) {
125-
total += n;
126-
}
127-
content = new String(buf, 0, total, StandardCharsets.ISO_8859_1);
128-
} catch (IOException ignored) {
129-
}
130-
filesContent.add(content);
117+
filesContent.add(readContent(fileItem));
131118
}
132119
if (filesContent.isEmpty()) {
133120
return;
@@ -144,5 +131,21 @@ static void after(
144131
}
145132
}
146133
}
134+
135+
static String readContent(FileItem fileItem) {
136+
try {
137+
InputStream is = fileItem.getInputStream();
138+
byte[] buf = new byte[MAX_FILE_CONTENT_BYTES];
139+
int total = 0;
140+
int n;
141+
while (total < MAX_FILE_CONTENT_BYTES
142+
&& (n = is.read(buf, total, MAX_FILE_CONTENT_BYTES - total)) != -1) {
143+
total += n;
144+
}
145+
return new String(buf, 0, total, StandardCharsets.ISO_8859_1);
146+
} catch (IOException ignored) {
147+
return "";
148+
}
149+
}
147150
}
148151
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import datadog.trace.instrumentation.commons.fileupload.CommonsFileUploadAppSecModule
2+
import org.apache.commons.fileupload.FileItem
3+
import spock.lang.Specification
4+
5+
class CommonsFileUploadAppSecModuleTest extends Specification {
6+
7+
def "readContent returns full content when smaller than limit"() {
8+
given:
9+
def content = 'Hello, World!'
10+
def item = fileItem(content)
11+
12+
expect:
13+
CommonsFileUploadAppSecModule.ParseRequestAdvice.readContent(item) == content
14+
}
15+
16+
def "readContent truncates content to MAX_FILE_CONTENT_BYTES"() {
17+
given:
18+
def largeContent = 'X' * (CommonsFileUploadAppSecModule.ParseRequestAdvice.MAX_FILE_CONTENT_BYTES + 500)
19+
def item = fileItem(largeContent)
20+
21+
when:
22+
def result = CommonsFileUploadAppSecModule.ParseRequestAdvice.readContent(item)
23+
24+
then:
25+
result.length() == CommonsFileUploadAppSecModule.ParseRequestAdvice.MAX_FILE_CONTENT_BYTES
26+
result == 'X' * CommonsFileUploadAppSecModule.ParseRequestAdvice.MAX_FILE_CONTENT_BYTES
27+
}
28+
29+
def "readContent returns empty string when getInputStream throws"() {
30+
given:
31+
FileItem item = Stub(FileItem)
32+
item.getInputStream() >> { throw new IOException('simulated error') }
33+
34+
expect:
35+
CommonsFileUploadAppSecModule.ParseRequestAdvice.readContent(item) == ''
36+
}
37+
38+
def "readContent returns empty string for empty content"() {
39+
given:
40+
def item = fileItem('')
41+
42+
expect:
43+
CommonsFileUploadAppSecModule.ParseRequestAdvice.readContent(item) == ''
44+
}
45+
46+
def "readContent reads exactly MAX_FILE_CONTENT_BYTES when content equals the limit"() {
47+
given:
48+
def content = 'A' * CommonsFileUploadAppSecModule.ParseRequestAdvice.MAX_FILE_CONTENT_BYTES
49+
def item = fileItem(content)
50+
51+
when:
52+
def result = CommonsFileUploadAppSecModule.ParseRequestAdvice.readContent(item)
53+
54+
then:
55+
result.length() == CommonsFileUploadAppSecModule.ParseRequestAdvice.MAX_FILE_CONTENT_BYTES
56+
result == content
57+
}
58+
59+
private FileItem fileItem(String content) {
60+
FileItem item = Stub(FileItem)
61+
item.getInputStream() >> new ByteArrayInputStream(content.getBytes('ISO-8859-1'))
62+
return item
63+
}
64+
}

0 commit comments

Comments
 (0)