Skip to content

Commit f340ebf

Browse files
committed
Fix server.request.body.filenames for in-memory uploads in Undertow 2.2
In undertow 2.2+, FormValueImpl.isFile() returns false for in-memory file uploads (file size below fileSizeThreshold) because it checks fileItem.isInMemory(). Use getFileName() to identify file uploads regardless of storage, which works across all undertow versions. Also check the filenames callback before building the list to avoid allocations on requests where the feature is inactive.
1 parent 08405ea commit f340ebf

2 files changed

Lines changed: 10 additions & 10 deletions

File tree

dd-java-agent/instrumentation/undertow/undertow-2.0/src/main/java/datadog/trace/instrumentation/undertow/FormDataMap.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ private Map<String, Collection<String>> getMap() {
2121
Deque<FormData.FormValue> formValues = formData.get(key);
2222
List<String> values = new ArrayList<>(formValues.size());
2323
for (FormData.FormValue formValue : formValues) {
24-
if (!formValue.isFile()) {
24+
// In undertow 2.2+, isFile() returns false for in-memory file uploads; getFileName()
25+
// correctly identifies all file uploads regardless of storage.
26+
if (formValue.getFileName() == null) {
2527
values.add(formValue.getValue());
2628
}
2729
}

dd-java-agent/instrumentation/undertow/undertow-2.0/src/main/java/datadog/trace/instrumentation/undertow/MultiPartUploadHandlerInstrumentation.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,19 @@ static void after(
106106
}
107107
}
108108

109-
List<String> filenames = new ArrayList<>();
110-
for (String key : attachment) {
111-
for (FormData.FormValue formValue : attachment.get(key)) {
112-
if (formValue.isFile()) {
109+
BiFunction<RequestContext, List<String>, Flow<Void>> filenamesCb =
110+
cbp.getCallback(EVENTS.requestFilesFilenames());
111+
if (filenamesCb != null) {
112+
List<String> filenames = new ArrayList<>();
113+
for (String key : attachment) {
114+
for (FormData.FormValue formValue : attachment.get(key)) {
113115
String filename = formValue.getFileName();
114116
if (filename != null && !filename.isEmpty()) {
115117
filenames.add(filename);
116118
}
117119
}
118120
}
119-
}
120-
if (!filenames.isEmpty()) {
121-
BiFunction<RequestContext, List<String>, Flow<Void>> filenamesCb =
122-
cbp.getCallback(EVENTS.requestFilesFilenames());
123-
if (filenamesCb != null) {
121+
if (!filenames.isEmpty()) {
124122
Flow<Void> filenamesFlow = filenamesCb.apply(reqCtx, filenames);
125123
Flow.Action filenamesAction = filenamesFlow.getAction();
126124
if (t == null && filenamesAction instanceof Flow.Action.RequestBlockingAction) {

0 commit comments

Comments
 (0)