You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cookie header with multiple cookies and the same key is handled incorrectly #182
According to this SO answer and the referenced sections from the RFCs, a request can specify multiple values for a cookie. Currently, webob keeps only the last mentioned value, which is wrong, since the order of the cookies sent has the ones with highest priority first. If we were to keep only one version, it should be the first one.
Also, the documentation says that req.cookie is a MultiDict which currently is incorrect.
IMO, it should either keep being a simple dict, but keep the first value as it appears in the header, or become a MultiDict which for each key has a list of the cookie values as it appears in the header.
This should be fixed, but unfortunately RFC6265 does not specify what a server should do with multiple cookie values, other than accept them. While certain user agents may sort the cookies so that the first one is the one with the highest precedence for the path value (since it matches the longest prefix), this is not a requirement and not something that should be depended on.
It makes the most sense to make it a MultiDict that can contain multiple cookies.
One thing that is also specified is that a server SHOULD NOT send multiple cookies with the same name. Right now that is not the case, and the server is allowed to send multiple cookies with the same name. I wonder how this last one should be implemented anyway, since it may make sense to send the same cookie value with different parameters (for example path/domain).
That's still something I need to decide on. I really don't want to break all the existing applications that use .cookies, so a new property may be introduced.
I'm pulling this out of the 1.5 release simply because I am not sure what the best way to fix this issue is. I want to make it a MultiDict but it breaks so many different applications that expect a single cookie to be returned.
Is there a compatibility issue with making it a MultiDict? I thought that object was bw-compat with a normal dictionary, exception .items() may return duplicate keys.
I think the larger issue is whether the first or last value should be returned when treated-as-a-dict code paths are executed.
The current implementation of RequestCookies._mutate_headers() can be a security issue as well, in one application we have code that more or less looks like this:
def sanitize(request):
""" make a copy of a request with sensitive information removed that can be safely logged """
sanitized_request = request.copy()
for cookie in sanitized_request.cookies:
sanitized_request.cookies[cookie] = '***'
return sanitized_request
which fails to sanitize the cookie header properly when multiple cookies have the same name, and there's no easy way to fix sanitize() without throwing away the entire RequestCookie().
Activity
digitalresistor commentedon Apr 14, 2015
This should be fixed, but unfortunately RFC6265 does not specify what a server should do with multiple cookie values, other than accept them. While certain user agents may sort the cookies so that the first one is the one with the highest precedence for the path value (since it matches the longest prefix), this is not a requirement and not something that should be depended on.
It makes the most sense to make it a MultiDict that can contain multiple cookies.
One thing that is also specified is that a server SHOULD NOT send multiple cookies with the same name. Right now that is not the case, and the server is allowed to send multiple cookies with the same name. I wonder how this last one should be implemented anyway, since it may make sense to send the same cookie value with different parameters (for example path/domain).
danmilon commentedon Apr 15, 2015
Right. If breaking API compatibility is ok, then
MultiDictis the way to go :)digitalresistor commentedon Apr 15, 2015
That's still something I need to decide on. I really don't want to break all the existing applications that use .cookies, so a new property may be introduced.
digitalresistor commentedon Jun 1, 2015
I'm pulling this out of the 1.5 release simply because I am not sure what the best way to fix this issue is. I want to make it a MultiDict but it breaks so many different applications that expect a single cookie to be returned.
I'll keep working on this.
mmerickel commentedon Jun 1, 2015
Is there a compatibility issue with making it a
MultiDict? I thought that object was bw-compat with a normal dictionary, exception.items()may return duplicate keys.I think the larger issue is whether the first or last value should be returned when treated-as-a-dict code paths are executed.
lieryan commentedon Mar 21, 2017
The current implementation of RequestCookies._mutate_headers() can be a security issue as well, in one application we have code that more or less looks like this:
which fails to sanitize the cookie header properly when multiple cookies have the same name, and there's no easy way to fix sanitize() without throwing away the entire RequestCookie().