Skip to content

DefaultHTTPUtility uses hard coded Header name/value lengths #656

Closed

Description

@Criptak

Generates an error when adding a header via DefaultHTTPUtilities:
org.owasp.esapi.errors.ValidationException: addHeader: Invalid input. The maximum length of 20 characters was exceeded.

String safeName = ESAPI.validator().getValidInput("addHeader", strippedName, "HTTPHeaderName", 20, false);

The DefaultHTTPUtility uses hard coded Header name/value lengths rather than reference the MaxHeaderNameSize and MaxHeaderValueSize set in properties. This should either be updated to reference the SecurityConfiguration similar to the SecurityWrapperResponse,

safeName = ESAPI.validator().getValidInput("addHeader", strippedName, "HTTPHeaderName", sc.getIntProp("HttpUtilities.MaxHeaderNameSize"), false);
, or be refactored to use the SecurityWrapperResponse if there is duplication.

Activity

  1. xeno6696 commented on Jan 30, 2022

    @xeno6696
    Collaborator

    I genuinely thought I had found all the ways that was being set. Kinda crazy it took 4yrs for someone to bring up.

  2. kwwall commented on Jan 30, 2022

    @kwwall
    Contributor

    @Criptak - Good catch.
    @xeno6696 - My guess it has taken 4 years to discover because we don't have sufficient test cases for (at least) the DefaultHTTPUtilities class. In fact, all I see is src/test/java/org/owasp/esapi/reference/HTTPUtilitiesTest.java and there and nothing that really tests that version of DefaultHTTPUtilities.addHeader() at all. I think if we would have had adequate test cases we would have caught this way earlier, at least when these changes were made. Can I assign this to you, Matt?

  3. self-assigned this
    on Jan 30, 2022
  4. xeno6696 commented on Jan 30, 2022

    @xeno6696
    Collaborator

    Yeah, I'll try to take a deeper sweep to see if there's anything else I might have missed. And of course yes, I'll get them under test.

  5. xeno6696 commented on Jan 30, 2022

    @xeno6696
    Collaborator

    FWIW I thought I had done a grep for "addHeader" and so I feel I should have snagged this one.

  6. kwojcikowski commented on Feb 25, 2022

    @kwojcikowski

    Hi,
    Could You consider also changing it in DefaultHTTPUtilities#addCookie(HttpServletResponse response, Cookie cookie)?
    String cookieName = ESAPI.validator().getValidInput("cookie name", name, "HTTPCookieName", 50, false, errors); String cookieValue = ESAPI.validator().getValidInput("cookie value", value, "HTTPCookieValue", 5000, false, errors);

  7. xeno6696 commented on Feb 25, 2022

    @xeno6696
    Collaborator

    It'll be changed to be user-configurable. I will bump the default value to 1024--somewhat arbitrary but in my experience cookies larger than 500 chars are rare. I question cookie lengths of 5K as you're suggesting. That sounds like something weird is going on. But the limit will be configurable so if you want 5000 you can set it.

  8. kwwall commented on Feb 25, 2022

    @kwwall
    Contributor

    I think there is a maximum length for all the cookies...something around 4K IIRC. So if you set something like 5000 per cookie, you're probably only asking for trouble. I suggest making it pick up the values from ESAPI's properties (Validator.HTTPCookieName, and Validator.HTTPCookieValue). You may also want to change the regex for Validator.HTTPCookieName to bump the max cookie length from 32 to 50.

  9. xeno6696 commented on Feb 25, 2022

    @xeno6696
    Collaborator

    Okay so @Criptak and @kwojcikowski --Just so we're clear, the current codebase says this:

    192.        String cookieName = ESAPI.validator().getValidInput("cookie name", name, "HTTPCookieName", 50, false, errors);
    
    193.        String cookieValue = ESAPI.validator().getValidInput("cookie value", value, "HTTPCookieValue", 5000, false, errors);
    

    image

    So, I'm thinking that if you update to use the most recent version of ESAPI, you are already covered.

    @kwwall just so you know we've had a default max cookie value of 4096 going back at least 12yrs. (I didn't push further than that.) See line 41 in HTTPUtilities.java.

  10. kwwall commented on Feb 25, 2022

    @kwwall
    Contributor

    I think (at least 12 years ago), 4K was the limitation for most browsers and was taken from RFC 2109. That # may have changed. The latest (draft) RFC for "HTTP State Management" is RFC 6265.

    Also, I grepped for MAX_COOKIE_LEN and the only place we use that is when dealing with encrypted cookies in the DefaultHTTPUtilities.encryptStateInCookie(). Was a bit surprised that was the only place that 4K value seems to be reflected.

  11. xeno6696 commented on Feb 25, 2022

    @xeno6696
    Collaborator

    I found the same thing, I wasn't sure if you felt that should be parameterized or left compilation-only for crypto purposes?

  12. kwwall commented on Feb 25, 2022

    @kwwall
    Contributor

    If we start using it more widely, it definitely should be parameterized. We should also look at the RFC to see if the value has increased. It likely has.

  13. kwojcikowski commented on Mar 17, 2022

    @kwojcikowski

    Hi @xeno6696,
    I see that You have updated cookies header and value to be parameterized. Will there be any update on HTTPHeaderName length parametrization?

  14. xeno6696 commented on Mar 19, 2022

    @xeno6696
    Collaborator

    @kwojcikowski I'll get those propagated as well, there were a few spots where that got missed. Appreciate the assist.

  15. xeno6696 commented on Mar 19, 2022

    @xeno6696
    Collaborator

    Yeah, the HTTPUtilities class didn't have any unit tests, the only ones that exist now are ones that I built back when I made this initial parameterization adjustment. Clearly I felt rushed. I will generate another issue to ensure that the whole class is properly tested. Much of this had to be mocked, and so it'll also be good to review my old tests to ensure they're actually testing things. I'm always cautious with mocks.

  16. xeno6696 commented on Mar 19, 2022

    @xeno6696
    Collaborator

    I'm gonna take that back. It had some unit tests, using mock classes. (back before Mockito/PowerMock.) The tests feel muddy here. There's functionality getting tested in HTTPUtilitiesTest that should probably be back in the Validator tests. Not sure I want to tackle that as part of this issue however.

  17. xeno6696 commented on Mar 19, 2022

    @xeno6696
    Collaborator

    So mocking the addHeader from the HTTPUtilities test is a bit much in regards to mock setup, but the following unit test covers what we're after.

        @Test
        public void testHeaderLengthChecks(){
        	Validator v = ESAPI.validator();
        	SecurityConfiguration sc = ESAPI.securityConfiguration();
        	assertFalse(v.isValidInput("addHeader", TestUtils.generateStringOfLength(257), "HTTPHeaderName", sc.getIntProp("HttpUtilities.MaxHeaderNameSize"), false));
        	assertFalse(v.isValidInput("addHeader", TestUtils.generateStringOfLength(4097), "HTTPHeaderValue", sc.getIntProp("HttpUtilities.MaxHeaderValueSize"), false));
        }
    
  18. kwwall commented on Jul 16, 2022

    @kwwall
    Contributor

    This seems to have been fixed via PR #663, which would have been first appeared in the ESAPI 2.3.0.0 release, so I am marking this as closed.

    @Criptak - Add a comment to this issue if it is not fixed in ESAPI 2.3.0.0 and later and we'll re-open this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions