Repository navigation
Fix CookieException thrown for invalid HttpListener cookies - #19138
Conversation
|
|
||
| // This is a modified copy of CoookieCollection.InternalAdd, which is internal to System.Net.Primitives. | ||
| // It differs from the original in some subtle ways. | ||
| // Firstly, new Variant cookies will not overwrite older ones. We can't have this behaviour as Cookie.Variant |
There was a problem hiding this comment.
If anyone has ideas for this, please let me know
| cookie.Domain = CheckQuoted(_tokenizer.Value); | ||
| cookie.IsQuotedDomain = _tokenizer.Quoted; | ||
| // TODO: this is internal. | ||
| // cookie.IsQuotedDomain = _tokenizer.Quoted; |
There was a problem hiding this comment.
Obviously this needs to be fixed before this goes in
There was a problem hiding this comment.
We don't have a way to solve this problem yet, I guess we can go with reflection option, add to this file here, and then explore options to expose the APIs tracked in issue #13607
There was a problem hiding this comment.
I guess we can go with reflection option
That might break this for UWP. Since we plan to take this managed listener implementation as the System.Net.HttpListener library for UWP, using reflection might not work the same. In the past, reflection APIs weren't supported in UWP. It might be better now....but something to investigate.
| } | ||
|
|
||
| // They are equal here even if variants are still different. | ||
| return 0; |
There was a problem hiding this comment.
you can replace line 27 to end with return string.Compare(left.Path, right.Path, StringComparison.Ordinal)
| bool cookieExists = false; | ||
| foreach (Cookie addedCookie in cookies) | ||
| { | ||
| if (CookieComparer.Compare(cookie, addedCookie) == 0) |
There was a problem hiding this comment.
This is the only place you're using CookieCompaerer, you could inline it, or at least chnage it to just compare equality, which is quicker.
I know nothing about Cookies/this change but I assume htere's a reason you're not just using cookie.Equals (eg you want to ignore cookie.Value)
There was a problem hiding this comment.
This is a copy of the code of CookieCollection.InternalAdd that I can't use because, well, it's internal. CookieCollection.InternalAdd uses this comparer for some reason. It'd be a backwards incompatible change to use cookie.Equals as I think cookie.Equals would return false for things cookiecomparer would return true.
|
Is this Cookie code copied from the existing sources? I really think we need a way to have only one copy of the code even though we might compile it into multiple libraries. We have solved this problem before putting the single copy of the code into a Common place and using ifdef in a sparing way to help change namespaces or whatever. But having multiple copies of this parsing code is going to get us into trouble when we have to make bug fixes. cc: @CIPop @stephentoub |
|
What I did was cut CookieParser and CookieComparer out of Cookie.cs in S.N.Primitives and moved them to their own files in Common. |
[edit] |
No problem. I expect test failures as long until Commit 5 (c81b1f1) is reverted or replaced but I'm not sure how |
|
@hughbe I'm confused where we're at. Are we waiting on something? |
|
.. jus conscious that this must be committed before 5/10 |
|
I'm stumped by the OS X failures. Could the best thing to do be disable these tests on OSX, I don't know what's causing them |
|
Oh wait that's a different PR, #19219 Priya gave some feedback last night which ill do today |
|
Responded to PR feedback - and fixed a bug in my implementation of InternalAdd where variants were ignored. |
| { | ||
| // We need to use Cookie.InternalSetName instead of the Cookie.set_Name wrapped in a try catch block, as | ||
| // Cookie.set_Name keeps the original name if the string is empty or null. | ||
| // Unfortunately this API is internal so we use reflection to access it. The method is cached for performance reasons. |
There was a problem hiding this comment.
@Priya91 unfortunately, we can't do
try
{
cookie.Name = name;
}
catch (CookieException)
{
Debug.Assert(cookie.Name == string.Empty);
}
because if name is null or empty, Cookie.set_Name doesn't override the old name, whereas Cookie.InternalSetName does override the old name unconditionally
| return result; | ||
| } | ||
|
|
||
| // NB: The only path is case sensitive as per spec. However, many Windows applications assume |
There was a problem hiding this comment.
Nit: "The only path" => "Only the path"
| catch (CookieException) | ||
| { | ||
| Debug.Assert(cookie.Name == string.Empty); | ||
| } |
There was a problem hiding this comment.
Why did this change away from using Cookie.InternalSetName? Will this behave identically?
There was a problem hiding this comment.
And if string.Empty will always be rejected, should we special-case it to avoid the exception being thrown?
There was a problem hiding this comment.
You're right, Cookie.set_Name and Cookie.InternalSetName have different properties when passing string.Empty to them.
Cookie:
public string Name
{
get
{
return _name;
}
set
{
if (String.IsNullOrEmpty(value) || !InternalSetName(value))
{
throw new CookieException(SR.Format(SR.net_cookie_attribute, "Name", value == null ? "<null>" : value));
}
}
}
internal bool InternalSetName(string value)
{
if (String.IsNullOrEmpty(value) || value[0] == '$' || value.IndexOfAny(ReservedToName) != -1)
{
_name = string.Empty;
return false;
}
_name = value;
return true;
}
Notice that if we pass string.Empty to set_Name, Cookie.Name stays the same as it was before, but if we pass string.Empty to InternalSetName, Cookie.Name is set to string.Empty.
I've worked around this by doing reflection. This is an older commit
| { | ||
| // This cookie will be rejected | ||
| cookie.InternalSetName(string.Empty); | ||
| SetCookieName(cookie, string.Empty); |
There was a problem hiding this comment.
Isn't this going to just be a nop that throws an exception and eats it? Why call it at all?
There was a problem hiding this comment.
(Same for all of the other call sites)
There was a problem hiding this comment.
Not any more, as we now call cookie.InternalSetName via reflection
| continue; | ||
| } | ||
|
|
||
| // This is a modified copy of CoookieCollection.InternalAdd, which is internal to System.Net.Primitives. |
There was a problem hiding this comment.
This is an old commit, the comment no longer exists :)
| if (NetEventSource.IsEnabled) NetEventSource.Info(this, "uri:" + uri + " setCookieHeader:" + setCookieHeader); | ||
| CookieCollection cookies = new CookieCollection(); | ||
| CookieParser parser = new CookieParser(setCookieHeader); | ||
| for (;;) |
There was a problem hiding this comment.
nit: I think while(true) is more readable but it doesn't really matter as they will compile identically
|
|
||
| return s_internalAddMethod; | ||
| } | ||
| } |
There was a problem hiding this comment.
It'd be more efficient to create and cache a delegate from this rather than using MethodInfo.Invoke each time.
There was a problem hiding this comment.
Could you please clarify this @stephentoub? I'm trying to use Delegate.CreateDelegate or MethodInfo.CreateDelegate but they require us to know the instance to invoke Cookie.InternalSetMethod on
| if (s_isQuotedDomainField == null) | ||
| { | ||
| FieldInfo field = typeof(Cookie).GetField("IsQuotedDomain", BindingFlags.NonPublic | BindingFlags.Instance); | ||
| Debug.Assert(field != null, "We need to use an internal property named IsQuotedDomain that is declared on Cookie."); |
There was a problem hiding this comment.
"an internal property"... property or field?
| for (;;) | ||
| { | ||
| Cookie cookie = parser.GetServer(); | ||
| if (NetEventSource.IsEnabled) NetEventSource.Info(this, "CookieParser returned cookie:" + cookie?.ToString()); |
There was a problem hiding this comment.
nit: space after "CookieParser returned cookie:".
I think the logging doesn't make sense if the cookie is null as the string then will be "CookieParser returned cookie:" //EOL. I would move it after the cookie null check
|
|
||
| internal Cookie GetServer() | ||
| { | ||
| Cookie cookie = _savedCookie; |
There was a problem hiding this comment.
nit: Declaration and assignment in one line
There was a problem hiding this comment.
Sorry I don't understand this, could you clarify? The code is
Cookie cookie = _savedCookie;
_savedCookie = null;
|
|
||
| do | ||
| { | ||
| bool first = cookie == null || cookie.Name == null || cookie.Name.Length == 0; |
There was a problem hiding this comment.
cookie.Name == null || cookie.Name.Length == 0 --> string.IsNullOrEmpty(cookie.Name)
| // this is a new cookie, this token is for the next cookie. | ||
| _savedCookie = new Cookie(); | ||
| int parsed; | ||
| if (int.TryParse(_tokenizer.Value, out parsed)) |
There was a problem hiding this comment.
inline declaration: if (int.TryParse(_tokenizer.Value, out int parsed))
| { | ||
| versionSet = true; | ||
| int parsed; | ||
| if (int.TryParse(CheckQuoted(_tokenizer.Value), out parsed)) |
There was a problem hiding this comment.
same here: inline declaration of int parsed
| { | ||
| expiresSet = true; | ||
| int parsed; | ||
| if (int.TryParse(CheckQuoted(_tokenizer.Value), out parsed)) |
There was a problem hiding this comment.
Why not using double.TryParse instead of int.TryParse? And inline declaration of parsed.
There was a problem hiding this comment.
Is that a breaking change? That means we'd now accept values of CookieToken.MaxAge that are doubles instead of integers only. Not sure that's intended
| expiresSet = true; | ||
|
|
||
| DateTime expires; | ||
| if (DateTime.TryParse(CheckQuoted(_tokenizer.Value), |
There was a problem hiding this comment.
nit: again inline declaration of DateTime expires.
| { | ||
| commentUriSet = true; | ||
| Uri parsed; | ||
| if (Uri.TryCreate(CheckQuoted(_tokenizer.Value), UriKind.Absolute, out parsed)) |
There was a problem hiding this comment.
nit: again inline declaration of Uri parsed
|
@hughbe do you expect to have this ready to merge by Wed ? No obligation: don't know your schedule. If not, someone may have to pick it up :) |
|
I can respond to the PR feedback for this one tomorrow. Timezones may get in the way of any additional PR feedback after I commit the feedback though |
|
I've addressed all PR feedback apart from @ViktorHofer's "nit: Declaration and assignment in one line" which I don't quite understand ;) |
We need to use this in HttpListenerRequest.Cookies as we need to add a custom implementation of CookieCollection.InternalAdd(cookie, true)
Previously, the tests in this PR would fail. Now we align with desktop
|
I rebased for Hugh to fix the merge conflict. |
Did you use that github tool or pulled down locally? I was not sure how the commits would be affected from the github link. |
I pulled it down locally: |
|
Thanks all |
Fixes #18482
Fixes #18486
Contributes to #18128
Best reviewed commit-by-commit, lot's of code movement here.