use std::path::Path;
use rusqlite::types::ValueRef;
use rusqlite::{Connection, OpenFlags, Row};
use tracing::debug;
use crate::error::{Aria2Error, Result};
use crate::http::cookie::parsing::is_numeric_host;
use crate::http::cookie::{Cookie, SameSite};
pub const MOZ_QUERY: &str = "SELECT host, path, isSecure, expiry, name, value, lastAccessed \
FROM moz_cookies";
pub const CHROMIUM_QUERY: &str = "SELECT host_key, path, secure, expires_utc / 1000000 - 11644473600 as expires_utc, \
name, value, last_access_utc / 1000000 - 11644473600 as last_access_utc \
FROM cookies";
pub const SQLITE_MAGIC: &[u8; 16] = b"SQLite format 3\0";
pub fn is_sqlite_file(data: &[u8]) -> bool {
data.len() >= SQLITE_MAGIC.len() && &data[..SQLITE_MAGIC.len()] == SQLITE_MAGIC.as_slice()
}
pub struct Sqlite3CookieParser;
impl Sqlite3CookieParser {
pub fn parse_firefox(path: &Path) -> Result<Vec<Cookie>> {
Self::parse_with_query(path, MOZ_QUERY)
}
pub fn parse_chromium(path: &Path) -> Result<Vec<Cookie>> {
Self::parse_with_query(path, CHROMIUM_QUERY)
}
pub fn parse_auto(path: &Path) -> Result<Vec<Cookie>> {
match Self::parse_firefox(path) {
Ok(cookies) => Ok(cookies),
Err(moz_err) => {
debug!(
error = %moz_err,
"Not a Firefox cookie database, retrying as Chromium"
);
Self::parse_chromium(path).map_err(|chromium_err| {
debug!(error = %chromium_err, "Chromium cookie parse also failed");
moz_err
})
}
}
}
fn parse_with_query(path: &Path, query: &str) -> Result<Vec<Cookie>> {
let flags = OpenFlags::SQLITE_OPEN_READ_ONLY | OpenFlags::SQLITE_OPEN_NO_MUTEX;
let conn = Connection::open_with_flags(path, flags).map_err(|e| {
Aria2Error::Io(format!(
"Failed to open SQLite cookie database {}: {e}",
path.display()
))
})?;
let mut stmt = conn
.prepare(query)
.map_err(|e| Aria2Error::Parse(format!("Failed to read SQLite3 database: {e}")))?;
let mut rows = stmt
.query([])
.map_err(|e| Aria2Error::Parse(format!("Failed to read SQLite3 database: {e}")))?;
let mut cookies = Vec::new();
loop {
let row = rows
.next()
.map_err(|e| Aria2Error::Parse(format!("Failed to read SQLite3 database: {e}")))?;
match row {
Some(row) => {
if let Some(cookie) = row_to_cookie(row) {
cookies.push(cookie);
}
}
None => break,
}
}
Ok(cookies)
}
}
fn column_text(row: &Row<'_>, idx: usize) -> Option<String> {
match row.get_ref(idx).ok()? {
ValueRef::Text(bytes) => Some(String::from_utf8_lossy(bytes).into_owned()),
_ => None,
}
}
fn column_i64(row: &Row<'_>, idx: usize) -> Option<i64> {
match row.get_ref(idx).ok()? {
ValueRef::Integer(v) => Some(v),
ValueRef::Real(v) if v.is_finite() => Some(v as i64),
ValueRef::Text(bytes) => std::str::from_utf8(bytes).ok()?.trim().parse::<i64>().ok(),
_ => None,
}
}
fn row_to_cookie(row: &Row<'_>) -> Option<Cookie> {
let raw_host = column_text(row, 0)?;
let path = column_text(row, 1)?;
let name = column_text(row, 4)?;
let had_leading_dot = raw_host.starts_with('.');
let domain = raw_host.trim_start_matches('.').to_string();
if name.is_empty() || domain.is_empty() || !path.starts_with('/') {
return None;
}
let expiry_time = column_i64(row, 3)?;
let last_access_time = column_i64(row, 6)?;
let value = column_text(row, 5).unwrap_or_default();
let host_only = is_numeric_host(&domain) || !had_leading_dot;
let secure = column_i64(row, 2) == Some(1);
Some(Cookie {
name,
value,
domain,
path,
expiry_time,
creation_time: last_access_time,
last_access_time,
persistent: true,
host_only,
secure,
http_only: false,
same_site: SameSite::default(),
})
}
#[cfg(test)]
mod tests {
use super::*;
use rusqlite::Connection;
use tempfile::TempDir;
#[allow(clippy::type_complexity)]
fn make_moz_db(
rows: &[(&str, &str, i64, i64, &str, Option<&str>, i64)],
) -> (TempDir, std::path::PathBuf) {
let dir = TempDir::new().unwrap();
let path = dir.path().join("cookies.sqlite");
let conn = Connection::open(&path).unwrap();
conn.execute(
"CREATE TABLE moz_cookies (
host TEXT, path TEXT, isSecure INTEGER, expiry INTEGER,
name TEXT, value TEXT, lastAccessed INTEGER)",
[],
)
.unwrap();
for (host, path_col, secure, expiry, name, value, last) in rows {
conn.execute(
"INSERT INTO moz_cookies VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
rusqlite::params![host, path_col, secure, expiry, name, value, last],
)
.unwrap();
}
drop(conn);
(dir, path)
}
fn make_chromium_db(
rows: &[(&str, &str, i64, i64, &str, &str, i64)],
) -> (TempDir, std::path::PathBuf) {
let dir = TempDir::new().unwrap();
let path = dir.path().join("Cookies");
let conn = Connection::open(&path).unwrap();
conn.execute(
"CREATE TABLE cookies (
host_key TEXT, path TEXT, secure INTEGER, expires_utc INTEGER,
name TEXT, value TEXT, last_access_utc INTEGER)",
[],
)
.unwrap();
for (host, path_col, secure, expires, name, value, last) in rows {
conn.execute(
"INSERT INTO cookies VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
rusqlite::params![host, path_col, secure, expires, name, value, last],
)
.unwrap();
}
drop(conn);
(dir, path)
}
#[test]
fn parses_firefox_database() {
let (_dir, path) = make_moz_db(&[(
".example.org",
"/",
0,
1_500_000_000,
"JSESSIONID",
Some("123456789"),
1_400_000_000,
)]);
let cookies = Sqlite3CookieParser::parse_firefox(&path).unwrap();
assert_eq!(cookies.len(), 1);
let c = &cookies[0];
assert_eq!(c.name, "JSESSIONID");
assert_eq!(c.value, "123456789");
assert_eq!(c.domain, "example.org", "leading dot must be stripped");
assert_eq!(c.path, "/");
assert_eq!(c.expiry_time, 1_500_000_000);
assert_eq!(c.last_access_time, 1_400_000_000);
assert_eq!(
c.creation_time, 1_400_000_000,
"C++ passes lastAccessed as creation time"
);
assert!(c.persistent);
assert!(!c.secure);
assert!(
!c.host_only,
"leading dot means the cookie spans subdomains"
);
}
#[test]
fn host_without_leading_dot_is_host_only() {
let (_dir, path) = make_moz_db(&[("example.org", "/", 1, 100, "k", Some("v"), 50)]);
let cookies = Sqlite3CookieParser::parse_firefox(&path).unwrap();
assert!(cookies[0].host_only);
assert!(cookies[0].secure, "isSecure=1 must map to secure");
}
#[test]
fn numeric_host_is_always_host_only() {
let (_dir, path) = make_moz_db(&[(".192.168.0.1", "/", 0, 100, "k", Some("v"), 50)]);
let cookies = Sqlite3CookieParser::parse_firefox(&path).unwrap();
assert_eq!(cookies[0].domain, "192.168.0.1");
assert!(cookies[0].host_only);
}
#[test]
fn null_value_becomes_empty_string() {
let (_dir, path) = make_moz_db(&[(".example.org", "/", 0, 100, "k", None, 50)]);
let cookies = Sqlite3CookieParser::parse_firefox(&path).unwrap();
assert_eq!(cookies[0].value, "");
}
#[test]
fn invalid_rows_are_skipped_without_failing_the_import() {
let (_dir, path) = make_moz_db(&[
("", "/", 0, 100, "empty-host", Some("v"), 50),
(
".example.org",
"relative",
0,
100,
"bad-path",
Some("v"),
50,
),
(".example.org", "/", 0, 100, "", Some("v"), 50),
(".example.org", "/", 0, 100, "good", Some("v"), 50),
]);
let cookies = Sqlite3CookieParser::parse_firefox(&path).unwrap();
assert_eq!(cookies.len(), 1);
assert_eq!(cookies[0].name, "good");
}
#[test]
fn parses_chromium_database_and_converts_epoch() {
let (_dir, path) = make_chromium_db(&[(
"example.com",
"/app",
1,
13_000_000_000_000_000,
"SID",
"abc",
12_900_000_000_000_000,
)]);
let cookies = Sqlite3CookieParser::parse_chromium(&path).unwrap();
assert_eq!(cookies.len(), 1);
let c = &cookies[0];
assert_eq!(c.name, "SID");
assert_eq!(c.domain, "example.com");
assert_eq!(c.path, "/app");
assert!(c.secure);
assert!(c.host_only, "no leading dot -> host only");
assert_eq!(c.expiry_time, 1_355_526_400);
assert_eq!(c.last_access_time, 1_255_526_400);
assert_eq!(
c.creation_time, 1_255_526_400,
"C++ passes last_access_utc as the creation time"
);
}
#[test]
fn parse_auto_detects_firefox() {
let (_dir, path) = make_moz_db(&[(".moz.test", "/", 0, 100, "k", Some("v"), 50)]);
let cookies = Sqlite3CookieParser::parse_auto(&path).unwrap();
assert_eq!(cookies[0].domain, "moz.test");
}
#[test]
fn parse_auto_falls_back_to_chromium() {
let (_dir, path) =
make_chromium_db(&[("chrome.test", "/", 0, 13_000_000_000_000_000, "k", "v", 0)]);
let cookies = Sqlite3CookieParser::parse_auto(&path).unwrap();
assert_eq!(cookies[0].domain, "chrome.test");
}
#[test]
fn parse_auto_reports_error_for_unknown_schema() {
let dir = TempDir::new().unwrap();
let path = dir.path().join("random.db");
let conn = Connection::open(&path).unwrap();
conn.execute("CREATE TABLE unrelated (a INTEGER)", [])
.unwrap();
drop(conn);
let err = Sqlite3CookieParser::parse_auto(&path).unwrap_err();
assert!(
err.to_string().contains("moz_cookies"),
"expected the Firefox error to be surfaced, got: {err}"
);
}
#[test]
fn missing_file_is_an_error_not_a_panic() {
let dir = TempDir::new().unwrap();
let path = dir.path().join("does-not-exist.sqlite");
assert!(Sqlite3CookieParser::parse_firefox(&path).is_err());
}
#[test]
fn cookie_storage_load_file_detects_sqlite_by_magic() {
use crate::http::cookie::CookieStorage;
let (_dir, path) = make_moz_db(&[
(".example.org", "/", 0, 4_000_000_000, "a", Some("1"), 50),
(".example.net", "/", 0, 4_000_000_000, "b", Some("2"), 50),
]);
let storage = CookieStorage::new();
let n = storage.load_file(&path).unwrap();
assert_eq!(n, 2);
assert_eq!(storage.count(), 2);
}
#[test]
fn cookie_storage_load_file_still_reads_netscape() {
use crate::http::cookie::CookieStorage;
let dir = TempDir::new().unwrap();
let path = dir.path().join("cookies.txt");
std::fs::write(
&path,
"# Netscape HTTP Cookie File\n\
.example.org\tTRUE\t/\tFALSE\t4000000000\tk\tv\n",
)
.unwrap();
let storage = CookieStorage::new();
assert_eq!(storage.load_file(&path).unwrap(), 1);
}
#[test]
fn detects_sqlite_magic() {
assert!(is_sqlite_file(b"SQLite format 3\0extra bytes"));
assert!(!is_sqlite_file(b"SQLite format 3"), "15 bytes is too short");
assert!(!is_sqlite_file(b"# Netscape HTTP Cookie File"));
assert!(!is_sqlite_file(b""));
}
#[test]
fn text_timestamps_are_accepted() {
let dir = TempDir::new().unwrap();
let path = dir.path().join("cookies.sqlite");
let conn = Connection::open(&path).unwrap();
conn.execute(
"CREATE TABLE moz_cookies (
host TEXT, path TEXT, isSecure TEXT, expiry TEXT,
name TEXT, value TEXT, lastAccessed TEXT)",
[],
)
.unwrap();
conn.execute(
"INSERT INTO moz_cookies VALUES ('.t.example', '/', '1', '999', 'k', 'v', '888')",
[],
)
.unwrap();
drop(conn);
let cookies = Sqlite3CookieParser::parse_firefox(&path).unwrap();
assert_eq!(cookies[0].expiry_time, 999);
assert_eq!(cookies[0].last_access_time, 888);
assert!(cookies[0].secure);
}
}