Skip to content

Commit 8f8f5fa

Browse files
atishpatelcodex
andauthored
fix(media): sanitize animated image uploads (#2524)
Co-authored-by: Codex <[email protected]>
1 parent 55c9211 commit 8f8f5fa

10 files changed

Lines changed: 2035 additions & 171 deletions

File tree

‎crates/buzz-media/src/validation.rs‎

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,40 @@ fn validate_png_metadata_free(bytes: &[u8]) -> Result<(), MediaError> {
626626
}
627627

628628
fn validate_webp_metadata_free(bytes: &[u8]) -> Result<(), MediaError> {
629+
fn validate_frame_payload(payload: &[u8]) -> Result<(), MediaError> {
630+
const FRAME_HEADER_LEN: usize = 16;
631+
if payload.len() < FRAME_HEADER_LEN {
632+
return Err(MediaError::InvalidImage);
633+
}
634+
635+
let mut i = FRAME_HEADER_LEN;
636+
let mut saw_alpha = false;
637+
let mut saw_image = false;
638+
while i < payload.len() {
639+
if i + 8 > payload.len() {
640+
return Err(MediaError::InvalidImage);
641+
}
642+
let kind: [u8; 4] = payload[i..i + 4].try_into().unwrap();
643+
let len = u32::from_le_bytes(payload[i + 4..i + 8].try_into().unwrap()) as usize;
644+
let padded = len.checked_add(len & 1).ok_or(MediaError::InvalidImage)?;
645+
i = i
646+
.checked_add(8)
647+
.and_then(|start| start.checked_add(padded))
648+
.filter(|&end| end <= payload.len())
649+
.ok_or(MediaError::InvalidImage)?;
650+
651+
match &kind {
652+
b"ALPH" if !saw_alpha && !saw_image => saw_alpha = true,
653+
b"VP8 " if !saw_image => saw_image = true,
654+
b"VP8L" if !saw_alpha && !saw_image => saw_image = true,
655+
b"ALPH" | b"VP8 " | b"VP8L" => return Err(MediaError::InvalidImage),
656+
_ => return Err(MediaError::MetadataForbidden),
657+
}
658+
}
659+
660+
saw_image.then_some(()).ok_or(MediaError::InvalidImage)
661+
}
662+
629663
if bytes.len() < 12 || &bytes[..4] != b"RIFF" || &bytes[8..12] != b"WEBP" {
630664
return Err(MediaError::InvalidImage);
631665
}
@@ -659,6 +693,8 @@ fn validate_webp_metadata_free(bytes: &[u8]) -> Result<(), MediaError> {
659693
if flags & (0x20 | 0x08 | 0x04) != 0 {
660694
return Err(MediaError::MetadataForbidden);
661695
}
696+
} else if &kind == b"ANMF" {
697+
validate_frame_payload(&bytes[payload_start..payload_start + len])?;
662698
}
663699
}
664700
Ok(())
@@ -740,7 +776,13 @@ fn validate_gif_metadata_free(bytes: &[u8]) -> Result<(), MediaError> {
740776
return Err(MediaError::MetadataForbidden);
741777
}
742778
i += 12;
743-
skip_sub_blocks(bytes, &mut i)?;
779+
if bytes.get(i) != Some(&3)
780+
|| bytes.get(i + 1) != Some(&1)
781+
|| bytes.get(i + 4) != Some(&0)
782+
{
783+
return Err(MediaError::MetadataForbidden);
784+
}
785+
i += 5;
744786
}
745787
_ => return Err(MediaError::MetadataForbidden),
746788
}
@@ -1296,6 +1338,30 @@ mod tests {
12961338
Err(MediaError::MetadataForbidden)
12971339
));
12981340
}
1341+
let mut frame = vec![0; 16];
1342+
frame.extend_from_slice(b"VP8 ");
1343+
frame.extend_from_slice(&3u32.to_le_bytes());
1344+
frame.extend_from_slice(&[1, 2, 3, 0]);
1345+
let clean_frame = frame.clone();
1346+
frame.extend_from_slice(b"JUNK");
1347+
frame.extend_from_slice(&8u32.to_le_bytes());
1348+
frame.extend_from_slice(b"location");
1349+
let nested_metadata = webp(&[
1350+
(b"VP8X", &[0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
1351+
(b"ANIM", &[0; 6]),
1352+
(b"ANMF", &frame),
1353+
]);
1354+
assert!(matches!(
1355+
validate_webp_metadata_free(&nested_metadata),
1356+
Err(MediaError::MetadataForbidden)
1357+
));
1358+
let canonical_animation = webp(&[
1359+
(b"VP8X", &[0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
1360+
(b"ANIM", &[0; 6]),
1361+
(b"ANMF", &clean_frame),
1362+
]);
1363+
assert!(validate_webp_metadata_free(&canonical_animation).is_ok());
1364+
12991365
let mut trailing = webp(&[(b"VP8 ", b"pixels")]);
13001366
trailing.extend_from_slice(b"hidden");
13011367
assert!(matches!(
@@ -1327,6 +1393,23 @@ mod tests {
13271393
validate_gif_metadata_free(&trailing),
13281394
Err(MediaError::MetadataForbidden)
13291395
));
1396+
1397+
let mut hidden_in_loop = TINY_GIF[..TINY_GIF.len() - 1].to_vec();
1398+
hidden_in_loop.extend_from_slice(&[0x21, 0xff, 11]);
1399+
hidden_in_loop.extend_from_slice(b"NETSCAPE2.0");
1400+
hidden_in_loop.extend_from_slice(&[3, 1, 0, 0, 8]);
1401+
hidden_in_loop.extend_from_slice(b"location");
1402+
hidden_in_loop.extend_from_slice(&[0, 0x3b]);
1403+
assert!(matches!(
1404+
validate_gif_metadata_free(&hidden_in_loop),
1405+
Err(MediaError::MetadataForbidden)
1406+
));
1407+
1408+
let mut canonical_loop = TINY_GIF[..TINY_GIF.len() - 1].to_vec();
1409+
canonical_loop.extend_from_slice(&[0x21, 0xff, 11]);
1410+
canonical_loop.extend_from_slice(b"NETSCAPE2.0");
1411+
canonical_loop.extend_from_slice(&[3, 1, 0, 0, 0, 0x3b]);
1412+
assert!(validate_gif_metadata_free(&canonical_loop).is_ok());
13301413
}
13311414

13321415
#[test]

‎desktop/src-tauri/src/commands/media.rs‎

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ pub(crate) fn sanitize_filename(name: &str) -> String {
155155

156156
/// Return true when a PNG/WebP payload declares animation.
157157
///
158-
/// Animated payloads are left byte-identical here so frame timing, looping,
159-
/// and disposal semantics are preserved. The relay's structural validator is
160-
/// still the authority that rejects any metadata-bearing animation.
158+
/// Animated payloads use structural sanitizers so frame timing, looping, and
159+
/// disposal semantics are preserved without flattening the image. The relay's
160+
/// validator remains the final authority for the sanitized container.
161161
fn is_animated_image(body: &[u8], mime: &str) -> bool {
162162
match mime {
163163
"image/png" if body.starts_with(b"\x89PNG\r\n\x1a\n") => {
@@ -228,7 +228,40 @@ pub(crate) fn sanitize_image_for_upload(body: Vec<u8>, mime: &str) -> Result<Vec
228228
};
229229

230230
if is_animated_image(&body, mime) {
231-
return Ok(body);
231+
let oriented_format = match mime {
232+
"image/png" if super::media_animated::animated_png_uses_exif_orientation(&body) => {
233+
Some("PNG")
234+
}
235+
"image/webp" if super::media_animated::animated_webp_uses_exif_orientation(&body) => {
236+
Some("WebP")
237+
}
238+
_ => None,
239+
};
240+
if let Some(format) = oriented_format {
241+
return Err(format!(
242+
"animated {format} with EXIF orientation cannot be uploaded without changing its appearance"
243+
));
244+
}
245+
let color_profile_format = match mime {
246+
"image/png" if super::media_animated::animated_png_uses_icc_profile(&body) => {
247+
Some("PNG")
248+
}
249+
"image/webp" if super::media_animated::animated_webp_uses_icc_profile(&body) => {
250+
Some("WebP")
251+
}
252+
_ => None,
253+
};
254+
if let Some(format) = color_profile_format {
255+
return Err(format!(
256+
"animated {format} with an ICC profile cannot be uploaded without changing its colors"
257+
));
258+
}
259+
let stripped = match mime {
260+
"image/png" => super::media_animated::strip_animated_png_metadata(&body),
261+
"image/webp" => super::media_animated::strip_animated_webp_metadata(&body),
262+
_ => None,
263+
};
264+
return Ok(stripped.unwrap_or(body));
232265
}
233266

234267
use image::ImageDecoder;
@@ -900,18 +933,12 @@ mod tests {
900933
apng.extend_from_slice(&[0; 8]);
901934
apng.extend_from_slice(&[0; 4]);
902935
assert!(is_animated_image(&apng, "image/png"));
903-
assert_eq!(
904-
sanitize_image_for_upload(apng.clone(), "image/png").unwrap(),
905-
apng
906-
);
936+
assert!(sanitize_image_for_upload(apng, "image/png").is_ok());
907937

908938
let mut webp = b"RIFF\x0c\0\0\0WEBPANIM".to_vec();
909939
webp.extend_from_slice(&0u32.to_le_bytes());
910940
assert!(is_animated_image(&webp, "image/webp"));
911-
assert_eq!(
912-
sanitize_image_for_upload(webp.clone(), "image/webp").unwrap(),
913-
webp
914-
);
941+
assert!(sanitize_image_for_upload(webp, "image/webp").is_ok());
915942
}
916943

917944
#[test]

0 commit comments

Comments
 (0)