#![cfg_attr(all(nightly, doc), feature(doc_cfg))]
#![cfg_attr(
not(all(
feature = "percent-encode",
feature = "private",
feature = "signed",
feature = "key-expansion",
)),
allow(rustdoc::broken_intra_doc_links)
)]
#![deny(missing_docs)]
mod builder;
mod date;
mod parse;
mod jar;
mod delta;
mod same_site;
mod expiration;
pub mod prefix;
#[cfg(any(feature = "private", feature = "signed"))] #[macro_use] mod secure;
#[cfg(any(feature = "private", feature = "signed"))] pub use secure::*;
use std::borrow::Cow;
use std::fmt;
use std::str::FromStr;
use std::time::{Duration, SystemTime};
use crate::parse::{parse_cookie, trim};
pub use crate::parse::ParseError;
pub use crate::builder::CookieBuilder;
pub use crate::jar::{CookieJar, Delta, Iter};
pub use crate::same_site::*;
pub use crate::expiration::*;
#[derive(Debug, Clone)]
enum CookieStr<'c> {
Indexed(usize, usize),
Concrete(Cow<'c, str>),
}
impl<'c> CookieStr<'c> {
fn indexed(needle: &str, haystack: &str) -> Option<CookieStr<'static>> {
let haystack_start = haystack.as_ptr() as usize;
let needle_start = needle.as_ptr() as usize;
if needle_start < haystack_start {
return None;
}
if (needle_start + needle.len()) > (haystack_start + haystack.len()) {
return None;
}
let start = needle_start - haystack_start;
let end = start + needle.len();
Some(CookieStr::Indexed(start, end))
}
fn to_str<'s>(&'s self, string: Option<&'s Cow<str>>) -> &'s str {
match *self {
CookieStr::Indexed(i, j) => {
let s = string.expect("`Some` base string must exist when \
converting indexed str to str! (This is a module invariant.)");
&s[i..j]
},
CookieStr::Concrete(ref cstr) => cstr,
}
}
#[allow(clippy::ptr_arg)]
fn to_raw_str<'s, 'b: 's>(&'s self, string: &'s Cow<'b, str>) -> Option<&'b str> {
match *self {
CookieStr::Indexed(i, j) => {
match *string {
Cow::Borrowed(s) => Some(&s[i..j]),
Cow::Owned(_) => None,
}
},
CookieStr::Concrete(_) => None,
}
}
fn into_owned(self) -> CookieStr<'static> {
use crate::CookieStr::*;
match self {
Indexed(a, b) => Indexed(a, b),
Concrete(Cow::Owned(c)) => Concrete(Cow::Owned(c)),
Concrete(Cow::Borrowed(c)) => Concrete(Cow::Owned(c.into())),
}
}
}
fn truncate_attribute(mut string: Cow<'_, str>) -> Cow<'_, str> {
if let Some(i) = string.find(|c: char| c == ';' || c.is_ascii_control()) {
match &mut string {
Cow::Borrowed(s) => *s = &s[..i],
Cow::Owned(s) => s.truncate(i),
}
}
string
}
#[derive(Debug, Clone)]
pub struct Cookie<'c> {
cookie_string: Option<Cow<'c, str>>,
name: CookieStr<'c>,
value: CookieStr<'c>,
expires: Option<Expiration>,
max_age: Option<Duration>,
domain: Option<CookieStr<'c>>,
path: Option<CookieStr<'c>>,
secure: Option<bool>,
http_only: Option<bool>,
same_site: Option<SameSite>,
partitioned: Option<bool>,
}
impl<'c> Cookie<'c> {
pub fn new<N, V>(name: N, value: V) -> Self
where N: Into<Cow<'c, str>>,
V: Into<Cow<'c, str>>
{
Cookie {
cookie_string: None,
name: CookieStr::Concrete(name.into()),
value: CookieStr::Concrete(value.into()),
expires: None,
max_age: None,
domain: None,
path: None,
secure: None,
http_only: None,
same_site: None,
partitioned: None,
}
}
#[deprecated(since = "0.18.0", note = "use `Cookie::build(name)` or `Cookie::from(name)`")]
pub fn named<N>(name: N) -> Cookie<'c>
where N: Into<Cow<'c, str>>
{
Cookie::new(name, "")
}
pub fn build<C: Into<Cookie<'c>>>(base: C) -> CookieBuilder<'c> {
CookieBuilder::from(base.into())
}
pub fn parse<S>(s: S) -> Result<Cookie<'c>, ParseError>
where S: Into<Cow<'c, str>>
{
parse_cookie(s.into(), false)
}
#[cfg(feature = "percent-encode")]
#[cfg_attr(all(nightly, doc), doc(cfg(feature = "percent-encode")))]
pub fn parse_encoded<S>(s: S) -> Result<Cookie<'c>, ParseError>
where S: Into<Cow<'c, str>>
{
parse_cookie(s.into(), true)
}
#[inline(always)]
pub fn split_parse<S>(string: S) -> SplitCookies<'c>
where S: Into<Cow<'c, str>>
{
SplitCookies {
string: string.into(),
last: 0,
decode: false,
}
}
#[cfg(feature = "percent-encode")]
#[cfg_attr(all(nightly, doc), doc(cfg(feature = "percent-encode")))]
#[inline(always)]
pub fn split_parse_encoded<S>(string: S) -> SplitCookies<'c>
where S: Into<Cow<'c, str>>
{
SplitCookies {
string: string.into(),
last: 0,
decode: true,
}
}
pub fn into_owned(self) -> Cookie<'static> {
Cookie {
cookie_string: self.cookie_string.map(|s| s.into_owned().into()),
name: self.name.into_owned(),
value: self.value.into_owned(),
expires: self.expires,
max_age: self.max_age,
domain: self.domain.map(|s| s.into_owned()),
path: self.path.map(|s| s.into_owned()),
secure: self.secure,
http_only: self.http_only,
same_site: self.same_site,
partitioned: self.partitioned,
}
}
#[inline]
pub fn name(&self) -> &str {
self.name.to_str(self.cookie_string.as_ref())
}
#[inline]
pub fn value(&self) -> &str {
self.value.to_str(self.cookie_string.as_ref())
}
#[inline]
pub fn value_trimmed(&self) -> &str {
#[inline(always)]
fn trim_quotes(s: &str) -> &str {
if s.len() < 2 {
return s;
}
let bytes = s.as_bytes();
match (bytes.first(), bytes.last()) {
(Some(b'"'), Some(b'"')) => &s[1..(s.len() - 1)],
_ => s
}
}
trim_quotes(self.value())
}
#[inline]
pub fn name_value(&self) -> (&str, &str) {
(self.name(), self.value())
}
#[inline]
pub fn name_value_trimmed(&self) -> (&str, &str) {
(self.name(), self.value_trimmed())
}
#[inline]
pub fn http_only(&self) -> Option<bool> {
self.http_only
}
#[inline]
pub fn secure(&self) -> Option<bool> {
self.secure
}
#[inline]
pub fn same_site(&self) -> Option<SameSite> {
self.same_site
}
#[inline]
pub fn partitioned(&self) -> Option<bool> {
self.partitioned
}
#[inline]
pub fn max_age(&self) -> Option<Duration> {
self.max_age
}
#[inline]
pub fn path(&self) -> Option<&str> {
match self.path {
Some(ref c) => Some(c.to_str(self.cookie_string.as_ref())),
None => None,
}
}
#[inline]
pub fn domain(&self) -> Option<&str> {
let domain = self.domain_verbatim()?;
domain.strip_prefix('.').or(Some(domain))
}
#[inline]
pub fn expires(&self) -> Option<Expiration> {
self.expires
}
#[inline]
pub fn expires_datetime(&self) -> Option<SystemTime> {
self.expires.and_then(|e| e.datetime())
}
pub fn set_name<N: Into<Cow<'c, str>>>(&mut self, name: N) {
self.name = CookieStr::Concrete(name.into())
}
pub fn set_value<V: Into<Cow<'c, str>>>(&mut self, value: V) {
self.value = CookieStr::Concrete(value.into())
}
#[inline]
pub fn set_http_only<T: Into<Option<bool>>>(&mut self, value: T) {
self.http_only = value.into();
}
#[inline]
pub fn set_secure<T: Into<Option<bool>>>(&mut self, value: T) {
self.secure = value.into();
}
#[inline]
pub fn set_same_site<T: Into<Option<SameSite>>>(&mut self, value: T) {
self.same_site = value.into();
}
#[inline]
pub fn set_partitioned<T: Into<Option<bool>>>(&mut self, value: T) {
self.partitioned = value.into();
}
#[inline]
pub fn set_max_age<D: Into<Option<Duration>>>(&mut self, value: D) {
self.max_age = value.into();
}
pub fn set_path<P: Into<Cow<'c, str>>>(&mut self, path: P) {
self.path = Some(CookieStr::Concrete(truncate_attribute(path.into())));
}
pub fn unset_path(&mut self) {
self.path = None;
}
pub fn set_domain<D: Into<Cow<'c, str>>>(&mut self, domain: D) {
self.domain = Some(CookieStr::Concrete(truncate_attribute(domain.into())));
}
pub fn unset_domain(&mut self) {
self.domain = None;
}
pub fn set_expires<T: Into<Expiration>>(&mut self, time: T) {
self.expires = Some(time.into()
.map(|time| std::cmp::min(time, date::max_datetime())));
}
pub fn unset_expires(&mut self) {
self.expires = None;
}
pub fn make_permanent(&mut self) {
let twenty_years = Duration::from_secs(20 * 365 * 24 * 60 * 60);
self.set_max_age(twenty_years);
self.set_expires(SystemTime::now() + twenty_years);
}
pub fn make_removal(&mut self) {
self.set_value("");
self.set_max_age(Duration::ZERO);
self.set_expires(SystemTime::UNIX_EPOCH);
}
fn fmt_parameters(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(true) = self.http_only() {
write!(f, "; HttpOnly")?;
}
if let Some(same_site) = self.same_site() {
write!(f, "; SameSite={}", same_site)?;
}
if let Some(true) = self.partitioned() {
write!(f, "; Partitioned")?;
}
if self.secure() == Some(true)
|| self.partitioned() == Some(true)
|| self.secure().is_none() && self.same_site() == Some(SameSite::None)
{
write!(f, "; Secure")?;
}
if let Some(path) = self.path() {
write!(f, "; Path={}", path)?;
}
if let Some(domain) = self.domain() {
write!(f, "; Domain={}", domain)?;
}
if let Some(max_age) = self.max_age() {
write!(f, "; Max-Age={}", max_age.as_secs().min(i64::MAX as u64))?;
}
if let Some(time) = self.expires_datetime() {
write!(f, "; Expires={}", date::HttpDate(time))?;
}
Ok(())
}
#[inline]
pub fn name_raw(&self) -> Option<&'c str> {
self.cookie_string.as_ref()
.and_then(|s| self.name.to_raw_str(s))
}
#[inline]
pub fn value_raw(&self) -> Option<&'c str> {
self.cookie_string.as_ref()
.and_then(|s| self.value.to_raw_str(s))
}
#[inline]
pub fn path_raw(&self) -> Option<&'c str> {
match (self.path.as_ref(), self.cookie_string.as_ref()) {
(Some(path), Some(string)) => path.to_raw_str(string),
_ => None,
}
}
#[inline]
pub fn domain_raw(&self) -> Option<&'c str> {
match (self.domain.as_ref(), self.cookie_string.as_ref()) {
(Some(domain), Some(string)) => domain.to_raw_str(string),
_ => None,
}
}
#[inline]
pub fn domain_verbatim(&self) -> Option<&str> {
match self.domain {
Some(ref c) => Some(c.to_str(self.cookie_string.as_ref())),
None => None,
}
}
#[cfg(feature = "percent-encode")]
#[cfg_attr(all(nightly, doc), doc(cfg(feature = "percent-encode")))]
#[inline(always)]
pub fn encoded<'a>(&'a self) -> Display<'a, 'c> {
Display::new_encoded(self)
}
#[cfg_attr(feature = "percent-encode", doc = r##"
// Note: `encoded()` is only available when `percent-encode` is enabled.
assert_eq!(&c.encoded().to_string(), "name=value%3B%20Domain%3Dcrates.io");
"##)]
#[inline(always)]
pub fn unescaped<'a>(&'a self) -> Display<'a, 'c> {
Display::new_unescaped(self)
}
}
pub struct SplitCookies<'c> {
string: Cow<'c, str>,
last: usize,
decode: bool,
}
impl<'c> Iterator for SplitCookies<'c> {
type Item = Result<Cookie<'c>, ParseError>;
fn next(&mut self) -> Option<Self::Item> {
while self.last < self.string.len() {
let i = self.last;
let j = self.string[i..]
.find(';')
.map(|k| i + k)
.unwrap_or(self.string.len());
self.last = j + 1;
if trim(&self.string[i..j]).is_empty() {
continue;
}
return Some(match self.string {
Cow::Borrowed(s) => parse_cookie(trim(&s[i..j]), self.decode),
Cow::Owned(ref s) => parse_cookie(trim(&s[i..j]).to_owned(), self.decode),
})
}
None
}
}
#[cfg(feature = "percent-encode")]
mod encoding {
use percent_encoding::{AsciiSet, CONTROLS};
const FRAGMENT: &AsciiSet = &CONTROLS
.add(b' ')
.add(b'"')
.add(b'<')
.add(b'>')
.add(b'`');
const PATH: &AsciiSet = &FRAGMENT
.add(b'#')
.add(b'?')
.add(b'{')
.add(b'}');
const USERINFO: &AsciiSet = &PATH
.add(b'/')
.add(b':')
.add(b';')
.add(b'=')
.add(b'@')
.add(b'[')
.add(b'\\')
.add(b']')
.add(b'^')
.add(b'|')
.add(b'%');
const COOKIE: &AsciiSet = &USERINFO
.add(b'(')
.add(b')')
.add(b',');
pub fn encode(string: &str) -> impl std::fmt::Display + '_ {
percent_encoding::percent_encode(string.as_bytes(), COOKIE)
}
}
#[cfg_attr(feature = "percent-encode", doc = r##"
// Note: `encoded()` is only available when `percent-encode` is enabled.
assert_eq!(&c.encoded().to_string(), "my%20name=this%3B%20value%25%3F; Secure");
assert_eq!(&c.encoded().stripped().to_string(), "my%20name=this%3B%20value%25%3F");
"##)]
pub struct Display<'a, 'c: 'a> {
cookie: &'a Cookie<'c>,
#[cfg(feature = "percent-encode")]
encode: bool,
strip: bool,
}
impl<'a, 'c: 'a> fmt::Display for Display<'a, 'c> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
#[cfg(feature = "percent-encode")] {
if self.encode {
let name = encoding::encode(self.cookie.name());
let value = encoding::encode(self.cookie.value());
write!(f, "{}={}", name, value)?;
} else {
write!(f, "{}={}", self.cookie.name(), self.cookie.value())?;
}
}
#[cfg(not(feature = "percent-encode"))] {
write!(f, "{}={}", self.cookie.name(), self.cookie.value())?;
}
match self.strip {
true => Ok(()),
false => self.cookie.fmt_parameters(f)
}
}
}
impl<'a, 'c> Display<'a, 'c> {
#[cfg(feature = "percent-encode")]
fn new_encoded(cookie: &'a Cookie<'c>) -> Self {
Display { cookie, strip: false, encode: true }
}
fn new_unescaped(cookie: &'a Cookie<'c>) -> Self {
Display { cookie, strip: false, #[cfg(feature = "percent-encode")] encode: false }
}
#[inline]
pub fn stripped(mut self) -> Self {
self.strip = true;
self
}
}
impl FromStr for Cookie<'static> {
type Err = ParseError;
fn from_str(s: &str) -> Result<Cookie<'static>, ParseError> {
Cookie::parse(s).map(|c| c.into_owned())
}
}
impl<'a, 'b> PartialEq<Cookie<'b>> for Cookie<'a> {
fn eq(&self, other: &Cookie<'b>) -> bool {
let so_far_so_good = self.name() == other.name()
&& self.value() == other.value()
&& self.http_only() == other.http_only()
&& self.secure() == other.secure()
&& self.partitioned() == other.partitioned()
&& self.same_site() == other.same_site()
&& self.max_age() == other.max_age()
&& self.expires() == other.expires()
&& self.path() == other.path();
if !so_far_so_good {
return false;
}
match (self.domain(), other.domain()) {
(Some(a), Some(b)) if a.eq_ignore_ascii_case(b) => {}
(None, None) => {}
_ => return false,
};
true
}
}
impl<'a> From<&'a str> for Cookie<'a> {
fn from(name: &'a str) -> Self {
Cookie::new(name, "")
}
}
impl From<String> for Cookie<'static> {
fn from(name: String) -> Self {
Cookie::new(name, "")
}
}
impl<'a> From<Cow<'a, str>> for Cookie<'a> {
fn from(name: Cow<'a, str>) -> Self {
Cookie::new(name, "")
}
}
impl<'a, N, V> From<(N, V)> for Cookie<'a>
where N: Into<Cow<'a, str>>,
V: Into<Cow<'a, str>>
{
fn from((name, value): (N, V)) -> Self {
Cookie::new(name, value)
}
}
impl<'a> From<CookieBuilder<'a>> for Cookie<'a> {
fn from(builder: CookieBuilder<'a>) -> Self {
builder.build()
}
}
impl<'a> AsRef<Cookie<'a>> for Cookie<'a> {
fn as_ref(&self) -> &Cookie<'a> {
self
}
}
impl<'a> AsMut<Cookie<'a>> for Cookie<'a> {
fn as_mut(&mut self) -> &mut Cookie<'a> {
self
}
}
#[cfg(test)]
mod tests {
use std::time::{Duration, SystemTime};
use crate::{Cookie, ParseError, SameSite};
#[test]
fn format() {
let cookie = Cookie::new("foo", "bar");
assert_eq!(&cookie.unescaped().to_string(), "foo=bar");
let cookie = Cookie::build(("foo", "bar")).http_only(true).build();
assert_eq!(&cookie.unescaped().to_string(), "foo=bar; HttpOnly");
let cookie = Cookie::build(("foo", "bar")).max_age(Duration::from_secs(10)).build();
assert_eq!(&cookie.unescaped().to_string(), "foo=bar; Max-Age=10");
let cookie = Cookie::build(("foo", "bar")).secure(true).build();
assert_eq!(&cookie.unescaped().to_string(), "foo=bar; Secure");
let cookie = Cookie::build(("foo", "bar")).path("/").build();
assert_eq!(&cookie.unescaped().to_string(), "foo=bar; Path=/");
let cookie = Cookie::build(("foo", "bar")).domain("www.rust-lang.org").build();
assert_eq!(&cookie.unescaped().to_string(), "foo=bar; Domain=www.rust-lang.org");
let cookie = Cookie::build(("foo", "bar")).domain(".rust-lang.org").build();
assert_eq!(&cookie.unescaped().to_string(), "foo=bar; Domain=rust-lang.org");
let cookie = Cookie::build(("foo", "bar")).domain("rust-lang.org").build();
assert_eq!(&cookie.unescaped().to_string(), "foo=bar; Domain=rust-lang.org");
let expires = SystemTime::UNIX_EPOCH + Duration::from_secs(1445412480);
let cookie = Cookie::build(("foo", "bar")).expires(expires).build();
assert_eq!(&cookie.unescaped().to_string(),
"foo=bar; Expires=Wed, 21 Oct 2015 07:28:00 GMT");
let cookie = Cookie::build(("foo", "bar")).same_site(SameSite::Strict).build();
assert_eq!(&cookie.unescaped().to_string(), "foo=bar; SameSite=Strict");
let cookie = Cookie::build(("foo", "bar")).same_site(SameSite::Lax).build();
assert_eq!(&cookie.unescaped().to_string(), "foo=bar; SameSite=Lax");
let mut cookie = Cookie::build(("foo", "bar")).same_site(SameSite::None).build();
assert_eq!(&cookie.unescaped().to_string(), "foo=bar; SameSite=None; Secure");
cookie.set_partitioned(true);
assert_eq!(&cookie.unescaped().to_string(), "foo=bar; SameSite=None; Partitioned; Secure");
cookie.set_same_site(None);
assert_eq!(&cookie.unescaped().to_string(), "foo=bar; Partitioned; Secure");
cookie.set_secure(false);
assert_eq!(&cookie.unescaped().to_string(), "foo=bar; Partitioned; Secure");
cookie.set_secure(None);
assert_eq!(&cookie.unescaped().to_string(), "foo=bar; Partitioned; Secure");
cookie.set_partitioned(None);
assert_eq!(&cookie.unescaped().to_string(), "foo=bar");
let mut c = Cookie::build(("foo", "bar")).same_site(SameSite::None).secure(false).build();
assert_eq!(&c.unescaped().to_string(), "foo=bar; SameSite=None");
c.set_secure(true);
assert_eq!(&c.unescaped().to_string(), "foo=bar; SameSite=None; Secure");
}
#[test]
fn attributes_truncated() {
let cases = [
("/", "/"),
("", ""),
("/a b/c", "/a b/c"),
("/app; Domain=evil.example", "/app"),
("/app;v=2/x", "/app"),
(";", ""),
("/a\r\nSet-Cookie: admin=1", "/a"),
("/a\nb", "/a"),
("/a\tb", "/a"),
("/a\0b", "/a"),
("/a\x7fb", "/a"),
("/caf\u{e9}; Secure", "/caf\u{e9}"),
];
for (input, expected) in cases {
let mut c = Cookie::new("foo", "bar");
c.set_path(input);
assert_eq!(c.path(), Some(expected), "borrowed {:?}", input);
c.set_path(input.to_string());
assert_eq!(c.path(), Some(expected), "owned {:?}", input);
c.set_domain(input);
assert_eq!(c.domain_verbatim(), Some(expected), "borrowed {:?}", input);
c.set_domain(input.to_string());
assert_eq!(c.domain_verbatim(), Some(expected), "owned {:?}", input);
let c = Cookie::build(("foo", "bar")).path(input).domain(input).build();
assert_eq!(c.path(), Some(expected), "builder {:?}", input);
assert_eq!(c.domain_verbatim(), Some(expected), "builder {:?}", input);
}
}
#[test]
fn attributes_cannot_inject() {
let c = Cookie::build(("sid", "abc"))
.path("/app; Domain=evil.example; SameSite=None")
.domain("example.com; Path=/; Max-Age=999999")
.build();
let string = c.unescaped().to_string();
assert_eq!(string, "sid=abc; Path=/app; Domain=example.com");
let parsed = Cookie::parse(string).unwrap();
assert_eq!(parsed.path(), Some("/app"));
assert_eq!(parsed.domain(), Some("example.com"));
assert_eq!(parsed.same_site(), None);
assert_eq!(parsed.max_age(), None);
let c = Cookie::build(("sid", "abc")).path("/a\r\nSet-Cookie: admin=1").build();
assert_eq!(c.unescaped().to_string(), "sid=abc; Path=/a");
}
#[test]
fn format_removal() {
let mut c = Cookie::build(("foo", "bar")).path("/").build();
c.make_removal();
let expected = "foo=; Path=/; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT";
assert_eq!(c.unescaped().to_string(), expected);
}
#[test]
fn make_permanent() {
let twenty_years = Duration::from_secs(20 * 365 * 24 * 60 * 60);
let before = SystemTime::now();
let mut c = Cookie::new("foo", "bar");
c.make_permanent();
let after = SystemTime::now();
let expires = c.expires_datetime().unwrap();
assert_eq!(c.max_age(), Some(twenty_years));
assert!(expires >= before + twenty_years);
assert!(expires <= after + twenty_years);
let c = Cookie::build(("foo", "bar")).permanent().build();
assert_eq!(c.max_age(), Some(twenty_years));
assert!(c.expires_datetime().unwrap() >= after + twenty_years);
}
#[test]
fn format_stripped() {
let c = Cookie::build(("foo", "bar")).path("/").secure(true).build();
assert_eq!(c.unescaped().stripped().to_string(), "foo=bar");
assert_eq!(c.unescaped().to_string(), "foo=bar; Secure; Path=/");
}
#[test]
fn unescaped_is_not_escaped() {
let c = Cookie::new("name", "value; Max-Age=86400; Domain=evil.example");
let string = c.unescaped().to_string();
assert_eq!(string, "name=value; Max-Age=86400; Domain=evil.example");
let parsed = Cookie::parse(string).unwrap();
assert_eq!(parsed.value(), "value");
assert_eq!(parsed.max_age(), Some(Duration::from_secs(86400)));
assert_eq!(parsed.domain(), Some("evil.example"));
}
#[test]
#[cfg(feature = "percent-encode")]
fn encoded_cannot_inject() {
let values = [
"value; Max-Age=86400; Domain=evil.example",
"x\r\nSet-Cookie: admin=1",
"a=b; Secure",
"\0\x7f;",
];
for value in values {
let c = Cookie::build((value, value)).path("/").build();
let string = c.encoded().to_string();
assert!(!string.contains(|c: char| c.is_ascii_control()), "{:?}", string);
assert_eq!(string.matches(';').count(), 1, "{:?}", string);
assert!(string.ends_with("; Path=/"), "{:?}", string);
let parsed = Cookie::parse_encoded(string).unwrap();
assert_eq!(parsed.name_value(), (value, value));
assert_eq!(parsed.path(), Some("/"));
assert_eq!(parsed.domain(), None);
assert_eq!(parsed.max_age(), None);
assert_eq!(parsed.secure(), None);
let stripped = c.encoded().stripped().to_string();
assert!(!stripped.contains(';'), "{:?}", stripped);
}
}
#[test]
fn format_date_wraps() {
let max = SystemTime::UNIX_EPOCH + Duration::from_secs(253402300799);
let expected = "foo=bar; Expires=Fri, 31 Dec 9999 23:59:59 GMT";
for extra in [0, 1, 1000 * 24 * 60 * 60, 1 << 40] {
let Some(expires) = max.checked_add(Duration::from_secs(extra)) else { continue };
let cookie = Cookie::build(("foo", "bar")).expires(expires).build();
assert_eq!(cookie.expires_datetime(), Some(max));
assert_eq!(&cookie.unescaped().to_string(), expected);
}
let mut cookie = Cookie::new("foo", "bar");
cookie.set_max_age(Duration::MAX);
assert_eq!(cookie.unescaped().to_string(), format!("foo=bar; Max-Age={}", i64::MAX));
cookie.set_max_age(Duration::from_secs(i64::MAX as u64));
assert_eq!(cookie.unescaped().to_string(), format!("foo=bar; Max-Age={}", i64::MAX));
cookie.set_max_age(Duration::from_secs(i64::MAX as u64 - 1));
assert_eq!(cookie.unescaped().to_string(), format!("foo=bar; Max-Age={}", i64::MAX - 1));
}
#[test]
fn format_date_before_epoch() {
let expected = "foo=bar; Expires=Thu, 01 Jan 1970 00:00:00 GMT";
for secs in [0, 1, 365 * 24 * 60 * 60, 11_644_473_600, 60_589_296_000, 1 << 40] {
let before = Duration::from_secs(secs);
let Some(expires) = SystemTime::UNIX_EPOCH.checked_sub(before) else { continue };
let cookie = Cookie::build(("foo", "bar")).expires(expires).build();
assert_eq!(cookie.expires_datetime(), Some(expires));
assert_eq!(&cookie.unescaped().to_string(), expected);
let parsed = Cookie::parse(expected).unwrap();
assert_eq!(parsed.expires_datetime(), Some(SystemTime::UNIX_EPOCH));
}
let cookie = Cookie::parse("foo=bar; Expires=Fri, 01 Jan 1965 00:00:00 GMT").unwrap();
assert!(cookie.expires_datetime().is_some_and(|t| t <= SystemTime::UNIX_EPOCH));
assert_eq!(&cookie.unescaped().to_string(), expected);
}
#[test]
fn cookie_string_long_lifetimes() {
let cookie_string = "bar=baz; Path=/subdir; HttpOnly; Domain=crates.io".to_owned();
let (name, value, path, domain) = {
let c = Cookie::parse(cookie_string.as_str()).unwrap();
(c.name_raw(), c.value_raw(), c.path_raw(), c.domain_raw())
};
assert_eq!(name, Some("bar"));
assert_eq!(value, Some("baz"));
assert_eq!(path, Some("/subdir"));
assert_eq!(domain, Some("crates.io"));
}
#[test]
fn domain_accessors() {
let mut c = Cookie::parse("bar=baz; Domain=.crates.io").unwrap();
assert_eq!(c.domain(), Some("crates.io"));
assert_eq!(c.domain_verbatim(), Some(".crates.io"));
assert_eq!(c.domain_raw(), Some(".crates.io"));
c.set_domain(".rust-lang.org");
assert_eq!(c.domain(), Some("rust-lang.org"));
assert_eq!(c.domain_verbatim(), Some(".rust-lang.org"));
assert_eq!(c.domain_raw(), None);
}
#[test]
fn owned_cookie_string() {
let cookie_string = "bar=baz; Path=/subdir; HttpOnly; Domain=crates.io".to_owned();
let (name, value, path, domain) = {
let c = Cookie::parse(cookie_string).unwrap();
(c.name_raw(), c.value_raw(), c.path_raw(), c.domain_raw())
};
assert_eq!(name, None);
assert_eq!(value, None);
assert_eq!(path, None);
assert_eq!(domain, None);
}
#[test]
fn owned_cookie_struct() {
let cookie_string = "bar=baz; Path=/subdir; HttpOnly; Domain=crates.io";
let (name, value, path, domain) = {
let c = Cookie::parse(cookie_string).unwrap().into_owned();
(c.name_raw(), c.value_raw(), c.path_raw(), c.domain_raw())
};
assert_eq!(name, None);
assert_eq!(value, None);
assert_eq!(path, None);
assert_eq!(domain, None);
}
#[test]
#[cfg(feature = "percent-encode")]
fn format_encoded() {
let cookie = Cookie::new("foo !%?=", "bar;;, a");
let cookie_str = cookie.encoded().to_string();
assert_eq!(&cookie_str, "foo%20!%25%3F%3D=bar%3B%3B%2C%20a");
let cookie = Cookie::parse_encoded(cookie_str).unwrap();
assert_eq!(cookie.name_value(), ("foo !%?=", "bar;;, a"));
}
#[test]
#[cfg(feature = "percent-encode")]
fn format_encoded_characters() {
let is_allowed = |c: char| {
c.is_ascii_alphanumeric() || "!#$&'*+-.^_`|~".contains(c)
};
let is_written = |c: char| is_allowed(c) && !"#^`|".contains(c);
for c in (0..128u8).map(char::from) {
let string = c.to_string();
let cookie = Cookie::new(string.clone(), string.clone());
let written = cookie.encoded().to_string();
let expected = match is_written(c) {
true => format!("{0}={0}", c),
false => format!("%{0:02X}=%{0:02X}", c as u8),
};
assert_eq!(written, expected, "{:?}", c);
assert!(written.chars().all(|c| is_allowed(c) || "%=".contains(c)));
let cookie = Cookie::parse_encoded(written).unwrap();
assert_eq!(cookie.name_value(), (&*string, &*string), "{:?}", c);
}
}
#[test]
fn split_parse() {
let cases = [
("", vec![]),
(";;", vec![]),
("name=value", vec![("name", "value")]),
("a=%20", vec![("a", "%20")]),
("a=d#$%^&*()_", vec![("a", "d#$%^&*()_")]),
(" name=value ", vec![("name", "value")]),
("name=value ", vec![("name", "value")]),
("name=value;;other=key", vec![("name", "value"), ("other", "key")]),
("name=value; ;other=key", vec![("name", "value"), ("other", "key")]),
("name=value ; ;other=key", vec![("name", "value"), ("other", "key")]),
("name=value ; ; other=key", vec![("name", "value"), ("other", "key")]),
("name=value ; ; other=key ", vec![("name", "value"), ("other", "key")]),
("name=value ; ; other=key;; ", vec![("name", "value"), ("other", "key")]),
(";name=value ; ; other=key ", vec![("name", "value"), ("other", "key")]),
(";a=1 ; ; b=2 ", vec![("a", "1"), ("b", "2")]),
(";a=1 ; ; b= ", vec![("a", "1"), ("b", "")]),
(";a=1 ; ; =v ; c=", vec![("a", "1"), ("c", "")]),
(" ; a=1 ; ; =v ; ;;c=", vec![("a", "1"), ("c", "")]),
(" ; a=1 ; ; =v ; ;;c=== ", vec![("a", "1"), ("c", "==")]),
];
for (string, expected) in cases {
let actual: Vec<_> = Cookie::split_parse(string)
.filter_map(|parse| parse.ok())
.map(|c| (c.name_raw().unwrap(), c.value_raw().unwrap()))
.collect();
assert_eq!(expected, actual);
}
}
#[test]
fn split_parse_whitespace() {
let string = " \t a=1 \t ; \t ; \t b=2 \t ";
let cookies: Vec<_> = Cookie::split_parse(string).collect();
let expected = [Ok(Cookie::new("a", "1")), Ok(Cookie::new("b", "2"))];
assert_eq!(cookies, expected);
for space in ['\u{85}', '\u{a0}', '\u{2003}', '\u{2028}', '\u{3000}'] {
let string = format!("a=1;{0}b=2{0};{0}; c=3", space);
let b = Cookie::new(format!("{0}b", space), format!("2{0}", space));
let expected = [
Ok(Cookie::new("a", "1")),
Ok(b),
Err(ParseError::MissingPair),
Ok(Cookie::new("c", "3")),
];
let borrowed = Cookie::split_parse(&*string).collect::<Vec<_>>();
let owned = Cookie::split_parse(string.clone()).collect::<Vec<_>>();
assert_eq!(borrowed, expected);
assert_eq!(owned, expected);
}
}
#[test]
#[cfg(feature = "percent-encode")]
fn split_parse_encoded() {
let cases = [
("", vec![]),
(";;", vec![]),
("name=val%20ue", vec![("name", "val ue")]),
("foo%20!%25%3F%3D=bar%3B%3B%2C%20a", vec![("foo !%?=", "bar;;, a")]),
(
"name=val%20ue ; ; foo%20!%25%3F%3D=bar%3B%3B%2C%20a",
vec![("name", "val ue"), ("foo !%?=", "bar;;, a")]
),
];
for (string, expected) in cases {
let cookies: Vec<_> = Cookie::split_parse_encoded(string)
.filter_map(|parse| parse.ok())
.collect();
let actual: Vec<_> = cookies.iter()
.map(|c| c.name_value())
.collect();
assert_eq!(expected, actual);
}
}
}