Fork me on GitHub
Edit on GitHub

Security

Security tips

The Apache Struts 2 doesn’t provide any security mechanism - it is just a pure web framework. Below are few tips you should consider during application development with the Apache Struts 2.

Restrict access to the Config Browser Plugin

Config Browser Plugin exposes internal configuration and should be used only during development phase. If you must use it on production site, we strictly recommend restricting access to it - you can use
Basic Authentication or any other security mechanism (e.g. Apache Shiro)

Don’t mix different access levels in the same namespace

Very often access to different resources is controlled based on URL patterns, see snippet below. Because of that you cannot mix actions with different security levels in the same namespace. Always group actions in one namespace by security level.

<security-constraint>
    <web-resource-collection>
        <web-resource-name>admin</web-resource-name>
        <url-pattern>/secure/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>admin</role-name>
    </auth-constraint>
</security-constraint>

A restriction by URL pattern in front of Struts, such as a security-constraint, a URL-pattern filter or a reverse proxy rule, is only as good as the agreement between the path it checks and the path Struts maps to an action. Since Struts 7.5.0 and 6.13.0 the framework maps actions from the servlet path and path info reported by the container. In earlier releases the two paths could differ and such a restriction could be bypassed, see S2-086. There is no configuration-only mitigation, so upgrade if your application relies on this kind of access control. Authorization enforced inside Struts, for example with an interceptor on the action, applies to the action that actually runs.

Restrict the HTTP methods an action accepts

An action that changes state should not be reachable with GET. You can restrict the HTTP methods an action accepts with the @AllowedHttpMethod, @HttpGet, @HttpPost, @HttpPut, @HttpDelete and @HttpGetOrPost annotations and, since Struts 7.5.0, with the http-method attribute of the <action> element, see HTTP Methods.

The restriction is enforced by the httpMethod interceptor, which is part of basicStack, defaultStack, paramsPrepareParamsStack and the stacks built on them. If you define your own interceptor stack, make sure it includes httpMethod, otherwise the restriction is not enforced.

Where the annotation has to be placed depends on the version:

Struts version What is enforced
7.5.0 and later An annotation on the action class or on the invoked method, the default method included, and an annotation inherited from a parent class or an interface. The default stacks of the REST and Bean Validation plugins enforce the restrictions as well.
7.0.0 - 7.4.0, 6.2.0 - 6.13.0 An annotation on the action class. An annotation on the action’s default method is not enforced when the action mapping does not name the method, see S2-088. The default stacks of the REST and Bean Validation plugins do not enforce the restrictions.
6.9.0 only In addition to the row above, an annotation declared only on the action class is not enforced when the action configuration names the method to invoke, see S2-079. Upgrade to Struts 6.10.0 at least.

The 6.x line does not carry the S2-088 fix. If you stay on 6.x, or on a 7.x release before 7.5.0, declare the restriction on the action class instead of on the method. On Struts 6.9.0, which is also affected by S2-079, declare it on both the action class and the invoked method, or upgrade to Struts 6.10.0 at least. From Struts 6.9.0 and 7.2.1 a mapping that names the method explicitly with the method attribute enforces a method-level annotation as well.

Never expose JSP files directly

You must always hide JSP file behind an action, you cannot allow for direct access to the JSP files as this can leads to unpredictable security vulnerabilities. You can achieve this by putting all your JSP files under the WEB-INF folder

Note: since Struts 7.2.0, the framework now logs a security warning when JSP tags are accessed directly outside of an action scope. This helps identify JSP files that are inadvertently exposed without action protection.

<!-- Restricts access to pure JSP files - access available only via Struts action -->
<security-constraint>
    <display-name>No direct JSP access</display-name>
    <web-resource-collection>
        <web-resource-name>No-JSP</web-resource-name>
        <url-pattern>*.jsp</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>no-users</role-name>
    </auth-constraint>
</security-constraint>

<security-role>
    <description>Don't assign users to this role</description>
    <role-name>no-users</role-name>
</security-role>

The best approach is to used the both solutions.

Disable devMode

The devMode is a very useful option during development time, allowing for deep introspection and debugging into you app.

However, in production it exposes your application to be presenting too many informations on application’s internals or to evaluating risky parameter expressions. Please always disable devMode before deploying your application to a production environment. While it is disabled by default, your struts.xml might include a line setting it to true. The best way is to ensure the following setting is applied to our struts.xml for production deployment:

<constant name ="struts.devMode" value="false" />

Reduce logging level

It’s a good practice to reduce logging level from DEBUG to INFO or less. Framework’s classes can produce a lot of logging entries which will pollute the log file. You can even set logging level to WARN for classes that belongs to the framework, see example Log4j2 configuration:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="com.opensymphony.xwork2" level="warn"/>
        <Logger name="org.apache.struts2" level="warn"/>
        <Root level="info">
            <AppenderRef ref="STDOUT"/>
        </Root>
    </Loggers>
</Configuration>

The com.opensymphony.xwork2 logger is needed on Struts 6.x only, the package no longer exists in Struts 7.

Use UTF-8 encoding

Always use UTF-8 encoding when building an application with the Apache Struts 2, when using JSPs please add the following header to each JSP file

<%@ page contentType="text/html; charset=UTF-8" %>

Defining and annotating your Action parameters

Note: Since 6.4 using struts.parameters.requireAnnotations=true. Or by default from 7.0.

Request parameters, such as those submitted by a form, can be stored on your Struts Action class by defining getters and setters for them. For example, if you have a form with a field called name, you can store the value of that field by defining a public void setName(String name) method on your Action class, and then importantly, annotating this method with @StrutsParameter. The presence of this annotation indicates that the method is intended for parameter injection and is safe to be invoked by any user who can view the Action.

private String name;

@StrutsParameter
public void setName(String name) {
    this.name = name;
}

If you wish to populate a DTO (Data Transfer Object) instead of setting the parameters directly on the Action class, you can define a getter for the DTO on your Action class instead. For example, define a method public MyDto getFormData() which is also annotated by @StrutsParameter(depth = 1). Then, a parameter with name formData.fullName will be mapped to the setter setFullName on that DTO. Note that the @StrutsParameter annotation has a depth field which dictates the depth to which parameter injection is permitted. The default value is 0, which only allows setting parameters directly on the Action class as in the first example. A depth of 1 indicates that the immediate public properties of an object returned by the getter are permitted to be set. If you have further nested objects, you can increase the depth accordingly. Do not set this depth field to a value greater than the minimum required for your use case.

private MyDto formData = new MyDto();

@StrutsParameter(depth = 1)
public MyDto getFormData() {
    return formData;
}

public static class MyDto {
    private String fullName;

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }
}

It is critical that any method you annotate with @StrutsParameter is safe for any user who can view that corresponding action to invoke (including any public methods on objects returned by that method and so forth). Any getters you annotate should only ever return a DTO or a collection/hierarchy of DTOs. Do NOT mix business logic or service references with your parameter injection methods and DTOs. Additionally, any database DTOs should be entirely separate from request parameter/form DTOs.

Do NOT, under any circumstance, annotate a method that returns one of the following unsafe objects:

If you are finding updating your application with this new annotation time-consuming, you can temporarily combine the above option with struts.parameters.requireAnnotations.transitionMode=true. When this mode is enabled, only ‘nested’ parameters, i.e. DTOs or Collections represented by public getters on Action classes, will require annotations. This means public setters will still be exposed for parameter injection. Notably, the auto-allowlisting capability, which is also supported by these annotations, is not degraded in any way, so it proves a useful transitioning option for applications that wish to enable the OGNL allowlist as soon as possible.

The annotation is required for values bound from a request. Values copied from one action to the next by the Chaining Interceptor are not checked against it by default. Since Struts 7.2.0 you can set struts.chaining.requireAnnotations=true to apply the same rule there; the option only has an effect when struts.parameters.requireAnnotations is enabled too, and since Struts 7.5.0 it also covers values the Alias Interceptor copies from the value stack. On earlier releases, including 6.x, chained values are not checked against the annotation. The StrutsParameter Annotation page lists every place where the annotation is checked.

Do not define setters when not needed

Note: Only relevant if you are not using struts.parameters.requireAnnotations=true as per the previous section.

You should carefully design your actions without exposing anything via setters and getters, this can lead to potential security vulnerabilities. Any action’s setter can be used to set incoming untrusted user’s value which can contain suspicious expression. Some Struts Results automatically populate params based on values in ValueStack (action in most cases is the root) which means incoming value will be evaluated as an expression during this process.

Do not use incoming values as an input for localisation logic

All TextProvider’s getText(...) methods (e.g. inActionSupport) perform evaluation of parameters included in a message to properly localize the text. This means using incoming request parameters with getText(...) methods is potentially dangerous and should be avoided. See example below, assuming that an action implements getter and setter for property message, the below code allows inject an OGNL expression:

public String execute() throws Exception {
    message = getText(getMessage());
    return SUCCESS;
}

Never use value of incoming request parameter as part of your localization logic.

The same care applies to messages that carry a submitted value. With the Bean Validation Plugin, before Struts 7.5.0 and 6.13.0, a validation message that places the submitted value into its text could be evaluated as an expression instead of being shown as text (on Struts 7 only when the OGNL allowlist is disabled), see S2-081. Upgrade, or give every validation constraint whose message includes the validated value an explicit message key and provide the text for that key in the application’s message resources.

Do not use incoming, untrusted user input in forced expression evaluation

You can use a forced expression evalaution in many tags’ attributes by using %{...} syntax. This is a very powerful option but used with wrong data can lead to the Remote Code Execution. Never use forced expression evalaution if you didn’t verify the input or it can be passed in by a user.

Never use value of incoming request parameter as input for forced expression evalaution.

Use Struts tags instead of raw EL expressions

JSP EL doesn’t perform any kind of escaping, you must perform this using a dedicated function, see this example. Never use a raw ${} EL expression on incoming values as this can lead to injecting a malicious code into the page.

The safest option is to use Struts Tags instead.

The tags escape the values they write by default. Do not switch the escaping off for a value that can come from a request:

Two defects in the escaping applied by the tags were fixed in Struts 7.5.0 and 6.13.0: additional tag attributes rendered by the Javatemplates Plugin (S2-083) and the doubleselect tag used with the doubleMultiple attribute (S2-087). Each bulletin describes a workaround for applications that cannot upgrade yet.

Define custom error pages

As mentioned in S2-006 it’s a good practicse to define your own error pages. This avoids exposing users to XSS attacks as Struts does not escape action’s names in automatically generated error pages.

You can either disable DMI

<constant name="struts.enable.DynamicMethodInvocation" value="false" />

or define an error page

<global-results>
  <result name="error">/error_page.jsp</result>
</global-results>
 
<global-exception-mappings>
  <exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>

Ambiguous Action Methods

This can impact actions which have large inheritance hierarchy and use the same method’s name throughout the hierarchy, this was reported as an issue WW-4405. See the example below:

public class RealAction extends BaseAction {  
    @Action("save")
    public String save() throws Exception {
        super.save();
        return SUCCESS;
    }
}

public class BaseAction extends AbstractAction {
    public String save() throws Exception {
        save(Double.MAX_VALUE);
        return SUCCESS;
    }
}

public abstract class AbstractAction extends ActionSupport {
    protected void save(Double val) {
        // some logic
    }
}

In such case OGNL cannot properly map which method to call when request is coming. This is do the OGNL limitation. To solve the problem don’t use the same method’s names through the hierarchy, you can simply change the action’s method from save() to saveAction() and leaving annotation as is to allow call this action via /save.action request.

Accepted / Excluded Patterns

The framework provides two interfaces which are used to accept / exclude param names and values - AcceptedPatternsChecker and ExcludedPatternsChecker with default implementations. These two interfaces are used by the Parameters Interceptor and Cookie Interceptor to check if param can be accepted or must be excluded. If you were using excludeParams previously please compare patterns used by you with these provided by the framework in default implementation.

Parameter node guard

Note: since Struts 7.6.0.

The accepted and excluded patterns check the text of a parameter or cookie name. Once a name is accepted, the framework parses it as an OGNL expression and checks the parsed expression too, before any part of it is evaluated: every node has to be allowed by an org.apache.struts2.ognl.ParameterNodeGuard. A name with a node the guard refuses is not set on the value stack. The refusal is logged at debug level; when devMode is on it is reported like any other parameter error, and for a cookie the exception propagates from the interceptor.

The default guard, StrutsParameterNodeGuard, admits the property paths that the default accepted pattern produces:

Name Parsed as
name ASTProperty with an ASTConst child
user.name, list[0], map['key'] an ASTChain of such properties
users(1), users('key') ASTMethod with exactly one ASTConst child

Any other node, such as a variable reference (#foo), a non-constant index (list[a]), a static member, a constructor or a method with no argument or several, is refused.

Names accepted by the default patterns are unaffected. An application that widens the accepted names with struts.override.acceptedPatterns or struts.additional.acceptedPatterns to admit other expression shapes has to supply a guard that admits them too. Extending the default guard keeps its rules and adds one:

public class TwoIndexMethodGuard extends StrutsParameterNodeGuard {

    @Override
    public boolean isAllowed(Node node) {
        if (node instanceof ASTMethod && node.jjtGetNumChildren() == 2) {
            return node.jjtGetChild(0) instanceof ASTConst && node.jjtGetChild(1) instanceof ASTConst;
        }
        return super.isAllowed(node);
    }
}

Set the struts.ognl.parameterNodeGuard constant to the class name:

<constant name="struts.ognl.parameterNodeGuard" value="com.example.TwoIndexMethodGuard"/>

or declare a bean and refer to it by name:

<bean type="org.apache.struts2.ognl.ParameterNodeGuard" name="twoIndexMethod" class="com.example.TwoIndexMethodGuard"/>
<constant name="struts.ognl.parameterNodeGuard" value="twoIndexMethod"/>

The guard is asked about every node of the parsed name, children included, so it only needs to judge one node at a time. A guard that implements ParameterNodeGuard directly must therefore also allow the ASTChain and ASTConst nodes of the names it expects. It is a singleton and is called from many threads, so it must decide from the node and its children only, never from the request or any other state that changes.

This guard applies only to names bound from a request, through the Parameters Interceptor, the ActionMappingParametersInterceptor and the Cookie Interceptor. Expressions in templates, results and validators are not affected by it. The Struts OGNL Guard covers every evaluated expression, parameter names included: struts.ognl.excludedNodeTypes and struts.ognl.nodeGuard still apply to them, and the parameter node guard is checked on top.

Strict Method Invocation

This mechanism was introduced in version 2.5. It allows control what methods can be accessed with the bang “!” operator via Dynamic Method Invocation. Please read more in the Strict Method Invocation section of Action Configuration.

Resource Isolation Using Fetch Metadata

Note: since Struts 6.0.0

Fetch Metadata is a mitigation against common cross-origin attacks such as Cross-Site Request Forgery (CSRF). It is a web platform security feature designed to help servers defend themselves against cross-origin attacks based on the preferred resource isolation policy. The browser provides information about the context of an HTTP request in a set of Sec-Fetch-* headers. This allows the server processing the request to make decisions on whether the request should be accepted or rejected based on the available resource isolation policies.

A Resource Isolation Policy prevents the resources on a server from being requested by external websites. This policy can be enabled for all endpoints of the application or the endpoints that are meant to be loaded in a cross-site context can be exempted from applying the policy. Read more about Fetch Metadata and resource isolation here.

This mechanism is implemented in Struts using the FetchMetadata Interceptor. The interceptor is part of the defaultStack and, since Struts 7.4.0 and 6.12.0, of the REST plugin’s restDefaultStack. The basicStack and paramsPrepareParamsStack do not include it; if you use one of them or define your own interceptor stack, add it there.

Releases before Struts 7.5.0 and 6.13.0 accepted some cross-site requests the policy rejects, see S2-080. The fixed releases reject them with a 403 response, which also affects applications that legitimately receive cross-site form submissions, for example from a single sign-on provider or a payment gateway. Exempt such endpoints as described in Cross-site form posts.

Requests without a Sec-Fetch-Site header are always accepted, so do not rely on Fetch Metadata alone to protect state-changing actions. The Token Interceptor and the Token Session Interceptor check a token issued by the application. When an action combines one of them with the Execute and Wait Interceptor, declare the token interceptor before it and submit to the action only from pages that carry a token. Releases before Struts 7.5.0 and 6.13.0 need this ordering to keep the token check effective, see S2-082.

Cross Origin Isolation with COOP and COEP

Note: since Struts 6.0.0

Cross-Origin Opener Policy is a security mitigation that lets developers isolate their resources against side-channel attacks and information leaks. The COOP response header allows a document to request a new browsing context group to better isolate itself from other untrustworthy origins.

Cross-Origin Embedder Policy prevents a document from loading any cross-origin resources which don’t explicitly grant the document permission to be loaded.

COOP and COEP are independent mechanisms that can be enabled, tested and deployed separately. While enabling one doesn’t require developers to enable the other, when set together COOP and COEP allows developers to use powerful features (such as SharedArrayBuffer, performance.measureMemory() and the JS Self-Profiling API) securely, without worrying about side channel attacks like Spectre. Further reading on COOP/COEP and why you need cross-origin isolation.

The recommended configuration for the policies are:

Cross-Origin-Embedder-Policy: require-corp;
Cross-Origin-Opener-Policy: same-origin;

COOP and COEP are implemented in Struts using CoopInterceptor and CoepInterceptor.

Limit what a single request can consume

Limit the size of a request body ahead of the application, in the reverse proxy or the servlet container, and set the limit as low as your endpoints allow. This bounds every request before the framework reads it and is the mitigation recommended for releases that read a request body without a bound, see S2-072, S2-073 and S2-077.

Inside the framework the following limits apply. Raise a limit only for a documented need and only as far as needed.

Setting Default Bounds Read more
struts.multipart.maxSize, struts.multipart.maxFiles, struts.multipart.maxFileSize, struts.multipart.maxStringLength, struts.multipart.maxParameterCount 2 MB, 256, not set, 4096, 256 Size of a multipart request, number of files, size of one file, length of a normal form field, number of normal form fields (since Struts 7.3.0) Action File Upload
struts.json.maxLength, struts.json.maxElements, struts.json.maxDepth 2 MB, 10000, 64 JSON request body read by the JSON plugin. maxLength bounds the read since Struts 7.3.0 and 6.11.0 JSON Plugin
struts.rest.content.maxLength 2 MB Request body read by the REST plugin, since Struts 7.4.0 and 6.12.0 REST Plugin
struts.csp.report.maxSize 8192 Content Security Policy violation report, since Struts 7.3.0 and 6.11.0 CSP Interceptor
struts.i18n.cacheMaxSize 10000 Entries of each localized-text cache, since Struts 7.3.0 and 6.11.0 Localization
struts.ognl.expressionMaxLength 256 Length of an evaluated OGNL expression below

Two more recommendations:

Handle uploaded files as untrusted input

Use the Action File Upload support and keep its limits on the number and size of files as low as the application allows. Restrict the accepted file types to the ones the application expects.

The file name and the content type of an upload are supplied by the client. Never use the submitted file name to build the path under which the file is stored, generate the name on the server instead, and do not store uploads in a location from which the container serves or executes content.

Leave legacy features disabled

Some features exist only for backward compatibility and are disabled by default, or deprecated. Do not enable them in a new application and plan to move away from them in an existing one:

Proactively protecting against OGNL Expression Injections attacks

The framework has a history of critical security bugs, many tied to its use of OGNL technology; Due to its ability to create or change executable code, OGNL is capable of introducing critical security flaws to any framework that uses it. Multiple Struts 2 versions have been vulnerable to OGNL security flaws. Consequently, we’ve equipped OGNL and the framework with a number of additional security capabilities, some of which need to be manually enabled.

NOTE: These might break your current app functionality. Before using in production environment, you’re recommended to comprehensively test your app UI and functionalities with these enabled.

Restricting access to the Struts Context (ActionContext)

The Struts ActionContext is a core construct of the Struts framework. It is shared and manipulated throughout the codebase. From the ActionContext, it is possible to access application parameters, the OgnlValueStack, the current request/response/session, the servlet context, the Guice container, and a number of other objects either directly or indirectly via the directly exposed objects. The Struts ActionContext enables powerful features and functionality, but it also presents a major security risk if not properly secured.

The Struts ActionContext is accessible to OGNL expressions. In the case of an OGNL expression exploit, usually achieved through some form of server-side template injection or parameter injection, the ActionContext is a prime gadget for escalation of the vulnerability, often to remote code execution (RCE). Whilst known harmful capabilities of the ActionContext items are blocked by the OGNL Member Access policy exclusion list (see below), this is not always effective due to the myriad of changing objects available on the ActionContext. The new allowlist capability (also see below) offers much stronger protection. However, for the strongest level of protection, we recommend disabling access to the ActionContext from OGNL expressions entirely.

Note that before disabling access to the ActionContext from OGNL expressions, you should ensure that your application does not rely on this capability. OGNL expressions may access the context directly using the # operator, or indirectly using the OgnlValueStack’s fallback to context lookup capability. As of Struts 6.4.0, the Set, Iterator and Action Struts components require ActionContext access from OGNL expressions.

To disable access to the ActionContext from OGNL expressions, set the following constants in your struts.xml or struts.properties file. The option struts.ognl.excludedNodeTypes is an OGNL Guard setting which completely forbids the context accessing syntax node. The struts.ognl.valueStackFallbackToContext option disables ValueStack behaviour which allows the context to be accessed indirectly via a fallback behaviour triggered when an OGNL expression does not evaluate to a valid value.

<constant name="struts.ognl.valueStackFallbackToContext" value="false"/>
<constant name="struts.ognl.excludedNodeTypes" value="
            ognl.ASTThisVarRef,
            ognl.ASTVarRef
"/>

Apply a maximum allowed length on OGNL expressions

You can enable this via Struts configuration key struts.ognl.expressionMaxLength (defaults to 256). OGNL thereupon doesn’t evaluate any expression longer than specified value. You would choose a value large enough to permit ALL valid OGNL expressions used within the application. Values larger than the 200-400 range have diminishing security value (at which point it is really only a “style guard” for long OGNL expressions in an application).

OGNL Member Access

Struts 2 implements an OGNL internal security mechanism which blocks access to particular classes and Java packages - it’s an OGNL-wide mechanism which means it affects any aspect of the framework i.e. incoming parameters, expressions used in JSPs, etc. Matching is done based on both the target and member class of all components of an OGNL expression.

There are 4 options that can be used to configure excluded packages and classes:

The defaults are defined here.

Since Struts 7.5.0 and 6.13.0 the default excluded packages also include java.beans, java.lang.reflect and java.lang.invoke, in both the regular and the devMode lists. An application that needs a class from one of these packages in an expression can re-allow that class with struts.excludedPackageExemptClasses (struts.devMode.excludedPackageExemptClasses in devMode).

Any expression or target which does not pass this criteria will be blocked, and you will see a warning in the logs:

[WARNING] Target class [class example.MyBean] or declaring class of member type [public example.MyBean()] are excluded!

In that case new MyBean() was used to create a new instance of class (inside JSP) - it’s blocked because the target of such expression is java.lang.Class which is excluded.

It is possible to redefine the above constants in struts.xml, but avoid reducing the list, instead extending the list with other known dangerous classes or packages in your application.

Additional Options

We additionally recommend enabling the following options (enabled by default in 7.0).

Allowlist Capability

Note: Since Struts 6.4. Or by default from 7.0.

For the most stringent OGNL protection, we recommend enabling the allowlist capability with struts.allowlist.enable.

Now, in addition to enforcing the exclusion list, classes involved in OGNL expression must also belong to a list of allowlisted classes and packages. By default, all required Struts classes are allowlisted as well as any classes that are defined in your struts.xml package configurations.

We highly recommend enabling the parameter annotation capability to ensure any necessary parameter injection types are allowlisted, in addition to its other benefits.

You can add additional classes and packages to the allowlist with:

Depending on the functionality of your application, you may not need to manually allowlist any classes. Please monitor your application logs for any warnings about blocked classes and add them to the allowlist as necessary.

Extensibility

Note: since Struts 6.4.

The OGNL Member Access mechanism is extensible, allowing you to define your own rules for blocking access to OGNL expression evaluations. To do so, you may use the struts.securityMemberAccess extension point. Please be vigilant when overriding methods as not to reduce protections offered by the default implementation.

Struts OGNL Guard

Note: since Struts 6.4.

The Struts OGNL Guard allows applications to completely disable certain OGNL expression features/capabilities. This feature is disabled by default but can be enabled and configured with struts.ognl.excludedNodeTypes.

It is recommended to disable any OGNL feature you are not leveraging in your application.

For example, if you do not need to use the addition operation in any OGNL expressions, you can add ognl.ASTAdd to your excluded node types. This will mitigate against a host of String concatenation attacks.

For applications using a minimal number of Struts features, you may find the following list a good starting point. Please be aware that this list WILL break certain Struts features.

<constant name="struts.ognl.excludedNodeTypes"
          value="
            ognl.ASTAdd,
            ognl.ASTAssign,
            ognl.ASTBitAnd,
            ognl.ASTBitNegate,
            ognl.ASTBitOr,
            ognl.ASTCtor,
            ognl.ASTDivide,
            ognl.ASTEval,
            ognl.ASTIn,
            ognl.ASTInstanceof,
            ognl.ASTKeyValue,
            ognl.ASTList,
            ognl.ASTMap,
            ognl.ASTMultiply,
            ognl.ASTNegate,
            ognl.ASTNotIn,
            ognl.ASTProject,
            ognl.ASTRootVarRef,
            ognl.ASTSelect,
            ognl.ASTSelectFirst,
            ognl.ASTSelectLast,
            ognl.ASTSequence,
            ognl.ASTShiftLeft,
            ognl.ASTShiftRight,
            ognl.ASTStaticField,
            ognl.ASTStaticMethod,
            ognl.ASTThisVarRef,
            ognl.ASTUnsignedShiftRight,
            ognl.ASTVarRef,
            ognl.ASTXor
"/>

Since Struts 7.6.0 the excluded node types are checked twice: when an expression is parsed, as before, and again by OGNL itself right before the expression is evaluated. The second check also covers an expression that was not parsed by the framework’s own OgnlUtil.

Extensibility

Note: since Struts 7.6.0.

A list of node types cannot express every rule. ognl.ASTCtor, for instance, stands for both new Foo() and new int[10], so excluding it forbids both. For a rule that depends on what a node contains, implement org.apache.struts2.ognl.OgnlNodeGuard and select it with the struts.ognl.nodeGuard constant. The guard is asked about every node of an expression before any part of it is evaluated; returning false for one node refuses the whole expression.

Extending OGNL’s ognl.NodeVisitorGuard gives one visit method per node type, each allowing the node unless overridden:

public class NoArrayCreationGuard extends NodeVisitorGuard implements OgnlNodeGuard {

    @Override
    public Boolean visit(ASTCtor node, Void data) {
        return !node.isArray();
    }
}

Set the constant to the class name:

<constant name="struts.ognl.nodeGuard" value="com.example.NoArrayCreationGuard"/>

or declare a bean and refer to it by name:

<bean type="org.apache.struts2.ognl.OgnlNodeGuard" name="noArrayCreation" class="com.example.NoArrayCreationGuard"/>
<constant name="struts.ognl.nodeGuard" value="noArrayCreation"/>

The two settings add up:

Setting What it does Default
struts.ognl.excludedNodeTypes Refuses every node of a listed type empty, no node type is refused
struts.ognl.nodeGuard Selects the OgnlNodeGuard asked about every other node StrutsNodeGuard, which allows them

A custom guard is applied on top of struts.ognl.excludedNodeTypes, it does not replace the list, and it cannot allow a node type the list excludes.

A guard is a singleton and is called from many threads. OGNL asks it once per parsed expression and remembers the answer, so a guard must give the same answer for the same node every time: decide from the node, its children and its parents only, never from the request, the user or any other state that changes.

OgnlNodeGuard is the only type an application implements. StrutsNodeGuard is the default implementation, and ContextNodeGuard is the internal class that combines the list with the selected guard.

Before Struts 7.6.0

The struts.ognlGuard extension point and the OgnlGuard interface let an application define its own rules for blocking both raw OGNL expressions and parsed syntax trees. They are deprecated since Struts 7.6.0 and will be removed in Struts 8.0.0; they keep working until then.

Run OGNL expressions inside sandbox

This is a legacy option. It relies on the Java Security Manager, which is deprecated for removal since JDK 17, and it does not work with JDK 21 and above. Prefer the allowlist and the Struts OGNL Guard described above.

You can enable it by adding -Dognl.security.manager to JVM arguments. OGNL thereupon utilizes Java Security Manager to run OGNL expressions (which includes your actions either!) inside a sandbox with no permission. It is worth noting that it affects only OGNL expression execution and thereafter OGNL reverts Java Security Manager to its previous state.

Follow @x