Skip to content

Commit ee18b79

Browse files
committed
Don't panic if passed in a large max age.
Duration will panic if the number of seconds is greater than 2^63/1000. This just caps the Max-Age to the highest value we can parse.
1 parent 32d74f8 commit ee18b79

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/parse.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::borrow::Cow;
2+
use std::cmp;
23
use std::error::Error;
34
use std::ascii::AsciiExt;
45
use std::str::Utf8Error;
@@ -152,7 +153,12 @@ fn parse_inner<'c>(s: &str, decode: bool) -> Result<Cookie<'c>, ParseError> {
152153
// max age as 0 seconds.
153154
cookie.max_age = match v.parse() {
154155
Ok(val) if val <= 0 => Some(Duration::zero()),
155-
Ok(val) => Some(Duration::seconds(val)),
156+
Ok(val) => {
157+
// Don't panic if the max age seconds is greater than what's supported by
158+
// `Duration`.
159+
let val = cmp::min(val, Duration::max_value().num_seconds());
160+
Some(Duration::seconds(val))
161+
}
156162
Err(_) => continue,
157163
};
158164
}
@@ -392,4 +398,13 @@ mod tests {
392398

393399
assert_eq!(cookie, expected);
394400
}
401+
402+
#[test]
403+
fn do_not_panic_on_large_max_ages() {
404+
let max_seconds = Duration::max_value().num_seconds();
405+
let expected = Cookie::build("foo", "bar")
406+
.max_age(Duration::seconds(max_seconds))
407+
.finish();
408+
assert_eq_parse!(format!(" foo=bar; Max-Age={:?}", max_seconds + 1), expected);
409+
}
395410
}

0 commit comments

Comments
 (0)