Skip to content

Commit dd68723

Browse files
authored
Merge pull request #1283 from apache/dependabot/maven/org.apache.commons-commons-fileupload2-jakarta-servlet6-2.0.0-M4
Bump org.apache.commons:commons-fileupload2-jakarta-servlet6 from 2.0.0-M2 to 2.0.0-M4
2 parents fa78ec4 + f16abd1 commit dd68723

File tree

8 files changed

+1482
-104
lines changed

8 files changed

+1482
-104
lines changed

core/src/main/java/org/apache/struts2/dispatcher/multipart/AbstractMultiPartRequest.java

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
package org.apache.struts2.dispatcher.multipart;
2020

21+
import org.apache.commons.fileupload2.core.DiskFileItemFactory;
22+
import org.apache.commons.fileupload2.core.RequestContext;
2123
import org.apache.struts2.inject.Inject;
2224
import jakarta.servlet.http.HttpServletRequest;
2325
import org.apache.commons.fileupload2.core.FileUploadByteCountLimitException;
@@ -33,6 +35,7 @@
3335
import org.apache.struts2.StrutsConstants;
3436
import org.apache.struts2.dispatcher.LocalizedMessage;
3537

38+
import java.io.File;
3639
import java.io.IOException;
3740
import java.nio.charset.Charset;
3841
import java.nio.file.Path;
@@ -42,6 +45,9 @@
4245
import java.util.HashMap;
4346
import java.util.List;
4447
import java.util.Map;
48+
import java.util.UUID;
49+
50+
import static org.apache.commons.lang3.StringUtils.normalizeSpace;
4551

4652
/**
4753
* Abstract class with some helper methods, it should be used
@@ -187,7 +193,21 @@ protected Charset readCharsetEncoding(HttpServletRequest request) {
187193
* @param charset used charset from incoming request
188194
* @param saveDir a temporary folder to store uploaded files (not always needed)
189195
*/
190-
protected abstract JakartaServletDiskFileUpload createJakartaFileUpload(Charset charset, Path saveDir);
196+
protected JakartaServletDiskFileUpload createJakartaFileUpload(Charset charset, Path saveDir) {
197+
DiskFileItemFactory.Builder builder = DiskFileItemFactory.builder();
198+
199+
LOG.debug("Using file save directory: {}", saveDir);
200+
builder.setPath(saveDir);
201+
202+
LOG.debug("Sets buffer size: {}", bufferSize);
203+
builder.setBufferSize(bufferSize);
204+
205+
LOG.debug("Using charset: {}", charset);
206+
builder.setCharset(charset);
207+
208+
DiskFileItemFactory factory = builder.get();
209+
return new JakartaServletDiskFileUpload(factory);
210+
}
191211

192212
protected JakartaServletDiskFileUpload prepareServletFileUpload(Charset charset, Path saveDir) {
193213
JakartaServletDiskFileUpload servletFileUpload = createJakartaFileUpload(charset, saveDir);
@@ -207,11 +227,15 @@ protected JakartaServletDiskFileUpload prepareServletFileUpload(Charset charset,
207227
return servletFileUpload;
208228
}
209229

230+
protected RequestContext createRequestContext(HttpServletRequest request) {
231+
return new StrutsRequestContext(request);
232+
}
233+
210234
protected boolean exceedsMaxStringLength(String fieldName, String fieldValue) {
211235
if (maxStringLength != null && fieldValue.length() > maxStringLength) {
212236
if (LOG.isDebugEnabled()) {
213237
LOG.debug("Form field: {} of size: {} bytes exceeds limit of: {}.",
214-
sanitizeNewlines(fieldName), fieldValue.length(), maxStringLength);
238+
normalizeSpace(fieldName), fieldValue.length(), maxStringLength);
215239
}
216240
LocalizedMessage localizedMessage = new LocalizedMessage(this.getClass(),
217241
STRUTS_MESSAGES_UPLOAD_ERROR_PARAMETER_TOO_LONG_KEY, null,
@@ -234,7 +258,7 @@ public void parse(HttpServletRequest request, String saveDir) throws IOException
234258
try {
235259
processUpload(request, saveDir);
236260
} catch (FileUploadException e) {
237-
LOG.debug("Error parsing the multi-part request!", e);
261+
LOG.warn("Error parsing the multi-part request!", e);
238262
Class<? extends Throwable> exClass = FileUploadException.class;
239263
Object[] args = new Object[]{};
240264

@@ -257,7 +281,7 @@ public void parse(HttpServletRequest request, String saveDir) throws IOException
257281
errors.add(errorMessage);
258282
}
259283
} catch (IOException e) {
260-
LOG.debug("Unable to parse request", e);
284+
LOG.warn("Unable to parse request", e);
261285
LocalizedMessage errorMessage = buildErrorMessage(e.getClass(), e.getMessage(), new Object[]{});
262286
if (!errors.contains(errorMessage)) {
263287
errors.add(errorMessage);
@@ -384,6 +408,22 @@ public String[] getParameterValues(String name) {
384408
return values.toArray(new String[0]);
385409
}
386410

411+
/**
412+
* Creates a secure temporary file in the specified directory using UUID-based naming.
413+
* This method ensures files are created in a controlled location rather than the
414+
* system temporary directory, reducing security risks.
415+
*
416+
* @param fileName the original filename for logging purposes
417+
* @param location the directory where the temporary file should be created
418+
* @return a new temporary file in the specified location
419+
*/
420+
protected File createTemporaryFile(String fileName, Path location) {
421+
String uid = UUID.randomUUID().toString().replace("-", "_");
422+
File file = location.resolve("upload_" + uid + ".tmp").toFile();
423+
LOG.debug("Creating temporary file: {} (originally: {})", file.getName(), fileName);
424+
return file;
425+
}
426+
387427
/* (non-Javadoc)
388428
* @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#cleanUp()
389429
*/

0 commit comments

Comments
 (0)