Skip to content

cookie header with multiple cookies and the same key is handled incorrectly #182

Open

Description

@danmilon

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.

What do you think?

Activity

  1. added this to the Version 1.5 milestone on Mar 23, 2015
  2. digitalresistor commented on Apr 14, 2015

    @digitalresistor
    Member

    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).

  3. danmilon commented on Apr 15, 2015

    @danmilon
    Author

    Right. If breaking API compatibility is ok, then MultiDict is the way to go :)

  4. digitalresistor commented on Apr 15, 2015

    @digitalresistor
    Member

    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.

  5. digitalresistor commented on Jun 1, 2015

    @digitalresistor
    Member

    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.

  6. removed this from the Version 1.5 milestone on Jun 1, 2015
  7. mmerickel commented on Jun 1, 2015

    @mmerickel
    Member

    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.

  8. lieryan commented on Mar 21, 2017

    @lieryan
    Contributor

    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().

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

Metadata

Metadata

Assignees

No one assigned

    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