mbutrovich commented on code in PR #3700:
URL: https://github.com/apache/iceberg-rust/pull/3700#discussion_r4589709829
##########
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:
`check_stream_header` takes any slice and has an error for a bad length, but
`split_at(4)` on line 96 runs first and panics when the slice is shorter than 4
bytes. `check_stream_header(b"AG")` panics, and
`check_stream_header(b"AGS1\x00\x00\x10")` returns the length error. Could we
convert to a fixed-size array up front, so every bad length gets the error and
the rest of the function can't panic?
```suggestion
fn check_stream_header(header: &[u8]) -> Result<()> {
let header: &[u8; GCM_STREAM_HEADER_LENGTH as usize] = header
.try_into()
.map_err(|_| invalid_data!("Invalid AGS1 header length: {}",
header.len()))?;
let magic = &header[..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([header[4], header[5],
header[6], header[7]]);
```
##########
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:
Could we also run this against the empty stream (header plus one empty
block)? It reaches the header check only through the `plain_stream_size != 0`
exception in the early return at [lines
299-303](https://github.com/apache/iceberg-rust/blob/ae9d18d107dc74e7ef290d9016685f74f52283a5/crates/iceberg/src/encryption/stream.rs#L299-L303).
Without the header check, `read(0..0)` on an empty stream with a tampered
header returns `Ok` with no bytes, because GCM doesn't cover the header.
```suggestion
async fn test_invalid_header_is_rejected() {
let key = b"0123456789abcdef";
let aad_prefix = b"test-aad-prefix!";
for plaintext in [b"".as_slice(), b"sensitive data".as_slice()] {
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);
}
}
}
```
--
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]