Skip to content

armgit / ente

Public Imported from GitHub
0

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

Harden CLI export and passkey handling

Manav Rathi21b10b4f0dc7
diff --git a/rust/apps/cli/src/commands/account.rs b/rust/apps/cli/src/commands/account.rs
index 385002983c..0b81d857d6 100644
--- a/rust/apps/cli/src/commands/account.rs
+++ b/rust/apps/cli/src/commands/account.rs
@@ -187,12 +187,13 @@ impl AuthFlowUi for DialoguerAuthFlowUi {
println!("\nPasskey verification required");
println!("Open this URL in your browser to verify your passkey:\n{url}");
- if !self.passkey_presented {
- if let Err(error) = open::that(url) {
- log::error!("failed to open browser: {error}");
- }
- self.passkey_presented = true;
+ if !self.passkey_presented
+ && can_open_automatically(url)
+ && let Err(error) = open::that(url)
+ {
+ log::error!("failed to open browser: {error}");
}
+ self.passkey_presented = true;
Ok(())
}
@@ -588,6 +589,12 @@ fn is_retryable_password_error(error: &AccountsError) -> bool {
) || error.is_http_status(&[401])
}
+fn can_open_automatically(url: &str) -> bool {
+ url.split_once("://").is_some_and(|(scheme, _)| {
+ scheme.eq_ignore_ascii_case("https") || scheme.eq_ignore_ascii_case("http")
+ })
+}
+
fn read_six_digit_code(prompt: &str) -> Result<String> {
Input::new()
.with_prompt(prompt)
@@ -605,3 +612,16 @@ fn read_six_digit_code(prompt: &str) -> Result<String> {
fn read_six_digit_code_for_accounts(prompt: &str) -> AccountsResult<String> {
read_six_digit_code(prompt).map_err(|error| AccountsError::InvalidInput(error.to_string()))
}
+
+#[cfg(test)]
+mod tests {
+ use super::can_open_automatically;
+
+ #[test]
+ fn automatic_opening_is_limited_to_web_urls() {
+ assert!(can_open_automatically("https://accounts.ente.io"));
+ assert!(can_open_automatically("HTTP://localhost:3000"));
+ assert!(!can_open_automatically("ente-cli://passkey"));
+ assert!(!can_open_automatically("file:///etc/hosts"));
+ }
+}
diff --git a/rust/apps/cli/src/commands/export.rs b/rust/apps/cli/src/commands/export.rs
index 5c0ece8444..36626f39da 100644
--- a/rust/apps/cli/src/commands/export.rs
+++ b/rust/apps/cli/src/commands/export.rs
@@ -1,6 +1,7 @@
use crate::Result;
use crate::api::AppClient;
use crate::api::methods::ApiMethods;
+use crate::live_photo::extract_live_photo;
use crate::models::{
account::Account,
export_metadata::{AlbumMetadata, DiskFileMetadata},
@@ -12,7 +13,6 @@ use crate::sync::SyncEngine;
use base64::Engine;
use ente_core::crypto;
use std::collections::HashMap;
-use std::io::Cursor;
use std::path::{Path, PathBuf};
use tokio::fs;
use tokio::io::AsyncWriteExt;
@@ -80,7 +80,9 @@ async fn load_album_metadata(
// Check if the actual file exists
for filename in &disk_metadata.info.file_names {
- let file_path = export_path.join(album_name).join(filename);
+ let file_path = export_path
+ .join(album_name)
+ .join(sanitize_filename(filename));
if file_path.exists() {
existing_files.insert(
disk_metadata.info.id,
@@ -1027,31 +1029,42 @@ fn decrypt_file_key(encrypted_key: &str, nonce: &str, collection_key: &[u8]) ->
// Removed generate_fallback_filename - Go CLI panics if no title, we return error instead
+/// Reduce a sanitized name to a single path component, never `.`, `..`, or empty.
+fn safe_path_component(name: String) -> String {
+ let trimmed = name.trim();
+ if trimmed.is_empty() || trimmed == "." || trimmed == ".." {
+ "_".to_string()
+ } else {
+ trimmed.to_string()
+ }
+}
+
/// Sanitize a filename for the filesystem
fn sanitize_filename(name: &str) -> String {
- name.chars()
+ let sanitized = name
+ .chars()
.map(|c| match c {
'/' | '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|' => '_',
'\0' => '_',
c if c.is_control() => '_',
c => c,
})
- .collect::<String>()
- .trim()
- .to_string()
+ .collect::<String>();
+ safe_path_component(sanitized)
}
-/// Sanitize an album/collection name for filesystem (matching Go CLI's logic)
+/// Sanitize an album/collection name into a safe single path component
fn sanitize_album_name(name: &str) -> String {
- // Go CLI replaces : and / with _ in album names
- name.chars()
+ let sanitized = name
+ .chars()
.map(|c| match c {
- ':' | '/' => '_',
+ '/' | '\\' | ':' => '_',
+ '\0' => '_',
+ c if c.is_control() => '_',
c => c,
})
- .collect::<String>()
- .trim()
- .to_string()
+ .collect::<String>();
+ safe_path_component(sanitized)
}
/// Decrypt file metadata
@@ -1109,79 +1122,6 @@ fn decrypt_magic_metadata(
Ok(Some(metadata))
}
-/// Extract live photo components from a ZIP file
-async fn extract_live_photo(zip_data: &[u8], output_path: &Path) -> Result<()> {
- use zip::ZipArchive;
-
- // Parse the ZIP archive
- let cursor = Cursor::new(zip_data);
- let mut archive = ZipArchive::new(cursor)?;
-
- // Get the parent directory and base name
- let parent_dir = output_path
- .parent()
- .ok_or_else(|| crate::Error::Generic("Invalid output path".into()))?;
-
- let base_name = output_path
- .file_stem()
- .and_then(|s| s.to_str())
- .ok_or_else(|| crate::Error::Generic("Invalid filename".into()))?;
-
- // Extract each file from the ZIP
- for i in 0..archive.len() {
- let mut file = archive.by_index(i)?;
- let file_name = file.name().to_string();
-
- // Determine the output filename preserving original extension
- // Following Go CLI's approach: use the actual extension from the file in the ZIP
- let output_file_path = if file_name.to_lowercase().contains("image") {
- // Image component - preserve its original extension
- let ext = std::path::Path::new(&file_name)
- .extension()
- .and_then(|e| e.to_str())
- .ok_or_else(|| {
- crate::Error::Generic(format!(
- "Live photo image component has no extension: {}",
- file_name
- ))
- })?;
- parent_dir.join(format!("{}.{}", base_name, ext))
- } else if file_name.to_lowercase().contains("video") {
- // Video component - preserve its original extension
- let ext = std::path::Path::new(&file_name)
- .extension()
- .and_then(|e| e.to_str())
- .ok_or_else(|| {
- crate::Error::Generic(format!(
- "Live photo video component has no extension: {}",
- file_name
- ))
- })?;
- parent_dir.join(format!("{}.{}", base_name, ext))
- } else {
- // Go CLI returns error for unexpected files in live photo ZIP
- return Err(crate::Error::Generic(format!(
- "Unexpected file in live photo ZIP: {}",
- file_name
- )));
- };
-
- // Read the file contents
- let mut contents = Vec::new();
- use std::io::Read;
- file.read_to_end(&mut contents)?;
-
- // Write to disk
- let mut output_file = fs::File::create(&output_file_path).await?;
- output_file.write_all(&contents).await?;
- output_file.sync_all().await?;
-
- log::debug!("Extracted live photo component: {:?}", output_file_path);
- }
-
- Ok(())
-}
-
/// Check if a collection is hidden based on its metadata
fn check_collection_visibility(
collection: &crate::api::models::Collection,
diff --git a/rust/apps/cli/src/lib.rs b/rust/apps/cli/src/lib.rs
index 16669ca82c..161733f46d 100644
--- a/rust/apps/cli/src/lib.rs
+++ b/rust/apps/cli/src/lib.rs
@@ -2,6 +2,7 @@ pub mod api;
pub mod auth_flow;
pub mod cli;
pub mod commands;
+mod live_photo;
pub mod models;
pub mod storage;
pub mod sync;
diff --git a/rust/apps/cli/src/live_photo.rs b/rust/apps/cli/src/live_photo.rs
new file mode 100644
index 0000000000..666ddbeca2
--- /dev/null
+++ b/rust/apps/cli/src/live_photo.rs
@@ -0,0 +1,164 @@
+use std::io::{Cursor, Read};
+use std::path::Path;
+
+use tokio::fs;
+use tokio::io::AsyncWriteExt;
+use zip::ZipArchive;
+
+use crate::{Error, Result};
+
+const MAX_EXPANDED_RATIO: u64 = 20;
+const MAX_EXPANDED_OVERHEAD: u64 = 16 * 1024 * 1024;
+
+struct Entry {
+ index: usize,
+ extension: String,
+ size: u64,
+}
+
+pub(crate) async fn extract_live_photo(zip_data: &[u8], output_path: &Path) -> Result<()> {
+ let mut archive = ZipArchive::new(Cursor::new(zip_data))?;
+ if archive.len() != 2 {
+ return Err(Error::Generic(
+ "Live photo ZIP must contain one image and one video".into(),
+ ));
+ }
+
+ let mut image = None;
+ let mut video = None;
+ for index in 0..archive.len() {
+ let file = archive.by_index(index)?;
+ let (kind, extension) = parse_entry_name(file.name())?;
+ let entry = Entry {
+ index,
+ extension: extension.to_string(),
+ size: file.size(),
+ };
+ let slot = if kind == "image" {
+ &mut image
+ } else {
+ &mut video
+ };
+ if slot.replace(entry).is_some() {
+ return Err(Error::Generic(format!(
+ "Live photo ZIP contains multiple {kind} entries"
+ )));
+ }
+ }
+
+ let image = image.ok_or_else(|| Error::Generic("Live photo ZIP has no image".into()))?;
+ let video = video.ok_or_else(|| Error::Generic("Live photo ZIP has no video".into()))?;
+ let max_expanded = zip_data.len() as u64 * MAX_EXPANDED_RATIO + MAX_EXPANDED_OVERHEAD;
+ if image.size + video.size > max_expanded {
+ return Err(Error::Generic("Live photo ZIP expands beyond limit".into()));
+ }
+
+ let parent = output_path
+ .parent()
+ .ok_or_else(|| Error::Generic("Invalid output path".into()))?;
+ let base = output_path
+ .file_stem()
+ .and_then(|name| name.to_str())
+ .ok_or_else(|| Error::Generic("Invalid filename".into()))?;
+ let mut expanded = 0;
+ for entry in [image, video] {
+ let mut input = archive.by_index(entry.index)?;
+ let path = parent.join(format!("{base}.{}", entry.extension));
+ let mut output = fs::File::create(&path).await?;
+ let mut buffer = [0; 64 * 1024];
+ loop {
+ let count = input.read(&mut buffer)?;
+ if count == 0 {
+ break;
+ }
+ expanded += count as u64;
+ if expanded > max_expanded {
+ return Err(Error::Generic("Live photo ZIP expands beyond limit".into()));
+ }
+ output.write_all(&buffer[..count]).await?;
+ }
+ output.sync_all().await?;
+ log::debug!("Extracted live photo component: {path:?}");
+ }
+ Ok(())
+}
+
+fn parse_entry_name(name: &str) -> Result<(&str, &str)> {
+ let (kind, extension) = name
+ .split_once('.')
+ .ok_or_else(|| Error::Generic(format!("Invalid live photo entry: {name}")))?;
+ if !matches!(kind, "image" | "video")
+ || extension.is_empty()
+ || extension.len() > 16
+ || !extension.bytes().all(|byte| byte.is_ascii_alphanumeric())
+ {
+ return Err(Error::Generic(format!("Invalid live photo entry: {name}")));
+ }
+ Ok((kind, extension))
+}
+
+#[cfg(test)]
+mod tests {
+ use std::io::{Cursor, Write};
+ use std::path::Path;
+
+ use zip::{CompressionMethod, ZipWriter, write::SimpleFileOptions};
+
+ use super::{extract_live_photo, parse_entry_name};
+
+ fn archive(entries: &[(&str, &[u8])]) -> Vec<u8> {
+ let mut writer = ZipWriter::new(Cursor::new(Vec::new()));
+ for (name, data) in entries {
+ writer
+ .start_file(
+ *name,
+ SimpleFileOptions::default().compression_method(CompressionMethod::Deflated),
+ )
+ .unwrap();
+ writer.write_all(data).unwrap();
+ }
+ writer.finish().unwrap().into_inner()
+ }
+
+ #[test]
+ fn validates_entry_names() {
+ assert_eq!(parse_entry_name("image.heic").unwrap(), ("image", "heic"));
+ assert_eq!(parse_entry_name("video.mov").unwrap(), ("video", "mov"));
+ assert!(parse_entry_name("thumbnail.jpg").is_err());
+ assert!(parse_entry_name("image.jpg.exe").is_err());
+ assert!(parse_entry_name("video../clip").is_err());
+ }
+
+ #[tokio::test]
+ async fn extracts_image_and_video() {
+ let data = archive(&[("image.jpg", b"image"), ("video.mov", b"video")]);
+ let directory =
+ std::env::temp_dir().join(format!("ente-live-photo-test-{}", uuid::Uuid::new_v4()));
+ std::fs::create_dir(&directory).unwrap();
+
+ extract_live_photo(&data, &directory.join("photo.zip"))
+ .await
+ .unwrap();
+ assert_eq!(
+ std::fs::read(directory.join("photo.jpg")).unwrap(),
+ b"image"
+ );
+ assert_eq!(
+ std::fs::read(directory.join("photo.mov")).unwrap(),
+ b"video"
+ );
+
+ std::fs::remove_dir_all(directory).unwrap();
+ }
+
+ #[tokio::test]
+ async fn rejects_excessive_expansion() {
+ let oversized = vec![0; super::MAX_EXPANDED_OVERHEAD as usize + 1024 * 1024];
+ let data = archive(&[("image.jpg", &oversized), ("video.mov", b"video")]);
+ assert!(
+ extract_live_photo(&data, Path::new("photo.zip"))
+ .await
+ .is_err()
+ );
+ }
+}
diff --git a/rust/apps/cli/src/sync/download.rs b/rust/apps/cli/src/sync/download.rs
index be1df4d1e4..4b92f6e761 100644
--- a/rust/apps/cli/src/sync/download.rs
+++ b/rust/apps/cli/src/sync/download.rs
@@ -1,12 +1,12 @@
use crate::Result;
use crate::api::client::AppClient;
use crate::api::methods::ApiMethods;
+use crate::live_photo::extract_live_photo;
use crate::models::file::RemoteFile;
use base64::{Engine, engine::general_purpose::STANDARD as BASE64};
use ente_core::crypto;
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use std::collections::HashMap;
-use std::io::Cursor;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tokio::fs;
@@ -100,7 +100,7 @@ impl DownloadManager {
if is_live_photo {
// Extract live photo components
- if let Err(e) = self.extract_live_photo(&decrypted_data, destination).await {
+ if let Err(e) = extract_live_photo(&decrypted_data, destination).await {
log::warn!(
"Failed to extract live photo components for file {}, saving as ZIP: {}",
file.id,
@@ -328,78 +328,3 @@ impl DownloadStats {
}
}
}
-
-impl DownloadManager {
- /// Extract live photo components from a ZIP file
- async fn extract_live_photo(&self, zip_data: &[u8], output_path: &Path) -> Result<()> {
- use std::io::Read;
- use zip::ZipArchive;
-
- // Parse the ZIP archive
- let cursor = Cursor::new(zip_data);
- let mut archive = ZipArchive::new(cursor)?;
-
- // Get the parent directory and base name
- let parent_dir = output_path
- .parent()
- .ok_or_else(|| crate::Error::Generic("Invalid output path".into()))?;
-
- let base_name = output_path
- .file_stem()
- .and_then(|s| s.to_str())
- .ok_or_else(|| crate::Error::Generic("Invalid filename".into()))?;
-
- // Extract each file from the ZIP
- for i in 0..archive.len() {
- let mut file = archive.by_index(i)?;
- let file_name = file.name().to_string();
-
- // Determine the output filename based on the content
- // Match Go CLI behavior: check for "image" or "video" in filename
- let output_file_path = if file_name.to_lowercase().contains("image") {
- // Image component - preserve original extension
- let ext = std::path::Path::new(&file_name)
- .extension()
- .and_then(|e| e.to_str())
- .ok_or_else(|| {
- crate::Error::Generic(format!(
- "Live photo image component has no extension: {}",
- file_name
- ))
- })?;
- parent_dir.join(format!("{}.{}", base_name, ext))
- } else if file_name.to_lowercase().contains("video") {
- // Video component - preserve original extension
- let ext = std::path::Path::new(&file_name)
- .extension()
- .and_then(|e| e.to_str())
- .ok_or_else(|| {
- crate::Error::Generic(format!(
- "Live photo video component has no extension: {}",
- file_name
- ))
- })?;
- parent_dir.join(format!("{}.{}", base_name, ext))
- } else {
- // Go CLI returns error for unexpected files in live photo ZIP
- return Err(crate::Error::Generic(format!(
- "Unexpected file in live photo ZIP: {}",
- file_name
- )));
- };
-
- // Read the file contents
- let mut contents = Vec::new();
- file.read_to_end(&mut contents)?;
-
- // Write to disk
- let mut output_file = fs::File::create(&output_file_path).await?;
- output_file.write_all(&contents).await?;
- output_file.sync_all().await?;
-
- log::info!("Extracted live photo component: {:?}", output_file_path);
- }
-
- Ok(())
- }
-}