use std::{
fmt,
sync::{Arc, RwLock},
};
use http::HeaderValue;
use url::Url;
use crate::{Error, ErrorKind, Result};
const MAX_COOKIE_BYTES: usize = 4 * 1024;
const MAX_COOKIES_PER_DOMAIN: usize = 180;
const MAX_COOKIES_TOTAL: usize = 3_000;
const MAX_COOKIE_HEADER_BYTES: usize = 8 * 1024;
#[derive(Clone)]
pub struct Jar {
store: Arc<RwLock<cookie_store::CookieStore>>,
}
impl Default for Jar {
fn default() -> Self {
Self::new()
}
}
impl Jar {
#[must_use]
pub fn new() -> Self {
Self {
store: Arc::new(RwLock::new(cookie_store::CookieStore::default())),
}
}
}
impl fmt::Debug for Jar {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.debug_struct("Jar").finish_non_exhaustive()
}
}
impl Jar {
pub(crate) fn set_cookies(
&self,
cookie_headers: &mut dyn Iterator<Item = &HeaderValue>,
url: &Url,
) {
let mut store = self
.store
.write()
.unwrap_or_else(|error| error.into_inner());
for value in cookie_headers {
if value.as_bytes().len() > MAX_COOKIE_BYTES {
continue;
}
let Some(raw) = std::str::from_utf8(value.as_bytes())
.ok()
.and_then(|value| cookie_store::RawCookie::parse(value).ok())
.map(cookie_store::RawCookie::into_owned)
else {
continue;
};
let Ok(cookie) = cookie_store::Cookie::try_from_raw_cookie(&raw, url) else {
continue;
};
let Some(domain) = cookie.domain.as_cow() else {
continue;
};
if matches!(cookie.domain, cookie_store::CookieDomain::Suffix(_))
&& psl::suffix_str(&domain).is_some_and(|suffix| suffix == domain)
{
continue;
}
let path = cookie.path.as_ref();
let replaces_existing = store.contains_any(&domain, path, cookie.name());
if !replaces_existing && !cookie.is_expired() {
let total = store.iter_unexpired().count();
let for_domain = store
.iter_unexpired()
.filter(|stored| stored.domain.as_cow().as_deref() == Some(domain.as_ref()))
.count();
if total >= MAX_COOKIES_TOTAL || for_domain >= MAX_COOKIES_PER_DOMAIN {
continue;
}
}
let _ = store.insert(cookie.into_owned(), url);
}
}
pub(crate) fn cookies(&self, url: &Url) -> Result<Option<HeaderValue>> {
let store = self.store.read().unwrap_or_else(|error| error.into_inner());
let mut cookies = store.matches(url);
cookies.sort_by(|left, right| {
right
.path
.as_ref()
.len()
.cmp(&left.path.as_ref().len())
.then_with(|| left.name().cmp(right.name()))
});
let value = cookies
.into_iter()
.map(|cookie| format!("{}={}", cookie.name(), cookie.value()))
.collect::<Vec<_>>()
.join("; ");
if value.is_empty() {
Ok(None)
} else if value.len() > MAX_COOKIE_HEADER_BYTES {
Err(Error::new(
ErrorKind::Request,
"cookie header exceeds the configured safety limit",
))
} else {
HeaderValue::from_str(&value)
.map(Some)
.map_err(|_| Error::new(ErrorKind::Request, "stored cookie value is invalid"))
}
}
}
#[cfg(test)]
mod describe_jar {
use std::{
io::{Read, Write},
net::TcpListener,
thread,
};
use crate::{Client, StatusCode};
use super::*;
#[test]
fn it_debug_output_does_not_expose_cookie_values() {
let jar = Jar::default();
let debug = format!("{jar:?}");
assert_eq!(debug, "Jar { .. }");
}
#[test]
fn it_public_suffix_cookies_are_rejected() {
let jar = Jar::new();
for (origin, value) in [
("https://example.com/", "bad=one; Domain=com; Path=/"),
("https://example.co.uk/", "bad=two; Domain=co.uk; Path=/"),
(
"https://account.github.io/",
"bad=three; Domain=github.io; Path=/",
),
] {
let header = HeaderValue::from_str(value).unwrap();
let origin = Url::parse(origin).unwrap();
jar.set_cookies(&mut std::iter::once(&header), &origin);
assert!(jar.cookies(&origin).unwrap().is_none());
}
}
#[test]
fn it_cookie_matching_keeps_valid_domain_and_path_rules() {
let jar = Jar::new();
let origin = Url::parse("https://api.example.com/account/login").unwrap();
let header =
HeaderValue::from_static("sid=good; Domain=example.com; Path=/account; Secure");
jar.set_cookies(&mut std::iter::once(&header), &origin);
let matching = Url::parse("https://www.example.com/account/profile").unwrap();
let wrong_path = Url::parse("https://www.example.com/public").unwrap();
let insecure = Url::parse("http://www.example.com/account/profile").unwrap();
assert_eq!(
jar.cookies(&matching).unwrap().unwrap(),
HeaderValue::from_static("sid=good")
);
assert!(jar.cookies(&wrong_path).unwrap().is_none());
assert!(jar.cookies(&insecure).unwrap().is_none());
}
#[test]
fn it_cookie_storage_and_wire_headers_are_bounded() {
let jar = Jar::new();
let origin = Url::parse("https://example.com/").unwrap();
for index in 0..=MAX_COOKIES_PER_DOMAIN {
let header = HeaderValue::from_str(&format!("c{index:03}=v; Path=/")).unwrap();
jar.set_cookies(&mut std::iter::once(&header), &origin);
}
let value = jar.cookies(&origin).unwrap().unwrap();
let value = value.to_str().unwrap();
assert!(value.contains("c179=v"));
assert!(!value.contains("c180=v"));
let jar = Jar::new();
for index in 0..MAX_COOKIES_PER_DOMAIN {
let header =
HeaderValue::from_str(&format!("c{index:03}={}; Path=/", "x".repeat(40))).unwrap();
jar.set_cookies(&mut std::iter::once(&header), &origin);
}
assert_eq!(jar.cookies(&origin).unwrap_err().kind(), ErrorKind::Request);
let oversized =
HeaderValue::from_str(&format!("large={}", "x".repeat(MAX_COOKIE_BYTES))).unwrap();
let jar = Jar::new();
jar.set_cookies(&mut std::iter::once(&oversized), &origin);
assert!(jar.cookies(&origin).unwrap().is_none());
let jar = Jar::new();
for index in 0..=MAX_COOKIES_TOTAL {
let origin = Url::parse(&format!("https://d{index}.example/")).unwrap();
let header = HeaderValue::from_static("bounded=value; Path=/; Secure");
jar.set_cookies(&mut std::iter::once(&header), &origin);
}
let first = Url::parse("https://d0.example/").unwrap();
let rejected = Url::parse(&format!("https://d{MAX_COOKIES_TOTAL}.example/")).unwrap();
assert!(jar.cookies(&first).unwrap().is_some());
assert!(jar.cookies(&rejected).unwrap().is_none());
}
#[test]
fn it_expiry_deletes_an_existing_cookie() {
let jar = Jar::new();
let origin = Url::parse("https://example.com/account").unwrap();
let live = HeaderValue::from_static("sid=live; Path=/; Secure");
jar.set_cookies(&mut std::iter::once(&live), &origin);
assert!(jar.cookies(&origin).unwrap().is_some());
let expired = HeaderValue::from_static("sid=gone; Path=/; Max-Age=0; Secure");
jar.set_cookies(&mut std::iter::once(&expired), &origin);
assert!(jar.cookies(&origin).unwrap().is_none());
}
#[tokio::test]
async fn it_default_backend_does_not_persist_cookies_without_a_jar() {
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let address = listener.local_addr().unwrap();
let server = thread::spawn(move || {
let (mut first, _) = listener.accept().unwrap();
read_request_headers(&mut first);
first
.write_all(
b"HTTP/1.1 204 No Content\r\nSet-Cookie: session=secret; Path=/\r\nContent-Length: 0\r\nConnection: close\r\n\r\n",
)
.unwrap();
let (mut second, _) = listener.accept().unwrap();
let request = read_request_headers(&mut second);
second
.write_all(
b"HTTP/1.1 204 No Content\r\nContent-Length: 0\r\nConnection: close\r\n\r\n",
)
.unwrap();
request
});
let client = Client::new().unwrap();
let first = client.get(format!("http://{address}/set")).await.unwrap();
let second = client.get(format!("http://{address}/read")).await.unwrap();
assert_eq!(first.status(), StatusCode::NO_CONTENT);
assert_eq!(second.status(), StatusCode::NO_CONTENT);
let request = String::from_utf8(server.join().unwrap())
.unwrap()
.to_ascii_lowercase();
assert!(!request.contains("\r\ncookie:"));
}
fn read_request_headers(stream: &mut std::net::TcpStream) -> Vec<u8> {
let mut request = Vec::new();
let mut buffer = [0_u8; 1024];
while !request.windows(4).any(|window| window == b"\r\n\r\n") {
let read = stream.read(&mut buffer).unwrap();
if read == 0 {
break;
}
request.extend_from_slice(&buffer[..read]);
}
request
}
}