Skip to content

Commit 84a51cc

Browse files
authored
WW-5546 Fixes NPE when uploaded file is empty (#1262)
1 parent 0f61944 commit 84a51cc

File tree

3 files changed

+127
-1
lines changed

3 files changed

+127
-1
lines changed

core/src/main/java/org/apache/struts2/interceptor/AbstractFileUploadInterceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ protected boolean acceptFile(Object action, UploadedFile file, String originalFi
116116
}
117117

118118
// If it's null the upload failed
119-
if (file == null) {
119+
if (file == null || file.getContent() == null) {
120120
String errMsg = getTextMessage(action, STRUTS_MESSAGES_ERROR_UPLOADING_KEY, new String[]{inputName});
121121
if (validation != null) {
122122
validation.addFieldError(inputName, errMsg);

core/src/test/java/org/apache/struts2/interceptor/ActionFileUploadInterceptorTest.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,53 @@ public String getInputName() {
9898
}
9999
};
100100

101+
public static final UploadedFile NULL_CONTENT = new UploadedFile() {
102+
@Override
103+
public Long length() {
104+
return 0L;
105+
}
106+
107+
@Override
108+
public String getName() {
109+
return "";
110+
}
111+
112+
@Override
113+
public boolean isFile() {
114+
return false;
115+
}
116+
117+
@Override
118+
public boolean delete() {
119+
return false;
120+
}
121+
122+
@Override
123+
public String getAbsolutePath() {
124+
return null;
125+
}
126+
127+
@Override
128+
public File getContent() {
129+
return null;
130+
}
131+
132+
@Override
133+
public String getOriginalName() {
134+
return null;
135+
}
136+
137+
@Override
138+
public String getContentType() {
139+
return null;
140+
}
141+
142+
@Override
143+
public String getInputName() {
144+
return null;
145+
}
146+
};
147+
101148
private ActionFileUploadInterceptor interceptor;
102149
private File tempDir;
103150

@@ -205,6 +252,22 @@ public void testAcceptFileWithNoFile() {
205252
assertTrue(msg.indexOf("inputName") > 0);
206253
}
207254

255+
public void testAcceptFileWithNoContent() {
256+
interceptor.setAllowedTypes("text/plain");
257+
258+
ValidationAwareSupport validation = new ValidationAwareSupport();
259+
boolean notOk = interceptor.acceptFile(validation, NULL_CONTENT, "filename.html", "text/plain", "inputName");
260+
261+
assertFalse(notOk);
262+
assertFalse(validation.getFieldErrors().isEmpty());
263+
assertTrue(validation.hasErrors());
264+
List<String> errors = validation.getFieldErrors().get("inputName");
265+
assertEquals(1, errors.size());
266+
String msg = errors.get(0);
267+
assertTrue(msg.startsWith("Error uploading:"));
268+
assertTrue(msg.indexOf("inputName") > 0);
269+
}
270+
208271
public void testAcceptFileWithMaxSize() throws Exception {
209272
interceptor.setMaximumSize(10L);
210273

core/src/test/java/org/apache/struts2/interceptor/FileUploadInterceptorTest.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,53 @@ public String getInputName() {
100100
}
101101
};
102102

103+
public static final UploadedFile NULL_CONTENT = new UploadedFile() {
104+
@Override
105+
public Long length() {
106+
return 0L;
107+
}
108+
109+
@Override
110+
public String getName() {
111+
return "";
112+
}
113+
114+
@Override
115+
public boolean isFile() {
116+
return false;
117+
}
118+
119+
@Override
120+
public boolean delete() {
121+
return false;
122+
}
123+
124+
@Override
125+
public String getAbsolutePath() {
126+
return null;
127+
}
128+
129+
@Override
130+
public File getContent() {
131+
return null;
132+
}
133+
134+
@Override
135+
public String getOriginalName() {
136+
return null;
137+
}
138+
139+
@Override
140+
public String getContentType() {
141+
return null;
142+
}
143+
144+
@Override
145+
public String getInputName() {
146+
return null;
147+
}
148+
};
149+
103150
private FileUploadInterceptor interceptor;
104151
private File tempDir;
105152

@@ -202,6 +249,22 @@ public void testAcceptFileWithNoFile() {
202249
assertTrue(msg.indexOf("inputName") > 0);
203250
}
204251

252+
public void testAcceptFileWithNoContent() {
253+
interceptor.setAllowedTypes("text/plain");
254+
255+
ValidationAwareSupport validation = new ValidationAwareSupport();
256+
boolean notOk = interceptor.acceptFile(validation, NULL_CONTENT, "filename.html", "text/plain", "inputName");
257+
258+
assertFalse(notOk);
259+
assertFalse(validation.getFieldErrors().isEmpty());
260+
assertTrue(validation.hasErrors());
261+
List<String> errors = validation.getFieldErrors().get("inputName");
262+
assertEquals(1, errors.size());
263+
String msg = errors.get(0);
264+
assertTrue(msg.startsWith("Error uploading:"));
265+
assertTrue(msg.indexOf("inputName") > 0);
266+
}
267+
205268
public void testAcceptFileWithMaxSize() throws Exception {
206269
interceptor.setMaximumSize(10L);
207270

0 commit comments

Comments
 (0)