Skip to content

armgit / ente

Public Imported from GitHub
0

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

Reject trailing data after final stream chunk

Manav Rathie676d64cdf04
diff --git a/rust/apps/ensu/src-tauri/src/commands.rs b/rust/apps/ensu/src-tauri/src/commands.rs
index 52b5c83218..487e032827 100644
--- a/rust/apps/ensu/src-tauri/src/commands.rs
+++ b/rust/apps/ensu/src-tauri/src/commands.rs
@@ -220,9 +220,11 @@ impl From<core_crypto::CryptoError> for ApiError {
E::StreamPushFailed => "stream_push_failed",
E::StreamPullFailed => "stream_pull_failed",
E::StreamTruncated => "stream_truncated",
+ E::StreamTrailingData => "stream_trailing_data",
E::SealedBoxOpenFailed => "sealed_box_open_failed",
E::InvalidPublicKey => "invalid_public_key",
E::HashFailed => "hash_failed",
+ E::Json(_) => "json",
E::Argon2(_) => "argon2",
E::Aead => "aead",
E::ArrayConversion => "array_conversion",
diff --git a/rust/core/src/crypto/error.rs b/rust/core/src/crypto/error.rs
index d7f3fe8520..cd45b92e7b 100644
--- a/rust/core/src/crypto/error.rs
+++ b/rust/core/src/crypto/error.rs
@@ -90,6 +90,10 @@ pub enum CryptoError {
#[error("Stream truncated: EOF before final tag")]
StreamTruncated,
+ /// Stream had trailing ciphertext after the final tag.
+ #[error("Stream has trailing data after final tag")]
+ StreamTrailingData,
+
/// Sealed box open failed.
#[error("Sealed box open failed")]
SealedBoxOpenFailed,
diff --git a/rust/core/src/crypto/impl_pure/stream.rs b/rust/core/src/crypto/impl_pure/stream.rs
index ff4d870d9a..10b44e6686 100644
--- a/rust/core/src/crypto/impl_pure/stream.rs
+++ b/rust/core/src/crypto/impl_pure/stream.rs
@@ -485,6 +485,19 @@ pub struct StreamingDecryptor<R: Read> {
seen_final: bool,
}
+fn ensure_reader_exhausted<R: Read>(reader: &mut R) -> Result<()> {
+ let mut extra = [0u8; 1];
+
+ loop {
+ match reader.read(&mut extra) {
+ Ok(0) => return Ok(()),
+ Ok(_) => return Err(CryptoError::StreamTrailingData),
+ Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
+ Err(e) => return Err(e.into()),
+ }
+ }
+}
+
impl<R: Read> StreamingDecryptor<R> {
/// Create a new streaming decryptor.
///
@@ -572,6 +585,7 @@ impl<R: Read> StreamingDecryptor<R> {
if tag == TAG_FINAL {
self.seen_final = true;
+ ensure_reader_exhausted(&mut self.reader)?;
self.finished = true;
}
@@ -746,6 +760,7 @@ pub fn encrypt_file_with_md5<R: Read, W: Write>(
/// # Errors
/// Returns `CryptoError::StreamTruncated` if EOF is reached without seeing TAG_FINAL.
/// This prevents silent truncation attacks at chunk boundaries.
+/// Returns `CryptoError::StreamTrailingData` if bytes remain after a TAG_FINAL chunk.
pub fn decrypt_file<R: Read, W: Write>(
reader: &mut R,
writer: &mut W,
@@ -784,6 +799,7 @@ pub fn decrypt_file<R: Read, W: Write>(
if tag == TAG_FINAL {
// Successfully decrypted the final chunk - stream is complete
+ ensure_reader_exhausted(reader)?;
return Ok(());
}
}
@@ -1714,6 +1730,27 @@ mod tests {
assert_eq!(output, plaintext);
}
+ #[test]
+ fn test_decrypt_file_rejects_trailing_data_after_exact_final_chunk() {
+ let key = generate_test_key();
+ let plaintext = vec![0xA5; ENCRYPTION_CHUNK_SIZE];
+
+ let mut encryptor = StreamEncryptor::new(&key).expect("encryptor creation failed");
+ let header = encryptor.header.clone();
+ let mut ciphertext = encryptor.push(&plaintext, true).expect("push failed");
+ ciphertext.extend_from_slice(b"TRAILING");
+
+ let mut reader = Cursor::new(&ciphertext);
+ let mut output = Vec::new();
+ let result = decrypt_file(&mut reader, &mut output, &header, &key);
+
+ assert!(
+ matches!(result, Err(CryptoError::StreamTrailingData)),
+ "Expected StreamTrailingData for trailing bytes after FINAL, got {:?}",
+ result
+ );
+ }
+
#[test]
fn test_decrypt_file_via_encrypt_file_roundtrip() {
// Verify that encrypt_file + decrypt_file work together properly
@@ -1754,6 +1791,25 @@ mod tests {
);
}
+ #[test]
+ fn test_decrypt_file_data_rejects_trailing_data_after_exact_final_chunk() {
+ let key = generate_test_key();
+ let plaintext = vec![0x5A; ENCRYPTION_CHUNK_SIZE];
+
+ let mut encryptor = StreamEncryptor::new(&key).expect("encryptor creation failed");
+ let header = encryptor.header.clone();
+ let mut ciphertext = encryptor.push(&plaintext, true).expect("push failed");
+ ciphertext.extend_from_slice(b"TRAILING");
+
+ let result = decrypt_file_data(&ciphertext, &header, &key);
+
+ assert!(
+ matches!(result, Err(CryptoError::StreamTrailingData)),
+ "Expected StreamTrailingData from decrypt_file_data, got {:?}",
+ result
+ );
+ }
+
#[test]
fn test_decrypt_oneshot_requires_final_tag() {
// Test that the one-shot decrypt() helper requires TAG_FINAL
@@ -1789,6 +1845,29 @@ mod tests {
assert_eq!(decrypted, plaintext);
}
+ #[test]
+ fn test_streaming_decryptor_rejects_trailing_data_after_exact_final_chunk() {
+ let key = generate_test_key();
+ let plaintext = vec![0x3C; ENCRYPTION_CHUNK_SIZE];
+
+ let mut encryptor = StreamEncryptor::new(&key).expect("encryptor creation failed");
+ let mut encrypted = encryptor.header.clone();
+ let mut ciphertext = encryptor.push(&plaintext, true).expect("push failed");
+ ciphertext.extend_from_slice(b"TRAILING");
+ encrypted.extend_from_slice(&ciphertext);
+
+ let reader = Cursor::new(&encrypted);
+ let mut decryptor =
+ StreamingDecryptor::new(&key, reader).expect("decryptor creation failed");
+ let result = decryptor.read_to_end();
+
+ assert!(
+ matches!(result, Err(CryptoError::StreamTrailingData)),
+ "Expected StreamTrailingData from StreamingDecryptor, got {:?}",
+ result
+ );
+ }
+
// ============ Size estimation consistency tests ============
#[test]
diff --git a/web/packages/wasm/src/crypto.rs b/web/packages/wasm/src/crypto.rs
index 8fed4bf8f3..538d5aded2 100644
--- a/web/packages/wasm/src/crypto.rs
+++ b/web/packages/wasm/src/crypto.rs
@@ -46,9 +46,11 @@ impl From<core_crypto::CryptoError> for CryptoError {
E::StreamPushFailed => "stream_push_failed",
E::StreamPullFailed => "stream_pull_failed",
E::StreamTruncated => "stream_truncated",
+ E::StreamTrailingData => "stream_trailing_data",
E::SealedBoxOpenFailed => "sealed_box_open_failed",
E::InvalidPublicKey => "invalid_public_key",
E::HashFailed => "hash_failed",
+ E::Json(_) => "json",
E::Argon2(_) => "argon2",
E::Aead => "aead",
E::ArrayConversion => "array_conversion",