use crate::cookies::{
domain_ok, parse_netscape, NetscapeCookie, MAX_COOKIES, MAX_COOKIES_FILE_BYTES,
};
use rightkit_fs::{AtomicWriteOptions, AtomicWriter, FsError, Privacy, ReplaceMode, RetryPolicy};
use std::fs;
use std::path::Path;
use std::time::Duration;
use thiserror::Error;
const HEADER: &str = "# Netscape HTTP Cookie File\n";
const HTTP_ONLY_PREFIX: &str = "#HttpOnly_";
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum CookieExportError {
#[error("no cookie domains were declared")]
NoDomains,
#[error("declared cookie domain {domain:?} is empty or malformed")]
BadDomain { domain: String },
#[error(
"no exportable cookies: {offered} offered, {in_scope} in the declared domains, \
{invalid} invalid; nothing was written"
)]
NothingToExport {
offered: usize,
in_scope: usize,
invalid: usize,
},
#[error(
"cookie {index} in the declared domains has an invalid {field} field; nothing was written"
)]
InvalidCookie { index: usize, field: &'static str },
#[error("{count} cookies match; a file holds at most {limit}; nothing was written")]
TooManyCookies { count: usize, limit: usize },
#[error("the export would be {bytes} bytes; the limit is {limit}; nothing was written")]
TooLarge { bytes: usize, limit: usize },
#[error("the export did not round-trip through parse_netscape ({0}); nothing was written")]
NotRoundTrippable(String),
#[error("publishing the cookie file failed: {0}")]
Publish(#[source] FsError),
#[error("clearing the cookie file failed: {0}")]
Clear(#[source] FsError),
}
impl CookieExportError {
pub fn target_replaced(&self) -> bool {
matches!(self, Self::Publish(e) if e.target_replaced())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ExportReport {
pub exported: usize,
pub out_of_scope: usize,
pub invalid_skipped: usize,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum ClearOutcome {
Removed,
Absent,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CookieExport {
domains: Vec<String>,
mark_http_only: bool,
skip_invalid: bool,
}
fn normalise_declared(raw: &str) -> Option<String> {
let d = raw.trim().trim_start_matches('.').to_ascii_lowercase();
domain_ok(&d).then_some(d)
}
impl CookieExport {
pub fn new<I, S>(domains: I) -> Result<Self, CookieExportError>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let mut out: Vec<String> = Vec::new();
for raw in domains {
let raw = raw.as_ref();
let d = normalise_declared(raw).ok_or_else(|| CookieExportError::BadDomain {
domain: raw.to_string(),
})?;
if !out.contains(&d) {
out.push(d);
}
}
if out.is_empty() {
return Err(CookieExportError::NoDomains);
}
Ok(Self {
domains: out,
mark_http_only: false,
skip_invalid: false,
})
}
#[must_use]
pub fn mark_http_only(mut self, mark: bool) -> Self {
self.mark_http_only = mark;
self
}
#[must_use]
pub fn skip_invalid(mut self, skip: bool) -> Self {
self.skip_invalid = skip;
self
}
pub fn domains(&self) -> &[String] {
&self.domains
}
pub fn matches(&self, cookie_domain: &str) -> bool {
let c = cookie_domain
.trim()
.trim_start_matches('.')
.to_ascii_lowercase();
self.domains.iter().any(|d| {
c == *d
|| (c.len() > d.len()
&& c.ends_with(d.as_str())
&& c.as_bytes()[c.len() - d.len() - 1] == b'.')
})
}
fn prepare(
&self,
cookies: &[NetscapeCookie],
) -> Result<(String, ExportReport), CookieExportError> {
let mut selected: Vec<NetscapeCookie> = Vec::new();
let mut out_of_scope = 0usize;
let mut in_scope = 0usize;
let mut invalid = 0usize;
for (index, cookie) in cookies.iter().enumerate() {
if !self.matches(&cookie.domain) {
out_of_scope += 1;
continue;
}
in_scope += 1;
let mut c = cookie.clone();
c.domain = c.domain.trim().trim_start_matches('.').to_ascii_lowercase();
if let Some(field) = c.invalid_field() {
if self.skip_invalid {
invalid += 1;
continue;
}
return Err(CookieExportError::InvalidCookie { index, field });
}
if !self.mark_http_only {
c.http_only = false;
}
selected.push(c);
}
if selected.is_empty() {
return Err(CookieExportError::NothingToExport {
offered: cookies.len(),
in_scope,
invalid,
});
}
if selected.len() > MAX_COOKIES {
return Err(CookieExportError::TooManyCookies {
count: selected.len(),
limit: MAX_COOKIES,
});
}
let text = render(&selected);
if text.len() > MAX_COOKIES_FILE_BYTES {
return Err(CookieExportError::TooLarge {
bytes: text.len(),
limit: MAX_COOKIES_FILE_BYTES,
});
}
match parse_netscape(&text) {
Err(e) => {
return Err(CookieExportError::NotRoundTrippable(e.to_string()));
}
Ok(parsed) if parsed != selected => {
return Err(CookieExportError::NotRoundTrippable(
"cookie fields differ".to_string(),
));
}
Ok(_) => {}
}
let report = ExportReport {
exported: selected.len(),
out_of_scope,
invalid_skipped: invalid,
};
Ok((text, report))
}
pub fn export_to(
&self,
path: &Path,
cookies: &[NetscapeCookie],
) -> Result<ExportReport, CookieExportError> {
let (text, report) = self.prepare(cookies)?;
publish(path, text.as_bytes())?;
Ok(report)
}
pub fn clear(path: &Path) -> Result<ClearOutcome, CookieExportError> {
let meta = match fs::symlink_metadata(path) {
Ok(m) => m,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(ClearOutcome::Absent),
Err(e) => return Err(CookieExportError::Clear(FsError::io(path, e))),
};
if meta.is_dir() {
return Err(CookieExportError::Clear(FsError::NotAFile(
path.to_path_buf(),
)));
}
match fs::remove_file(path) {
Ok(()) => {
if let Some(parent) = path.parent().filter(|p| !p.as_os_str().is_empty()) {
let _ = rightkit_fs::try_sync_dir(parent);
}
Ok(ClearOutcome::Removed)
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(ClearOutcome::Absent),
Err(e) => Err(CookieExportError::Clear(FsError::io(path, e))),
}
}
pub fn clear_with(
path: &Path,
invalidate: impl FnOnce(ClearOutcome),
) -> Result<ClearOutcome, CookieExportError> {
let outcome = Self::clear(path)?;
invalidate(outcome);
Ok(outcome)
}
}
fn render(cookies: &[NetscapeCookie]) -> String {
let mut text = String::from(HEADER);
for c in cookies {
if c.http_only {
text.push_str(HTTP_ONLY_PREFIX);
}
if c.include_subdomains {
text.push('.');
}
text.push_str(&c.domain);
text.push('\t');
text.push_str(if c.include_subdomains {
"TRUE"
} else {
"FALSE"
});
text.push('\t');
text.push_str(&c.path);
text.push('\t');
text.push_str(if c.secure { "TRUE" } else { "FALSE" });
text.push('\t');
text.push_str(&c.expires.unwrap_or(0).to_string());
text.push('\t');
text.push_str(&c.name);
text.push('\t');
text.push_str(c.value());
text.push('\n');
}
text
}
fn publish_options() -> AtomicWriteOptions {
AtomicWriteOptions {
private: true,
create_parents: true,
durable: true,
replace: ReplaceMode::Rename,
retry_sharing_violation: Some(RetryPolicy::new(5, Duration::from_millis(50))),
privacy: Privacy::OwnerOnly,
}
}
fn verify_staged(temp: &Path, expected: &[u8]) -> std::io::Result<()> {
let staged = fs::read(temp)?;
if staged == expected {
Ok(())
} else {
Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"staged cookie file does not match the export",
))
}
}
fn publish(path: &Path, bytes: &[u8]) -> Result<(), CookieExportError> {
let mut writer =
AtomicWriter::create_with(path, publish_options()).map_err(CookieExportError::Publish)?;
writer
.write_all(bytes)
.map_err(CookieExportError::Publish)?;
writer
.commit_checked(|temp| verify_staged(temp, bytes))
.map_err(CookieExportError::Publish)
}
#[cfg(test)]
mod tests {
use super::*;
const SECRET: &str = "s3cr3t-VALUE-9f2a";
fn ck(domain: &str, name: &str, value: &str) -> NetscapeCookie {
NetscapeCookie::new(domain, name, value).unwrap()
}
fn export(domains: &[&str]) -> CookieExport {
CookieExport::new(domains).unwrap()
}
fn dir_names(dir: &Path) -> Vec<String> {
let mut v: Vec<String> = fs::read_dir(dir)
.unwrap()
.map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
.collect();
v.sort();
v
}
fn old_artifact(dir: &Path) -> (std::path::PathBuf, Vec<u8>) {
let path = dir.join("auth").join("x_cookies.txt");
fs::create_dir_all(path.parent().unwrap()).unwrap();
let old = b"# Netscape HTTP Cookie File\n.x.com\tTRUE\t/\tTRUE\t0\tauth_token\tOLD-GOOD\n"
.to_vec();
rightkit_fs::atomic_write_private(&path, &old).unwrap();
(path, old)
}
#[test]
fn declared_domains_are_validated_and_normalised() {
assert!(matches!(
CookieExport::new(Vec::<&str>::new()),
Err(CookieExportError::NoDomains)
));
for bad in ["", ".", " ", "a b.com", "a/b.com", "a;b", "#x.com"] {
let e = CookieExport::new([bad]).unwrap_err();
assert!(matches!(e, CookieExportError::BadDomain { .. }), "{bad:?}");
}
let e = export(&[" .Instagram.COM ", "instagram.com", "Threads.net"]);
assert_eq!(e.domains(), ["instagram.com", "threads.net"]);
}
#[test]
fn matching_covers_subdomains_and_host_only_but_not_lookalikes() {
let e = export(&["instagram.com"]);
for yes in [
"instagram.com",
".instagram.com",
"INSTAGRAM.com",
"www.instagram.com",
".i.instagram.com",
] {
assert!(e.matches(yes), "{yes}");
}
for no in [
"notinstagram.com",
"instagram.com.evil.example",
"instagram.co",
"com",
"",
"x.com",
"xinstagram.com",
] {
assert!(!e.matches(no), "{no}");
}
let sub = export(&["www.instagram.com"]);
assert!(sub.matches("www.instagram.com") && sub.matches("a.www.instagram.com"));
assert!(!sub.matches("instagram.com") && !sub.matches("api.instagram.com"));
}
fn mixed() -> Vec<NetscapeCookie> {
vec![
ck(".instagram.com", "sessionid", SECRET)
.with_secure(true)
.with_http_only(true)
.with_expires(Some(1_900_000_000))
.unwrap(),
ck("www.instagram.com", "csrftoken", "tok")
.with_path("/app")
.unwrap(),
ck("instagram.com", "mid", "m1").with_include_subdomains(true),
ck("notinstagram.com", "other", "leak-1"),
ck(".x.com", "auth_token", "leak-2"),
ck("instagram.com.evil.example", "evil", "leak-3"),
]
}
#[test]
fn exports_exactly_the_declared_subset_and_round_trips() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("auth").join("instagram_cookies.txt");
let report = export(&["instagram.com"])
.mark_http_only(true)
.export_to(&path, &mixed())
.unwrap();
assert_eq!(
report,
ExportReport {
exported: 3,
out_of_scope: 3,
invalid_skipped: 0
}
);
let text = fs::read_to_string(&path).unwrap();
assert!(text.starts_with("# Netscape HTTP Cookie File\n"));
assert!(!text.contains("leak-"), "out-of-scope cookie written");
assert!(text.contains(SECRET));
let parsed = parse_netscape(&text).unwrap();
let expected: Vec<NetscapeCookie> = mixed().into_iter().take(3).collect();
assert!(expected[0].http_only);
assert_eq!(parsed, expected);
let lines: Vec<&str> = text.lines().collect();
assert_eq!(
lines[1],
format!("#HttpOnly_.instagram.com\tTRUE\t/\tTRUE\t1900000000\tsessionid\t{SECRET}")
);
assert_eq!(
lines[2],
"www.instagram.com\tFALSE\t/app\tFALSE\t0\tcsrftoken\ttok"
);
assert_eq!(lines[3], ".instagram.com\tTRUE\t/\tFALSE\t0\tmid\tm1");
assert_eq!(dir_names(path.parent().unwrap()), ["instagram_cookies.txt"]);
}
#[test]
fn http_only_prefix_is_off_by_default_and_the_cookie_is_still_exported() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("c.txt");
export(&["instagram.com"])
.export_to(&path, &mixed())
.unwrap();
let text = fs::read_to_string(&path).unwrap();
assert!(!text.contains("#HttpOnly_"));
let visible: Vec<&str> = text.lines().filter(|l| !l.starts_with('#')).collect();
assert!(visible.iter().any(|l| l.contains("\tsessionid\t")));
let parsed = parse_netscape(&text).unwrap();
assert!(parsed.iter().all(|c| !c.http_only));
assert_eq!(parsed.len(), 3);
assert_eq!(parsed[0].value(), SECRET);
assert!(parsed[0].secure);
assert_eq!(parsed[0].expires, Some(1_900_000_000));
}
#[test]
fn session_host_only_secure_and_path_fields_survive() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("c.txt");
let cookies = vec![
ck("a.com", "s", "").with_secure(true),
ck("b.a.com", "t", "x y").with_path("/p/q").unwrap(),
];
export(&["a.com"]).export_to(&path, &cookies).unwrap();
let parsed = parse_netscape(&fs::read_to_string(&path).unwrap()).unwrap();
assert_eq!(parsed, cookies);
assert!(!parsed[0].include_subdomains && parsed[0].expires.is_none());
assert_eq!(parsed[1].value(), "x y");
}
#[test]
fn caller_edits_to_public_fields_are_normalised_or_refused() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("c.txt");
let mut shouty = ck("a.com", "n", "v");
shouty.domain = ".A.COM".to_string();
export(&["a.com"]).export_to(&path, &[shouty]).unwrap();
assert_eq!(
parse_netscape(&fs::read_to_string(&path).unwrap()).unwrap()[0].domain,
"a.com"
);
let (old_path, old) = old_artifact(dir.path());
let mut broken = ck("a.com", "n", SECRET);
broken.name = "a\tb".to_string();
let e = export(&["a.com"])
.export_to(&old_path, &[broken])
.unwrap_err();
assert!(matches!(
e,
CookieExportError::InvalidCookie {
index: 0,
field: "name"
}
));
assert_eq!(fs::read(&old_path).unwrap(), old);
}
#[test]
fn empty_or_unmatched_export_leaves_the_old_artifact_byte_identical() {
let dir = tempfile::tempdir().unwrap();
let (path, old) = old_artifact(dir.path());
let before = dir_names(path.parent().unwrap());
for cookies in [
Vec::new(),
vec![ck("other.example", "n", SECRET)],
vec![
ck("notx.com", "n", SECRET),
ck("x.com.evil.example", "m", SECRET),
],
] {
let e = export(&["x.com"]).export_to(&path, &cookies).unwrap_err();
assert!(
matches!(e, CookieExportError::NothingToExport { .. }),
"{e}"
);
assert!(!e.target_replaced());
assert!(!e.to_string().contains(SECRET));
assert_eq!(fs::read(&path).unwrap(), old);
assert_eq!(dir_names(path.parent().unwrap()), before);
}
}
#[test]
fn invalid_cookie_fails_the_export_by_default_and_leaves_the_old_file() {
let dir = tempfile::tempdir().unwrap();
let (path, old) = old_artifact(dir.path());
let good = ck("x.com", "auth_token", "new");
let mut bad = ck("x.com", "ct0", SECRET);
bad.path = "relative".to_string();
let e = export(&["x.com"])
.export_to(&path, &[good.clone(), bad.clone()])
.unwrap_err();
assert!(matches!(
e,
CookieExportError::InvalidCookie {
index: 1,
field: "path"
}
));
assert!(!e.to_string().contains(SECRET) && !format!("{e:?}").contains(SECRET));
assert_eq!(fs::read(&path).unwrap(), old);
let report = export(&["x.com"])
.skip_invalid(true)
.export_to(&path, &[good, bad.clone()])
.unwrap();
assert_eq!(
report,
ExportReport {
exported: 1,
out_of_scope: 0,
invalid_skipped: 1
}
);
assert_ne!(fs::read(&path).unwrap(), old);
let kept = fs::read(&path).unwrap();
let e = export(&["x.com"])
.skip_invalid(true)
.export_to(&path, &[bad])
.unwrap_err();
assert!(matches!(
e,
CookieExportError::NothingToExport {
offered: 1,
in_scope: 1,
invalid: 1
}
));
assert_eq!(fs::read(&path).unwrap(), kept);
}
#[test]
fn too_many_cookies_is_refused_before_writing() {
let dir = tempfile::tempdir().unwrap();
let (path, old) = old_artifact(dir.path());
let one = ck("x.com", "n", "v");
let many = vec![one; MAX_COOKIES + 1];
let e = export(&["x.com"]).export_to(&path, &many).unwrap_err();
assert!(matches!(
e,
CookieExportError::TooManyCookies {
limit: MAX_COOKIES,
..
}
));
assert_eq!(fs::read(&path).unwrap(), old);
}
#[test]
fn oversized_export_is_refused_before_writing() {
let dir = tempfile::tempdir().unwrap();
let (path, old) = old_artifact(dir.path());
let big = "v".repeat(4000);
let cookies: Vec<NetscapeCookie> = (0..1100)
.map(|i| ck("x.com", &format!("n{i}"), &big))
.collect();
let e = export(&["x.com"]).export_to(&path, &cookies).unwrap_err();
assert!(matches!(e, CookieExportError::TooLarge { .. }), "{e}");
assert_eq!(fs::read(&path).unwrap(), old);
}
#[test]
fn staged_file_mismatch_aborts_the_publish() {
let dir = tempfile::tempdir().unwrap();
let staged = dir.path().join("staged");
fs::write(&staged, b"abc").unwrap();
assert!(verify_staged(&staged, b"abc").is_ok());
let e = verify_staged(&staged, b"abd").unwrap_err();
assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
assert!(verify_staged(&staged, b"ab").is_err());
assert!(verify_staged(&dir.path().join("missing"), b"abc").is_err());
}
#[test]
fn failed_publish_leaves_the_old_artifact_and_no_temp_file() {
use rightkit_fs::atomic::seams::fail_write_after;
let dir = tempfile::tempdir().unwrap();
let (path, old) = old_artifact(dir.path());
let parent = path.parent().unwrap().to_path_buf();
let result = {
let _guard = fail_write_after(8, std::io::ErrorKind::StorageFull);
export(&["x.com"]).export_to(&path, &[ck("x.com", "n", SECRET)])
};
let e = result.unwrap_err();
assert!(matches!(e, CookieExportError::Publish(_)), "{e}");
assert!(!e.target_replaced());
assert!(!e.to_string().contains(SECRET));
assert_eq!(fs::read(&path).unwrap(), old);
assert_eq!(dir_names(&parent), ["x_cookies.txt"]);
}
#[cfg(unix)]
#[test]
fn artifact_and_created_parents_are_private_even_over_a_world_readable_file() {
use std::os::unix::fs::PermissionsExt;
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("auth").join("deeper").join("c.txt");
export(&["x.com"])
.export_to(&path, &[ck("x.com", "n", SECRET)])
.unwrap();
let mode = |p: &Path| fs::metadata(p).unwrap().permissions().mode() & 0o777;
assert_eq!(mode(&path), 0o600);
assert_eq!(mode(path.parent().unwrap()), 0o700);
fs::set_permissions(&path, fs::Permissions::from_mode(0o644)).unwrap();
export(&["x.com"])
.export_to(&path, &[ck("x.com", "n", "second")])
.unwrap();
assert_eq!(mode(&path), 0o600);
assert!(fs::read_to_string(&path).unwrap().contains("second"));
}
#[test]
fn second_export_replaces_the_first_completely() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("c.txt");
let e = export(&["x.com"]);
e.export_to(&path, &[ck("x.com", "a", "1"), ck("x.com", "b", "2")])
.unwrap();
e.export_to(&path, &[ck("x.com", "c", "3")]).unwrap();
let parsed = parse_netscape(&fs::read_to_string(&path).unwrap()).unwrap();
assert_eq!(parsed.len(), 1);
assert_eq!(parsed[0].name, "c");
assert_eq!(dir_names(dir.path()), ["c.txt"]);
}
#[test]
fn clear_removes_the_artifact_and_reports_absence() {
let dir = tempfile::tempdir().unwrap();
let (path, _old) = old_artifact(dir.path());
assert_eq!(CookieExport::clear(&path).unwrap(), ClearOutcome::Removed);
assert!(!path.exists());
assert_eq!(CookieExport::clear(&path).unwrap(), ClearOutcome::Absent);
assert_eq!(
CookieExport::clear(&dir.path().join("never-existed")).unwrap(),
ClearOutcome::Absent
);
}
#[test]
fn clear_with_invalidates_only_after_the_file_is_gone() {
let dir = tempfile::tempdir().unwrap();
let (path, _old) = old_artifact(dir.path());
let mut calls = Vec::new();
let r = CookieExport::clear_with(&path, |o| calls.push(o)).unwrap();
assert_eq!(r, ClearOutcome::Removed);
assert_eq!(calls, [ClearOutcome::Removed]);
assert!(!path.exists());
CookieExport::clear_with(&path, |o| calls.push(o)).unwrap();
assert_eq!(calls, [ClearOutcome::Removed, ClearOutcome::Absent]);
let sub = dir.path().join("a-directory");
fs::create_dir(&sub).unwrap();
let mut ran = false;
let e = CookieExport::clear_with(&sub, |_| ran = true).unwrap_err();
assert!(matches!(e, CookieExportError::Clear(_)), "{e}");
assert!(!ran);
assert!(sub.is_dir());
}
#[test]
fn debug_of_export_and_report_holds_no_cookie_data() {
let e = export(&["x.com"]).mark_http_only(true);
let s = format!("{e:?}");
assert!(s.contains("x.com"));
let dir = tempfile::tempdir().unwrap();
let r = e
.export_to(&dir.path().join("c.txt"), &[ck("x.com", "n", SECRET)])
.unwrap();
assert!(!format!("{r:?}").contains(SECRET));
}
}