Skip to content

armgit / ente

Public Imported from GitHub
0

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

Isolate Ensu platform unsafe without adding dependencies

laurenspriem5ee3d60d784f
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index 5b49497860..94a2bb4ade 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -2371,14 +2371,13 @@ dependencies = [
"ente-core",
"ente-ensu",
"ente-ensu-crypto",
- "filepath",
"keyring",
+ "libc",
"log",
"notify",
"rustix",
"serde",
"serde_json",
- "sysinfo",
"tauri",
"tauri-build",
"tauri-plugin-dialog",
@@ -2574,7 +2573,6 @@ dependencies = [
"self_cell",
"serde",
"serde_json",
- "sha2 0.11.0",
"tempfile",
"thiserror 2.0.18",
"tokio",
@@ -3288,16 +3286,6 @@ dependencies = [
"rustc_version",
]
-[[package]]
-name = "filepath"
-version = "0.2.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f81dd23d67f94e07ac941a8da9bd0c2a82d3ea063b7d8a921d1dd05f9052a407"
-dependencies = [
- "libc",
- "windows 0.58.0",
-]
-
[[package]]
name = "filetime"
version = "0.2.27"
@@ -4436,7 +4424,7 @@ dependencies = [
"js-sys",
"log",
"wasm-bindgen",
- "windows-core 0.62.2",
+ "windows-core",
]
[[package]]
@@ -6341,15 +6329,6 @@ dependencies = [
"notify",
]
-[[package]]
-name = "ntapi"
-version = "0.4.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c3b335231dfd352ffb0f8017f3b6027a4917f7df785ea2143d8af2adc66980ae"
-dependencies = [
- "winapi",
-]
-
[[package]]
name = "num"
version = "0.4.3"
@@ -6610,16 +6589,6 @@ dependencies = [
"objc2-core-foundation",
]
-[[package]]
-name = "objc2-io-kit"
-version = "0.3.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "33fafba39597d6dc1fb709123dfa8289d39406734be322956a69f0931c73bb15"
-dependencies = [
- "libc",
- "objc2-core-foundation",
-]
-
[[package]]
name = "objc2-io-surface"
version = "0.3.2"
@@ -9262,20 +9231,6 @@ dependencies = [
"syn 2.0.117",
]
-[[package]]
-name = "sysinfo"
-version = "0.39.6"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d2071df9448915b71c4fe6d25deaf1c22f12bd234f01540b77312bb8e41361e6"
-dependencies = [
- "libc",
- "memchr",
- "ntapi",
- "objc2-core-foundation",
- "objc2-io-kit",
- "windows 0.62.2",
-]
-
[[package]]
name = "system-configuration"
version = "0.7.0"
@@ -9344,8 +9299,8 @@ dependencies = [
"tao-macros",
"unicode-segmentation",
"url",
- "windows 0.61.3",
- "windows-core 0.61.2",
+ "windows",
+ "windows-core",
"windows-version",
"x11-dl",
]
@@ -9432,7 +9387,7 @@ dependencies = [
"webkit2gtk",
"webview2-com",
"window-vibrancy",
- "windows 0.61.3",
+ "windows",
]
[[package]]
@@ -9573,7 +9528,7 @@ dependencies = [
"tauri-plugin",
"thiserror 2.0.18",
"url",
- "windows 0.61.3",
+ "windows",
"zbus 5.15.0",
]
@@ -9658,7 +9613,7 @@ dependencies = [
"url",
"webkit2gtk",
"webview2-com",
- "windows 0.61.3",
+ "windows",
]
[[package]]
@@ -9683,7 +9638,7 @@ dependencies = [
"url",
"webkit2gtk",
"webview2-com",
- "windows 0.61.3",
+ "windows",
"wry",
]
@@ -11011,10 +10966,10 @@ checksum = "7130243a7a5b33c54a444e54842e6a9e133de08b5ad7b5861cd8ed9a6a5bc96a"
dependencies = [
"webview2-com-macros",
"webview2-com-sys",
- "windows 0.61.3",
- "windows-core 0.61.2",
- "windows-implement 0.60.2",
- "windows-interface 0.59.3",
+ "windows",
+ "windows-core",
+ "windows-implement",
+ "windows-interface",
]
[[package]]
@@ -11035,8 +10990,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "381336cfffd772377d291702245447a5251a2ffa5bad679c99e61bc48bacbf9c"
dependencies = [
"thiserror 2.0.18",
- "windows 0.61.3",
- "windows-core 0.61.2",
+ "windows",
+ "windows-core",
]
[[package]]
@@ -11125,39 +11080,17 @@ dependencies = [
"num-traits",
]
-[[package]]
-name = "windows"
-version = "0.58.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "dd04d41d93c4992d421894c18c8b43496aa748dd4c081bac0dc93eb0489272b6"
-dependencies = [
- "windows-core 0.58.0",
- "windows-targets 0.52.6",
-]
-
[[package]]
name = "windows"
version = "0.61.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9babd3a767a4c1aef6900409f85f5d53ce2544ccdfaa86dad48c91782c6d6893"
dependencies = [
- "windows-collections 0.2.0",
- "windows-core 0.61.2",
- "windows-future 0.2.1",
+ "windows-collections",
+ "windows-core",
+ "windows-future",
"windows-link 0.1.3",
- "windows-numerics 0.2.0",
-]
-
-[[package]]
-name = "windows"
-version = "0.62.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "527fadee13e0c05939a6a05d5bd6eec6cd2e3dbd648b9f8e447c6518133d8580"
-dependencies = [
- "windows-collections 0.3.2",
- "windows-core 0.62.2",
- "windows-future 0.3.2",
- "windows-numerics 0.3.1",
+ "windows-numerics",
]
[[package]]
@@ -11166,29 +11099,7 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3beeceb5e5cfd9eb1d76b381630e82c4241ccd0d27f1a39ed41b2760b255c5e8"
dependencies = [
- "windows-core 0.61.2",
-]
-
-[[package]]
-name = "windows-collections"
-version = "0.3.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "23b2d95af1a8a14a3c7367e1ed4fc9c20e0a26e79551b1454d72583c97cc6610"
-dependencies = [
- "windows-core 0.62.2",
-]
-
-[[package]]
-name = "windows-core"
-version = "0.58.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6ba6d44ec8c2591c134257ce647b7ea6b20335bf6379a27dac5f1641fcf59f99"
-dependencies = [
- "windows-implement 0.58.0",
- "windows-interface 0.58.0",
- "windows-result 0.2.0",
- "windows-strings 0.1.0",
- "windows-targets 0.52.6",
+ "windows-core",
]
[[package]]
@@ -11197,57 +11108,22 @@ version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0fdd3ddb90610c7638aa2b3a3ab2904fb9e5cdbecc643ddb3647212781c4ae3"
dependencies = [
- "windows-implement 0.60.2",
- "windows-interface 0.59.3",
+ "windows-implement",
+ "windows-interface",
"windows-link 0.1.3",
"windows-result 0.3.4",
"windows-strings 0.4.2",
]
-[[package]]
-name = "windows-core"
-version = "0.62.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb"
-dependencies = [
- "windows-implement 0.60.2",
- "windows-interface 0.59.3",
- "windows-link 0.2.1",
- "windows-result 0.4.1",
- "windows-strings 0.5.1",
-]
-
[[package]]
name = "windows-future"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc6a41e98427b19fe4b73c550f060b59fa592d7d686537eebf9385621bfbad8e"
dependencies = [
- "windows-core 0.61.2",
+ "windows-core",
"windows-link 0.1.3",
- "windows-threading 0.1.0",
-]
-
-[[package]]
-name = "windows-future"
-version = "0.3.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e1d6f90251fe18a279739e78025bd6ddc52a7e22f921070ccdc67dde84c605cb"
-dependencies = [
- "windows-core 0.62.2",
- "windows-link 0.2.1",
- "windows-threading 0.2.1",
-]
-
-[[package]]
-name = "windows-implement"
-version = "0.58.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2bbd5b46c938e506ecbce286b6628a02171d56153ba733b6c741fc627ec9579b"
-dependencies = [
- "proc-macro2",
- "quote",
- "syn 2.0.117",
+ "windows-threading",
]
[[package]]
@@ -11261,17 +11137,6 @@ dependencies = [
"syn 2.0.117",
]
-[[package]]
-name = "windows-interface"
-version = "0.58.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "053c4c462dc91d3b1504c6fe5a726dd15e216ba718e84a0e46a88fbe5ded3515"
-dependencies = [
- "proc-macro2",
- "quote",
- "syn 2.0.117",
-]
-
[[package]]
name = "windows-interface"
version = "0.59.3"
@@ -11301,20 +11166,10 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9150af68066c4c5c07ddc0ce30421554771e528bde427614c61038bc2c92c2b1"
dependencies = [
- "windows-core 0.61.2",
+ "windows-core",
"windows-link 0.1.3",
]
-[[package]]
-name = "windows-numerics"
-version = "0.3.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6e2e40844ac143cdb44aead537bbf727de9b044e107a0f1220392177d15b0f26"
-dependencies = [
- "windows-core 0.62.2",
- "windows-link 0.2.1",
-]
-
[[package]]
name = "windows-registry"
version = "0.6.1"
@@ -11326,15 +11181,6 @@ dependencies = [
"windows-strings 0.5.1",
]
-[[package]]
-name = "windows-result"
-version = "0.2.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1d1043d8214f791817bab27572aaa8af63732e11bf84aa21a45a78d6c317ae0e"
-dependencies = [
- "windows-targets 0.52.6",
-]
-
[[package]]
name = "windows-result"
version = "0.3.4"
@@ -11353,16 +11199,6 @@ dependencies = [
"windows-link 0.2.1",
]
-[[package]]
-name = "windows-strings"
-version = "0.1.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10"
-dependencies = [
- "windows-result 0.2.0",
- "windows-targets 0.52.6",
-]
-
[[package]]
name = "windows-strings"
version = "0.4.2"
@@ -11507,15 +11343,6 @@ dependencies = [
"windows-link 0.1.3",
]
-[[package]]
-name = "windows-threading"
-version = "0.2.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "3949bd5b99cafdf1c7ca86b43ca564028dfe27d66958f2470940f73d86d75b37"
-dependencies = [
- "windows-link 0.2.1",
-]
-
[[package]]
name = "windows-version"
version = "0.1.7"
@@ -11807,8 +11634,8 @@ dependencies = [
"webkit2gtk",
"webkit2gtk-sys",
"webview2-com",
- "windows 0.61.3",
- "windows-core 0.61.2",
+ "windows",
+ "windows-core",
"windows-version",
"x11-dl",
]
diff --git a/rust/apps/ensu/src-tauri/Cargo.toml b/rust/apps/ensu/src-tauri/Cargo.toml
index ae43146b44..ddbc3584af 100644
--- a/rust/apps/ensu/src-tauri/Cargo.toml
+++ b/rust/apps/ensu/src-tauri/Cargo.toml
@@ -16,7 +16,6 @@ log.workspace = true
notify = "6.1.1"
serde.workspace = true
serde_json.workspace = true
-sysinfo = { version = "0.39.6", default-features = false, features = ["system"] }
tauri = { version = "2.11.2", features = [] }
tauri-plugin-dialog = "2.7.1"
tauri-plugin-fs = "2.5.1"
@@ -26,11 +25,11 @@ tauri-plugin-updater = "2.10.1"
uuid = { workspace = true, features = ["v4"] }
[target.'cfg(unix)'.dependencies]
+libc = "0.2.0"
rustix = { version = "1.1.4", features = ["fs"] }
[target.'cfg(windows)'.dependencies]
-filepath = "0.2.0"
-windows-sys = { version = "0.61.2", features = ["Win32_Foundation"] }
+windows-sys = { version = "0.61.2", features = ["Win32_Foundation", "Win32_Storage_FileSystem", "Win32_System_SystemInformation"] }
[target.'cfg(any(windows, target_os = "linux"))'.dependencies]
tauri-plugin-single-instance = "2.4.4"
@@ -38,8 +37,5 @@ tauri-plugin-single-instance = "2.4.4"
[features]
custom-protocol = ["tauri/custom-protocol"]
-[dev-dependencies]
-filepath = "0.2.0"
-
[lints]
workspace = true
diff --git a/rust/apps/ensu/src-tauri/src/commands/notes/source/scoped.rs b/rust/apps/ensu/src-tauri/src/commands/notes/source/scoped.rs
index e977972076..32b13554f7 100644
--- a/rust/apps/ensu/src-tauri/src/commands/notes/source/scoped.rs
+++ b/rust/apps/ensu/src-tauri/src/commands/notes/source/scoped.rs
@@ -1,9 +1,10 @@
+#[cfg(windows)]
use std::fs::File;
+#[cfg(windows)]
use std::io;
use std::path::Path;
-use filepath::FilePath;
-
+#[cfg(windows)]
pub(super) fn open(root: &Path, document_id: &str) -> io::Result<File> {
if !root.is_absolute() {
return Err(io::Error::new(
@@ -16,7 +17,7 @@ pub(super) fn open(root: &Path, document_id: &str) -> io::Result<File> {
path.push(component);
}
let file = File::open(path)?;
- if !path_is_beneath(&file.path()?, root) {
+ if !path_is_beneath(&crate::platform::final_handle_path(&file)?, root) {
return Err(io::Error::new(
io::ErrorKind::PermissionDenied,
"Source note escaped its collection",
@@ -64,14 +65,22 @@ fn comparison_path(path: &Path) -> Option<String> {
#[cfg(test)]
mod tests {
+ #[cfg(windows)]
use std::fs;
+ #[cfg(windows)]
use std::io::Read;
- use std::path::{Path, PathBuf};
+ use std::path::Path;
+ #[cfg(windows)]
+ use std::path::PathBuf;
- use super::{open, path_is_beneath};
+ #[cfg(windows)]
+ use super::open;
+ use super::path_is_beneath;
+ #[cfg(windows)]
struct Directory(PathBuf);
+ #[cfg(windows)]
impl Directory {
fn new() -> Self {
let directory =
@@ -84,12 +93,14 @@ mod tests {
}
}
+ #[cfg(windows)]
impl Drop for Directory {
fn drop(&mut self) {
let _ = fs::remove_dir_all(&self.0);
}
}
+ #[cfg(windows)]
#[test]
fn opens_nested_files_and_rejects_parent_and_absolute_paths() {
let directory = Directory::new();
@@ -104,28 +115,6 @@ mod tests {
assert!(open(&root, directory.0.join("outside/note.md").to_str().unwrap()).is_err());
}
- #[cfg(unix)]
- #[test]
- fn rejects_a_swapped_file_or_directory_symlink() {
- use std::os::unix::fs::symlink;
- let directory = Directory::new();
- let root = directory.0.join("root");
- fs::remove_file(root.join("nested/note.md")).unwrap();
- symlink(
- directory.0.join("outside/note.md"),
- root.join("nested/note.md"),
- )
- .unwrap();
- assert!(open(&root, "nested/note.md").is_err());
- fs::remove_file(root.join("nested/note.md")).unwrap();
- fs::remove_dir(root.join("nested")).unwrap();
- symlink(directory.0.join("outside"), root.join("nested")).unwrap();
- assert!(open(&root, "nested/note.md").is_err());
- fs::rename(&root, directory.0.join("original-root")).unwrap();
- symlink(directory.0.join("outside"), &root).unwrap();
- assert!(open(&root, "note.md").is_err());
- }
-
#[cfg(windows)]
#[test]
fn rejects_directory_junction_escapes_and_device_names() {
@@ -164,6 +153,7 @@ mod tests {
assert_eq!(error.raw_os_error(), None);
}
+ #[cfg(windows)]
#[test]
fn supports_unicode_names_and_reads_from_the_opened_handle() {
let directory = Directory::new();
@@ -177,6 +167,7 @@ mod tests {
assert_eq!(contents, "original");
}
+ #[cfg(windows)]
#[test]
fn supports_paths_longer_than_legacy_windows_limits() {
let directory = Directory::new();
@@ -225,6 +216,7 @@ mod tests {
}
}
+ #[cfg(windows)]
#[test]
fn reads_literal_reserved_windows_filenames() {
let directory = Directory::new();
diff --git a/rust/apps/ensu/src-tauri/src/commands/system.rs b/rust/apps/ensu/src-tauri/src/commands/system.rs
index 573f10fb8e..5b44f45c0a 100644
--- a/rust/apps/ensu/src-tauri/src/commands/system.rs
+++ b/rust/apps/ensu/src-tauri/src/commands/system.rs
@@ -7,17 +7,10 @@ pub struct SystemInfo {
total_memory_bytes: Option<u64>,
}
-fn total_memory_bytes() -> Option<u64> {
- let mut system = sysinfo::System::new();
- system.refresh_memory_specifics(sysinfo::MemoryRefreshKind::nothing().with_ram());
- let total = system.total_memory();
- (total > 0).then_some(total)
-}
-
#[tauri::command]
pub fn system_info() -> SystemInfo {
SystemInfo {
platform: std::env::consts::OS.to_string(),
- total_memory_bytes: total_memory_bytes(),
+ total_memory_bytes: crate::platform::total_memory_bytes(),
}
}
diff --git a/rust/apps/ensu/src-tauri/src/main.rs b/rust/apps/ensu/src-tauri/src/main.rs
index 8d7513be00..d0330eb87a 100644
--- a/rust/apps/ensu/src-tauri/src/main.rs
+++ b/rust/apps/ensu/src-tauri/src/main.rs
@@ -1,13 +1,18 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
-#![forbid(unsafe_code)]
+#![deny(unsafe_code)]
use tauri::{Manager, RunEvent, async_runtime};
+#[forbid(unsafe_code)]
mod commands;
+#[forbid(unsafe_code)]
mod logging;
+mod platform;
#[cfg(any(windows, target_os = "linux"))]
+#[forbid(unsafe_code)]
mod single_instance;
+#[forbid(unsafe_code)]
fn main() {
logging::install_panic_hook();
logging::log("App", "starting Tauri backend");
diff --git a/rust/apps/ensu/src-tauri/src/platform.rs b/rust/apps/ensu/src-tauri/src/platform.rs
new file mode 100644
index 0000000000..4f89110096
--- /dev/null
+++ b/rust/apps/ensu/src-tauri/src/platform.rs
@@ -0,0 +1,70 @@
+#[cfg(unix)]
+#[expect(
+ unsafe_code,
+ reason = "sysconf reads process-independent constants without caller-owned pointers"
+)]
+pub(crate) fn total_memory_bytes() -> Option<u64> {
+ let pages = unsafe { libc::sysconf(libc::_SC_PHYS_PAGES) };
+ let page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) };
+ if pages <= 0 || page_size <= 0 {
+ return None;
+ }
+ u64::try_from(pages)
+ .ok()?
+ .checked_mul(u64::try_from(page_size).ok()?)
+}
+
+#[cfg(windows)]
+#[expect(
+ unsafe_code,
+ reason = "GlobalMemoryStatusEx receives an initialized, correctly sized struct borrowed for this call"
+)]
+pub(crate) fn total_memory_bytes() -> Option<u64> {
+ use windows_sys::Win32::System::SystemInformation::{GlobalMemoryStatusEx, MEMORYSTATUSEX};
+
+ let mut status = MEMORYSTATUSEX {
+ dwLength: std::mem::size_of::<MEMORYSTATUSEX>() as u32,
+ ..Default::default()
+ };
+ let ok = unsafe { GlobalMemoryStatusEx(&mut status) };
+ (ok != 0 && status.ullTotalPhys > 0).then_some(status.ullTotalPhys)
+}
+
+#[cfg(not(any(unix, windows)))]
+pub(crate) fn total_memory_bytes() -> Option<u64> {
+ None
+}
+
+#[cfg(windows)]
+#[expect(
+ unsafe_code,
+ reason = "The File keeps its handle live; the size query has no output buffer and the fill receives the initialized buffer's exact capacity"
+)]
+pub(crate) fn final_handle_path(file: &std::fs::File) -> std::io::Result<std::path::PathBuf> {
+ use std::ffi::OsString;
+ use std::io;
+ use std::os::windows::ffi::OsStringExt;
+ use std::os::windows::io::AsRawHandle;
+ use std::path::PathBuf;
+ use windows_sys::Win32::Storage::FileSystem::{
+ FILE_NAME_NORMALIZED, GetFinalPathNameByHandleW, VOLUME_NAME_DOS,
+ };
+
+ let handle = file.as_raw_handle();
+ let flags = FILE_NAME_NORMALIZED | VOLUME_NAME_DOS;
+ let length = unsafe { GetFinalPathNameByHandleW(handle, std::ptr::null_mut(), 0, flags) };
+ if length == 0 {
+ return Err(io::Error::last_os_error());
+ }
+ let mut buffer = vec![0_u16; length as usize];
+ let written = unsafe { GetFinalPathNameByHandleW(handle, buffer.as_mut_ptr(), length, flags) };
+ if written == 0 {
+ return Err(io::Error::last_os_error());
+ }
+ if written >= length {
+ return Err(io::Error::other("Source path changed while being resolved"));
+ }
+ Ok(PathBuf::from(OsString::from_wide(
+ &buffer[..written as usize],
+ )))
+}
diff --git a/rust/crates/ensu-crypto/src/lib.rs b/rust/crates/ensu-crypto/src/lib.rs
index 75ae9bd599..17b59261ea 100644
--- a/rust/crates/ensu-crypto/src/lib.rs
+++ b/rust/crates/ensu-crypto/src/lib.rs
@@ -1,3 +1,5 @@
+#![forbid(unsafe_code)]
+
use ente_core::{
b64,
crypto::{self, Header, Key, blob},
@@ -33,16 +35,33 @@ pub struct EncryptedChatPayload {
pub header: String,
}
+#[derive(Default)]
+pub struct Sha256Hasher(Sha256);
+
+impl Sha256Hasher {
+ pub fn new() -> Self {
+ Self::default()
+ }
+
+ pub fn update(&mut self, bytes: impl AsRef<[u8]>) {
+ self.0.update(bytes);
+ }
+
+ pub fn finalize(self) -> [u8; 32] {
+ self.0.finalize().into()
+ }
+}
+
pub fn sha256(bytes: &[u8]) -> [u8; 32] {
sha256_parts(&[bytes])
}
pub fn sha256_parts(parts: &[&[u8]]) -> [u8; 32] {
- let mut hash = Sha256::new();
+ let mut hash = Sha256Hasher::new();
for bytes in parts {
hash.update(bytes);
}
- hash.finalize().into()
+ hash.finalize()
}
pub fn generate_chat_key() -> String {
@@ -159,6 +178,37 @@ fn attachment_key(chat_key_b64: &str, session_uuid: &str) -> Result<Key> {
mod tests {
use super::*;
+ #[test]
+ fn streaming_sha256_matches_known_vectors() {
+ for (input, expected) in [
+ (
+ "",
+ "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
+ ),
+ (
+ "abc",
+ "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
+ ),
+ (
+ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
+ "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1",
+ ),
+ ] {
+ for chunk_size in [1, 7, 64] {
+ let mut hash = Sha256Hasher::new();
+ for chunk in input.as_bytes().chunks(chunk_size) {
+ hash.update(chunk);
+ }
+ let digest: String = hash
+ .finalize()
+ .iter()
+ .map(|byte| format!("{byte:02x}"))
+ .collect();
+ assert_eq!(digest, expected);
+ }
+ }
+ }
+
#[test]
fn payload_roundtrip() {
let key = generate_chat_key();
diff --git a/rust/crates/ensu/Cargo.toml b/rust/crates/ensu/Cargo.toml
index f5566bfea2..d5d4cf9188 100644
--- a/rust/crates/ensu/Cargo.toml
+++ b/rust/crates/ensu/Cargo.toml
@@ -13,7 +13,6 @@ rusqlite = { workspace = true, default-features = true, optional = true }
self_cell = "1.0.0"
serde.workspace = true
serde_json.workspace = true
-sha2.workspace = true
thiserror.workspace = true
tokio = { workspace = true, features = ["rt", "time"] }
transcribe-rs = { git = "https://github.com/cjpais/transcribe-rs.git", rev = "efc66111d519a434e28d51d94740008a7ce29360", default-features = false, features = ["onnx", "vad-silero"], optional = true }
diff --git a/rust/crates/ensu/src/retrieval/index.rs b/rust/crates/ensu/src/retrieval/index.rs
index 59ca50bb8c..22ffb575da 100644
--- a/rust/crates/ensu/src/retrieval/index.rs
+++ b/rust/crates/ensu/src/retrieval/index.rs
@@ -5,8 +5,8 @@ use std::io::{Read, Seek, SeekFrom};
use std::path::Path;
use std::sync::{Mutex, MutexGuard, OnceLock};
+use ente_ensu_crypto::Sha256Hasher;
use serde::Deserialize;
-use sha2::{Digest, Sha256};
use url::Url;
use crate::config::{
@@ -237,7 +237,7 @@ impl RetrievalIndex {
return Ok(digest);
}
self.metadata.verify()?;
- let mut hash = Sha256::new();
+ let mut hash = Sha256Hasher::new();
hash.update(b"ensu-pack-text-revision-v1\0");
hash.update((self.manifest_bytes.len() as u64).to_le_bytes());
hash.update(&self.manifest_bytes);