Implement cookie expiry date parsing algorithm (#37715)

The cookie-rs library parses the cookie expiry date based on the format
from RFC 2616 (for HTTP/1.1), which is stricter than the format from RFC
6265 (for HTTP cookie).

This patch implements the cookie expiry date algorithm from RFC 6265.
When Cookie::parse fails in parsing the expiry date, we try to parse the
expiry again with this algorithm, to provide extra compatibility with
legacy systems.

Testing: Pass a WPT test that was expected to fail before, and add a
unit test.
Fixes: #36452

---------

Signed-off-by: Kingsley Yung <kingsley@kkoyung.dev>
This commit is contained in:
Kingsley Yung 2025-07-11 13:18:11 +08:00 committed by GitHub
parent 099d20fe94
commit 464d71ecfc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 310 additions and 6 deletions

View file

@ -6,6 +6,7 @@ use net::cookie::ServoCookie;
use net::cookie_storage::CookieStorage;
use net_traits::CookieSource;
use servo_url::ServoUrl;
use time::macros::datetime;
#[test]
fn test_domain_match() {
@ -473,3 +474,27 @@ fn test_cookie_eviction_all_nonsecure_new_nonsecure() {
"extra2=bar; extra3=bar; extra4=bar; extra5=bar; foo=bar"
);
}
#[test]
fn test_parse_date() {
assert_eq!(
ServoCookie::parse_date("26 Jun 2024 15:35:10 GMT"), // without day of week
Some(datetime!(2024-06-26 15:35:10).assume_utc())
);
assert_eq!(
ServoCookie::parse_date("26-Jun-2024 15:35:10 GMT"), // dashed
Some(datetime!(2024-06-26 15:35:10).assume_utc())
);
assert_eq!(
ServoCookie::parse_date("26 Jun 2024 15:35:10"), // no GMT
Some(datetime!(2024-06-26 15:35:10).assume_utc())
);
assert_eq!(
ServoCookie::parse_date("26 Jun 24 15:35:10 GMT"), // 2-digit year
Some(datetime!(2024-06-26 15:35:10).assume_utc())
);
assert_eq!(
ServoCookie::parse_date("26 jun 2024 15:35:10 gmt"), // Lowercase
Some(datetime!(2024-06-26 15:35:10).assume_utc())
);
}