NES for Apache Struts 1.x File Upload
NES for Apache Struts 1.x File Upload Information and Details
NES for Apache Struts 1.x File Upload
NES for Apache Struts 1.x includes support for file upload functionality with additional security features to protect against common vulnerabilities. The file upload component is designed to handle multipart/form-data requests securely, ensuring that uploaded files are processed safely.
Required Libraries
Switching to the NES for Apache Struts 1.x requires NES for Apache Commons FileUpload commons-fileupload to be on the classpath. This version includes security enhancements and bug fixes that are essential for secure file handling.
Use the maven coordinates below:
| Group ID | Artifact ID | Version |
|---|---|---|
| commons-fileupload | commons-fileupload | 1.5.0-commons-fileupload-1.5.2 |
Or directly from the registry:
| Portion | Value |
|---|---|
| base_url | https://registry.nes.herodevs.com/maven/commons-fileupload/ |
| package_name | commons-fileupload |
| version | 1.5.0-commons-fileupload-1.5.2 |
| filename | package_name + version + extension |
Example Link
https://registry.nes.herodevs.com/maven/commons-fileupload/commons-fileupload/1.5.0-commons-fileupload-1.5.2/commons-fileupload-1.5.0-commons-fileupload-1.5.2.pomIt's important to ensure that only the NES version of commons-fileupload is on the classpath to avoid conflicts and ensure that all security patches are applied. If you are using a build tool like Maven or Gradle, make sure to exclude any older versions of commons-fileupload that may be included as transitive dependencies. If using Ant, ensure that the correct version is included in your classpath.
Additional Struts Configuration for File Upload
struts-config.dtd defines the following additional configuration parameters for file upload handling:
Existing parameters
maxFileSize The maximum size (in bytes) of a file to be accepted as a
file upload. Can be expressed as a number followed by a
"K", "M", or "G", which are interpreted to mean kilobytes,
megabytes, or gigabytes, respectively.
["250M"]
New parameters
maxSize The maximum size (in bytes) of a complete request to be
accepted as a file upload. Can be expressed as a number
followed by a "K", "M", or "G", which are interpreted to
mean kilobytes, megabytes, or gigabytes, respectively.
["250M"]
maxStringLen The maximum length (in bytes) of a string parameter in a
multipart request. Can be expressed as a number followed by a
"K", "M", or "G", which are interpreted to mean kilobytes,
megabytes, or gigabytes, respectively.
["4K"]
fileCountMax The maximum number of parts (uploaded files and text fields
together) permitted in a single request. A value of -1
indicates no maximum.
["-1"]
maxHeaderSize The maximum number of header bytes permitted per part in a request
where the content type is <code>multipart/form-data</code>. Requests that
exceed this limit will be rejected. A value of less than 0 means no limit.
["512"]
example:
<struts-config>
<!-- ... -->
<controller fileCountMax="4" maxStringLen="1K" maxSize="5M" maxFileSize="1M" maxHeaderSize="256" inputForward="true" />
<!-- ... -->
</struts-config>
When a request exceeds a limit
With the default CommonsMultipartRequestHandler, Struts reports every upload limit through request attributes and carries on with an empty ActionForm; it doesn't throw. The limits differ in how much of the request Struts reads first. This applies to NES for Apache Struts 1.1.5, 1.2.11, 1.3.18 and later, and to NES for Apache Struts 1.3 Forward Compatibility. Earlier releases throw for every limit except maxStringLen; see Earlier releases. The legacy DiskMultipartRequestHandler on 1.1 and 1.2 reads fewer limits; see The legacy handler.
Text fields over maxStringLen
Struts still reads the whole request. For each text field larger than maxStringLen, it drops the field's value, sets two request attributes to Boolean.TRUE, and logs a warning like this one:
WARNING ... CommonsMultipartRequestHandler.addTextParameter Max-String-Length: FieldName: description, FileName: null, MaxSize: 4096, CurrentSize: 5000
| Request attribute | Constant in org.apache.struts.upload.MultipartRequestHandler |
|---|---|
org.apache.struts.upload.MaxLengthExceeded | ATTRIBUTE_MAX_LENGTH_EXCEEDED |
org.apache.struts.upload.MaxStringLengthExceeded | ATTRIBUTE_MAX_STRING_LENGTH_EXCEEDED |
When MaxLengthExceeded is set, Struts doesn't populate the ActionForm at all. Apache Struts 1 has always reported upload limits this way. Every form property arrives empty, including the other text fields and the uploaded files, and the request carries on:
- With
validate="true", validation runs against the empty form and usually reports missing required fields instead of the real problem. - With
validate="false", the action runs against the empty form.
So a form that accepts long text should check for the attribute. The form's validate() method is the simplest place, because returning errors sends the user back to the mapping's input page:
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
if (Boolean.TRUE.equals(request.getAttribute(
MultipartRequestHandler.ATTRIBUTE_MAX_STRING_LENGTH_EXCEEDED))) {
ActionErrors errors = new ActionErrors();
errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.text.tooLong"));
return errors;
}
return super.validate(mapping, request);
}
Define error.text.tooLong in your message resources. On Struts 1.1, <html:errors/> displays only ActionError objects, so use new ActionError("error.text.tooLong") there. Struts 1.3 has no ActionError class.
For mappings with validate="false", check the same attribute in the action, or check it once for every mapping in a custom RequestProcessor that overrides processValidate(). If the application uses the Tiles plug-in, extend TilesRequestProcessor instead: the plug-in stops Struts from starting when processorClass names any other RequestProcessor subclass. On Struts 1.3, a custom RequestProcessor replaces the default chain-based processor.
The input page comes back without what the user typed, because the form was never populated. A maxlength attribute or a script check on the text field stops most oversized submissions before the browser sends them.
Choosing a maxStringLen value
Set maxStringLen to the largest text value the application accepts. There's no setting for unlimited, because this limit is the fix for CVE-2023-34396: Struts holds each text field in memory as a string, and an unbounded field can exhaust the heap.
The limit counts bytes, not characters. UTF-8 uses two or three bytes for most non-Latin characters, so the 4K default holds about 1,300 Chinese characters.
Other limits
With the default handler, a request over maxSize, maxFileSize, maxHeaderSize or fileCountMax stops Struts parsing it. Struts deletes any file it has already written, sets the request attributes below to Boolean.TRUE, logs a warning, and carries on with an empty ActionForm, as described above for maxStringLen.
| Limit | Request attributes | Constants in org.apache.struts.upload.MultipartRequestHandler |
|---|---|---|
maxSize | MaxLengthExceeded | ATTRIBUTE_MAX_LENGTH_EXCEEDED |
maxHeaderSize | MaxLengthExceeded | ATTRIBUTE_MAX_LENGTH_EXCEEDED |
maxFileSize | MaxLengthExceeded and MaxByteLengthExceeded | ATTRIBUTE_MAX_LENGTH_EXCEEDED and ATTRIBUTE_MAX_BYTE_LENGTH_EXCEEDED |
fileCountMax | MaxLengthExceeded and MaxFileCountExceeded | ATTRIBUTE_MAX_LENGTH_EXCEEDED and ATTRIBUTE_MAX_FILE_COUNT_EXCEEDED |
Each attribute name starts with org.apache.struts.upload.. To handle every limit in one place, test ATTRIBUTE_MAX_LENGTH_EXCEEDED instead of ATTRIBUTE_MAX_STRING_LENGTH_EXCEEDED in the validate() example above.
maxHeaderSize caps the headers of each part, and the part's file name is one of them. With the 512-byte default, a file name longer than about 400 characters is rejected, and fewer in non-Latin scripts, because the limit counts bytes. maxSize and maxHeaderSize set the same attribute, so the application can't tell them apart.
fileCountMax counts every part of the request, text fields included. A form with three text fields and one file sends four parts, so fileCountMax="3" rejects it.
The browser can lose the response when a large upload is rejected. Struts stops reading the request at the limit, and Tomcat reads and discards at most 2 MB more of it (the connector's maxSwallowSize) before it closes the connection. A browser that is still sending may then show a connection error instead of the page.
The legacy handler
Struts 1.1 and 1.2 also ship DiskMultipartRequestHandler, which you select with the controller's multipartClass attribute. Struts 1.3 doesn't include it. It reads only two limits:
maxSizelimits the whole request.maxFileSizelimits each file.
It ignores maxHeaderSize, fileCountMax and maxStringLen. A request over either limit keeps none of its parts and sets only MaxLengthExceeded, never MaxByteLengthExceeded, and Struts carries on with an empty ActionForm. A maxSize or maxFileSize that isn't a valid size logs a warning and uses the 250M default.
Earlier releases
With the default handler, NES for Apache Struts 1.1.4, 1.2.10, 1.3.17 and earlier throw a ServletException for maxSize, maxFileSize, maxHeaderSize and fileCountMax. Struts logs Failed to parse multipart request as an error, the action doesn't run, and the container returns HTTP 500 unless you map an error page. maxStringLen works as described above in every release.
Some settings also applied differently before these releases:
| Releases | What was different |
|---|---|
| 1.3.17 and earlier, and Forward Compatibility 1.4.5 and earlier | Struts ignored maxSize and used maxFileSize as the limit for each file and for the whole request. |
| Forward Compatibility 1.4.5 and earlier | Struts ignored maxHeaderSize and always limited part headers to 512 bytes. |
| Forward Compatibility 1.4.5 and earlier | A memFileSize of 0 or less kept uploads of up to 8 KB in memory; it now writes every non-empty upload to disk, as Apache Struts 1.3.10 does. A memFileSize of 2G or more wrapped around; it now keeps uploads of up to 2 GB in memory. |
| 1.1.4, 1.2.10 and earlier | The legacy handler used maxFileSize as the request limit and had no per-file limit, and a maxFileSize that wasn't a valid size failed every upload with a ServletException. |
| 1.1.4 and earlier | A maxSize of 2G or more wrapped around: 4G rejected every upload, and 2G and 3G removed the limit. |
On 1.3, on Forward Compatibility and with the legacy handler on 1.1 and 1.2, if you set maxFileSize but not maxSize, your request limit is now the 250M default. A small maxFileSize no longer caps the whole request, and a maxFileSize above 250M, or -1, no longer raises it. To keep the old request limit, set maxSize to your maxFileSize value. On 1.3, a validated struts-config.xml needs the 1.3 DOCTYPE to set maxSize. The default handler on 1.1 and 1.2 already read maxSize, so its request limit hasn't changed.