|
| 1 | +//! Structural GIF metadata stripping for the upload sanitizer. |
| 2 | +//! |
| 3 | +//! Split out of `media.rs` to keep that file under the desktop line-size |
| 4 | +//! limit. The relay rejects media carrying metadata (`MetadataForbidden` in |
| 5 | +//! buzz-media's `validate_gif_metadata_free`); these helpers drop the GIF |
| 6 | +//! metadata channels it forbids without re-encoding, so animation timing, |
| 7 | +//! disposal, and pixel data survive byte-identical. |
| 8 | +
|
| 9 | +/// Walk length-prefixed GIF data sub-blocks starting at `i`; return the index |
| 10 | +/// just past the block terminator. |
| 11 | +fn gif_sub_blocks_end(body: &[u8], mut i: usize) -> Option<usize> { |
| 12 | + loop { |
| 13 | + let len = *body.get(i)? as usize; |
| 14 | + i += 1; |
| 15 | + if len == 0 { |
| 16 | + return Some(i); |
| 17 | + } |
| 18 | + i = i.checked_add(len).filter(|&end| end <= body.len())?; |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +/// Strip metadata channels from a GIF without re-encoding. |
| 23 | +/// |
| 24 | +/// GIF carries three unrestricted metadata channels — comment extensions |
| 25 | +/// (0xFE), plain-text extensions (0x01), and application extensions (0xFF) |
| 26 | +/// other than the standard NETSCAPE2.0/ANIMEXTS1.0 looping ones. The relay |
| 27 | +/// rejects all of them (`MetadataForbidden` in buzz-media's |
| 28 | +/// `validate_gif_metadata_free`), and encoders like Photoshop and Giphy emit |
| 29 | +/// them routinely, so uploads fail unless the client drops them first. |
| 30 | +/// |
| 31 | +/// Everything the relay accepts — header, colour tables, graphic-control |
| 32 | +/// extensions, image descriptors and frame data, standard looping extensions — |
| 33 | +/// is copied verbatim, so animation timing, disposal, and pixel data stay |
| 34 | +/// byte-identical. Trailing bytes after the trailer (another relay reject) are |
| 35 | +/// truncated. Returns `None` when the payload isn't structurally parseable as |
| 36 | +/// GIF; the caller then uploads the original bytes and the relay's validator |
| 37 | +/// remains the authority. |
| 38 | +pub(crate) fn strip_gif_metadata(body: &[u8]) -> Option<Vec<u8>> { |
| 39 | + if !(body.starts_with(b"GIF87a") || body.starts_with(b"GIF89a")) || body.len() < 13 { |
| 40 | + return None; |
| 41 | + } |
| 42 | + |
| 43 | + let packed = body[10]; |
| 44 | + let mut i = 13usize; |
| 45 | + if packed & 0x80 != 0 { |
| 46 | + let table_len = 3usize << ((packed & 0x07) as usize + 1); |
| 47 | + i = i.checked_add(table_len).filter(|&end| end <= body.len())?; |
| 48 | + } |
| 49 | + |
| 50 | + let mut out = Vec::with_capacity(body.len()); |
| 51 | + out.extend_from_slice(&body[..i]); |
| 52 | + |
| 53 | + loop { |
| 54 | + match *body.get(i)? { |
| 55 | + // Image descriptor: optional local colour table, LZW minimum code |
| 56 | + // size, then image-data sub-blocks. Copied verbatim. |
| 57 | + 0x2c => { |
| 58 | + if i + 10 > body.len() { |
| 59 | + return None; |
| 60 | + } |
| 61 | + let image_packed = body[i + 9]; |
| 62 | + let mut end = i + 10; |
| 63 | + if image_packed & 0x80 != 0 { |
| 64 | + let table_len = 3usize << ((image_packed & 0x07) as usize + 1); |
| 65 | + end = end.checked_add(table_len).filter(|&e| e <= body.len())?; |
| 66 | + } |
| 67 | + end = end.checked_add(1).filter(|&e| e <= body.len())?; |
| 68 | + end = gif_sub_blocks_end(body, end)?; |
| 69 | + out.extend_from_slice(&body[i..end]); |
| 70 | + i = end; |
| 71 | + } |
| 72 | + 0x21 => { |
| 73 | + let label = *body.get(i + 1)?; |
| 74 | + let start = i; |
| 75 | + i += 2; |
| 76 | + match label { |
| 77 | + // Graphic Control Extension: fixed-shape rendering state |
| 78 | + // (delay, disposal, transparency). Kept verbatim. |
| 79 | + 0xf9 => { |
| 80 | + if body.get(i) != Some(&4) || i + 6 > body.len() || body[i + 5] != 0 { |
| 81 | + return None; |
| 82 | + } |
| 83 | + i += 6; |
| 84 | + out.extend_from_slice(&body[start..i]); |
| 85 | + } |
| 86 | + // Application extension: keep only the standard looping |
| 87 | + // extensions; anything else (XMP, Photoshop, Giphy…) is a |
| 88 | + // metadata channel and is dropped. |
| 89 | + 0xff => { |
| 90 | + if body.get(i) != Some(&11) || i + 12 > body.len() { |
| 91 | + return None; |
| 92 | + } |
| 93 | + let app = &body[i + 1..i + 12]; |
| 94 | + let keep = app == b"NETSCAPE2.0" || app == b"ANIMEXTS1.0"; |
| 95 | + i = gif_sub_blocks_end(body, i + 12)?; |
| 96 | + if keep { |
| 97 | + out.extend_from_slice(&body[start..i]); |
| 98 | + } |
| 99 | + } |
| 100 | + // Comment (0xFE), plain-text (0x01), and unknown |
| 101 | + // extensions: pure metadata channels, dropped. Their |
| 102 | + // bodies are all length-prefixed sub-block sequences |
| 103 | + // (plain-text's 12-byte header is itself a sub-block). |
| 104 | + _ => { |
| 105 | + i = gif_sub_blocks_end(body, i)?; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + // Trailer: emit and stop, truncating any trailing bytes. |
| 110 | + 0x3b => { |
| 111 | + out.push(0x3b); |
| 112 | + return Some(out); |
| 113 | + } |
| 114 | + _ => return None, |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +#[cfg(test)] |
| 120 | +mod tests { |
| 121 | + use super::*; |
| 122 | + use crate::commands::media::sanitize_image_for_upload; |
| 123 | + |
| 124 | + /// Minimal single-frame GIF89a: header, 2×2 logical screen, 2-entry global |
| 125 | + /// colour table, NETSCAPE looping extension, graphic control extension, |
| 126 | + /// one image descriptor with LZW data, trailer. Structurally canonical — |
| 127 | + /// the relay's validator accepts exactly this shape. |
| 128 | + fn minimal_gif() -> Vec<u8> { |
| 129 | + let mut gif = b"GIF89a".to_vec(); |
| 130 | + gif.extend_from_slice(&[ |
| 131 | + 0x02, 0x00, 0x02, 0x00, // logical screen 2×2 |
| 132 | + 0x80, 0x00, 0x00, // GCT flag, 2 entries |
| 133 | + 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, // colour table |
| 134 | + ]); |
| 135 | + // NETSCAPE2.0 looping application extension. |
| 136 | + gif.extend_from_slice(&[0x21, 0xff, 11]); |
| 137 | + gif.extend_from_slice(b"NETSCAPE2.0"); |
| 138 | + gif.extend_from_slice(&[3, 0x01, 0x00, 0x00, 0x00]); |
| 139 | + // Graphic control extension. |
| 140 | + gif.extend_from_slice(&[0x21, 0xf9, 4, 0x00, 0x0a, 0x00, 0x00, 0x00]); |
| 141 | + // Image descriptor + 2-bit LZW data. |
| 142 | + gif.extend_from_slice(&[0x2c, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00]); |
| 143 | + gif.extend_from_slice(&[0x02, 0x02, 0x44, 0x01, 0x00]); |
| 144 | + gif.push(0x3b); |
| 145 | + gif |
| 146 | + } |
| 147 | + |
| 148 | + /// Comment extension: `0x21 0xFE`, one data sub-block, terminator. |
| 149 | + fn gif_comment_ext() -> Vec<u8> { |
| 150 | + let mut ext = vec![0x21, 0xfe, 5]; |
| 151 | + ext.extend_from_slice(b"hello"); |
| 152 | + ext.push(0); |
| 153 | + ext |
| 154 | + } |
| 155 | + |
| 156 | + /// XMP-style application extension (what Photoshop/Giphy emit). |
| 157 | + fn gif_xmp_ext() -> Vec<u8> { |
| 158 | + let mut ext = vec![0x21, 0xff, 11]; |
| 159 | + ext.extend_from_slice(b"XMP DataXMP"); |
| 160 | + ext.extend_from_slice(&[4]); |
| 161 | + ext.extend_from_slice(b"<x/>"); |
| 162 | + ext.push(0); |
| 163 | + ext |
| 164 | + } |
| 165 | + |
| 166 | + #[test] |
| 167 | + fn test_strip_gif_metadata_removes_comment_and_foreign_app_extensions() { |
| 168 | + let clean = minimal_gif(); |
| 169 | + // Splice metadata extensions after the global colour table (offset 19). |
| 170 | + let mut dirty = clean[..19].to_vec(); |
| 171 | + dirty.extend_from_slice(&gif_comment_ext()); |
| 172 | + dirty.extend_from_slice(&gif_xmp_ext()); |
| 173 | + dirty.extend_from_slice(&clean[19..]); |
| 174 | + |
| 175 | + let stripped = strip_gif_metadata(&dirty).unwrap(); |
| 176 | + assert_eq!(stripped, clean); |
| 177 | + } |
| 178 | + |
| 179 | + #[test] |
| 180 | + fn test_strip_gif_metadata_preserves_clean_gif_byte_identical() { |
| 181 | + let clean = minimal_gif(); |
| 182 | + assert_eq!(strip_gif_metadata(&clean).unwrap(), clean); |
| 183 | + } |
| 184 | + |
| 185 | + #[test] |
| 186 | + fn test_strip_gif_metadata_truncates_bytes_after_trailer() { |
| 187 | + let mut padded = minimal_gif(); |
| 188 | + padded.extend_from_slice(b"junk after trailer"); |
| 189 | + assert_eq!(strip_gif_metadata(&padded).unwrap(), minimal_gif()); |
| 190 | + } |
| 191 | + |
| 192 | + #[test] |
| 193 | + fn test_strip_gif_metadata_rejects_unparseable_payloads() { |
| 194 | + // Not a GIF at all. |
| 195 | + assert!(strip_gif_metadata(b"GIF89a").is_none()); |
| 196 | + assert!(strip_gif_metadata(b"\x89PNG\r\n\x1a\n").is_none()); |
| 197 | + // Truncated mid-structure. |
| 198 | + let clean = minimal_gif(); |
| 199 | + assert!(strip_gif_metadata(&clean[..clean.len() - 3]).is_none()); |
| 200 | + } |
| 201 | + |
| 202 | + #[test] |
| 203 | + fn test_sanitize_gif_strips_metadata_and_passes_through_unparseable() { |
| 204 | + let clean = minimal_gif(); |
| 205 | + let mut dirty = clean[..19].to_vec(); |
| 206 | + dirty.extend_from_slice(&gif_comment_ext()); |
| 207 | + dirty.extend_from_slice(&clean[19..]); |
| 208 | + assert_eq!( |
| 209 | + sanitize_image_for_upload(dirty, "image/gif").unwrap(), |
| 210 | + clean |
| 211 | + ); |
| 212 | + |
| 213 | + // Unparseable GIF payloads pass through unchanged — the relay's |
| 214 | + // validator stays the authority. |
| 215 | + let junk = b"GIF89a\x00\x01".to_vec(); |
| 216 | + assert_eq!( |
| 217 | + sanitize_image_for_upload(junk.clone(), "image/gif").unwrap(), |
| 218 | + junk |
| 219 | + ); |
| 220 | + } |
| 221 | +} |
0 commit comments