xanderbailey commented on code in PR #3700:
URL: https://github.com/apache/iceberg-rust/pull/3700#discussion_r4590934026
##########
crates/iceberg/src/encryption/stream.rs:
##########
@@ -91,6 +91,29 @@ pub(crate) fn stream_block_aad(aad_prefix: &[u8],
block_index: u32) -> Vec<u8> {
}
}
+/// Checks that `header` is an AGS1 header declaring the supported plaintext
block size.
+fn check_stream_header(header: &[u8]) -> Result<()> {
+ let (magic, plain_block_size) = header.split_at(GCM_STREAM_MAGIC.len());
+ if magic != GCM_STREAM_MAGIC {
+ return Err(invalid_data!(
+ "Invalid AGS1 header: magic \"{}\" does not match \"AGS1\"",
+ magic.escape_ascii()
+ ));
+ }
+
+ let plain_block_size = u32::from_le_bytes(
+ plain_block_size
+ .try_into()
+ .map_err(|_| invalid_data!("Invalid AGS1 header length: {}",
header.len()))?,
+ );
Review Comment:
Went a step further: it now takes `&[u8; 8]`, split off with
`split_first_chunk`, so a short header doesn't compile. Fixed in c52cf46.
##########
crates/iceberg/src/encryption/stream.rs:
##########
@@ -934,6 +968,39 @@ mod tests {
assert!(result.is_err(), "Decryption with wrong key should fail");
}
+ #[tokio::test]
+ async fn test_invalid_header_is_rejected() {
+ let key = b"0123456789abcdef";
+ let aad_prefix = b"test-aad-prefix!";
+ let plaintext = b"sensitive data";
+ let encrypted = encrypt_ags1(plaintext, &make_cipher(key), aad_prefix);
+
+ for (header, message) in [
+ (
+ *b"AGS2\x00\x00\x10\x00",
+ "Invalid AGS1 header: magic \"AGS2\" does not match \"AGS1\"",
+ ),
+ (
+ *b"AGS1\x00\x02\x00\x00",
+ "Unsupported AGS1 block size: 512 (expected 1048576)",
+ ),
+ ] {
+ let mut tampered = encrypted.clone();
+ tampered[..GCM_STREAM_HEADER_LENGTH as
usize].copy_from_slice(&header);
+ let reader = AesGcmFileRead::new(
+ memory_reader(tampered),
+ Arc::new(make_cipher(key)),
+ aad_prefix.as_slice().into(),
+ encrypted.len() as u64,
+ )
+ .unwrap();
+
+ let err = reader.read(0..plaintext.len() as
u64).await.unwrap_err();
+ assert_eq!(err.kind(), ErrorKind::DataInvalid);
+ assert_eq!(err.message(), message);
+ }
+ }
Review Comment:
Done, thanks. Checked it fails on the empty stream without the header check.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]