Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

feat(profile): support profile import from chrome/firefox/curl

SpringCorele454458be7be
diff --git a/Cargo.lock b/Cargo.lock
index 1d42ad064..2bbb32e20 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -731,6 +731,15 @@ dependencies = [
"libc",
]
+[[package]]
+name = "crc32c"
+version = "0.6.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3a47af21622d091a8f0fb295b88bc886ac74efcc613efc19f5d0b21de5c89e47"
+dependencies = [
+ "rustc_version",
+]
+
[[package]]
name = "crc32fast"
version = "1.5.0"
@@ -884,7 +893,7 @@ dependencies = [
"cxxbridge-cmd",
"cxxbridge-flags",
"cxxbridge-macro",
- "foldhash",
+ "foldhash 0.2.0",
"link-cplusplus",
]
@@ -1231,6 +1240,17 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
+[[package]]
+name = "errno"
+version = "0.2.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1"
+dependencies = [
+ "errno-dragonfly",
+ "libc",
+ "winapi",
+]
+
[[package]]
name = "errno"
version = "0.3.14"
@@ -1241,6 +1261,16 @@ dependencies = [
"windows-sys 0.61.2",
]
+[[package]]
+name = "errno-dragonfly"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
+dependencies = [
+ "cc",
+ "libc",
+]
+
[[package]]
name = "euclid"
version = "0.22.14"
@@ -1251,6 +1281,18 @@ dependencies = [
"serde",
]
+[[package]]
+name = "fallible-iterator"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649"
+
+[[package]]
+name = "fallible-streaming-iterator"
+version = "0.1.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"
+
[[package]]
name = "fastrand"
version = "2.3.0"
@@ -1311,6 +1353,12 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
+[[package]]
+name = "foldhash"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
+
[[package]]
name = "foldhash"
version = "0.2.0"
@@ -1400,6 +1448,16 @@ dependencies = [
"percent-encoding",
]
+[[package]]
+name = "fs2"
+version = "0.4.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9564fc758e15025b46aa6643b1b77d047d1a56a1aea6e01002ac0c7026876213"
+dependencies = [
+ "libc",
+ "winapi",
+]
+
[[package]]
name = "fs_extra"
version = "1.3.0"
@@ -1518,7 +1576,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282a26c1e23de04bdab3e34a21b6f877a479b96737792516b9b8b8f69b6661be"
dependencies = [
"bytemuck",
- "foldhash",
+ "foldhash 0.2.0",
"hashbrown 0.17.1",
"log",
"peniko",
@@ -1604,6 +1662,15 @@ dependencies = [
"elliptic-curve",
]
+[[package]]
+name = "hashbrown"
+version = "0.15.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1"
+dependencies = [
+ "foldhash 0.1.5",
+]
+
[[package]]
name = "hashbrown"
version = "0.16.1"
@@ -1617,7 +1684,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"
dependencies = [
"allocator-api2",
- "foldhash",
+ "foldhash 0.2.0",
+]
+
+[[package]]
+name = "hashlink"
+version = "0.10.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7382cf6263419f2d8df38c55d7da83da5c18aef87fc7a7fc1fb1e344edfe14c1"
+dependencies = [
+ "hashbrown 0.15.5",
]
[[package]]
@@ -2016,6 +2092,12 @@ dependencies = [
"serde_core",
]
+[[package]]
+name = "integer-encoding"
+version = "3.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8bb03732005da905c88227371639bf1ad885cc712789c011c31c5fb3ab3ccf02"
+
[[package]]
name = "is_terminal_polyfill"
version = "1.70.2"
@@ -2137,6 +2219,17 @@ dependencies = [
"libc",
]
+[[package]]
+name = "libsqlite3-sys"
+version = "0.35.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "133c182a6a2c87864fe97778797e46c7e999672690dc9fa3ee8e241aa4a9c13f"
+dependencies = [
+ "cc",
+ "pkg-config",
+ "vcpkg",
+]
+
[[package]]
name = "libz-sys"
version = "1.1.25"
@@ -2170,6 +2263,12 @@ version = "0.4.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab"
+[[package]]
+name = "linux-raw-sys"
+version = "0.12.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53"
+
[[package]]
name = "litemap"
version = "0.8.1"
@@ -2294,6 +2393,7 @@ dependencies = [
"libc",
"moli-browser-profile",
"moli-cookie-cache",
+ "moli-cookie-import",
"moli-cookie-jar",
"moli-core",
"moli-fetch",
@@ -2388,6 +2488,24 @@ dependencies = [
"typed-num",
]
+[[package]]
+name = "moli-cookie-import"
+version = "0.1.0"
+dependencies = [
+ "anyhow",
+ "base64 0.22.1",
+ "moli-browser-profile",
+ "moli-cookie-cache",
+ "moli-cookie-jar",
+ "moli-crypto",
+ "rusqlite",
+ "rusty-leveldb",
+ "serde_json",
+ "snap",
+ "tempfile",
+ "time",
+]
+
[[package]]
name = "moli-cookie-jar"
version = "0.1.0"
@@ -4035,16 +4153,37 @@ version = "5.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
+[[package]]
+name = "rand"
+version = "0.8.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e058c7de0b26af77780c769414d6257830bb240f3c38477dbc2c16e5f54d6d4c"
+dependencies = [
+ "libc",
+ "rand_chacha 0.3.1",
+ "rand_core 0.6.4",
+]
+
[[package]]
name = "rand"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1"
dependencies = [
- "rand_chacha",
+ "rand_chacha 0.9.0",
"rand_core 0.9.5",
]
+[[package]]
+name = "rand_chacha"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
+dependencies = [
+ "ppv-lite86",
+ "rand_core 0.6.4",
+]
+
[[package]]
name = "rand_chacha"
version = "0.9.0"
@@ -4055,6 +4194,15 @@ dependencies = [
"rand_core 0.9.5",
]
+[[package]]
+name = "rand_core"
+version = "0.6.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
+dependencies = [
+ "getrandom 0.2.17",
+]
+
[[package]]
name = "rand_core"
version = "0.9.5"
@@ -4197,6 +4345,20 @@ dependencies = [
"memchr",
]
+[[package]]
+name = "rusqlite"
+version = "0.37.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "165ca6e57b20e1351573e3729b958bc62f0e48025386970b6e4d29e7a7e71f3f"
+dependencies = [
+ "bitflags",
+ "fallible-iterator",
+ "fallible-streaming-iterator",
+ "hashlink",
+ "libsqlite3-sys",
+ "smallvec",
+]
+
[[package]]
name = "rustc-hash"
version = "1.1.0"
@@ -4225,12 +4387,25 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154"
dependencies = [
"bitflags",
- "errno",
+ "errno 0.3.14",
"libc",
- "linux-raw-sys",
+ "linux-raw-sys 0.4.15",
"windows-sys 0.59.0",
]
+[[package]]
+name = "rustix"
+version = "1.1.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190"
+dependencies = [
+ "bitflags",
+ "errno 0.3.14",
+ "libc",
+ "linux-raw-sys 0.12.1",
+ "windows-sys 0.61.2",
+]
+
[[package]]
name = "rustls"
version = "0.23.38"
@@ -4273,6 +4448,21 @@ version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f"
+[[package]]
+name = "rusty-leveldb"
+version = "4.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "43ddda8f34100fca4bf1b0bbf743d12d27b5d84c35c74a52f392aac26f54e82d"
+dependencies = [
+ "bytes",
+ "crc32c",
+ "errno 0.2.8",
+ "fs2",
+ "integer-encoding",
+ "rand 0.8.8",
+ "snap",
+]
+
[[package]]
name = "ryu"
version = "1.0.23"
@@ -4516,7 +4706,7 @@ version = "1.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b"
dependencies = [
- "errno",
+ "errno 0.3.14",
"libc",
]
@@ -4594,6 +4784,12 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7c388c1b5e93756d0c740965c41e8822f866621d41acbdf6336a6a168f8840c"
+[[package]]
+name = "snap"
+version = "1.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "199905e6153d6405f9728fe44daace35f8f837bbf830bb6e85fbd5828709a886"
+
[[package]]
name = "socket2"
version = "0.6.3"
@@ -4919,6 +5115,19 @@ dependencies = [
"smallvec",
]
+[[package]]
+name = "tempfile"
+version = "3.27.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd"
+dependencies = [
+ "fastrand",
+ "getrandom 0.3.4",
+ "once_cell",
+ "rustix 1.1.4",
+ "windows-sys 0.61.2",
+]
+
[[package]]
name = "temporal_capi"
version = "0.2.6"
@@ -5360,7 +5569,7 @@ dependencies = [
"http",
"httparse",
"log",
- "rand",
+ "rand 0.9.2",
"rustls",
"rustls-pki-types",
"sha1",
@@ -5707,7 +5916,7 @@ checksum = "b4ee928febd44d98f2f459a4a79bd4d928591333a494a10a868418ac1b39cf1f"
dependencies = [
"either",
"home",
- "rustix",
+ "rustix 0.38.44",
"winsafe",
]
diff --git a/Cargo.toml b/Cargo.toml
index fc22a5c65..9aa15d2e6 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -10,6 +10,7 @@ members = [
"moli-canvas",
"moli-charset-parser",
"moli-cookie-cache",
+ "moli-cookie-import",
"moli-protocol-cdp",
"moli-cookie-store",
"moli-content-type",
diff --git a/README.md b/README.md
index cb832505f..97de86881 100644
--- a/README.md
+++ b/README.md
@@ -134,6 +134,55 @@ Run `fetch --help` for the complete option list, including output formats,
page-load/response waits, profiles, proxy settings, resource policies, and
tracing options.
+### Import browser session state
+
+Import cookies and local storage from a Chrome profile into a Moli profile:
+
+```bash
+moli import \
+ --profile-dir ./moli-profile \
+ --chrome-profile-dir "$HOME/Library/Application Support/Google/Chrome/Default" \
+ --includes all \
+ --chrome-crypto-key system
+```
+
+On macOS, `system` asks Keychain for Chrome Safe Storage access when encrypted
+cookies are encountered. For automation or another platform, pass a raw AES
+key as `--chrome-crypto-key base64:<key>`. Omitting the option defaults to the
+system key source. The importer snapshots Chrome's SQLite and LevelDB files
+before reading them, so Chrome may remain open.
+
+`--includes` accepts `cookies`, `storage`, `indexeddb`, or a comma-separated
+combination and defaults to `all`. Here, `all` means every data type currently
+supported by the selected importer; for Chrome this is cookies and local
+storage. Chromium's IndexedDB LevelDB/V8 format is not yet compatible with
+Moli's JSON IndexedDB backend, so explicitly requesting `indexeddb` fails before
+changing the destination.
+
+Netscape-format cookie files, including curl cookie jars, can be imported
+independently or combined with a browser source. Repeat `--cookie-jar` to merge
+multiple files:
+
+```bash
+moli import \
+ --profile-dir ./moli-profile \
+ --cookie-jar ./cookies.txt
+```
+
+Firefox cookies and local storage use the same destination and selection flags:
+
+```bash
+moli import \
+ --profile-dir ./moli-profile \
+ --firefox-profile-dir "$HOME/Library/Application Support/Firefox/Profiles/example.default-release" \
+ --includes all
+```
+
+Firefox container, private-browsing, and partitioned cookies are intentionally
+not flattened into Moli's default partition because that would weaken their
+isolation semantics. Firefox local-storage values support both native UTF-16
+and UTF-8 representations, including Snappy-compressed values.
+
### Start the automation server
```bash
diff --git a/moli-cookie-import/Cargo.toml b/moli-cookie-import/Cargo.toml
new file mode 100644
index 000000000..0f34a919f
--- /dev/null
+++ b/moli-cookie-import/Cargo.toml
@@ -0,0 +1,22 @@
+[package]
+license.workspace = true
+name = "moli-cookie-import"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
+anyhow = "1.0.100"
+base64 = "0.22.1"
+moli-browser-profile = { path = "../moli-browser-profile" }
+moli-cookie-cache = { path = "../moli-cookie-cache" }
+moli-cookie-jar = { path = "../moli-cookie-jar" }
+moli-crypto = { path = "../moli-crypto" }
+rusqlite = { version = "0.37.0", features = ["bundled"] }
+rusty-leveldb = "4.0.1"
+serde_json = "1.0.145"
+snap = "1.1.1"
+tempfile = "3.23.0"
+time = "0.3.44"
+
+[lints]
+workspace = true
diff --git a/moli-cookie-import/src/chrome.rs b/moli-cookie-import/src/chrome.rs
new file mode 100644
index 000000000..68aaf1d77
--- /dev/null
+++ b/moli-cookie-import/src/chrome.rs
@@ -0,0 +1,398 @@
+use std::{collections::BTreeMap, fs, path::Path, process::Command};
+
+use anyhow::{Context, Result, anyhow, bail};
+use base64::{Engine as _, engine::general_purpose::STANDARD};
+use moli_cookie_jar::{
+ CookiePriority, StoredCookie, StoredCookiePartitionKey, StoredCookieSameSite,
+ StoredCookieSourceScheme,
+};
+use moli_crypto::{
+ aes_128_cbc_pkcs7_decrypt, aes_256_gcm_decrypt, derive_pbkdf2_hmac_sha1, sha256_digest,
+};
+use rusqlite::Connection;
+use rusty_leveldb::{DB, LdbIterator, Options};
+use tempfile::TempDir;
+use time::OffsetDateTime;
+
+use crate::ChromeCryptoKey;
+
+const CHROME_EPOCH_OFFSET_MICROS: i64 = 11_644_473_600_000_000;
+
+pub(crate) fn validate_source(path: &Path) -> Result<()> {
+ if !path.is_dir() {
+ bail!(
+ "Chrome profile directory `{}` does not exist",
+ path.display()
+ );
+ }
+ Ok(())
+}
+
+pub(crate) fn import_cookies(
+ profile: &Path,
+ key_source: &ChromeCryptoKey,
+) -> Result<Vec<StoredCookie>> {
+ let source = [profile.join("Network/Cookies"), profile.join("Cookies")]
+ .into_iter()
+ .find(|path| path.is_file())
+ .ok_or_else(|| {
+ anyhow!(
+ "Chrome Cookies database was not found under `{}`",
+ profile.display()
+ )
+ })?;
+ let temp = tempfile::NamedTempFile::new().context("failed to create Cookies snapshot")?;
+ fs::copy(&source, temp.path()).with_context(|| {
+ format!(
+ "failed to snapshot Chrome Cookies database `{}`",
+ source.display()
+ )
+ })?;
+ let connection =
+ Connection::open(temp.path()).context("failed to open Chrome Cookies snapshot")?;
+ let schema_version = connection
+ .query_row("SELECT value FROM meta WHERE key = 'version'", [], |row| {
+ row.get::<_, String>(0)
+ })
+ .ok()
+ .and_then(|value| value.parse::<i64>().ok())
+ .unwrap_or(0);
+ let has_partition_key = cookie_column_exists(&connection, "top_frame_site_key")?;
+ let has_cross_site_ancestor = cookie_column_exists(&connection, "has_cross_site_ancestor")?;
+ let partition_key_column = if has_partition_key {
+ "top_frame_site_key"
+ } else {
+ "NULL"
+ };
+ let cross_site_column = if has_cross_site_ancestor {
+ "has_cross_site_ancestor"
+ } else {
+ "0"
+ };
+ let sql = format!(
+ "SELECT host_key,name,value,encrypted_value,path,expires_utc,is_secure,is_httponly,samesite,priority,source_scheme,source_port,{partition_key_column},{cross_site_column} FROM cookies"
+ );
+ let mut statement = connection
+ .prepare(&sql)
+ .context("Chrome Cookies database has an unsupported schema")?;
+ let rows = statement.query_map([], |row| {
+ Ok(ChromeCookieRow {
+ host: row.get(0)?,
+ name: row.get(1)?,
+ value: row.get(2)?,
+ encrypted_value: row.get(3)?,
+ path: row.get(4)?,
+ expires_utc: row.get(5)?,
+ secure: row.get::<_, i64>(6)? != 0,
+ http_only: row.get::<_, i64>(7)? != 0,
+ same_site: row.get(8)?,
+ priority: row.get(9)?,
+ source_scheme: row.get(10)?,
+ source_port: row.get(11)?,
+ partition_key: row.get(12)?,
+ has_cross_site_ancestor: row.get::<_, i64>(13)? != 0,
+ })
+ })?;
+ let mut cookies = Vec::new();
+ let mut key = None;
+ for row in rows {
+ let mut row = row?;
+ let value = if row.encrypted_value.is_empty() {
+ std::mem::take(&mut row.value)
+ } else {
+ if key.is_none() {
+ key = Some(resolve_crypto_key(key_source)?);
+ }
+ decrypt_cookie_value(
+ &row.encrypted_value,
+ key.as_deref().expect("key was resolved"),
+ &row.host,
+ schema_version,
+ )
+ .with_context(|| {
+ format!(
+ "failed to decrypt Chrome cookie `{}` for `{}`",
+ row.name, row.host
+ )
+ })?
+ };
+ cookies.push(row.into_stored(value));
+ }
+ Ok(cookies)
+}
+
+struct ChromeCookieRow {
+ host: String,
+ name: String,
+ value: String,
+ encrypted_value: Vec<u8>,
+ path: String,
+ expires_utc: i64,
+ secure: bool,
+ http_only: bool,
+ same_site: i64,
+ priority: i64,
+ source_scheme: i64,
+ source_port: i32,
+ partition_key: Option<String>,
+ has_cross_site_ancestor: bool,
+}
+
+impl ChromeCookieRow {
+ fn into_stored(self, value: String) -> StoredCookie {
+ StoredCookie {
+ name: self.name,
+ value,
+ host_only: !self.host.starts_with('.'),
+ domain: self.host,
+ path: self.path,
+ secure: self.secure,
+ http_only: self.http_only,
+ expires: chrome_expiry(self.expires_utc),
+ same_site: match self.same_site {
+ 0 => StoredCookieSameSite::None,
+ 1 => StoredCookieSameSite::Lax,
+ 2 => StoredCookieSameSite::Strict,
+ _ => StoredCookieSameSite::Unspecified,
+ },
+ priority: Some(match self.priority {
+ 0 => CookiePriority::Low,
+ 2 => CookiePriority::High,
+ _ => CookiePriority::Medium,
+ }),
+ partition_key: self
+ .partition_key
+ .filter(|key| !key.is_empty())
+ .map(|key| StoredCookiePartitionKey::site(key, self.has_cross_site_ancestor)),
+ source_scheme: match self.source_scheme {
+ 1 => StoredCookieSourceScheme::NonSecure,
+ 2 => StoredCookieSourceScheme::Secure,
+ _ => StoredCookieSourceScheme::Unset,
+ },
+ source_port: self.source_port,
+ creation_index: 0,
+ last_access_index: 0,
+ }
+ }
+}
+
+fn cookie_column_exists(connection: &Connection, name: &str) -> Result<bool> {
+ let mut statement = connection.prepare("PRAGMA table_info(cookies)")?;
+ let columns = statement.query_map([], |row| row.get::<_, String>(1))?;
+ for column in columns {
+ if column? == name {
+ return Ok(true);
+ }
+ }
+ Ok(false)
+}
+
+fn chrome_expiry(micros: i64) -> Option<OffsetDateTime> {
+ if micros == 0 {
+ return None;
+ }
+ let unix_micros = micros.checked_sub(CHROME_EPOCH_OFFSET_MICROS)?;
+ OffsetDateTime::from_unix_timestamp_nanos(i128::from(unix_micros) * 1_000).ok()
+}
+
+fn resolve_crypto_key(source: &ChromeCryptoKey) -> Result<Vec<u8>> {
+ match source {
+ ChromeCryptoKey::Base64(value) => STANDARD
+ .decode(value)
+ .context("--chrome-crypto-key base64 value is invalid"),
+ ChromeCryptoKey::System => system_crypto_key(),
+ }
+}
+
+#[cfg(target_os = "macos")]
+fn system_crypto_key() -> Result<Vec<u8>> {
+ let output = Command::new("security")
+ .args(["find-generic-password", "-w", "-s", "Chrome Safe Storage"])
+ .output()
+ .context("failed to ask macOS Keychain for Chrome Safe Storage")?;
+ if !output.status.success() {
+ bail!("macOS Keychain did not grant access to Chrome Safe Storage");
+ }
+ let password =
+ String::from_utf8(output.stdout).context("Chrome Safe Storage password was not UTF-8")?;
+ let mut key = [0_u8; 16];
+ derive_pbkdf2_hmac_sha1(
+ password.trim_end().as_bytes(),
+ b"saltysalt",
+ std::num::NonZeroU32::new(1003).expect("Chrome iteration count is non-zero"),
+ &mut key,
+ );
+ Ok(key.to_vec())
+}
+
+#[cfg(not(target_os = "macos"))]
+fn system_crypto_key() -> Result<Vec<u8>> {
+ bail!(
+ "system Chrome key import is currently available on macOS; use --chrome-crypto-key base64:<raw-key> on this platform"
+ )
+}
+
+fn decrypt_cookie_value(
+ encrypted: &[u8],
+ key: &[u8],
+ host: &str,
+ schema_version: i64,
+) -> Result<String> {
+ let payload = encrypted
+ .strip_prefix(b"v10")
+ .or_else(|| encrypted.strip_prefix(b"v11"))
+ .ok_or_else(|| anyhow!("unsupported encrypted cookie prefix"))?;
+ let plaintext = if key.len() == 32 && payload.len() > 12 + 16 {
+ aes_256_gcm_decrypt(key, &payload[..12], &payload[12..])
+ .map_err(|_| anyhow!("AES-GCM authentication failed"))?
+ } else if key.len() == 16 {
+ aes_128_cbc_pkcs7_decrypt(key, &[b' '; 16], payload)
+ .map_err(|_| anyhow!("AES-CBC decryption failed"))?
+ } else {
+ bail!("Chrome crypto key must decode to 16 or 32 bytes");
+ };
+ let plaintext = if schema_version >= 24 {
+ let digest = sha256_digest(host.as_bytes());
+ plaintext
+ .strip_prefix(&digest)
+ .ok_or_else(|| anyhow!("decrypted cookie host digest does not match"))?
+ } else {
+ &plaintext
+ };
+ String::from_utf8(plaintext.to_vec()).context("decrypted cookie value is not UTF-8")
+}
+
+pub(crate) fn import_local_storage(profile: &Path, destination: &Path) -> Result<usize> {
+ let source = profile.join("Local Storage/leveldb");
+ if !source.is_dir() {
+ return Ok(0);
+ }
+ let snapshot = TempDir::new().context("failed to create localStorage snapshot directory")?;
+ copy_dir(&source, snapshot.path())?;
+ let options = Options {
+ create_if_missing: false,
+ ..Options::default()
+ };
+ let mut database = DB::open(snapshot.path(), options)
+ .map_err(|error| anyhow!("failed to open Chrome localStorage LevelDB snapshot: {error}"))?;
+ let mut origins = BTreeMap::<String, BTreeMap<Vec<u16>, Vec<u16>>>::new();
+ let mut iterator = database
+ .new_iter()
+ .map_err(|error| anyhow!("failed to iterate Chrome localStorage: {error}"))?;
+ iterator.seek_to_first();
+ let mut imported = 0;
+ while let Some((key, value)) = iterator.next() {
+ let Some((origin, item_key)) = decode_local_storage_key(&key) else {
+ continue;
+ };
+ let Some(item_value) = decode_chrome_string(&value) else {
+ continue;
+ };
+ origins
+ .entry(origin)
+ .or_default()
+ .insert(item_key, item_value);
+ imported += 1;
+ }
+ let origins = origins
+ .into_iter()
+ .map(|(origin, entries)| {
+ let entries = entries
+ .into_iter()
+ .map(|(key, value)| {
+ serde_json::json!({
+ "key": json_dom_string(key),
+ "value": json_dom_string(value),
+ })
+ })
+ .collect::<Vec<_>>();
+ (origin, serde_json::json!({ "entries": entries }))
+ })
+ .collect::<serde_json::Map<_, _>>();
+ let bytes = serde_json::to_vec_pretty(&serde_json::json!({
+ "version": 1,
+ "origins": origins,
+ }))?;
+ moli_browser_profile::write_file_atomically(destination, &bytes, "Chrome localStorage import")?;
+ Ok(imported)
+}
+
+fn json_dom_string(units: Vec<u16>) -> serde_json::Value {
+ match String::from_utf16(&units) {
+ Ok(text) => serde_json::Value::String(text),
+ Err(_) => serde_json::json!({ "utf16": units }),
+ }
+}
+
+fn decode_local_storage_key(key: &[u8]) -> Option<(String, Vec<u16>)> {
+ let key = key.strip_prefix(b"_")?;
+ let separator = key.iter().position(|byte| *byte == 0)?;
+ let origin = std::str::from_utf8(&key[..separator]).ok()?.to_owned();
+ let item = decode_chrome_string(&key[separator + 1..])?;
+ Some((origin, item))
+}
+
+fn decode_chrome_string(value: &[u8]) -> Option<Vec<u16>> {
+ let (&encoding, bytes) = value.split_first()?;
+ match encoding {
+ 0 => {
+ if bytes.len() % 2 != 0 {
+ return None;
+ }
+ Some(
+ bytes
+ .chunks_exact(2)
+ .map(|pair| u16::from_le_bytes([pair[0], pair[1]]))
+ .collect(),
+ )
+ }
+ 1 => Some(bytes.iter().map(|byte| u16::from(*byte)).collect()),
+ _ => None,
+ }
+}
+
+fn copy_dir(source: &Path, destination: &Path) -> Result<()> {
+ for entry in
+ fs::read_dir(source).with_context(|| format!("failed to read `{}`", source.display()))?
+ {
+ let entry = entry?;
+ let target = destination.join(entry.file_name());
+ if entry.file_type()?.is_dir() {
+ fs::create_dir_all(&target)?;
+ copy_dir(&entry.path(), &target)?;
+ } else {
+ fs::copy(entry.path(), target)?;
+ }
+ }
+ Ok(())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::{chrome_expiry, decode_chrome_string, decode_local_storage_key};
+
+ #[test]
+ fn decodes_chrome_local_storage_latin1_key() {
+ let decoded = decode_local_storage_key(b"_https://example.test\0\x01token").unwrap();
+ assert_eq!(decoded.0, "https://example.test");
+ assert_eq!(String::from_utf16(&decoded.1).unwrap(), "token");
+ }
+
+ #[test]
+ fn decodes_chrome_utf16_string() {
+ assert_eq!(
+ decode_chrome_string(&[0, b'a', 0, 0xac, 0x20]),
+ Some(vec![97, 8364])
+ );
+ }
+
+ #[test]
+ fn converts_chrome_epoch() {
+ assert_eq!(
+ chrome_expiry(11_644_473_601_000_000)
+ .unwrap()
+ .unix_timestamp(),
+ 1
+ );
+ }
+}
diff --git a/moli-cookie-import/src/firefox.rs b/moli-cookie-import/src/firefox.rs
new file mode 100644
index 000000000..9ec0f83a7
--- /dev/null
+++ b/moli-cookie-import/src/firefox.rs
@@ -0,0 +1,331 @@
+use std::{collections::BTreeMap, fs, path::Path};
+
+use anyhow::{Context, Result, bail};
+use moli_cookie_jar::{
+ CookiePriority, StoredCookie, StoredCookieSameSite, StoredCookieSourceScheme,
+};
+use rusqlite::Connection;
+use time::OffsetDateTime;
+
+pub(crate) fn validate_source(path: &Path) -> Result<()> {
+ if !path.is_dir() {
+ bail!(
+ "Firefox profile directory `{}` does not exist",
+ path.display()
+ );
+ }
+ Ok(())
+}
+
+pub(crate) fn import_cookies(profile: &Path) -> Result<Vec<StoredCookie>> {
+ let source = profile.join("cookies.sqlite");
+ if !source.is_file() {
+ return Ok(Vec::new());
+ }
+ let snapshot = snapshot_sqlite(&source, "Firefox cookies")?;
+ let connection = Connection::open(snapshot.path().join("database.sqlite"))
+ .context("failed to open Firefox cookies snapshot")?;
+ let mut statement = connection
+ .prepare("SELECT name,value,host,path,expiry,isSecure,isHttpOnly,sameSite,schemeMap,originAttributes FROM moz_cookies")
+ .context("Firefox cookies database has an unsupported schema")?;
+ let rows = statement.query_map([], |row| {
+ Ok(FirefoxCookieRow {
+ name: row.get(0)?,
+ value: row.get(1)?,
+ host: row.get(2)?,
+ path: row.get(3)?,
+ expiry: row.get(4)?,
+ secure: row.get::<_, i64>(5)? != 0,
+ http_only: row.get::<_, i64>(6)? != 0,
+ same_site: row.get(7)?,
+ scheme_map: row.get(8)?,
+ origin_attributes: row.get(9)?,
+ })
+ })?;
+ let mut cookies = Vec::new();
+ for row in rows {
+ let row = row?;
+ // Do not flatten container/private/partitioned cookies into Moli's
+ // default partition, which would change their isolation semantics.
+ if !row.origin_attributes.is_empty() {
+ continue;
+ }
+ let expires = if row.expiry <= 0 {
+ None
+ } else {
+ Some(
+ OffsetDateTime::from_unix_timestamp(row.expiry)
+ .with_context(|| format!("invalid Firefox cookie expiry for `{}`", row.name))?,
+ )
+ };
+ cookies.push(StoredCookie {
+ name: row.name,
+ value: row.value,
+ host_only: !row.host.starts_with('.'),
+ domain: row.host,
+ path: row.path,
+ secure: row.secure,
+ http_only: row.http_only,
+ expires,
+ same_site: match row.same_site {
+ 0 => StoredCookieSameSite::None,
+ 1 => StoredCookieSameSite::Lax,
+ 2 => StoredCookieSameSite::Strict,
+ _ => StoredCookieSameSite::Unspecified,
+ },
+ priority: Some(CookiePriority::Medium),
+ partition_key: None,
+ source_scheme: if row.scheme_map & 0x02 != 0 || row.secure {
+ StoredCookieSourceScheme::Secure
+ } else if row.scheme_map & 0x01 != 0 {
+ StoredCookieSourceScheme::NonSecure
+ } else {
+ StoredCookieSourceScheme::Unset
+ },
+ source_port: -1,
+ creation_index: cookies.len() as u64,
+ last_access_index: 0,
+ });
+ }
+ Ok(cookies)
+}
+
+pub(crate) fn import_local_storage(profile: &Path, destination: &Path) -> Result<usize> {
+ let root = profile.join("storage/default");
+ if !root.is_dir() {
+ return Ok(0);
+ }
+ let mut databases = Vec::new();
+ collect_local_storage_databases(&root, &mut databases)?;
+ let mut origins = BTreeMap::<String, BTreeMap<String, String>>::new();
+ let mut imported = 0;
+ for source in databases {
+ let snapshot = snapshot_sqlite(&source, "Firefox localStorage")?;
+ let connection = Connection::open(snapshot.path().join("database.sqlite"))?;
+ let origin: String = connection
+ .query_row("SELECT origin FROM database LIMIT 1", [], |row| row.get(0))
+ .with_context(|| format!("failed to read origin from `{}`", source.display()))?;
+ let mut statement =
+ connection.prepare("SELECT key,conversion_type,compression_type,value FROM data")?;
+ let rows = statement.query_map([], |row| {
+ Ok((
+ row.get::<_, String>(0)?,
+ row.get::<_, i64>(1)?,
+ row.get::<_, i64>(2)?,
+ row.get::<_, Vec<u8>>(3)?,
+ ))
+ })?;
+ for row in rows {
+ let (key, conversion, compression, value) = row?;
+ let value = decode_local_storage_value(&value, conversion, compression)
+ .with_context(|| format!("failed to decode Firefox localStorage `{key}`"))?;
+ origins
+ .entry(origin.clone())
+ .or_default()
+ .insert(key, value);
+ imported += 1;
+ }
+ }
+ write_local_storage(destination, origins)?;
+ Ok(imported)
+}
+
+struct FirefoxCookieRow {
+ name: String,
+ value: String,
+ host: String,
+ path: String,
+ expiry: i64,
+ secure: bool,
+ http_only: bool,
+ same_site: i64,
+ scheme_map: i64,
+ origin_attributes: String,
+}
+
+fn collect_local_storage_databases(
+ directory: &Path,
+ databases: &mut Vec<std::path::PathBuf>,
+) -> Result<()> {
+ for entry in fs::read_dir(directory).with_context(|| {
+ format!(
+ "failed to read Firefox storage directory `{}`",
+ directory.display()
+ )
+ })? {
+ let path = entry?.path();
+ if path.is_dir() {
+ collect_local_storage_databases(&path, databases)?;
+ } else if path.file_name().and_then(|name| name.to_str()) == Some("data.sqlite")
+ && path
+ .parent()
+ .and_then(Path::file_name)
+ .and_then(|name| name.to_str())
+ == Some("ls")
+ {
+ databases.push(path);
+ }
+ }
+ Ok(())
+}
+
+fn decode_local_storage_value(value: &[u8], conversion: i64, compression: i64) -> Result<String> {
+ let value = match compression {
+ 0 => value.to_vec(),
+ 1 => snap::raw::Decoder::new()
+ .decompress_vec(value)
+ .context("invalid Snappy payload")?,
+ other => bail!("unsupported Firefox localStorage compression type {other}"),
+ };
+ match conversion {
+ 0 => {
+ if value.len() % 2 != 0 {
+ bail!("UTF-16 value has an odd byte length");
+ }
+ let units = value
+ .chunks_exact(2)
+ .map(|pair| u16::from_ne_bytes([pair[0], pair[1]]))
+ .collect::<Vec<_>>();
+ String::from_utf16(&units).context("invalid UTF-16 value")
+ }
+ 1 => String::from_utf8(value).context("invalid UTF-8 value"),
+ other => bail!("unsupported Firefox localStorage conversion type {other}"),
+ }
+}
+
+fn write_local_storage(
+ destination: &Path,
+ origins: BTreeMap<String, BTreeMap<String, String>>,
+) -> Result<()> {
+ let mut persisted_origins = if destination.is_file() {
+ let existing: serde_json::Value = serde_json::from_slice(&fs::read(destination)?)
+ .with_context(|| {
+ format!(
+ "failed to parse existing Moli localStorage `{}`",
+ destination.display()
+ )
+ })?;
+ existing
+ .get("origins")
+ .and_then(serde_json::Value::as_object)
+ .cloned()
+ .unwrap_or_default()
+ } else {
+ serde_json::Map::new()
+ };
+ for (origin, entries) in origins {
+ let entries = entries
+ .into_iter()
+ .map(|(key, value)| serde_json::json!({ "key": key, "value": value }))
+ .collect::<Vec<_>>();
+ persisted_origins.insert(origin, serde_json::json!({ "entries": entries }));
+ }
+ let bytes = serde_json::to_vec_pretty(&serde_json::json!({
+ "version": 1,
+ "origins": persisted_origins,
+ }))?;
+ moli_browser_profile::write_file_atomically(destination, &bytes, "Firefox localStorage import")
+}
+
+fn snapshot_sqlite(source: &Path, label: &str) -> Result<tempfile::TempDir> {
+ let snapshot =
+ tempfile::tempdir().with_context(|| format!("failed to create {label} snapshot"))?;
+ fs::copy(source, snapshot.path().join("database.sqlite"))
+ .with_context(|| format!("failed to snapshot {label} `{}`", source.display()))?;
+ for suffix in ["-wal", "-shm"] {
+ let companion = std::path::PathBuf::from(format!("{}{suffix}", source.display()));
+ if companion.is_file() {
+ fs::copy(
+ &companion,
+ snapshot.path().join(format!("database.sqlite{suffix}")),
+ )?;
+ }
+ }
+ Ok(snapshot)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use rusqlite::params;
+
+ #[test]
+ fn decodes_utf8_and_snappy_values() -> Result<()> {
+ assert_eq!(decode_local_storage_value(b"hello", 1, 0)?, "hello");
+ let compressed = snap::raw::Encoder::new().compress_vec("世界".as_bytes())?;
+ assert_eq!(decode_local_storage_value(&compressed, 1, 1)?, "世界");
+ Ok(())
+ }
+
+ #[test]
+ fn decodes_native_utf16_values() -> Result<()> {
+ let bytes = "hello"
+ .encode_utf16()
+ .flat_map(u16::to_ne_bytes)
+ .collect::<Vec<_>>();
+ assert_eq!(decode_local_storage_value(&bytes, 0, 0)?, "hello");
+ Ok(())
+ }
+
+ #[test]
+ fn imports_firefox_cookies_and_local_storage_databases() -> Result<()> {
+ let temp = tempfile::tempdir()?;
+ let profile = temp.path().join("firefox");
+ fs::create_dir_all(&profile)?;
+ let cookies_db = Connection::open(profile.join("cookies.sqlite"))?;
+ cookies_db.execute_batch(
+ "CREATE TABLE moz_cookies (
+ name TEXT, value TEXT, host TEXT, path TEXT, expiry INTEGER,
+ isSecure INTEGER, isHttpOnly INTEGER, sameSite INTEGER,
+ schemeMap INTEGER, originAttributes TEXT
+ );",
+ )?;
+ cookies_db.execute(
+ "INSERT INTO moz_cookies VALUES (?1,?2,?3,?4,?5,?6,?7,?8,?9,?10)",
+ params![
+ "session",
+ "value",
+ ".example.com",
+ "/",
+ 2_147_483_647_i64,
+ 1,
+ 1,
+ 1,
+ 2,
+ ""
+ ],
+ )?;
+ drop(cookies_db);
+
+ let storage_dir = profile.join("storage/default/https+++example.com/ls");
+ fs::create_dir_all(&storage_dir)?;
+ let storage_db = Connection::open(storage_dir.join("data.sqlite"))?;
+ storage_db.execute_batch(
+ "CREATE TABLE database (origin TEXT NOT NULL);
+ CREATE TABLE data (
+ key TEXT PRIMARY KEY, utf16_length INTEGER NOT NULL,
+ conversion_type INTEGER NOT NULL, compression_type INTEGER NOT NULL,
+ last_access_time INTEGER NOT NULL DEFAULT 0, value BLOB NOT NULL
+ );",
+ )?;
+ storage_db.execute("INSERT INTO database VALUES ('https://example.com')", [])?;
+ let compressed = snap::raw::Encoder::new().compress_vec(b"stored-value")?;
+ storage_db.execute(
+ "INSERT INTO data VALUES (?1,?2,?3,?4,0,?5)",
+ params!["stored-key", 12, 1, 1, compressed],
+ )?;
+ drop(storage_db);
+
+ let cookies = import_cookies(&profile)?;
+ assert_eq!(cookies.len(), 1);
+ assert_eq!(cookies[0].same_site, StoredCookieSameSite::Lax);
+ let destination = temp.path().join("localstorage.json");
+ assert_eq!(import_local_storage(&profile, &destination)?, 1);
+ let json: serde_json::Value = serde_json::from_slice(&fs::read(destination)?)?;
+ assert_eq!(
+ json["origins"]["https://example.com"]["entries"][0]["value"],
+ "stored-value"
+ );
+ Ok(())
+ }
+}
diff --git a/moli-cookie-import/src/lib.rs b/moli-cookie-import/src/lib.rs
new file mode 100644
index 000000000..02a738597
--- /dev/null
+++ b/moli-cookie-import/src/lib.rs
@@ -0,0 +1,207 @@
+//! Import browser cookies and local storage into a Moli browser profile.
+
+mod chrome;
+mod firefox;
+
+use std::{
+ fmt,
+ path::{Path, PathBuf},
+ str::FromStr,
+};
+
+use anyhow::{Context, Result, bail};
+use moli_browser_profile::BrowserProfile;
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub struct ImportIncludes(u8);
+
+impl ImportIncludes {
+ pub const COOKIES: u8 = 1;
+ pub const STORAGE: u8 = 2;
+ pub const INDEXED_DB: u8 = 4;
+ pub const ALL: u8 = Self::COOKIES | Self::STORAGE;
+
+ pub fn contains(self, kind: u8) -> bool {
+ self.0 & kind != 0
+ }
+}
+
+impl FromStr for ImportIncludes {
+ type Err = String;
+
+ fn from_str(value: &str) -> Result<Self, Self::Err> {
+ let mut bits = 0;
+ for item in value
+ .split(',')
+ .map(str::trim)
+ .filter(|item| !item.is_empty())
+ {
+ bits |= match item.to_ascii_lowercase().as_str() {
+ "all" => Self::ALL,
+ "cookies" => Self::COOKIES,
+ "storage" | "localstorage" => Self::STORAGE,
+ "indexeddb" => Self::INDEXED_DB,
+ other => return Err(format!("unknown import kind `{other}`")),
+ };
+ }
+ if bits == 0 {
+ return Err("at least one import kind is required".to_owned());
+ }
+ Ok(Self(bits))
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum ChromeCryptoKey {
+ System,
+ Base64(String),
+}
+
+impl FromStr for ChromeCryptoKey {
+ type Err = String;
+
+ fn from_str(value: &str) -> Result<Self, Self::Err> {
+ if value.eq_ignore_ascii_case("system") {
+ return Ok(Self::System);
+ }
+ value
+ .strip_prefix("base64:")
+ .filter(|key| !key.is_empty())
+ .map(|key| Self::Base64(key.to_owned()))
+ .ok_or_else(|| "crypto key must be `system` or `base64:<raw-key>`".to_owned())
+ }
+}
+
+pub struct ImportRequest<'a> {
+ pub profile_dir: &'a Path,
+ pub includes: ImportIncludes,
+ pub chrome_profile_dir: Option<&'a Path>,
+ pub chrome_crypto_key: Option<&'a ChromeCryptoKey>,
+ pub firefox_profile_dir: Option<&'a Path>,
+ pub cookie_jars: &'a [PathBuf],
+}
+
+#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
+pub struct ImportSummary {
+ pub cookies: usize,
+ pub storage: usize,
+}
+
+impl fmt::Display for ImportSummary {
+ fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
+ let mut parts = Vec::new();
+ if self.storage > 0 {
+ parts.push(format!("storage={}", self.storage));
+ }
+ if self.cookies > 0 {
+ parts.push(format!("cookies={}", self.cookies));
+ }
+ formatter.write_str(&parts.join(", "))
+ }
+}
+
+pub fn import_session_state(request: &ImportRequest<'_>) -> Result<ImportSummary> {
+ validate_request(request)?;
+ let profile = BrowserProfile::open(request.profile_dir).with_context(|| {
+ format!(
+ "failed to open destination Moli profile `{}`",
+ request.profile_dir.display()
+ )
+ })?;
+ let partition = profile.default_partition();
+ let mut summary = ImportSummary::default();
+ let mut imported_cookies = Vec::new();
+
+ if let Some(source) = request.chrome_profile_dir {
+ chrome::validate_source(source)?;
+ if request.includes.contains(ImportIncludes::COOKIES) {
+ imported_cookies.extend(chrome::import_cookies(
+ source,
+ request
+ .chrome_crypto_key
+ .unwrap_or(&ChromeCryptoKey::System),
+ )?);
+ }
+ if request.includes.contains(ImportIncludes::STORAGE) {
+ summary.storage +=
+ chrome::import_local_storage(source, partition.local_storage_path())?;
+ }
+ }
+
+ if let Some(source) = request.firefox_profile_dir {
+ firefox::validate_source(source)?;
+ if request.includes.contains(ImportIncludes::COOKIES) {
+ imported_cookies.extend(firefox::import_cookies(source)?);
+ }
+ if request.includes.contains(ImportIncludes::STORAGE) {
+ summary.storage +=
+ firefox::import_local_storage(source, partition.local_storage_path())?;
+ }
+ }
+
+ if request.includes.contains(ImportIncludes::COOKIES) {
+ for jar in request.cookie_jars {
+ imported_cookies.extend(
+ moli_cookie_cache::load_cookie_file(jar)
+ .with_context(|| format!("failed to import cookie jar `{}`", jar.display()))?,
+ );
+ }
+ summary.cookies = imported_cookies.len();
+ if summary.cookies > 0 {
+ let mut cookies = moli_cookie_cache::load_cookie_cache(partition.cookies_path())?;
+ cookies.extend(imported_cookies);
+ moli_cookie_cache::save_cookie_cache(partition.cookies_path(), cookies)?;
+ }
+ }
+
+ Ok(summary)
+}
+
+fn validate_request(request: &ImportRequest<'_>) -> Result<()> {
+ if request.includes.contains(ImportIncludes::INDEXED_DB) {
+ bail!(
+ "IndexedDB import is not safe yet: browser LevelDB/V8 formats are not compatible with Moli's JSON backend"
+ );
+ }
+ if !request.cookie_jars.is_empty() && !request.includes.contains(ImportIncludes::COOKIES) {
+ bail!("cookie jars can only import cookies; include `cookies` in the import selection");
+ }
+ Ok(())
+}
+
+#[cfg(test)]
+mod tests {
+ use std::fs;
+
+ use anyhow::Result;
+
+ use super::*;
+
+ #[test]
+ fn imports_a_netscape_cookie_jar() -> Result<()> {
+ let temp = tempfile::tempdir()?;
+ let jar = temp.path().join("cookies.txt");
+ let destination = temp.path().join("moli-profile");
+ fs::write(
+ &jar,
+ "# Netscape HTTP Cookie File\nexample.com\tFALSE\t/\tFALSE\t0\tsession\tvalue\n",
+ )?;
+
+ let summary = import_session_state(&ImportRequest {
+ profile_dir: &destination,
+ includes: "all".parse().expect("valid import selection"),
+ chrome_profile_dir: None,
+ chrome_crypto_key: None,
+ firefox_profile_dir: None,
+ cookie_jars: &[jar],
+ })?;
+
+ let profile = BrowserProfile::open(&destination)?;
+ let cookies =
+ moli_cookie_cache::load_cookie_cache(profile.default_partition().cookies_path())?;
+ assert_eq!(summary.cookies, 1);
+ assert_eq!(cookies.len(), 1);
+ assert_eq!(cookies[0].name, "session");
+ Ok(())
+ }
+}
diff --git a/moli-crypto/src/legacy.rs b/moli-crypto/src/legacy.rs
new file mode 100644
index 000000000..4d5e971b4
--- /dev/null
+++ b/moli-crypto/src/legacy.rs
@@ -0,0 +1,116 @@
+use std::num::NonZeroU32;
+
+use aws_lc_rs::{
+ aead::{AES_256_GCM, Aad, LessSafeKey, Nonce, UnboundKey},
+ cipher::{AES_128, DecryptionContext, PaddedBlockDecryptingKey, UnboundCipherKey},
+ iv::{FixedLength, IV_LEN_128_BIT},
+ pbkdf2::{self, PBKDF2_HMAC_SHA1},
+};
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub struct LegacyCryptoError;
+
+impl std::fmt::Display for LegacyCryptoError {
+ fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ formatter.write_str("legacy cryptographic operation failed")
+ }
+}
+
+impl std::error::Error for LegacyCryptoError {}
+
+/// Decrypts an AES-128-CBC value and removes PKCS#7 padding.
+pub fn aes_128_cbc_pkcs7_decrypt(
+ key: &[u8],
+ iv: &[u8; 16],
+ ciphertext: &[u8],
+) -> Result<Vec<u8>, LegacyCryptoError> {
+ let key = UnboundCipherKey::new(&AES_128, key).map_err(|_| LegacyCryptoError)?;
+ let key = PaddedBlockDecryptingKey::cbc_pkcs7(key).map_err(|_| LegacyCryptoError)?;
+ let context = DecryptionContext::Iv128(FixedLength::<IV_LEN_128_BIT>::from(iv));
+ let mut output = ciphertext.to_vec();
+ let plaintext = key
+ .decrypt(&mut output, context)
+ .map_err(|_| LegacyCryptoError)?;
+ Ok(plaintext.to_vec())
+}
+
+/// Authenticates and decrypts an AES-256-GCM value with a 128-bit tag.
+pub fn aes_256_gcm_decrypt(
+ key: &[u8],
+ nonce: &[u8],
+ ciphertext_and_tag: &[u8],
+) -> Result<Vec<u8>, LegacyCryptoError> {
+ let key = UnboundKey::new(&AES_256_GCM, key).map_err(|_| LegacyCryptoError)?;
+ let key = LessSafeKey::new(key);
+ let nonce = Nonce::try_assume_unique_for_key(nonce).map_err(|_| LegacyCryptoError)?;
+ let mut output = ciphertext_and_tag.to_vec();
+ let plaintext = key
+ .open_in_place(nonce, Aad::empty(), &mut output)
+ .map_err(|_| LegacyCryptoError)?;
+ Ok(plaintext.to_vec())
+}
+
+/// Derives bytes using the legacy PBKDF2-HMAC-SHA1 construction.
+pub fn derive_pbkdf2_hmac_sha1(
+ password: &[u8],
+ salt: &[u8],
+ iterations: NonZeroU32,
+ output: &mut [u8],
+) {
+ pbkdf2::derive(PBKDF2_HMAC_SHA1, iterations, salt, password, output);
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn hex(input: &str) -> Vec<u8> {
+ input
+ .as_bytes()
+ .chunks_exact(2)
+ .map(|pair| u8::from_str_radix(std::str::from_utf8(pair).unwrap(), 16).unwrap())
+ .collect()
+ }
+
+ #[test]
+ fn aes_cbc_pkcs7_matches_known_answer() {
+ let key = hex("000102030405060708090a0b0c0d0e0f");
+ let ciphertext = hex("29aee51004eb86ec78f2b81e2dd7e543");
+ assert_eq!(
+ aes_128_cbc_pkcs7_decrypt(&key, &[b' '; 16], &ciphertext).unwrap(),
+ b"hello"
+ );
+ }
+
+ #[test]
+ fn aes_256_gcm_matches_nist_known_answer() {
+ let key = [0_u8; 32];
+ let nonce = [0_u8; 12];
+ let ciphertext_and_tag = hex(concat!(
+ "cea7403d4d606b6e074ec5d3baf39d18",
+ "d0d1c8a799996bf0265b98b5d48ab919"
+ ));
+ assert_eq!(
+ aes_256_gcm_decrypt(&key, &nonce, &ciphertext_and_tag).unwrap(),
+ [0_u8; 16]
+ );
+ }
+
+ #[test]
+ fn pbkdf2_sha1_matches_rfc_6070() {
+ let mut output = [0_u8; 20];
+ derive_pbkdf2_hmac_sha1(
+ b"password",
+ b"salt",
+ NonZeroU32::new(1).unwrap(),
+ &mut output,
+ );
+ assert_eq!(
+ output,
+ [
+ 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71, 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60,
+ 0x12, 0x06, 0x2f, 0xe0, 0x37, 0xa6,
+ ]
+ );
+ }
+}
diff --git a/moli-crypto/src/lib.rs b/moli-crypto/src/lib.rs
index 959a31ee5..6d3989099 100644
--- a/moli-crypto/src/lib.rs
+++ b/moli-crypto/src/lib.rs
@@ -7,9 +7,13 @@
mod digest;
mod ed25519;
mod hkdf;
+mod legacy;
mod random;
pub use digest::{DigestAlgorithm, Sha256Context, sha1_digest, sha256_digest, sha256_hex};
pub use ed25519::{Ed25519Error, Ed25519SigningKey};
pub use hkdf::{HkdfError, derive_hkdf_bytes};
+pub use legacy::{
+ LegacyCryptoError, aes_128_cbc_pkcs7_decrypt, aes_256_gcm_decrypt, derive_pbkdf2_hmac_sha1,
+};
pub use random::fill_secure_random;
diff --git a/moli/Cargo.toml b/moli/Cargo.toml
index 6060dfc97..71a77e5a5 100644
--- a/moli/Cargo.toml
+++ b/moli/Cargo.toml
@@ -20,6 +20,7 @@ cidr = "0.3.2"
clap = { version = "4.5.39", features = ["derive", "env"] }
moli-browser-profile = { path = "../moli-browser-profile" }
moli-cookie-cache = { path = "../moli-cookie-cache" }
+moli-cookie-import = { path = "../moli-cookie-import" }
moli-protocol = { path = "../moli-protocol" }
moli-cookie-jar = { path = "../moli-cookie-jar" }
moli-fetch = { path = "../moli-fetch" }
diff --git a/moli/src/app.rs b/moli/src/app.rs
index 8c0bff730..60c91604b 100644
--- a/moli/src/app.rs
+++ b/moli/src/app.rs
@@ -170,6 +170,19 @@ pub async fn run_cli_with_config<W: Write>(
);
server.serve().await.context("protocol server failed")?;
}
+ Commands::Import(args) => {
+ let summary =
+ moli_cookie_import::import_session_state(&moli_cookie_import::ImportRequest {
+ profile_dir: &args.profile_dir,
+ includes: args.includes,
+ chrome_profile_dir: args.chrome_profile_dir.as_deref(),
+ chrome_crypto_key: args.chrome_crypto_key.as_ref(),
+ firefox_profile_dir: args.firefox_profile_dir.as_deref(),
+ cookie_jars: &args.cookie_jar,
+ })?;
+ writeln!(stdout, "Imported session state: {summary}")
+ .context("failed to write import summary")?;
+ }
}
Ok(())
diff --git a/moli/src/cli.rs b/moli/src/cli.rs
index b1a6641f8..92483f7d7 100644
--- a/moli/src/cli.rs
+++ b/moli/src/cli.rs
@@ -1,6 +1,7 @@
use std::{ffi::OsString, num::NonZeroU32, path::PathBuf, str::FromStr};
-use clap::{Args, Parser, Subcommand, ValueEnum};
+use clap::{ArgGroup, Args, Parser, Subcommand, ValueEnum};
+pub use moli_cookie_import::{ChromeCryptoKey, ImportIncludes};
use regex::Regex;
const DUMP_MODES: &[&str] = &[
@@ -77,6 +78,41 @@ pub struct Cli {
pub enum Commands {
Fetch(Box<FetchArgs>),
Serve(Box<ServeArgs>),
+ /// Import browser session state into a Moli profile.
+ Import(Box<ImportArgs>),
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Args)]
+#[command(group(
+ ArgGroup::new("import_sources")
+ .required(true)
+ .multiple(true)
+ .args(["chrome_profile_dir", "firefox_profile_dir", "cookie_jar"])
+))]
+pub struct ImportArgs {
+ /// Destination Moli profile directory.
+ #[arg(short = 'P', long, value_name = "PATH")]
+ pub profile_dir: PathBuf,
+
+ /// Comma-separated state to import: cookies,storage,indexeddb, or all.
+ #[arg(long, value_name = "KINDS", default_value = "all")]
+ pub includes: ImportIncludes,
+
+ /// Chrome profile directory (for example, `.../Chrome/Default`).
+ #[arg(long, value_name = "PATH")]
+ pub chrome_profile_dir: Option<PathBuf>,
+
+ /// Chrome cookie key source: system, or base64:<raw-key>.
+ #[arg(long, value_name = "SOURCE", requires = "chrome_profile_dir")]
+ pub chrome_crypto_key: Option<ChromeCryptoKey>,
+
+ /// Firefox profile directory (for example, `.../Profiles/name.default-release`).
+ #[arg(long, value_name = "PATH")]
+ pub firefox_profile_dir: Option<PathBuf>,
+
+ /// Netscape/curl cookie-jar file. May be specified more than once.
+ #[arg(long, value_name = "PATH")]
+ pub cookie_jar: Vec<PathBuf>,
}
#[derive(Debug, Clone, PartialEq, Eq, Args)]
@@ -816,6 +852,7 @@ mod tests {
let common = match cli.command {
Commands::Fetch(args) => args.common,
Commands::Serve(args) => args.common,
+ Commands::Import(_) => unreachable!("test only parses fetch and serve"),
};
assert_eq!(common.ca_cert, Some(PathBuf::from("ca.pem")));
let certificate = common.client_cert.expect("client certificate");
@@ -825,6 +862,51 @@ mod tests {
}
}
+ #[test]
+ fn parses_import_sources_includes_and_explicit_key() {
+ let cli = Cli::parse_from([
+ "moli",
+ "import",
+ "--chrome-profile-dir",
+ "/tmp/chrome/Default",
+ "--profile-dir",
+ "/tmp/moli",
+ "--includes",
+ "cookies,storage",
+ "--chrome-crypto-key",
+ "base64:YWJjZA==",
+ ]);
+ let Commands::Import(args) = cli.command else {
+ panic!("expected import command");
+ };
+ assert!(args.includes.contains(ImportIncludes::COOKIES));
+ assert!(args.includes.contains(ImportIncludes::STORAGE));
+ assert!(!args.includes.contains(ImportIncludes::INDEXED_DB));
+ assert_eq!(
+ args.chrome_crypto_key,
+ Some(ChromeCryptoKey::Base64("YWJjZA==".into()))
+ );
+ }
+
+ #[test]
+ fn import_defaults_to_all_and_requires_a_source() {
+ let cli = Cli::parse_from([
+ "moli",
+ "import",
+ "--profile-dir",
+ "/tmp/moli",
+ "--cookie-jar",
+ "/tmp/cookies.txt",
+ ]);
+ let Commands::Import(args) = cli.command else {
+ panic!("expected import command");
+ };
+ assert!(args.includes.contains(ImportIncludes::COOKIES));
+ assert!(args.includes.contains(ImportIncludes::STORAGE));
+
+ assert!(Cli::try_parse_from(["moli", "import", "--profile-dir", "/tmp/moli"]).is_err());
+ }
+
#[test]
fn client_certificate_can_contain_its_key_but_key_requires_certificate() {
let combined = Cli::try_parse_from([
diff --git a/moli/src/config.rs b/moli/src/config.rs
index 3f7b36e39..e9e6f659d 100644
--- a/moli/src/config.rs
+++ b/moli/src/config.rs
@@ -64,6 +64,7 @@ impl AppConfig {
config.server.port = args.port;
config.server.timeout_secs = args.timeout;
}
+ Commands::Import(_) => {}
}
Ok(config)