Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Add Auth export and decryption to the Rust CLI

Manav Rathi82e14ac43592
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index 24a2fde91c..f61e08a103 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -2482,6 +2482,19 @@ dependencies = [
"tokio",
]
+[[package]]
+name = "ente-auth"
+version = "0.0.0"
+dependencies = [
+ "ente-core",
+ "serde",
+ "serde_json",
+ "thiserror 2.0.21",
+ "url",
+ "uuid",
+ "zeroize",
+]
+
[[package]]
name = "ente-auth-wasm"
version = "0.0.0"
@@ -2533,6 +2546,7 @@ dependencies = [
"dialoguer",
"dirs",
"ente-accounts",
+ "ente-auth",
"ente-collections",
"ente-core",
"ente-photos",
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index 3067ae73d3..a6e6d3e373 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -24,6 +24,7 @@ members = [
"bindings/wasm/space",
"crates/accounts",
"crates/assets",
+ "crates/auth",
"crates/cast",
"crates/collections",
"crates/contacts",
@@ -59,6 +60,7 @@ dirs = "7.0.0"
encoding_rs = "0.8.35"
ente-accounts = { path = "crates/accounts" }
ente-assets = { path = "crates/assets" }
+ente-auth = { path = "crates/auth" }
ente-cast = { path = "crates/cast" }
ente-collections = { path = "crates/collections" }
ente-contacts = { path = "crates/contacts" }
diff --git a/rust/apps/cli-next/Cargo.toml b/rust/apps/cli-next/Cargo.toml
index bb2219360f..4867a4920a 100644
--- a/rust/apps/cli-next/Cargo.toml
+++ b/rust/apps/cli-next/Cargo.toml
@@ -9,11 +9,12 @@ museum = []
[dependencies]
anyhow.workspace = true
-chrono = { workspace = true, features = ["std"] }
+chrono = { workspace = true, features = ["std", "clock"] }
clap = { workspace = true, features = ["derive"] }
dialoguer = { workspace = true, features = ["password"] }
dirs.workspace = true
ente-accounts.workspace = true
+ente-auth.workspace = true
ente-collections.workspace = true
ente-core.workspace = true
ente-photos.workspace = true
diff --git a/rust/apps/cli-next/src/args.rs b/rust/apps/cli-next/src/args.rs
index 0bc782bafb..5219ef60ef 100644
--- a/rust/apps/cli-next/src/args.rs
+++ b/rust/apps/cli-next/src/args.rs
@@ -77,7 +77,7 @@ pub enum Command {
#[command(flatten)]
selector: AccountSelector,
#[command(subcommand)]
- command: SessionCommand,
+ command: AuthCommand,
},
#[command(about = "Manage accounts on this device", alias = "account")]
Accounts {
@@ -98,6 +98,50 @@ Keep the same home and key across unattended invocations."
},
}
+#[derive(Subcommand)]
+pub enum AuthCommand {
+ #[command(flatten)]
+ Session(SessionCommand),
+ #[command(about = "Export your Auth codes; encrypted by default")]
+ Export(AuthExportArgs),
+ #[command(about = "Decrypt an Auth export without an account")]
+ Decrypt {
+ #[arg(value_name = "PATH")]
+ input: PathBuf,
+ #[arg(
+ long,
+ value_name = "PATH|-",
+ help = "Write to a new file; defaults to stdout"
+ )]
+ output: Option<PathBuf>,
+ },
+}
+
+#[derive(Args)]
+pub struct AuthExportArgs {
+ #[arg(
+ value_name = "DIR",
+ required_unless_present = "output",
+ conflicts_with = "output"
+ )]
+ pub directory: Option<PathBuf>,
+ #[arg(
+ long,
+ value_name = "PATH|-",
+ help = "Write to a new file, or stdout with -"
+ )]
+ pub output: Option<PathBuf>,
+ #[arg(long, help = "Export readable URI lines without encryption")]
+ pub plaintext: bool,
+ #[arg(
+ long,
+ value_name = "N",
+ requires = "directory",
+ help = "Keep the N newest backups in this format"
+ )]
+ pub keep: Option<NonZeroUsize>,
+}
+
#[derive(Subcommand)]
pub enum PhotosCommand {
#[command(flatten)]
diff --git a/rust/apps/cli-next/src/auth/mod.rs b/rust/apps/cli-next/src/auth/mod.rs
new file mode 100644
index 0000000000..9e8cc44208
--- /dev/null
+++ b/rust/apps/cli-next/src/auth/mod.rs
@@ -0,0 +1,148 @@
+mod output;
+pub mod replica;
+
+use std::{
+ env, fs,
+ io::{self, IsTerminal},
+ path::PathBuf,
+};
+
+use anyhow::{Context, Result, bail, ensure};
+use dialoguer::{Password, console::Term};
+use ente_auth::backup;
+use serde_json::json;
+use zeroize::Zeroizing;
+
+use crate::{
+ api,
+ args::{AuthCommand, AuthExportArgs, Options, Product},
+ db, home,
+};
+use output::Destination;
+
+pub async fn run(command: AuthCommand, selected: Option<&str>, options: &Options) -> Result<()> {
+ match command {
+ AuthCommand::Session(command) => {
+ crate::session(Product::Auth, command, selected, options).await
+ }
+ AuthCommand::Export(args) => export(args, selected, options).await,
+ AuthCommand::Decrypt { input, output } => {
+ let destination = Destination::file(output.unwrap_or_else(|| PathBuf::from("-")));
+ destination.prepare()?;
+ let password = export_password(false)?;
+ let plaintext = backup::decrypt(&fs::read(input)?, &password)?;
+ let published = destination.publish(&plaintext)?;
+ if let Some(path) = published.path {
+ crate::output::action(
+ options.json,
+ &json!({"output": path.to_string_lossy()}),
+ &format!("Decrypted to {}.", path.display()),
+ )?;
+ }
+ Ok(())
+ }
+ }
+}
+
+async fn export(args: AuthExportArgs, selected: Option<&str>, options: &Options) -> Result<()> {
+ let destination = if let Some(path) = args.directory {
+ Destination::Directory {
+ path,
+ extension: if args.plaintext { "txt" } else { "json" },
+ keep: args.keep,
+ }
+ } else {
+ Destination::file(
+ args.output
+ .context("export requires DIR or --output PATH|-")?,
+ )
+ };
+ destination.prepare()?;
+ let password = if args.plaintext {
+ None
+ } else {
+ Some(export_password(true)?)
+ };
+ let payload = {
+ let (account, home) = home::open_account(selected, !options.offline)?;
+ let session = api::session(&account, Product::Auth)?;
+ let mut db = db::open(&home.path, &account.db_key, !options.offline)?;
+ if !options.offline {
+ replica::refresh(&mut db, &session).await?;
+ }
+ replica::read_payload(&db, &session)?
+ };
+ if payload.failed > 0 {
+ let plural = if payload.failed == 1 { "" } else { "s" };
+ if !matches!(destination, Destination::Stdout) {
+ crate::output::action(
+ options.json,
+ &json!({"output": null, "records": 0, "pruned": 0, "failed": payload.failed}),
+ &format!(
+ "{} unreadable Auth record{plural}; no new backup was written.",
+ payload.failed
+ ),
+ )?;
+ }
+ bail!(
+ "{} unreadable Auth record{plural}; no new backup was written (record ID{plural}: {})",
+ payload.failed,
+ payload.failed_ids.join(", ")
+ );
+ }
+ let artifact = match password {
+ Some(password) => Zeroizing::new(backup::encrypt(&payload.bytes, &password)?),
+ None => payload.bytes,
+ };
+ let published = destination.publish(&artifact)?;
+ if let Some(path) = published.path {
+ let mut summary = format!(
+ "Exported {} code{} to {}.",
+ payload.records,
+ if payload.records == 1 { "" } else { "s" },
+ path.display(),
+ );
+ if published.pruned > 0 {
+ summary.push_str(&format!(
+ " Pruned {} backup{}.",
+ published.pruned,
+ if published.pruned == 1 { "" } else { "s" },
+ ));
+ }
+ crate::output::action(
+ options.json,
+ &json!({"output": path.to_string_lossy(), "records": payload.records, "pruned": published.pruned, "failed": 0}),
+ &summary,
+ )?;
+ }
+ if let Some(error) = published.prune_error {
+ return Err(error);
+ }
+ Ok(())
+}
+
+fn export_password(confirm: bool) -> Result<Zeroizing<String>> {
+ let password = match env::var("ENTE_CLI_EXPORT_PASSWORD") {
+ Ok(password) => Zeroizing::new(password),
+ Err(env::VarError::NotUnicode(_)) => bail!("ENTE_CLI_EXPORT_PASSWORD must be UTF-8"),
+ Err(env::VarError::NotPresent) => {
+ ensure!(
+ io::stdin().is_terminal() && io::stderr().is_terminal(),
+ "set ENTE_CLI_EXPORT_PASSWORD for noninteractive encrypted export or decryption"
+ );
+ let mut prompt = Password::new()
+ .with_prompt("Export password")
+ .allow_empty_password(true);
+ if confirm {
+ prompt =
+ prompt.with_confirmation("Confirm export password", "Passwords do not match");
+ }
+ Zeroizing::new(prompt.interact_on(&Term::stderr())?)
+ }
+ };
+ ensure!(
+ !password.is_empty(),
+ "export password cannot be empty; set ENTE_CLI_EXPORT_PASSWORD to a nonempty value"
+ );
+ Ok(password)
+}
diff --git a/rust/apps/cli-next/src/auth/output.rs b/rust/apps/cli-next/src/auth/output.rs
new file mode 100644
index 0000000000..6fdb661c65
--- /dev/null
+++ b/rust/apps/cli-next/src/auth/output.rs
@@ -0,0 +1,333 @@
+use std::{
+ fs,
+ io::{self, Write},
+ num::NonZeroUsize,
+ path::{Path, PathBuf},
+};
+
+use anyhow::{Context, Result, ensure};
+use chrono::{Local, NaiveDate};
+
+pub enum Destination {
+ Stdout,
+ File(PathBuf),
+ Directory {
+ path: PathBuf,
+ extension: &'static str,
+ keep: Option<NonZeroUsize>,
+ },
+}
+
+pub struct Published {
+ pub path: Option<PathBuf>,
+ pub pruned: usize,
+ pub prune_error: Option<anyhow::Error>,
+}
+
+impl Destination {
+ pub fn file(path: PathBuf) -> Self {
+ if path == Path::new("-") {
+ Self::Stdout
+ } else {
+ Self::File(path)
+ }
+ }
+
+ pub fn prepare(&self) -> Result<()> {
+ match self {
+ Self::Stdout => Ok(()),
+ Self::File(path) => {
+ match fs::symlink_metadata(path) {
+ Ok(_) => anyhow::bail!("output already exists: {}", path.display()),
+ Err(error) if error.kind() == io::ErrorKind::NotFound => {}
+ Err(error) => return Err(error.into()),
+ }
+ ensure!(
+ parent(path).is_dir(),
+ "output parent directory does not exist: {}",
+ parent(path).display()
+ );
+ ensure!(path.file_name().is_some(), "output requires a filename");
+ Ok(())
+ }
+ Self::Directory { path, .. } => {
+ let mut builder = fs::DirBuilder::new();
+ builder.recursive(true);
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::DirBuilderExt;
+ builder.mode(0o700);
+ }
+ builder
+ .create(path)
+ .with_context(|| format!("cannot create export directory {}", path.display()))
+ }
+ }
+ }
+
+ pub fn publish(&self, bytes: &[u8]) -> Result<Published> {
+ match self {
+ Self::Stdout => {
+ let mut stdout = io::stdout().lock();
+ stdout.write_all(bytes)?;
+ stdout.flush()?;
+ Ok(Published {
+ path: None,
+ pruned: 0,
+ prune_error: None,
+ })
+ }
+ Self::File(path) => {
+ stage(parent(path), bytes)?
+ .persist_noclobber(path)
+ .map_err(|error| error.error)
+ .with_context(|| format!("cannot publish {}", path.display()))?;
+ Ok(Published {
+ path: Some(path.clone()),
+ pruned: 0,
+ prune_error: None,
+ })
+ }
+ Self::Directory {
+ path,
+ extension,
+ keep,
+ } => publish_directory(path, extension, *keep, bytes, Local::now().date_naive()),
+ }
+ }
+}
+
+fn parent(path: &Path) -> &Path {
+ path.parent()
+ .filter(|path| !path.as_os_str().is_empty())
+ .unwrap_or(Path::new("."))
+}
+
+fn stage(directory: &Path, bytes: &[u8]) -> Result<tempfile::NamedTempFile> {
+ let mut temporary = tempfile::NamedTempFile::new_in(directory)?;
+ temporary.write_all(bytes)?;
+ temporary.as_file().sync_all()?;
+ Ok(temporary)
+}
+
+fn backup_name(name: &str, extension: &str) -> Option<(NaiveDate, u64)> {
+ let stem = name
+ .strip_prefix("ente-auth-")?
+ .strip_suffix(&format!(".{extension}"))?;
+ let date_text = stem.get(..10)?;
+ let date = NaiveDate::parse_from_str(date_text, "%Y-%m-%d").ok()?;
+ if date.format("%Y-%m-%d").to_string() != date_text {
+ return None;
+ }
+ let suffix = stem.get(10..)?;
+ let sequence = if suffix.is_empty() {
+ 1
+ } else {
+ let digits = suffix.strip_prefix('-')?;
+ let sequence: u64 = digits.parse().ok()?;
+ if sequence < 2 || sequence.to_string() != digits {
+ return None;
+ }
+ sequence
+ };
+ Some((date, sequence))
+}
+
+fn publish_directory(
+ directory: &Path,
+ extension: &str,
+ keep: Option<NonZeroUsize>,
+ bytes: &[u8],
+ today: NaiveDate,
+) -> Result<Published> {
+ let mut candidates = Vec::new();
+ let mut sequence = 0;
+ for entry in fs::read_dir(directory)? {
+ let entry = entry?;
+ if !entry.file_type()?.is_file() {
+ continue;
+ }
+ let name = entry.file_name();
+ let Some((date, number)) = name.to_str().and_then(|name| backup_name(name, extension))
+ else {
+ continue;
+ };
+ if date == today {
+ sequence = sequence.max(number);
+ }
+ candidates.push(((date, number), entry.path()));
+ }
+ let mut temporary = stage(directory, bytes)?;
+ let path = loop {
+ sequence = sequence
+ .checked_add(1)
+ .context("Auth backup filename suffix exhausted")?;
+ let suffix = if sequence == 1 {
+ String::new()
+ } else {
+ format!("-{sequence}")
+ };
+ let path = directory.join(format!("ente-auth-{today}{suffix}.{extension}"));
+ match temporary.persist_noclobber(&path) {
+ Ok(_) => break path,
+ Err(error) if error.error.kind() == io::ErrorKind::AlreadyExists => {
+ temporary = error.file
+ }
+ Err(error) => return Err(error.error).context("cannot publish Auth backup"),
+ }
+ };
+ #[cfg(unix)]
+ fs::File::open(directory)
+ .and_then(|directory| directory.sync_all())
+ .context("backup published, but cannot sync its directory; older backups were kept")?;
+ let mut result = Published {
+ path: Some(path),
+ pruned: 0,
+ prune_error: None,
+ };
+ if let Some(keep) = keep {
+ candidates.sort_unstable_by_key(|candidate| std::cmp::Reverse(candidate.0));
+ prune(
+ candidates
+ .into_iter()
+ .skip(keep.get() - 1)
+ .map(|(_, path)| path),
+ &mut result,
+ );
+ }
+ Ok(result)
+}
+
+fn prune(paths: impl Iterator<Item = PathBuf>, result: &mut Published) {
+ for path in paths {
+ match fs::remove_file(&path) {
+ Ok(()) => result.pruned += 1,
+ Err(error) if error.kind() == io::ErrorKind::NotFound => {}
+ Err(error) => {
+ result.prune_error = Some(
+ anyhow::Error::new(error)
+ .context(format!("backup saved, but cannot prune {}", path.display())),
+ );
+ break;
+ }
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn publication_allocates_dates_and_prunes_only_its_format() {
+ let directory = tempfile::tempdir().unwrap();
+ let today = NaiveDate::from_ymd_opt(2026, 10, 8).unwrap();
+ let publish = |day, extension, keep| {
+ publish_directory(
+ directory.path(),
+ extension,
+ NonZeroUsize::new(keep),
+ b"complete",
+ day,
+ )
+ .unwrap()
+ };
+ for suffix in ["", "-2", "-3"] {
+ let result = publish(today, "json", 0);
+ assert_eq!(
+ result.path.unwrap().file_name().unwrap(),
+ format!("ente-auth-2026-10-08{suffix}.json").as_str()
+ );
+ assert_eq!(result.pruned, 0);
+ }
+ for name in [
+ "unrelated.json",
+ "ente-auth-2026-10-08-01.json",
+ "ente-auth-2026-10-08-1.json",
+ "ente-auth-2026-02-30.json",
+ ] {
+ fs::write(directory.path().join(name), b"untouched").unwrap();
+ }
+ let text = publish(today, "txt", 1).path.unwrap();
+ assert_eq!(fs::read_dir(directory.path()).unwrap().count(), 8);
+ let next_day = today.succ_opt().unwrap();
+ let result = publish(next_day, "json", 2);
+ assert_eq!(result.pruned, 2);
+ assert_eq!(
+ result.path.unwrap().file_name().unwrap(),
+ "ente-auth-2026-10-09.json"
+ );
+ assert!(
+ directory
+ .path()
+ .join("ente-auth-2026-10-08-3.json")
+ .exists()
+ );
+ assert!(text.exists());
+ let result = publish(today, "txt", 1);
+ assert_eq!(result.pruned, 1);
+ assert!(!text.exists());
+ assert!(directory.path().join("ente-auth-2026-10-09.json").exists());
+ let earlier_day = NaiveDate::from_ymd_opt(2026, 1, 1).unwrap();
+ let result = publish(earlier_day, "json", 1);
+ assert_eq!(result.pruned, 2);
+ assert!(result.path.unwrap().exists());
+ for name in [
+ "unrelated.json",
+ "ente-auth-2026-10-08-01.json",
+ "ente-auth-2026-10-08-1.json",
+ "ente-auth-2026-02-30.json",
+ ] {
+ assert_eq!(fs::read(directory.path().join(name)).unwrap(), b"untouched");
+ }
+ }
+
+ #[test]
+ fn exact_publication_refuses_a_path_created_after_preflight() {
+ let directory = tempfile::tempdir().unwrap();
+ let path = directory.path().join("output");
+ let destination = Destination::File(path.clone());
+ destination.prepare().unwrap();
+ fs::write(&path, b"concurrent writer").unwrap();
+ assert!(destination.publish(b"replacement").is_err());
+ assert_eq!(fs::read(&path).unwrap(), b"concurrent writer");
+ #[cfg(unix)]
+ {
+ let link = directory.path().join("symlink");
+ std::os::unix::fs::symlink(directory.path().join("missing"), &link).unwrap();
+ assert!(Destination::File(link).prepare().is_err());
+ }
+ fs::create_dir(directory.path().join("ente-auth-2026-10-08.json")).unwrap();
+ let result = publish_directory(
+ directory.path(),
+ "json",
+ None,
+ b"new",
+ NaiveDate::from_ymd_opt(2026, 10, 8).unwrap(),
+ )
+ .unwrap();
+ assert_eq!(
+ result.path.unwrap().file_name().unwrap(),
+ "ente-auth-2026-10-08-2.json"
+ );
+ }
+
+ #[test]
+ fn write_and_prune_failures_preserve_complete_backups() {
+ let directory = tempfile::tempdir().unwrap();
+ let old = directory.path().join("old");
+ fs::write(&old, b"old").unwrap();
+ let missing = Destination::File(directory.path().join("missing/new"));
+ assert!(missing.publish(b"new").is_err());
+ assert_eq!(fs::read(&old).unwrap(), b"old");
+ let new = directory.path().join("new");
+ let mut result = Destination::File(new.clone()).publish(b"new").unwrap();
+ let replaced_candidate = directory.path().join("replaced");
+ fs::create_dir(&replaced_candidate).unwrap();
+ prune([old.clone(), replaced_candidate].into_iter(), &mut result);
+ assert_eq!(result.pruned, 1);
+ assert!(result.prune_error.is_some());
+ assert_eq!(fs::read(new).unwrap(), b"new");
+ assert!(!old.exists());
+ }
+}
diff --git a/rust/apps/cli-next/src/auth/replica.rs b/rust/apps/cli-next/src/auth/replica.rs
new file mode 100644
index 0000000000..f5f7568499
--- /dev/null
+++ b/rust/apps/cli-next/src/auth/replica.rs
@@ -0,0 +1,162 @@
+use anyhow::{Context, Result, ensure};
+use ente_auth::{WrappedKey, record};
+use ente_core::Session;
+use zeroize::Zeroizing;
+
+use crate::core_db::{Db, OptionalExtension, params};
+
+pub const SCHEMA: &str = "
+ CREATE TABLE auth_sync (
+ id INTEGER PRIMARY KEY CHECK(id=1), encrypted_key TEXT, key_header TEXT,
+ cursor INTEGER NOT NULL, complete INTEGER NOT NULL
+ );
+ CREATE TABLE auth_entities (
+ id TEXT PRIMARY KEY, updated_at INTEGER NOT NULL, encrypted_data TEXT, header TEXT
+ );
+";
+
+struct Sync {
+ key: Option<WrappedKey>,
+ cursor: i64,
+ complete: bool,
+}
+
+pub struct Payload {
+ pub bytes: Zeroizing<Vec<u8>>,
+ pub records: usize,
+ pub failed: usize,
+ pub failed_ids: Vec<String>,
+}
+
+fn sync_state(db: &Db) -> Result<Option<Sync>> {
+ Ok(db.read(|db| {
+ Ok(db
+ .query_row(
+ "SELECT encrypted_key, key_header, cursor, complete FROM auth_sync WHERE id=1",
+ [],
+ |row| {
+ let encrypted_key: Option<String> = row.get(0)?;
+ let key = encrypted_key
+ .map(|encrypted_key| {
+ Ok::<_, crate::core_db::SqliteError>(WrappedKey {
+ encrypted_key,
+ header: row.get(1)?,
+ })
+ })
+ .transpose()?;
+ Ok(Sync {
+ key,
+ cursor: row.get(2)?,
+ complete: row.get(3)?,
+ })
+ },
+ )
+ .optional()?)
+ })?)
+}
+
+pub async fn refresh(db: &mut Db, session: &Session) -> Result<()> {
+ let key = ente_auth::fetch_key(session).await?;
+ let saved = sync_state(db)?;
+ let reset = saved.as_ref().is_none_or(|saved| saved.key != key);
+ let mut cursor = saved.as_ref().map_or(0, |saved| saved.cursor);
+ if reset {
+ db.write(|tx| {
+ tx.execute("DELETE FROM auth_entities", [])?;
+ tx.execute(
+ "INSERT INTO auth_sync(id, encrypted_key, key_header, cursor, complete)
+ VALUES(1, ?1, ?2, 0, ?3)
+ ON CONFLICT(id) DO UPDATE SET encrypted_key=excluded.encrypted_key,
+ key_header=excluded.key_header, cursor=0, complete=excluded.complete",
+ params![
+ key.as_ref().map(|key| &key.encrypted_key),
+ key.as_ref().map(|key| &key.header),
+ key.is_none()
+ ],
+ )?;
+ Ok(())
+ })?;
+ cursor = 0;
+ }
+ if key.is_none() {
+ return Ok(());
+ }
+ loop {
+ let page = ente_auth::diff(session, cursor).await?;
+ let complete = page.len() < ente_auth::PAGE_SIZE;
+ let next = page
+ .last()
+ .map_or(cursor, |entity| entity.updated_at.saturating_sub(1).max(0));
+ db.write(|tx| {
+ let mut upsert = tx.prepare_cached(
+ "INSERT INTO auth_entities(id, updated_at, encrypted_data, header) VALUES(?1, ?2, ?3, ?4)
+ ON CONFLICT(id) DO UPDATE SET updated_at=excluded.updated_at,
+ encrypted_data=excluded.encrypted_data, header=excluded.header",
+ )?;
+ let mut delete = tx.prepare_cached("DELETE FROM auth_entities WHERE id=?1")?;
+ for entity in &page {
+ let id = entity.id.to_string();
+ if entity.is_deleted {
+ delete.execute([&id])?;
+ } else {
+ upsert.execute(params![id, entity.updated_at, entity.encrypted_data, entity.header])?;
+ }
+ }
+ tx.execute(
+ "UPDATE auth_sync SET cursor=?1, complete=?2 WHERE id=1",
+ params![next, complete],
+ )?;
+ Ok(())
+ })?;
+ if complete {
+ return Ok(());
+ }
+ ensure!(
+ next > cursor,
+ "Auth diff timestamp boundary did not advance; no new backup was written"
+ );
+ cursor = next;
+ }
+}
+
+pub fn read_payload(db: &Db, session: &Session) -> Result<Payload> {
+ let state = sync_state(db)?.context("no local Auth data; run export online first")?;
+ ensure!(
+ state.complete,
+ "Auth refresh is incomplete; run export online first"
+ );
+ let mut payload = Payload {
+ bytes: Zeroizing::new(Vec::new()),
+ records: 0,
+ failed: 0,
+ failed_ids: Vec::new(),
+ };
+ let Some(wrapped) = state.key else {
+ return Ok(payload);
+ };
+ let key = ente_auth::open_key(&wrapped, &session.master_key)?;
+ db.read(|db| {
+ let mut statement =
+ db.prepare("SELECT id, encrypted_data, header FROM auth_entities ORDER BY id")?;
+ let mut rows = statement.query([])?;
+ while let Some(row) = rows.next()? {
+ let id: String = row.get(0)?;
+ let encrypted_data: Option<String> = row.get(1)?;
+ let header: Option<String> = row.get(2)?;
+ match record::uri_line(encrypted_data.as_deref(), header.as_deref(), &key) {
+ Ok(line) => {
+ payload.bytes.extend_from_slice(line.as_bytes());
+ payload.records += 1;
+ }
+ Err(_) => {
+ payload.failed += 1;
+ if payload.failed_ids.len() < 10 {
+ payload.failed_ids.push(id);
+ }
+ }
+ }
+ }
+ Ok(())
+ })?;
+ Ok(payload)
+}
diff --git a/rust/apps/cli-next/src/db.rs b/rust/apps/cli-next/src/db.rs
index e754fa3da2..18e6abba8e 100644
--- a/rust/apps/cli-next/src/db.rs
+++ b/rust/apps/cli-next/src/db.rs
@@ -4,9 +4,9 @@ use anyhow::{Context, Result};
use rusqlite::{Connection, OpenFlags};
use zeroize::Zeroizing;
-use crate::{core_db::Db, replica, vault::DbKey};
+use crate::{auth, core_db::Db, replica, vault::DbKey};
-const MIGRATIONS: &[&str] = &[replica::SCHEMA];
+const MIGRATIONS: &[&str] = &[replica::SCHEMA, auth::replica::SCHEMA];
pub fn open(account_dir: &Path, key: &DbKey, create: bool) -> Result<Db> {
let connection = connect(&account_dir.join("data.db"), key, create)?;
diff --git a/rust/apps/cli-next/src/home.rs b/rust/apps/cli-next/src/home.rs
index 3c267dcf5a..3f05cf9ec3 100644
--- a/rust/apps/cli-next/src/home.rs
+++ b/rust/apps/cli-next/src/home.rs
@@ -8,6 +8,8 @@ use std::{
use anyhow::{Context, Result, bail, ensure};
use uuid::Uuid;
+use crate::vault::{Account, State};
+
pub struct AccountHome {
pub path: PathBuf,
_lock: File,
@@ -18,6 +20,24 @@ pub struct RemovalGuard {
home: AccountHome,
}
+pub fn open_account(selected: Option<&str>, create: bool) -> Result<(Account, AccountHome)> {
+ let state = State::load()?;
+ let id = state.accounts[state.resolve(selected)?].storage_id;
+ let home = lock_account(id, create)?;
+ let Some(account) = State::load()?
+ .accounts
+ .into_iter()
+ .find(|account| account.storage_id == id)
+ else {
+ home.for_removal()?.remove()?;
+ bail!("account was removed while waiting for access");
+ };
+ if create {
+ self::create(&home.path)?;
+ }
+ Ok((account, home))
+}
+
pub fn application_home() -> Result<PathBuf> {
if let Some(home) = env::var_os("ENTE_CLI_HOME") {
ensure!(!home.is_empty(), "ENTE_CLI_HOME cannot be empty");
diff --git a/rust/apps/cli-next/src/main.rs b/rust/apps/cli-next/src/main.rs
index e333942cc0..e8d77d5df6 100644
--- a/rust/apps/cli-next/src/main.rs
+++ b/rust/apps/cli-next/src/main.rs
@@ -1,5 +1,6 @@
mod api;
mod args;
+mod auth;
mod core_db;
mod db;
mod export;
@@ -66,13 +67,7 @@ async fn run(cli: Cli) -> Result<()> {
.await
}
Command::Auth { selector, command } => {
- session(
- Product::Auth,
- command,
- selector.account.as_deref(),
- &options,
- )
- .await
+ auth::run(command, selector.account.as_deref(), &options).await
}
Command::Accounts { command } => account_command(command, &options).await,
Command::Vault {
diff --git a/rust/apps/cli-next/src/photos.rs b/rust/apps/cli-next/src/photos.rs
index 30c20407c7..2f8b7b64a9 100644
--- a/rust/apps/cli-next/src/photos.rs
+++ b/rust/apps/cli-next/src/photos.rs
@@ -11,7 +11,6 @@ use crate::{
db, home,
output::{self, AlbumView, FileView},
replica::Replica,
- vault::{Account, State},
};
const MAX_CANDIDATES: usize = 10;
@@ -35,7 +34,7 @@ pub async fn run(
),
"original files are not stored locally; download requires network access"
);
- let (account, home) = open_account(selected, !options.offline)?;
+ let (account, home) = home::open_account(selected, !options.offline)?;
let session = api::session(&account, Product::Photos)?;
let mut db = db::open(&home.path, &account.db_key, !options.offline)?;
let mut replica = Replica::new(&mut db, session.user_id);
@@ -223,24 +222,6 @@ async fn file(
Ok(())
}
-fn open_account(selected: Option<&str>, create: bool) -> Result<(Account, home::AccountHome)> {
- let state = State::load()?;
- let id = state.accounts[state.resolve(selected)?].storage_id;
- let home = home::lock_account(id, create)?;
- let Some(account) = State::load()?
- .accounts
- .into_iter()
- .find(|account| account.storage_id == id)
- else {
- home.for_removal()?.remove()?;
- bail!("account was removed while waiting for access");
- };
- if create {
- home::create(&home.path)?;
- }
- Ok((account, home))
-}
-
fn select<T>(
items: impl Iterator<Item = Result<T>>,
selector: &str,
diff --git a/rust/apps/cli-next/tests/cli.rs b/rust/apps/cli-next/tests/cli.rs
index 4a526708fa..00495d611f 100644
--- a/rust/apps/cli-next/tests/cli.rs
+++ b/rust/apps/cli-next/tests/cli.rs
@@ -744,6 +744,9 @@ fn failure(output: &Output) -> String {
#[path = "support/files.rs"]
mod files;
+#[path = "support/auth.rs"]
+mod auth;
+
#[cfg(feature = "museum")]
#[path = "support/museum.rs"]
mod museum;
diff --git a/rust/apps/cli-next/tests/support/auth.rs b/rust/apps/cli-next/tests/support/auth.rs
new file mode 100644
index 0000000000..457aec3c26
--- /dev/null
+++ b/rust/apps/cli-next/tests/support/auth.rs
@@ -0,0 +1,447 @@
+use ente_core::crypto::secretbox;
+use mockito::{Matcher, Mock, Server};
+use uuid::Uuid;
+
+use super::*;
+
+const URI: &str = "otpauth://totp/Test:account?secret=JBSWY3DPEHPK3PXP&x=%252C&codeDisplay=%7B%22trashed%22%3Atrue%7D";
+const EXPORT: &[&str] = &["auth", "export", "--plaintext", "--output", "-", "--json"];
+
+fn seed(home: &TestHome, origin: &str) {
+ home.seed(origin);
+ let mut state = home.read_vault();
+ state["accounts"][0]["sessions"]["auth"] = state["accounts"][0]["sessions"]["photos"].clone();
+ home.write_vault(&state);
+}
+
+fn key_response(server: &mut Server, key: &Key) -> Mock {
+ let wrapped = secretbox::encrypt(key.as_bytes(), &Key::from_bytes([0; 32]));
+ server.mock("GET", "/authenticator/key")
+ .match_header("x-client-package", "io.ente.auth")
+ .match_header("x-auth-token", "--8=")
+ .with_body(json!({"encryptedKey": b64::encode(&wrapped.encrypted_data), "header": b64::encode(wrapped.nonce.as_bytes())}).to_string())
+ .expect_at_least(1).create()
+}
+
+fn entity(id: u128, updated_at: i64, value: &Value, key: &Key) -> Value {
+ let encrypted = blob::encrypt(&serde_json::to_vec(value).unwrap(), key).unwrap();
+ json!({"id": Uuid::from_u128(id), "updatedAt": updated_at, "isDeleted": false,
+ "encryptedData": b64::encode(&encrypted.encrypted_data), "header": b64::encode(encrypted.decryption_header.as_bytes())})
+}
+
+fn deleted(id: u128, updated_at: i64) -> Value {
+ json!({"id": Uuid::from_u128(id), "updatedAt": updated_at, "isDeleted": true, "encryptedData": null, "header": null})
+}
+
+fn page(server: &mut Server, since: i64, rows: Vec<Value>) -> Mock {
+ server
+ .mock("GET", "/authenticator/entity/diff")
+ .match_query(Matcher::AllOf(vec![
+ Matcher::UrlEncoded("sinceTime".into(), since.to_string()),
+ Matcher::UrlEncoded("limit".into(), "5000".into()),
+ ]))
+ .with_body(json!({"diff": rows, "timestamp": 9999999999_i64}).to_string())
+ .create()
+}
+
+#[test]
+fn refresh_keeps_progress_and_refuses_unreadable_records_until_updated() {
+ let mut server = Server::new();
+ let home = TestHome::new();
+ seed(&home, &server.url());
+ assert!(
+ failure(&home.command(EXPORT).arg("--offline").output().unwrap()).contains("no local data")
+ );
+ let key = Key::generate();
+ let key_request = key_response(&mut server, &key);
+ let initial = page(
+ &mut server,
+ 0,
+ vec![
+ entity(2, 10, &json!(URI), &key),
+ entity(1, 10, &json!(URI), &key),
+ ],
+ );
+ let first = success(home.run(EXPORT));
+ assert_eq!(first.stdout, format!("{URI}\n{URI}\n").as_bytes());
+ initial.assert();
+ initial.remove();
+
+ let mut unreadable = entity(1, 30, &json!(URI), &key);
+ unreadable["encryptedData"] = json!("broken");
+ let malformed = entity(
+ 3,
+ 30,
+ &json!({"rawData": "private-secret", "display": false}),
+ &key,
+ );
+ let changed_uri = "otpauth://hotp/Updated?secret=JBSWY3DPEHPK3PXP&counter=42";
+ let changed = page(
+ &mut server,
+ 9,
+ vec![
+ entity(2, 20, &json!(changed_uri), &key),
+ unreadable,
+ malformed,
+ ],
+ );
+ let output_dir = tempfile::tempdir().unwrap();
+ fs::write(
+ output_dir.path().join("ente-auth-2026-01-01.txt"),
+ &first.stdout,
+ )
+ .unwrap();
+ let failed = home.run(&[
+ "auth",
+ "export",
+ output_dir.path().to_str().unwrap(),
+ "--plaintext",
+ "--keep",
+ "1",
+ "--json",
+ ]);
+ let stderr = failure(&failed);
+ assert!(stderr.contains("2 unreadable Auth records"));
+ assert!(stderr.contains(&Uuid::from_u128(1).to_string()));
+ assert!(!stderr.contains("private-secret"));
+ assert!(!stderr.contains("JBSWY3DPEHPK3PXP"));
+ assert_eq!(
+ serde_json::from_slice::<Value>(&failed.stdout).unwrap(),
+ json!({"output": null, "records": 0, "failed": 2, "pruned": 0})
+ );
+ assert_eq!(fs::read_dir(output_dir.path()).unwrap().count(), 1);
+ changed.assert();
+ changed.remove();
+ let offline = home.command(EXPORT).arg("--offline").output().unwrap();
+ assert!(failure(&offline).contains("2 unreadable"));
+ assert!(offline.stdout.is_empty());
+
+ let recovered = page(
+ &mut server,
+ 29,
+ vec![entity(1, 40, &json!(URI), &key), deleted(3, 40)],
+ );
+ let result = success(home.run(EXPORT));
+ assert_eq!(result.stdout, format!("{URI}\n{changed_uri}\n").as_bytes());
+ recovered.assert();
+ recovered.remove();
+ let failed_refresh = server
+ .mock("GET", "/authenticator/entity/diff")
+ .match_query(Matcher::Any)
+ .with_status(400)
+ .create();
+ let failed = home.run(EXPORT);
+ failure(&failed);
+ assert!(failed.stdout.is_empty());
+ failed_refresh.assert();
+ key_request.assert();
+ drop(server);
+ assert_eq!(
+ success(
+ home.command(EXPORT)
+ .args(["--offline", "--account", "fixture"])
+ .output()
+ .unwrap()
+ )
+ .stdout,
+ result.stdout
+ );
+}
+
+#[test]
+fn overlapping_pages_resume_and_read_only_the_final_current_set() {
+ let mut server = Server::new();
+ let home = TestHome::new();
+ seed(&home, &server.url());
+ let key = Key::generate();
+ let key_request = key_response(&mut server, &key);
+ let template = entity(1, 10, &json!(URI), &key);
+ let mut rows: Vec<_> = (1..=4999)
+ .map(|id| {
+ let mut row = template.clone();
+ row["id"] = json!(Uuid::from_u128(id));
+ row
+ })
+ .collect();
+ rows[0]["encryptedData"] = json!("unreadable until permanently deleted");
+ rows[4998]["encryptedData"] = json!("unreadable until replaced");
+ rows.push(deleted(5000, 20));
+ let first = page(&mut server, 0, rows);
+ let interrupted = server
+ .mock("GET", "/authenticator/entity/diff")
+ .match_query(Matcher::UrlEncoded("sinceTime".into(), "19".into()))
+ .with_status(400)
+ .create();
+ let failed = home.run(EXPORT);
+ failure(&failed);
+ assert!(failed.stdout.is_empty());
+ first.assert();
+ interrupted.assert();
+ first.remove();
+ interrupted.remove();
+ let offline = home.command(EXPORT).arg("--offline").output().unwrap();
+ assert!(failure(&offline).contains("incomplete"));
+ let second = page(
+ &mut server,
+ 19,
+ vec![
+ deleted(5000, 20),
+ entity(5001, 20, &json!(URI), &key),
+ deleted(1, 21),
+ entity(4999, 21, &json!(URI), &key),
+ ],
+ );
+ let result = success(home.run(EXPORT));
+ assert_eq!(result.stdout, format!("{URI}\n").repeat(4999).as_bytes());
+ second.assert();
+ key_request.assert();
+}
+
+#[test]
+fn key_resets_and_unadvanceable_pages_cannot_publish_stale_data() {
+ let mut server = Server::new();
+ let home = TestHome::new();
+ seed(&home, &server.url());
+ let key = Key::generate();
+ let key_request = key_response(&mut server, &key);
+ let initial = page(&mut server, 0, vec![entity(1, 10, &json!(URI), &key)]);
+ success(home.run(EXPORT));
+ initial.assert();
+ initial.remove();
+ key_request.remove();
+ let missing = server
+ .mock("GET", "/authenticator/key")
+ .with_status(404)
+ .create();
+ assert!(success(home.run(EXPORT)).stdout.is_empty());
+ assert!(
+ success(home.command(EXPORT).arg("--offline").output().unwrap())
+ .stdout
+ .is_empty()
+ );
+ missing.assert();
+ missing.remove();
+
+ let next_key = Key::generate();
+ let next = key_response(&mut server, &next_key);
+ let template = entity(1, 10, &json!(URI), &next_key);
+ let rows: Vec<_> = (1..=5000)
+ .map(|id| {
+ let mut row = template.clone();
+ row["id"] = json!(Uuid::from_u128(id));
+ row
+ })
+ .collect();
+ let first = page(&mut server, 0, rows.clone());
+ let stuck = page(&mut server, 9, rows);
+ let failed = home.run(EXPORT);
+ assert!(failure(&failed).contains("boundary did not advance"));
+ assert!(failed.stdout.is_empty());
+ assert!(
+ failure(&home.command(EXPORT).arg("--offline").output().unwrap()).contains("incomplete")
+ );
+ first.assert();
+ stuck.assert();
+ first.remove();
+ stuck.remove();
+ next.remove();
+ let reset = key_response(&mut server, &Key::generate());
+ let interrupted = server
+ .mock("GET", "/authenticator/entity/diff")
+ .match_query(Matcher::UrlEncoded("sinceTime".into(), "0".into()))
+ .with_status(400)
+ .create();
+ failure(&home.run(EXPORT));
+ assert!(
+ failure(&home.command(EXPORT).arg("--offline").output().unwrap()).contains("incomplete")
+ );
+ interrupted.assert();
+ interrupted.remove();
+ let empty = page(&mut server, 0, vec![]);
+ assert!(success(home.run(EXPORT)).stdout.is_empty());
+ empty.assert();
+ reset.assert();
+}
+
+#[test]
+fn encrypted_export_and_independent_decrypt_keep_artifacts_separate_from_summaries() {
+ let mut server = Server::new();
+ let home = TestHome::new();
+ seed(&home, &server.url());
+ let key = Key::generate();
+ let key_request = key_response(&mut server, &key);
+ let initial = page(&mut server, 0, vec![entity(1, 10, &json!(URI), &key)]);
+ let plain = success(
+ home.command(EXPORT)
+ .env("ENTE_CLI_EXPORT_PASSWORD", "")
+ .output()
+ .unwrap(),
+ )
+ .stdout;
+ initial.assert();
+ initial.remove();
+ let directory = tempfile::tempdir().unwrap();
+ let encrypted = directory.path().join("backup.json");
+ let plaintext_file = directory.path().join("backup.txt");
+ let plain_summary = home.json(&[
+ "auth",
+ "export",
+ "--offline",
+ "--plaintext",
+ "--output",
+ plaintext_file.to_str().unwrap(),
+ ]);
+ assert_eq!(plain_summary["records"], 1);
+ assert_eq!(fs::read(plaintext_file).unwrap(), plain);
+ let password = " independent export password ";
+ let result = success(
+ home.command(&[
+ "auth",
+ "export",
+ "--offline",
+ "--output",
+ encrypted.to_str().unwrap(),
+ "--json",
+ ])
+ .env("ENTE_CLI_EXPORT_PASSWORD", password)
+ .output()
+ .unwrap(),
+ );
+ let summary: Value = serde_json::from_slice(&result.stdout).unwrap();
+ assert_eq!(summary["records"], 1);
+ assert_eq!(summary["pruned"], 0);
+ assert_eq!(summary["failed"], 0);
+ let encoded = fs::read(&encrypted).unwrap();
+ let stream = success(
+ home.command(&["auth", "export", "--offline", "--output", "-", "--json"])
+ .env("ENTE_CLI_EXPORT_PASSWORD", password)
+ .output()
+ .unwrap(),
+ );
+ let stream: Value = serde_json::from_slice(&stream.stdout).unwrap();
+ assert_eq!(stream["version"], 1);
+ assert!(stream.get("records").is_none());
+ assert!(stream["encryptedData"].is_string());
+ let restore = TestHome::new();
+ let absent_home = restore.dir.path().join("absent");
+ let command = || {
+ let mut command =
+ restore.command(&["auth", "decrypt", encrypted.to_str().unwrap(), "--json"]);
+ command
+ .env_remove("ENTE_CLI_VAULT_KEY")
+ .env("ENTE_CLI_HOME", &absent_home)
+ .env("ENTE_CLI_EXPORT_PASSWORD", password);
+ command
+ };
+ assert_eq!(success(command().output().unwrap()).stdout, plain);
+ assert!(!absent_home.exists());
+ let output = directory.path().join("restored.txt");
+ let summary = success(
+ command()
+ .args(["--output", output.to_str().unwrap()])
+ .output()
+ .unwrap(),
+ );
+ assert_eq!(
+ serde_json::from_slice::<Value>(&summary.stdout).unwrap()["output"],
+ output.to_string_lossy().as_ref()
+ );
+ assert_eq!(fs::read(&output).unwrap(), plain);
+ failure(
+ &command()
+ .args(["--output", output.to_str().unwrap()])
+ .output()
+ .unwrap(),
+ );
+ assert_eq!(fs::read(&output).unwrap(), plain);
+ failure(
+ &command()
+ .args(["--output", encrypted.to_str().unwrap()])
+ .output()
+ .unwrap(),
+ );
+ assert_eq!(fs::read(&encrypted).unwrap(), encoded);
+ let wrong = command()
+ .env("ENTE_CLI_EXPORT_PASSWORD", "wrong")
+ .output()
+ .unwrap();
+ failure(&wrong);
+ assert!(wrong.stdout.is_empty());
+ let mut damaged: Value = serde_json::from_slice(&encoded).unwrap();
+ let mut ciphertext = b64::decode(damaged["encryptedData"].as_str().unwrap()).unwrap();
+ ciphertext[0] ^= 1;
+ damaged["encryptedData"] = json!(b64::encode(&ciphertext));
+ fs::write(&encrypted, serde_json::to_vec(&damaged).unwrap()).unwrap();
+ let damaged = command().output().unwrap();
+ failure(&damaged);
+ assert!(damaged.stdout.is_empty());
+ key_request.assert();
+}
+
+#[test]
+fn password_and_destination_errors_are_resolved_before_account_access() {
+ let home = TestHome::new();
+ let directory = tempfile::tempdir().unwrap();
+ let path = directory.path().join("exact");
+ for value in [None, Some("")] {
+ let mut command = home.command(&["auth", "export", "--output", path.to_str().unwrap()]);
+ if let Some(value) = value {
+ command.env("ENTE_CLI_EXPORT_PASSWORD", value);
+ }
+ let output = command.output().unwrap();
+ assert!(failure(&output).contains("ENTE_CLI_EXPORT_PASSWORD"));
+ assert!(output.stdout.is_empty());
+ assert!(!path.exists());
+ }
+ #[cfg(unix)]
+ {
+ use std::os::unix::ffi::OsStringExt;
+ let output = home
+ .command(&["auth", "export", "--output", path.to_str().unwrap()])
+ .env(
+ "ENTE_CLI_EXPORT_PASSWORD",
+ std::ffi::OsString::from_vec(vec![255]),
+ )
+ .output()
+ .unwrap();
+ assert!(failure(&output).contains("must be UTF-8"));
+ }
+ fs::write(&path, "existing").unwrap();
+ let occupied = home.run(&["auth", "export", "--output", path.to_str().unwrap()]);
+ assert!(failure(&occupied).contains("already exists"));
+ assert_eq!(fs::read(&path).unwrap(), b"existing");
+ for args in [
+ vec!["auth", "export"],
+ vec!["auth", "export", "dir", "--output", "-"],
+ vec!["auth", "export", "--output", "-", "--keep", "1"],
+ vec!["auth", "export", "dir", "--keep", "0"],
+ ] {
+ failure(&home.run(&args));
+ }
+ assert!(!home.dir.path().join("accounts").exists());
+}
+
+#[test]
+fn slow_stdout_releases_account_storage_before_publication() {
+ use std::io::Read;
+
+ let mut server = Server::new();
+ let home = TestHome::new();
+ seed(&home, &server.url());
+ let key = Key::generate();
+ let key_request = key_response(&mut server, &key);
+ let uri = format!("{URI}&note={}", "x".repeat(1024 * 1024));
+ let initial = page(&mut server, 0, vec![entity(1, 10, &json!(uri), &key)]);
+ let mut child = home.command(EXPORT).spawn().unwrap();
+ let mut stdout = child.stdout.take().unwrap();
+ let mut first = [0];
+ stdout.read_exact(&mut first).unwrap();
+ success(home.run(&["accounts", "logout", "fixture", "--local", "--offline"]));
+ let mut rest = Vec::new();
+ stdout.read_to_end(&mut rest).unwrap();
+ success(child.wait_with_output().unwrap());
+ assert_eq!(first[0], b'o');
+ assert_eq!(rest, format!("{}\n", &uri[1..]).as_bytes());
+ initial.assert();
+ key_request.assert();
+}
diff --git a/rust/apps/cli-next/tests/support/museum.rs b/rust/apps/cli-next/tests/support/museum.rs
index 26c54adc60..7fa735dbf2 100644
--- a/rust/apps/cli-next/tests/support/museum.rs
+++ b/rust/apps/cli-next/tests/support/museum.rs
@@ -15,6 +15,81 @@ fn login_and_photos_across_processes() -> TestResult {
Museum::run_async(exercise)
}
+#[test]
+fn auth_login_refresh_and_offline_export_across_processes() -> TestResult {
+ Museum::run_async(|origin| async move {
+ let email = format!("auth-export-{}@example.org", Uuid::new_v4());
+ let owner = create_account(&origin, &email).await;
+ let home = TestHome::new();
+ login(&home, "auth", &email, &["--host", &origin]);
+ let export = ["auth", "export", "--output", "-", "--plaintext"];
+ assert!(success(home.run(&export)).stdout.is_empty());
+ let key = Key::generate();
+ let wrapped = secretbox::encrypt(
+ key.as_bytes(),
+ &Key::try_from_slice(&owner.secrets.master_key).unwrap(),
+ );
+ let body = home.dir.path().join("request.json");
+ fs::write(&body, serde_json::to_vec(&json!({"encryptedKey": b64::encode(&wrapped.encrypted_data), "header": b64::encode(wrapped.nonce.as_bytes())})).unwrap()).unwrap();
+ success(home.run(&[
+ "auth",
+ "api",
+ "/authenticator/key",
+ "--method",
+ "POST",
+ "--body",
+ body.to_str().unwrap(),
+ ]));
+ let uri = "otpauth://hotp/Museum?secret=JBSWY3DPEHPK3PXP&counter=17";
+ let encrypted = blob::encrypt_json(&uri, &key).unwrap();
+ fs::write(&body, serde_json::to_vec(&json!({"encryptedData": b64::encode(&encrypted.encrypted_data), "header": b64::encode(encrypted.decryption_header.as_bytes())})).unwrap()).unwrap();
+ let created = home.json(&[
+ "auth",
+ "api",
+ "/authenticator/entity",
+ "--method",
+ "POST",
+ "--body",
+ body.to_str().unwrap(),
+ ]);
+ assert_eq!(
+ success(home.run(&export)).stdout,
+ format!("{uri}\n").as_bytes()
+ );
+ let changed = format!("{uri}&codeDisplay=%7B%22trashed%22%3Atrue%7D");
+ let encrypted = blob::encrypt_json(&changed, &key).unwrap();
+ fs::write(&body, serde_json::to_vec(&json!({"id": created["id"], "encryptedData": b64::encode(&encrypted.encrypted_data), "header": b64::encode(encrypted.decryption_header.as_bytes())})).unwrap()).unwrap();
+ success(home.run(&[
+ "auth",
+ "api",
+ "/authenticator/entity",
+ "--method",
+ "PUT",
+ "--body",
+ body.to_str().unwrap(),
+ ]));
+ assert_eq!(
+ success(home.run(&export)).stdout,
+ format!("{changed}\n").as_bytes()
+ );
+ assert_eq!(
+ success(home.command(&export).arg("--offline").output().unwrap()).stdout,
+ format!("{changed}\n").as_bytes()
+ );
+ success(home.run(&[
+ "auth",
+ "api",
+ "/authenticator/entity",
+ "--method",
+ "DELETE",
+ "--query",
+ &format!("id={}", created["id"].as_str().unwrap()),
+ ]));
+ assert!(success(home.run(&export)).stdout.is_empty());
+ Ok(())
+ })
+}
+
#[cfg(unix)]
#[test]
fn login_save_failures_follow_the_vault_replacement() -> TestResult {
diff --git a/rust/crates/auth/Cargo.toml b/rust/crates/auth/Cargo.toml
new file mode 100644
index 0000000000..93bb58dd43
--- /dev/null
+++ b/rust/crates/auth/Cargo.toml
@@ -0,0 +1,15 @@
+[package]
+name = "ente-auth"
+edition.workspace = true
+
+[dependencies]
+ente-core.workspace = true
+serde.workspace = true
+serde_json = { workspace = true, features = ["raw_value"] }
+thiserror.workspace = true
+url.workspace = true
+uuid = { workspace = true, features = ["serde"] }
+zeroize.workspace = true
+
+[lints]
+workspace = true
diff --git a/rust/crates/auth/src/backup.rs b/rust/crates/auth/src/backup.rs
new file mode 100644
index 0000000000..6a03ead6aa
--- /dev/null
+++ b/rust/crates/auth/src/backup.rs
@@ -0,0 +1,146 @@
+use ente_core::{
+ b64,
+ crypto::{Header, Salt, argon, blob},
+};
+use serde::{Deserialize, Serialize};
+use zeroize::Zeroizing;
+
+use crate::{Error, Result};
+
+#[derive(Deserialize, Serialize)]
+#[serde(rename_all = "camelCase")]
+struct Backup {
+ version: u32,
+ kdf_params: KdfParams,
+ encrypted_data: String,
+ encryption_nonce: String,
+}
+
+#[derive(Deserialize, Serialize)]
+#[serde(rename_all = "camelCase")]
+struct KdfParams {
+ mem_limit: u32,
+ ops_limit: u32,
+ salt: String,
+}
+
+fn validate_params(params: argon::Params) -> Result<()> {
+ params.validate()?;
+ let strength = u64::from(params.mem_limit) * u64::from(params.ops_limit);
+ if params.mem_limit > argon::Params::SENSITIVE.mem_limit || strength > 1_u64 << 32 {
+ return Err(Error::UnsafeKdf);
+ }
+ Ok(())
+}
+
+pub fn encrypt(plaintext: &[u8], password: &str) -> Result<Vec<u8>> {
+ let derived = argon::derive_sensitive_key(password)?;
+ let encrypted = blob::encrypt(plaintext, &derived.key)?;
+ serde_json::to_vec(&Backup {
+ version: 1,
+ kdf_params: KdfParams {
+ mem_limit: derived.params.mem_limit,
+ ops_limit: derived.params.ops_limit,
+ salt: b64::encode(derived.salt.as_bytes()),
+ },
+ encrypted_data: b64::encode(&encrypted.encrypted_data),
+ encryption_nonce: b64::encode(encrypted.decryption_header.as_bytes()),
+ })
+ .map_err(|_| Error::InvalidBackup)
+}
+
+pub fn decrypt(input: &[u8], password: &str) -> Result<Zeroizing<Vec<u8>>> {
+ let backup: Backup = serde_json::from_slice(input).map_err(|_| Error::InvalidBackup)?;
+ if backup.version != 1 {
+ return Err(Error::UnsupportedVersion);
+ }
+ let params = argon::Params {
+ mem_limit: backup.kdf_params.mem_limit,
+ ops_limit: backup.kdf_params.ops_limit,
+ };
+ validate_params(params)?;
+ let salt = Salt::try_from_slice(&b64::decode(&backup.kdf_params.salt)?)?;
+ let header = Header::try_from_slice(&b64::decode(&backup.encryption_nonce)?)?;
+ let encrypted = b64::decode(&backup.encrypted_data)?;
+ if encrypted.len() < blob::ABYTES {
+ return Err(Error::InvalidBackup);
+ }
+ let key = argon::derive_key(password, &salt, params)?;
+ Ok(Zeroizing::new(blob::decrypt_legacy(
+ &encrypted, &header, &key,
+ )?))
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use ente_core::crypto::stream::Encryptor;
+
+ #[test]
+ fn admits_existing_producer_profiles_without_deriving_keys() {
+ let mut memory = 1 << 30;
+ let mut operations = 4;
+ while memory >= 8192 {
+ assert!(
+ validate_params(argon::Params {
+ mem_limit: memory,
+ ops_limit: operations
+ })
+ .is_ok()
+ );
+ memory /= 2;
+ operations *= 2;
+ }
+ for (memory, operations) in [
+ (8191, 1),
+ (8193, 1),
+ (8192, 0),
+ (1 << 30, 5),
+ ((1 << 30) + 1024, 1),
+ (8192, u32::MAX),
+ ] {
+ assert!(
+ validate_params(argon::Params {
+ mem_limit: memory,
+ ops_limit: operations
+ })
+ .is_err()
+ );
+ }
+ }
+
+ #[test]
+ fn decryption_authenticates_legacy_bytes_without_interpreting_them() {
+ let password = " preserve whitespace ";
+ let salt = Salt::generate();
+ let key = argon::derive_key(password, &salt, argon::Params::MIN).unwrap();
+ let bytes = b"\xff\0original\r\nbytes without final newline";
+ let mut stream = Encryptor::new(&key);
+ let encrypted = stream.push(bytes, false).unwrap();
+ let mut backup = Backup {
+ version: 1,
+ kdf_params: KdfParams {
+ mem_limit: 8192,
+ ops_limit: 1,
+ salt: b64::encode(salt.as_bytes()),
+ },
+ encrypted_data: b64::encode(&encrypted),
+ encryption_nonce: b64::encode(stream.header().as_bytes()),
+ };
+ let encoded = serde_json::to_vec(&backup).unwrap();
+ assert_eq!(decrypt(&encoded, password).unwrap().as_slice(), bytes);
+ assert!(decrypt(&encoded, "wrong").is_err());
+ let mut damaged = encrypted;
+ damaged[0] ^= 1;
+ backup.encrypted_data = b64::encode(&damaged);
+ assert!(decrypt(&serde_json::to_vec(&backup).unwrap(), password).is_err());
+ backup.version = 2;
+ assert!(matches!(
+ decrypt(&serde_json::to_vec(&backup).unwrap(), password),
+ Err(Error::UnsupportedVersion)
+ ));
+ backup.version = 1;
+ backup.encryption_nonce = b64::encode(&[0; 23]);
+ assert!(decrypt(&serde_json::to_vec(&backup).unwrap(), password).is_err());
+ }
+}
diff --git a/rust/crates/auth/src/lib.rs b/rust/crates/auth/src/lib.rs
new file mode 100644
index 0000000000..329f801fcd
--- /dev/null
+++ b/rust/crates/auth/src/lib.rs
@@ -0,0 +1,92 @@
+pub mod backup;
+pub mod record;
+
+use ente_core::{
+ Session, b64,
+ crypto::{self, Key, Nonce, secretbox},
+ http,
+};
+use serde::Deserialize;
+use uuid::Uuid;
+use zeroize::Zeroizing;
+
+pub const PAGE_SIZE: usize = 5000;
+
+#[derive(thiserror::Error, Debug)]
+pub enum Error {
+ #[error(transparent)]
+ Http(#[from] http::Error),
+ #[error(transparent)]
+ Crypto(#[from] crypto::Error),
+ #[error("invalid Auth base64 encoding")]
+ Base64(#[from] b64::DecodeError),
+ #[error("unreadable Auth record")]
+ InvalidRecord,
+ #[error("invalid encrypted Auth export")]
+ InvalidBackup,
+ #[error("unsupported Auth export version")]
+ UnsupportedVersion,
+ #[error("Auth export key derivation parameters exceed supported bounds")]
+ UnsafeKdf,
+}
+
+pub type Result<T> = std::result::Result<T, Error>;
+
+#[derive(Deserialize, PartialEq, Eq)]
+#[serde(rename_all = "camelCase")]
+pub struct WrappedKey {
+ pub encrypted_key: String,
+ pub header: String,
+}
+
+#[derive(Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct Entity {
+ pub id: Uuid,
+ pub updated_at: i64,
+ pub is_deleted: bool,
+ pub encrypted_data: Option<String>,
+ pub header: Option<String>,
+}
+
+pub async fn fetch_key(session: &Session) -> Result<Option<WrappedKey>> {
+ Ok(http::retry(|| async {
+ let response = session.api.get("/authenticator/key").send().await?;
+ if response.status() == 404 {
+ return Ok(None);
+ }
+ response.error_for_code().await?.json().await.map(Some)
+ })
+ .await?)
+}
+
+pub async fn diff(session: &Session, since: i64) -> Result<Vec<Entity>> {
+ #[derive(Deserialize)]
+ struct Response {
+ diff: Vec<Entity>,
+ }
+ let response: Response = http::retry(|| async {
+ session
+ .api
+ .get("/authenticator/entity/diff")
+ .query(&[("sinceTime", since), ("limit", PAGE_SIZE as i64)])
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await
+ })
+ .await?;
+ Ok(response.diff)
+}
+
+pub fn open_key(wrapped: &WrappedKey, master_key: &Key) -> Result<Key> {
+ let nonce = Nonce::try_from_slice(&b64::decode(&wrapped.header)?)?;
+ let plaintext = Zeroizing::new(secretbox::decrypt(
+ &b64::decode(&wrapped.encrypted_key)?,
+ &nonce,
+ master_key,
+ )?);
+ Ok(Key::try_from_slice(&plaintext)?)
+}
diff --git a/rust/crates/auth/src/record.rs b/rust/crates/auth/src/record.rs
new file mode 100644
index 0000000000..db42f5ece4
--- /dev/null
+++ b/rust/crates/auth/src/record.rs
@@ -0,0 +1,196 @@
+use ente_core::{
+ b64,
+ crypto::{Header, Key, blob},
+};
+use serde::Deserialize;
+use serde_json::value::RawValue;
+use url::{Url, form_urlencoded};
+use zeroize::Zeroizing;
+
+use crate::{Error, Result};
+
+pub fn uri_line(
+ encrypted_data: Option<&str>,
+ header: Option<&str>,
+ key: &Key,
+) -> Result<Zeroizing<String>> {
+ let data = b64::decode(encrypted_data.ok_or(Error::InvalidRecord)?)?;
+ let header = Header::try_from_slice(&b64::decode(header.ok_or(Error::InvalidRecord)?)?)?;
+ let plaintext = Zeroizing::new(blob::decrypt_legacy(&data, &header, key)?);
+ interpret(&plaintext)
+}
+
+fn interpret(plaintext: &[u8]) -> Result<Zeroizing<String>> {
+ #[derive(Deserialize)]
+ struct Legacy<'a> {
+ #[serde(rename = "rawData")]
+ raw_data: String,
+ #[serde(borrow)]
+ display: Option<&'a RawValue>,
+ }
+
+ let value: &RawValue = serde_json::from_slice(plaintext).map_err(|_| Error::InvalidRecord)?;
+ let mut line = match value.get().as_bytes().first() {
+ Some(b'"') => {
+ let uri = Zeroizing::new(
+ serde_json::from_str::<String>(value.get()).map_err(|_| Error::InvalidRecord)?,
+ );
+ validate_uri(&uri)?;
+ uri
+ }
+ Some(b'{') => {
+ let legacy: Legacy<'_> =
+ serde_json::from_str(value.get()).map_err(|_| Error::InvalidRecord)?;
+ let raw = Zeroizing::new(legacy.raw_data);
+ let raw = Zeroizing::new(raw.replace('#', "%23"));
+ validate_uri(&raw)?;
+ let display = match legacy.display {
+ Some(display) if display.get().starts_with('{') => display.get(),
+ None => {
+ r#"{"pinned":false,"trashed":false,"lastUsedAt":0,"tapCount":0,"tags":[],"note":"","position":0,"iconSrc":"","iconID":""}"#
+ }
+ Some(_) => return Err(Error::InvalidRecord),
+ };
+ let (prefix, query) = raw.split_once('?').unwrap_or((&raw, ""));
+ let mut uri = Zeroizing::new(String::from(prefix));
+ uri.push('?');
+ for pair in query.split('&').filter(|pair| !pair.is_empty()) {
+ if form_urlencoded::parse(pair.as_bytes()).any(|(name, _)| name == "codeDisplay") {
+ continue;
+ }
+ uri.push_str(pair);
+ uri.push('&');
+ }
+ uri.push_str(
+ &form_urlencoded::Serializer::new(String::new())
+ .append_pair("codeDisplay", display)
+ .finish(),
+ );
+ uri
+ }
+ _ => return Err(Error::InvalidRecord),
+ };
+ line.push('\n');
+ Ok(line)
+}
+
+fn validate_uri(uri: &str) -> Result<()> {
+ if !uri.starts_with("otpauth://") || uri.chars().any(char::is_control) {
+ return Err(Error::InvalidRecord);
+ }
+ let parsed = Url::parse(uri).map_err(|_| Error::InvalidRecord)?;
+ if parsed.host_str().is_none()
+ || !parsed
+ .query_pairs()
+ .any(|(name, value)| name == "secret" && !value.is_empty())
+ {
+ return Err(Error::InvalidRecord);
+ }
+ Ok(())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use ente_core::crypto::stream::Encryptor;
+ use serde_json::{Value, json};
+
+ #[test]
+ fn records_preserve_uri_text_and_legacy_display() {
+ let uri = "otpauth://hotp/Issuer:label?secret=JBSWY3DPEHPK3PXP&counter=19&digits=8&algorithm=SHA512&x=%252C&x=two&codeDisplay=%7B%22note%22%3A%22literal%252C%22%7D";
+ assert_eq!(
+ interpret(&serde_json::to_vec(uri).unwrap())
+ .unwrap()
+ .as_str(),
+ format!("{uri}\n")
+ );
+ let display = json!({"note": "literal%2C, + # πŸ˜€", "tags": ["one,two", "%2C"], "pinned": true,
+ "trashed": true, "position": 45, "lastUsedAt": 123, "tapCount": 7,
+ "iconSrc": "custom", "iconID": "saved", "future": {"unknown": true}});
+ for kind in ["totp", "hotp", "steam"] {
+ let raw = format!(
+ "otpauth://{kind}/Issuer:label?secret=JBSWY3DPEHPK3PXP&period=60&counter=19&digits=8&algorithm=SHA512&x=%252C&x=two&codeDisplay=old"
+ );
+ let line = interpret(
+ &serde_json::to_vec(&json!({"rawData": raw, "display": display})).unwrap(),
+ )
+ .unwrap();
+ assert!(line.starts_with(raw.strip_suffix("&codeDisplay=old").unwrap()));
+ let parsed = Url::parse(line.trim_end()).unwrap();
+ let metadata: Vec<_> = parsed
+ .query_pairs()
+ .filter(|(name, _)| name == "codeDisplay")
+ .collect();
+ assert_eq!(metadata.len(), 1);
+ assert_eq!(
+ serde_json::from_str::<Value>(&metadata[0].1).unwrap(),
+ display
+ );
+ }
+ for object in [
+ json!({"rawData": uri}),
+ json!({"rawData": uri, "display": null}),
+ ] {
+ let line = interpret(&serde_json::to_vec(&object).unwrap()).unwrap();
+ let parsed = Url::parse(line.trim_end()).unwrap();
+ let display = parsed
+ .query_pairs()
+ .find(|(name, _)| name == "codeDisplay")
+ .unwrap()
+ .1;
+ assert_eq!(
+ serde_json::from_str::<Value>(&display).unwrap(),
+ json!({
+ "pinned": false, "trashed": false, "lastUsedAt": 0, "tapCount": 0, "tags": [],
+ "note": "", "position": 0, "iconSrc": "", "iconID": ""
+ })
+ );
+ }
+ let display = r#"{"unknown":184467440737095516160001,"decimal":0.123456789012345678901}"#;
+ let legacy = format!(r#"{{"rawData":"{uri}","display":{display}}}"#);
+ let line = interpret(legacy.as_bytes()).unwrap();
+ let parsed = Url::parse(line.trim_end()).unwrap();
+ assert_eq!(
+ parsed
+ .query_pairs()
+ .find(|(name, _)| name == "codeDisplay")
+ .unwrap()
+ .1,
+ display
+ );
+ let key = Key::generate();
+ let mut stream = Encryptor::new(&key);
+ let ciphertext = stream
+ .push(&serde_json::to_vec(uri).unwrap(), false)
+ .unwrap();
+ assert_eq!(
+ uri_line(
+ Some(&b64::encode(&ciphertext)),
+ Some(&b64::encode(stream.header().as_bytes())),
+ &key
+ )
+ .unwrap()
+ .as_str(),
+ format!("{uri}\n")
+ );
+ }
+
+ #[test]
+ fn malformed_records_fail_without_disclosing_plaintext() {
+ for value in [
+ json!("private-secret"),
+ json!("otpauth://totp/a?secret=private-secret\nsecond"),
+ json!({"rawData": "otpauth://totp/a?secret=private-secret", "display": "private-secret"}),
+ json!(["private-secret"]),
+ json!({"rawData": 123}),
+ ] {
+ let error = interpret(&serde_json::to_vec(&value).unwrap()).unwrap_err();
+ assert_eq!(error.to_string(), "unreadable Auth record");
+ assert!(!format!("{error:?}").contains("private-secret"));
+ }
+ assert_eq!(
+ interpret(b"{\"private-secret\"").unwrap_err().to_string(),
+ "unreadable Auth record"
+ );
+ }
+}
diff --git a/rust/crates/core/src/crypto/argon.rs b/rust/crates/core/src/crypto/argon.rs
index 67dce5679c..4011fe8a76 100644
--- a/rust/crates/core/src/crypto/argon.rs
+++ b/rust/crates/core/src/crypto/argon.rs
@@ -31,6 +31,30 @@ impl Params {
mem_limit: 8_192,
ops_limit: 1,
};
+
+ pub fn validate(self) -> Result<()> {
+ if self.mem_limit < Self::MIN.mem_limit {
+ return Err(Error::InvalidKeyDerivationParams(format!(
+ "Memory limit {} is below minimum {}",
+ self.mem_limit,
+ Self::MIN.mem_limit
+ )));
+ }
+ if !self.mem_limit.is_multiple_of(1024) {
+ return Err(Error::InvalidKeyDerivationParams(format!(
+ "Memory limit {} must be a multiple of 1024 bytes",
+ self.mem_limit
+ )));
+ }
+ if self.ops_limit < Self::MIN.ops_limit {
+ return Err(Error::InvalidKeyDerivationParams(format!(
+ "Operations limit {} is below minimum {}",
+ self.ops_limit,
+ Self::MIN.ops_limit
+ )));
+ }
+ Ok(())
+ }
}
const MEMLIMIT_SENSITIVE_MIN: u32 = 134_217_728;
@@ -58,28 +82,7 @@ pub fn derive_key(password: &str, salt: &Salt, params: Params) -> Result<Key> {
}
fn derive_key_impl(password: &[u8], salt: &Salt, params: Params) -> Result<Key> {
- if params.mem_limit < Params::MIN.mem_limit {
- return Err(Error::InvalidKeyDerivationParams(format!(
- "Memory limit {} is below minimum {}",
- params.mem_limit,
- Params::MIN.mem_limit
- )));
- }
-
- if !params.mem_limit.is_multiple_of(1024) {
- return Err(Error::InvalidKeyDerivationParams(format!(
- "Memory limit {} must be a multiple of 1024 bytes",
- params.mem_limit
- )));
- }
-
- if params.ops_limit < Params::MIN.ops_limit {
- return Err(Error::InvalidKeyDerivationParams(format!(
- "Operations limit {} is below minimum {}",
- params.ops_limit,
- Params::MIN.ops_limit
- )));
- }
+ params.validate()?;
let m_cost = params.mem_limit / 1024;
let t_cost = params.ops_limit;