How to Harden Struts 2 Against Remote Code Execution
A defensive checklist for Struts 2: upgrade, restrict method and parameter access, limit OGNL, check uploads and reduce what the server can do.

Struts 2 has a history of critical vulnerabilities, including the remote code execution flaw behind a widely reported breach in 2017 (CVE-2017-5638, in the multipart parser). The pattern repeats across advisories. The framework or the application hands attacker-controlled text to the OGNL expression evaluator, or lets a request parameter reach an object it was never meant to touch. The fix for each one shipped in a release, and most compromises happened on servers that did not install it.
This is a defender's checklist, ordered by effect. It builds on using OGNL safely and assumes a Struts 6 or 7 application. Nothing here is an exploit; each item is a configuration, a line of code or a test that closes a door. The order matters: items one to three change what an attacker can reach, items four to six limit what a successful one can do.
1. Run a supported version and patch fast
The single most effective control is the version. Compare the release you run against the Struts security bulletins. Struts 2.5 and earlier are end of life, and a current Struts 6 or 7 release is the baseline. Make that comparison automatic, because an advisory that nobody reads is not a control:
<plugin>
<groupId>org.owasp</groupId>
<artifactId>dependency-check-maven</artifactId>
<configuration>
<failBuildOnCVSS>7</failBuildOnCVSS>
</configuration>
<executions>
<execution><goals><goal>check</goal></goals></execution>
</executions>
</plugin>failBuildOnCVSS set to 7 fails the build on any high-severity finding in the dependency tree, so a new advisory stops a deploy instead of waiting for someone to notice. Run the same check on a schedule against the main branch, because a library you have not touched in a year is exactly the one that gets a new CVE.
Treat Struts patch releases as security work. Upgrading within a minor line is usually a version bump and a regression run. The expensive upgrades are the ones postponed for three years.
2. Keep strict method invocation on
Struts can call a method named in the configuration or, with dynamic method invocation, in the URL. Keep the second off, and restrict what the first can reach:
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.strictMethodInvocation" value="true" />
<action name="order/*" class="shop.OrderAction" method="{1}">
<allowed-methods>view,list,create</allowed-methods>
<result>/WEB-INF/order.jsp</result>
</action>strictMethodInvocation is on by default in current releases, and a wildcard action with no allowed-methods is where it bites. Without the list, order/* would map a URL segment to any public method of OrderAction. With it, a request for order/delete is refused. Every public method a URL can reach is an entry point, so keep the action classes small and the lists short. The configuration model is covered in configuring actions, results and interceptors.
3. Accept only parameters you wrote down
The params interceptor writes request parameters into action properties by evaluating the parameter name as an OGNL path. That is the mechanism behind a long list of advisories: a parameter named a.b.c walks object graphs. Narrow what it accepts. First, constrain the name:
<interceptor-ref name="params">
<param name="acceptedParamNames">[a-zA-Z0-9\.\]\[_'\s]{1,100}</param>
<param name="paramNameMaxLength">100</param>
</interceptor-ref>Second, in Struts 6.4 and later, require the annotation. A property a request is allowed to set carries @StrutsParameter, and nothing else is writable:
<constant name="struts.parameters.requireAnnotations" value="true" />import org.apache.struts2.ActionSupport;
import org.apache.struts2.interceptor.parameter.StrutsParameter;
public class OrderAction extends ActionSupport {
private String customerId;
private OrderService service; // injected, never a request parameter
@StrutsParameter
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
public String view() {
// service is not reachable from the request: no annotation on its setter
return SUCCESS;
}
}In Struts 7 this requirement is on by default. In a 6.x application you turn it on and run the test suite, and the failures list every property that was reachable by accident. That list is the audit. Keep the framework's default exclusions that block class, classLoader and internal containers; they are the lines between a parameter and the JVM.
4. Leave OGNL locked down
Do not disable the OGNL restrictions to make something work. The usual way they get relaxed is a copied struts.xml from an old project that overrides the new defaults. After every upgrade, search the configuration and fail the build if the dangerous values reappear:
grep -RnE "allowStaticMethodAccess|allowStaticFieldAccess|excludedClasses|excludedPackageNames|struts\.devMode|allowlist\.enable" \
src/main/resources src/main/webapp/WEB-INFThe constants to be suspicious of:
| Constant | Safe value | Why |
|---|---|---|
struts.devMode | false | Dev mode turns on debug interceptors and detailed error pages that disclose internals |
struts.ognl.allowStaticFieldAccess | false | Static access lets an expression reach arbitrary classes |
struts.allowlist.enable | true | Restricts OGNL to classes in the allowed packages |
struts.excludedClasses, struts.excludedPackageNames | Framework defaults, extended and never replaced | The default lists block Runtime, ProcessBuilder and class loaders |
struts.ognl.expressionMaxLength | Default or lower | A payload that needs 5,000 characters of expression is not a valid parameter |
The most important rule has no constant: never put request data into an expression the framework will evaluate again. In a JSP, <s:property value="%{#parameters.q}"/> is an expression built around attacker text, while <s:property value="q"/> reads an action property. Tag attributes that accept %{...} re-evaluate their content, so keep user input out of them.
5. Treat uploads as hostile
File uploads were involved in several advisories. The parser that reads the multipart body runs before any of your code, so limit it in configuration, then validate in the action:
<constant name="struts.multipart.maxSize" value="2097152" />
<constant name="struts.multipart.parser" value="jakarta" /><interceptor-ref name="fileUpload">
<param name="maximumSize">1048576</param>
<param name="allowedTypes">image/png,image/jpeg</param>
<param name="allowedExtensions">.png,.jpg,.jpeg</param>
</interceptor-ref>A parameter-naming flaw in the upload handling was fixed in Struts 2.5.33 and 6.3.0.2 (CVE-2023-50164), a good example of why items one and five go together. The size cap stops a memory attack, the type and extension lists stop the obvious, and neither stops a file named ../../x. Store uploads under a generated name outside the web root, never under the name the client chose. The full handling is in uploading files in Struts 2 safely.
6. Reduce what a compromise can do
Assume that one day an input reaches the evaluator. The remaining controls decide whether that is an incident report or a footnote:
- Run the servlet container as an unprivileged user with no shell and no write access to the application directory.
- Mount the file system read-only where you can, with a writable temp directory and nothing else.
- Restrict outbound connections to the hosts the application needs. A payload that cannot download its second stage is far less useful.
- Keep secrets out of the web root, and out of environment variables the JVM can read when a secret store is available.
- Log requests, and alert on a
Content-Typethat is notmultipart/form-dataorapplication/x-www-form-urlencoded, on very long parameter names, and on%{or${in any parameter.
In a container, the first three are a few lines:
securityContext:
runAsNonRoot: true
runAsUser: 10001
readOnlyRootFilesystem: true
allowPrivilegeEscalation: false
capabilities: { drop: ["ALL"] }7. Test that the controls hold
A control you did not test is a hope. Add a test that fails when the configuration regresses, for example that dynamic method invocation is refused:
import org.apache.struts2.junit.StrutsTestCase;
public class OrderActionSecurityTest extends StrutsTestCase {
public void testUnlistedMethodIsRefused() throws Exception {
request.setParameter("customerId", "42");
try {
executeAction("/order/delete");
fail("delete must not be reachable");
} catch (Exception expected) {
// the action mapping has no such method in allowed-methods
}
}
public void testDevModeIsOff() {
assertFalse(Boolean.parseBoolean(
container.getInstance(String.class, "struts.devMode")));
}
}Run it in CI with the production configuration, not a test copy. A test that loads a different struts.xml than production proves nothing.
8. A filter in front buys time, not safety
A web application firewall rule can block known attack strings and buy a day between disclosure and patch. It is bypassable by design, and a plan that depends on it has already lost. Request size limits at the proxy are a useful second line. Neither replaces item one.
Where this does not apply
If you are planning to leave Struts, this list still applies until the last instance is switched off, and the migration should start with the page that handles the most untrusted input. A new service should use a framework with an active, boring security record, such as Spring MVC. For the broader category of injection flaws see the OWASP Top Ten for Java developers. The Struts security guide is the primary source for every constant above.


