Skip to content

Commit

Permalink
Handle "abbreviated" dates like Chromium.
Browse files Browse the repository at this point in the history
Resolves #162.
  • Loading branch information
SergioBenitez committed Aug 16, 2020
1 parent 0d379c0 commit 3795f2e
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::ascii::AsciiExt;

#[cfg(feature = "percent-encode")]
use percent_encoding::percent_decode;
use time::{Duration, OffsetDateTime, PrimitiveDateTime, UtcOffset};
use time::{Duration, OffsetDateTime, PrimitiveDateTime, UtcOffset, Date};

use crate::{Cookie, SameSite, CookieStr};

Expand Down Expand Up @@ -248,7 +248,7 @@ fn parse_inner<'c>(s: &str, decode: bool) -> Result<Cookie<'c>, ParseError> {
Ok(cookie)
}

pub fn parse_cookie<'c, S>(cow: S, decode: bool) -> Result<Cookie<'c>, ParseError>
pub(crate) fn parse_cookie<'c, S>(cow: S, decode: bool) -> Result<Cookie<'c>, ParseError>
where S: Into<Cow<'c, str>>
{
let s = cow.into();
Expand All @@ -260,6 +260,17 @@ pub fn parse_cookie<'c, S>(cow: S, decode: bool) -> Result<Cookie<'c>, ParseErro
pub(crate) fn parse_gmt_date(s: &str, format: &str) -> Result<OffsetDateTime, time::ParseError> {
PrimitiveDateTime::parse(s, format)
.map(|t| t.assume_utc().to_offset(UtcOffset::UTC))
// Handle malformed "abbreviated" dates like Chromium. See cookie#162.
.map(|date| {
let offset = match date.year() {
0..=68 => 2000,
69..=99 => 1900,
_ => return date,
};

let new_date = Date::try_from_ymd(date.year() + offset, date.month(), date.day());
PrimitiveDateTime::new(new_date.expect("date from date"), date.time()).assume_utc()
})
}

#[cfg(test)]
Expand Down Expand Up @@ -450,6 +461,25 @@ mod tests {
Domain=foo.com; Expires=Wed, 21 Oct 2015 07:28:00 GMT", unexpected);
}

#[test]
fn parse_abbreviated_years() {
let cookie_str = "foo=bar; expires=Thu, 10-Sep-20 20:00:00 GMT";
let cookie = Cookie::parse(cookie_str).unwrap();
assert_eq!(cookie.expires().unwrap().year(), 2020);

let cookie_str = "foo=bar; expires=Thu, 10-Sep-68 20:00:00 GMT";
let cookie = Cookie::parse(cookie_str).unwrap();
assert_eq!(cookie.expires().unwrap().year(), 2068);

let cookie_str = "foo=bar; expires=Thu, 10-Sep-69 20:00:00 GMT";
let cookie = Cookie::parse(cookie_str).unwrap();
assert_eq!(cookie.expires().unwrap().year(), 1969);

let cookie_str = "foo=bar; expires=Thu, 10-Sep-99 20:00:00 GMT";
let cookie = Cookie::parse(cookie_str).unwrap();
assert_eq!(cookie.expires().unwrap().year(), 1999);
}

#[test]
fn parse_very_large_max_ages() {
let mut expected = Cookie::build("foo", "bar")
Expand Down

0 comments on commit 3795f2e

Please sign in to comment.