Skip to content

Conversation

@JCgH4164838Gh792C124B5
Copy link
Contributor

Disable struts.ognl.expressionMaxLength by default for Struts 2.5.x.

  • Commented out struts.ognl.expressionMaxLength line in default.properties and provided in-place comments about its usage.
  • Changed OgnlValueStack.handleOgnlException() methods to output error instead of warn for failures to evaluate expressions due to security constraints.
  • Updated existing unit tests to compensate for change in default behaviour.
  • Added a unit test to confirm default behaviour for struts.ognl.expressionMaxLength is disabled.

- Commented out struts.ognl.expressionMaxLength line in default.properties
and provided in-place comments about its usage.
- Changed OgnlValueStack.handleOgnlException() methods to output error
instead of warn for failures to evaluate expressions due to security
constraints.
- Updated existing unit tests to compensate for change in default
behaviour.
- Added a unit test to confirm default behaviour for
struts.ognl.expressionMaxLength is disabled.
@coveralls
Copy link

coveralls commented Nov 10, 2019

Coverage Status

Coverage remained the same at 47.069% when pulling b10e18d on JCgH4164838Gh792C124B5:local_25x_CfgChg2 into fdfeb32 on apache:struts-2-5-x.

@JCgH4164838Gh792C124B5
Copy link
Contributor Author

Hello Apache Struts Team.

This PR is an attempt to address concerns raised on the Struts Dev List concerning struts.ognl.expressionMaxLength introduction and usage in Struts 2.5.21. It was applied to the current 2.5.x branch (2.5.22-SNAPSHOT) but I'm hoping that the maintainers/committers can apply it to 2.5.21 fairly easily ... assuming there is consensus on the changes.

@lukaszlenart and @yasserzamani , please take a look at this PR and see if you think it is an appropriate response to the concerns raised on the Dev list.

Note: I don't know if either of you think there should also be a minimum length check applied to the property set in OgnlUtil as well and refuse setting lower than it (maybe something in the 128 to 200 range) ?

@JCgH4164838Gh792C124B5
Copy link
Contributor Author

Hello.

Coveralls complains that code coverage has fallen ... but I don't see it myself. If there's something extra we should add in the unit tests for OgnlValueStack please let me know.

Copy link
Member

@lukaszlenart lukaszlenart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Comment on lines +223 to +231
### Applies maximum length allowed on OGNL expressions for security enhancement (optional)
###
### **WARNING**: If developers enable this option (by configuration) they should make sure that they understand the implications of setting
### struts.ognl.expressionMaxLength. They must 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. Setting a value of null or "" will also disable the feature.
###
### NOTE: The sample line below is *INTENTIONALLY* commented out, as this feature is disabled by default.
# struts.ognl.expressionMaxLength=256
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

@yasserzamani
Copy link
Member

Thank you so much! It just is missing a unit test to ensure failure when user applies a max length e.g. testFailOnExpressionLongerThan2 - coveralls is sad because of this also (for instance see here).

I think this test should look like this:

    public void testFailOnExpressionLongerThan2() {
        loadConfigurationProviders(new StubConfigurationProvider() {
            @Override
            public void register(ContainerBuilder builder,
                                 LocatableProperties props) throws ConfigurationException {
                props.setProperty(StrutsConstants.STRUTS_OGNL_EXPRESSION_MAX_LENGTH, "2");
            }
        });
//the rest of code should be same as my previous testFailOnTooLongExpressionWithDefaultProperties
}

… for

Struts 2.5.x
- Additional unit test requested by Y. Zamani for code coverage.
- Corrected accidental use of wrong (static) toString method in one test.
- Addition of a minimum struts.ognl.expressionMaxLength value permitted
by Struts 2 (128).  Any value smaller than that is likely to be a
configuration error and if a user really wishes to force it they may go to
OGNL directly to do so.
@JCgH4164838Gh792C124B5
Copy link
Contributor Author

Hello Apache Struts Team.

The PR has been updated with a new unit test suggested by Y. Zamani (code coverage) that seems to make Coveralls happier. 😄

A new minimum maxlength limit (128, applied as a constant) was also added since comments in the Dev List pointed out that setting a really low value does not make much sense either. The unit tests were updated to reflect that and test it as well.

Please let me know if you think applying a minimum maxlength limit makes sense ? If it does make sense but you think a different value (than 128) should be used, just let me know what value you think it should be and I can update the PR to reflect that.

@yasserzamani
Copy link
Member

Thank you @JCgH4164838Gh792C124B5 but IMHO it is a not needed complexity. Furthermore I think there are no reason to restrict user to set a low value because maybe his/her app may not exceed that value really. It isn't Struts business. If so why we don't restrict user to set a high value which is more rational than this?

@JCgH4164838Gh792C124B5
Copy link
Contributor Author

Hello @yasserzamani. Thanks for the feedback.

I could not envision a Struts scenario where a really low value (e.g. 0) would make sense and very low values are more likely to produce failed expressions in even a typical application. The other concern was that a "bad actor" might somehow find a way to abuse the setting in the future and cause a sort of service denial in an application (by setting a very low maximum expression length).

On the other hand putting a lower limit restricts the flexibility for configuration by a developer. I could simply adjust the lower-bound constant to 0 for now (which will allow any non-negative value to be set) and the mechanism would be there to enforce a lower-limit in the future if circumstances call for it. Please let me know if that would be OK in your view ?

@lukaszlenart
Copy link
Member

I have some concerns that adding that minimum maxlength can really confuse users - they will have to know that there is a limit in which they have to operate. And I'm concern that users won't use such functionality at all IMO.

… for

Struts 2.5.x
- Removed minimum struts.ognl.expressionMaxLength (restored to previous
behaviour) as requested by Y. Zamani and L. Lenart.
- Updated unit tests to compensate for the above change.
- Changed log output from warn to error in applyExpressionMaxLength() on
exception since it will likely be considered a fatal condition.
@JCgH4164838Gh792C124B5
Copy link
Contributor Author

Hello @lukaszlenart and @yasserzamani .

As requested the "minimum maxlength" idea has been removed, returning to the original logic by Yasser. The only change to OgnlUtil in this PR now is that applyExpressionMaxLength() will log an error instead of a warn when setting the value fails (as per justification in the commit comment).

I think the PR now has everything that was requested. Let me know if it is OK now or more changes are needed.

@lukaszlenart
Copy link
Member

LGTM 👍

@yasserzamani yasserzamani merged commit 3dfc5a4 into apache:struts-2-5-x Nov 16, 2019
@JCgH4164838Gh792C124B5 JCgH4164838Gh792C124B5 deleted the local_25x_CfgChg2 branch November 30, 2019 19:04
yasserzamani pushed a commit to yasserzamani/struts that referenced this pull request Dec 21, 2019
* Disable struts.ognl.expressionMaxLength by default for Struts 2.5.x.
- Commented out struts.ognl.expressionMaxLength line in default.properties
and provided in-place comments about its usage.
- Changed OgnlValueStack.handleOgnlException() methods to output error
instead of warn for failures to evaluate expressions due to security
constraints.
- Updated existing unit tests to compensate for change in default
behaviour.
- Added a unit test to confirm default behaviour for
struts.ognl.expressionMaxLength is disabled.

* Updated commit for disable struts.ognl.expressionMaxLength by default for
Struts 2.5.x
- Additional unit test requested by Y. Zamani for code coverage.
- Corrected accidental use of wrong (static) toString method in one test.
- Addition of a minimum struts.ognl.expressionMaxLength value permitted
by Struts 2 (128).  Any value smaller than that is likely to be a
configuration error and if a user really wishes to force it they may go to
OGNL directly to do so.

* Updated commit for disable struts.ognl.expressionMaxLength by default for
Struts 2.5.x
- Removed minimum struts.ognl.expressionMaxLength (restored to previous
behaviour) as requested by Y. Zamani and L. Lenart.
- Updated unit tests to compensate for the above change.
- Changed log output from warn to error in applyExpressionMaxLength() on
exception since it will likely be considered a fatal condition.

(cherry picked from commit 3dfc5a4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants