Repository navigation
reject cookies with control characters as invalid #900
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dxbjavid
wants to merge
2
commits into
apache:master
Choose a base branch
from
dxbjavid:cookie-header-control-chars
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+103
−2
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -199,6 +199,12 @@ public final void validate(final Cookie cookie, final CookieOrigin origin) | |
| throws MalformedCookieException { | ||
| Args.notNull(cookie, "Cookie"); | ||
| Args.notNull(origin, "Cookie origin"); | ||
| if (containsControlChar(cookie.getName())) { | ||
| throw new MalformedCookieException("Cookie name contains control characters"); | ||
| } | ||
| if (containsControlChar(cookie.getValue())) { | ||
| throw new MalformedCookieException("Cookie value contains control characters"); | ||
| } | ||
| for (final CookieAttributeHandler handler: this.attribHandlers) { | ||
| handler.validate(cookie, origin); | ||
| } | ||
|
|
@@ -236,8 +242,9 @@ public List<Header> formatCookies(final List<Cookie> cookies) { | |
| buffer.append(PARAM_DELIMITER); | ||
| buffer.append(' '); | ||
| } | ||
| buffer.append(cookie.getName()); | ||
| final String s = cookie.getValue(); | ||
| final String name = replaceControlChars(cookie.getName()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dxbjavid Let's make it more efficient. One can transfer the name and value content directly into |
||
| buffer.append(name); | ||
| final String s = replaceControlChars(cookie.getValue()); | ||
| if (s != null) { | ||
| buffer.append(EQUAL_CHAR); | ||
| if (containsSpecialChar(s)) { | ||
|
|
@@ -278,4 +285,51 @@ boolean containsChars(final CharSequence s, final Tokenizer.Delimiter chars) { | |
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Tests whether the given cookie name or value contains a control character | ||
| * ({@code %x00-1F} or {@code %x7F}). RFC 6265 permits neither in a cookie-name (a token) | ||
| * nor in a cookie-value (cookie-octet), and such characters must never be copied into | ||
| * a {@code Cookie} request header. | ||
| */ | ||
| static boolean containsControlChar(final CharSequence s) { | ||
| if (s == null) { | ||
| return false; | ||
| } | ||
| for (int i = 0; i < s.length(); i++) { | ||
| final char ch = s.charAt(i); | ||
| if (ch < 0x20 || ch == 0x7f) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Replaces every control character ({@code %x00-1F} or {@code %x7F}) in the given cookie | ||
| * name or value with {@code '?'}, mirroring the substitution the session output buffer | ||
| * already performs, so such characters can never be copied verbatim into a {@code Cookie} | ||
| * request header. Cookies carrying control characters are rejected by {@link #validate} | ||
| * before they reach the cookie store; this is a defensive fallback for a value that bypassed | ||
| * validation. | ||
| */ | ||
| static String replaceControlChars(final CharSequence s) { | ||
| if (s == null) { | ||
| return null; | ||
| } | ||
| StringBuilder buf = null; | ||
| for (int i = 0; i < s.length(); i++) { | ||
| final char ch = s.charAt(i); | ||
| if (ch < 0x20 || ch == 0x7f) { | ||
| if (buf == null) { | ||
| buf = new StringBuilder(s.length()); | ||
| buf.append(s, 0, i); | ||
| } | ||
| buf.append('?'); | ||
| } else if (buf != null) { | ||
| buf.append(ch); | ||
| } | ||
| } | ||
| return buf != null ? buf.toString() : s.toString(); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dxbjavid
#formatCookieslooks like the wrong place to throwIllegalArgumentExceptionand rather too late. Those cookies come from theCookieStoreof the caller and can be considered validated. What we can do instead is filter non-printable characters out or replace with?.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fair point, formatCookies is the wrong place to fail. it now runs the name and value through a replacement that swaps each control char (%x00-1F or %x7F) for '?' rather than throwing, which matches what the session output buffer already does on write, so a cookie coming back out of the store can't split the header. validate() still rejects one on the way in, so this is just a defensive fallback for anything that bypassed validation. tests updated to assert the sanitised wire output.