From 9d66fcc7fd81ec7333fab8ecfc2ae55d80181a5b Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Mon, 30 Nov 2015 22:49:39 +0501 Subject: [PATCH] servo: Merge #8752 - Make `path_matches` match the spec (fixes cookies) (from Manishearth:om-nom); r=metajack Source-Repo: https://github.com/servo/servo Source-Revision: 595bda478ebd54b8be878980852b38c61a2cef2c --- servo/components/net/cookie.rs | 17 ++++++++++++++--- servo/tests/unit/net/cookie.rs | 17 +++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/servo/components/net/cookie.rs b/servo/components/net/cookie.rs index c0f6b1645f6b..b496eb7256e2 100644 --- a/servo/components/net/cookie.rs +++ b/servo/components/net/cookie.rs @@ -113,10 +113,21 @@ impl Cookie { // http://tools.ietf.org/html/rfc6265#section-5.1.4 pub fn path_match(request_path: &str, cookie_path: &str) -> bool { + // A request-path path-matches a given cookie-path if at least one of + // the following conditions holds: + + // The cookie-path and the request-path are identical. request_path == cookie_path || - ( request_path.starts_with(cookie_path) && - ( request_path.ends_with("/") || request_path[cookie_path.len()..].starts_with("/")) - ) + + (request_path.starts_with(cookie_path) && ( + // The cookie-path is a prefix of the request-path, and the last + // character of the cookie-path is %x2F ("/"). + cookie_path.ends_with("/") || + // 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. + request_path[cookie_path.len()..].starts_with("/") + )) } // http://tools.ietf.org/html/rfc6265#section-5.1.3 diff --git a/servo/tests/unit/net/cookie.rs b/servo/tests/unit/net/cookie.rs index c2e1486aa3c6..18058cc336f2 100644 --- a/servo/tests/unit/net/cookie.rs +++ b/servo/tests/unit/net/cookie.rs @@ -25,6 +25,23 @@ fn test_domain_match() { assert!(!Cookie::domain_match("235.132.2.3", ".2.3")); } +#[test] +fn test_path_match() { + assert!(Cookie::path_match("/", "/")); + assert!(Cookie::path_match("/index.html", "/")); + assert!(Cookie::path_match("/w/index.html", "/")); + assert!(Cookie::path_match("/w/index.html", "/w/index.html")); + assert!(Cookie::path_match("/w/index.html", "/w/")); + assert!(Cookie::path_match("/w/index.html", "/w")); + + assert!(!Cookie::path_match("/", "/w/")); + assert!(!Cookie::path_match("/a", "/w/")); + assert!(!Cookie::path_match("/", "/w")); + assert!(!Cookie::path_match("/w/index.html", "/w/index")); + assert!(!Cookie::path_match("/windex.html", "/w/")); + assert!(!Cookie::path_match("/windex.html", "/w")); +} + #[test] fn test_default_path() { assert!(&*Cookie::default_path("/foo/bar/baz/") == "/foo/bar/baz");