New submission from Karthikeyan Singaravelan <tir.kar...@gmail.com>:

I came across the issue during https://bugs.python.org/issue35121#msg332583 . I 
think this can be dealt as a separate issue not blocking the original report. I 
am classifying it as security but can be reclassified as a bug fix given the 
section on weak confidentiality in RFC 6265. I have a fix implemented at 
https://github.com/tirkarthi/cpython/tree/bpo35121-cookie-path.

Report : 

I have come across another behavior change between path checks while using the 
cookie jar implementation available in Python. This is related to incorrect 
cookie validation but with respect to path. I observed the following difference 
: 

1. Make a request to "/" that sets a cookie with "path=/any"
2. Make a request to "/any" and the set cookie is passed since the path matches
3. Make a request to "/anybad" and cookie with "path=/any" is also passed too.

On using golang stdlib implementation of cookiejar the cookie "path=/any" is 
not passed when a request to "/anybad" is made. So I checked with RFC 6265 
where the path match check is defined in section-5.1.4 . RFC 6265 also 
obsoletes RFC 2965 upon which cookiejar is based I hope since original 
implementation of cookiejar is from 2004 and RFC 6265 was standardized later. 
So I think it's good to enforce RFC 6265 since RFC 2965 is obsolete at least in 
Python 3.8 unless this is considered as a security issue. I think this is a 
security issue. The current implementation can potentially cause cookies to be 
sent to incorrect paths in the same domain that share the same prefix. This is 
a behavior change with more strict checks but I could see no tests failing with 
RFC 6265 implementation too.

RFC 2965 also gives a loose definition of path-match without mentioning about / 
check in the paths based on which Python implementation is based as a simple 
prefix match.

> For two strings that represent paths, P1 and P2, P1 path-matches P2
> if P2 is a prefix of P1 (including the case where P1 and P2 string-
> compare equal).  Thus, the string /tec/waldo path-matches /tec.

RFC 6265 path-match definition : 
https://tools.ietf.org/html/rfc6265#section-5.1.4

   A request-path path-matches a given cookie-path if at least one of
   the following conditions holds:

   o  The cookie-path and the request-path are identical.

   o  The cookie-path is a prefix of the request-path, and the last
      character of the cookie-path is %x2F ("/").

   o  The cookie-path is a prefix of the request-path, and the first
      character of the request-path that is not included in the cookie-
      path is a %x2F ("/") character.

The current implementation in cookiejar is as below : 

def path_return_ok(self, path, request):
    _debug("- checking cookie path=%s", path)
    req_path = request_path(request)
    if not req_path.startswith(path):
        _debug("  %s does not path-match %s", req_path, path)
        return False
    return True

Translating the RFC 6265 steps (a literal translation of go implementation) 
would have something like below and no tests fail on master. So the python 
implementation goes in line with the RFC not passing cookies of "path=/any" to 
/anybody

def path_return_ok(self, path, request):
    req_path = request_path(request)
    if req_path == path:
        return True
    elif req_path.startswith(path) and ((path.endswith("/") or 
req_path[len(path)] == "/")):
        return True
        
    return False


The golang implementation is as below which is a literal translation of RFC 
6265 steps at 
https://github.com/golang/go/blob/50bd1c4d4eb4fac8ddeb5f063c099daccfb71b26/src/net/http/cookiejar/jar.go#L130

// pathMatch implements "path-match" according to RFC 6265 section 5.1.4.
func (e *entry) pathMatch(requestPath string) bool {
        if requestPath == e.Path {
                return true
        }
        if strings.HasPrefix(requestPath, e.Path) {
                if e.Path[len(e.Path)-1] == '/' {
                        return true // The "/any/" matches "/any/path" case.
                } else if requestPath[len(e.Path)] == '/' {
                        return true // The "/any" matches "/any/path" case.
                }
        }
        return false
}


RFC 6265 on weak confidentiality 
(https://tools.ietf.org/html/rfc6265#section-8.5)

   Cookies do not always provide isolation by path.  Although the
   network-level protocol does not send cookies stored for one path to
   another, some user agents expose cookies via non-HTTP APIs, such as
   HTML's document.cookie API.  Because some of these user agents (e.g.,
   web browsers) do not isolate resources received from different paths,
   a resource retrieved from one path might be able to access cookies
   stored for another path.

----------
components: Library (Lib)
messages: 332919
nosy: martin.panter, ned.deily, orsenthil, serhiy.storchaka, xtreak
priority: normal
severity: normal
status: open
title: Cookie path check returns incorrect results
type: security
versions: Python 3.7, Python 3.8

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue35647>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to