Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Fix invalid exception from urlencoded cookie value #23

Merged
merged 3 commits into from
Sep 14, 2015

Conversation

zerocrates
Copy link
Contributor

This is my alternative solution to #15, as a replacement for the existing PR #16.

This PR fixes the problem by simply re-urlencoding the cookie value before validating it. This is allowable because the actual cookie as output also urlencodes the value (see SetCookie::getFieldValue.

As a concrete example, this PR removes the test testPreventsCRLFAttackViaConstructor. The change makes that test fail, but the test was already failing for an input that wouldn't have actually enabled a CRLF attack. The "evil" example is:

$header = new SetCookie("leo_auth_token", "example\r\n\r\nevilContent");

However, under both the existing code and after the commit in this PR, that SetCookie header creates no CRLF injection problems, because SetCookie already automatically urlencodes values on output. So, the output of $header->toString() on the above "evil" cookie is already safe:

Set-Cookie: leo_auth_token=example%0D%0A%0D%0AevilContent

I think this change is the best way to fix the regression without compromising the security the validation was introduced to provide and without surprising users by changing the header's behavior.

Fix #15
Supersede & close #16

@@ -302,7 +302,7 @@ public function getName()
*/
public function setValue($value)
{
HeaderValue::assertValid($value);
HeaderValue::assertValid(urlencode($value));
Copy link
Member

Choose a reason for hiding this comment

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

When this won't be valid? if there no case exists then remove whole line.

@zerocrates
Copy link
Contributor Author

I see, yeah that's a much better solution. I've replaced my initial commit with one that follows your suggestions.

$header = new SetCookie("leo_auth_token", "example\r\n\r\nevilContent");
$this->assertTrue(HeaderValue::isValid($header->getFieldValue()));
Copy link
Member

Choose a reason for hiding this comment

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

I preffer if you assertEquals against the __toString() output. Will be more clear to understand because the encoded characters will be visible while reading this code.

Copy link
Member

Choose a reason for hiding this comment

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

Could you add an additional test method for unit check of setValue?

@zerocrates
Copy link
Contributor Author

Okay. (I assume the extra test method is what you were looking for, if not let me know)

@Maks3w Maks3w added the bug label Sep 12, 2015
@Maks3w Maks3w added this to the 2.4.3 milestone Sep 12, 2015
@Maks3w
Copy link
Member

Maks3w commented Sep 12, 2015

@weierophinney Please add this one to 2.4.8 LTS

@weierophinney weierophinney merged commit 0a3911c into zendframework:master Sep 14, 2015
weierophinney added a commit that referenced this pull request Sep 14, 2015
Fix invalid exception from urlencoded cookie value
weierophinney added a commit that referenced this pull request Sep 14, 2015
weierophinney added a commit that referenced this pull request Sep 14, 2015
weierophinney added a commit that referenced this pull request Sep 14, 2015
@weierophinney
Copy link
Member

Tagged in release-2.5.2 and 2.4.8.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cookie values decode error
3 participants