mdk-core 0.8.0

A simplified interface to build secure messaging apps on nostr with MLS.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
//! Image validation functions
//!
//! This module provides validation functions for image files, MIME types,
//! filenames, and other input parameters to ensure they meet security
//! and protocol requirements.

use std::io::Cursor;

use image::ImageReader;

use crate::media_processing::types::{
    MAX_FILE_SIZE, MAX_IMAGE_MEMORY_MB, MAX_IMAGE_PIXELS, MediaProcessingError,
    MediaProcessingOptions,
};

#[cfg(feature = "mip04")]
use crate::media_processing::types::MAX_FILENAME_LENGTH;

/// Supported MIME types for encrypted media upload
///
/// This allowlist restricts the types of media that can be encrypted and uploaded,
/// preventing spoofing and ensuring only supported formats are processed.
#[cfg(feature = "mip04")]
pub(crate) const SUPPORTED_MIME_TYPES: &[&str] = &[
    // Image types
    "image/png",
    "image/jpeg",
    "image/gif",
    "image/webp",
    "image/bmp",
    "image/x-icon",
    "image/tiff",
    "image/x-farbfeld",
    "image/avif",
    "image/qoi",
    // Video types
    "video/mp4",
    "video/quicktime",
    "video/x-matroska",
    "video/webm",
    "video/x-msvideo",
    "video/ogg",
    // Audio types
    "audio/ogg",
    "audio/flac",
    "audio/x-flac",
    "audio/aac",
    "audio/mp4",
    "audio/webm",
    "audio/mpeg",
    "audio/wav",
    "audio/x-matroska",
    // Document types
    "application/pdf",
    "text/plain",
];

/// Escape hatch MIME type that allows applications to skip validation
///
/// When an application uses this MIME type, MDK will not validate the file type,
/// allowing the application to handle validation themselves. This is useful for
/// applications that need to support file types not in the allowlist.
///
/// **Warning**: Using this type means MDK provides no validation - the application
/// is responsible for ensuring the file is safe to process.
#[cfg(feature = "mip04")]
pub(crate) const ESCAPE_HATCH_MIME_TYPE: &str = "application/octet-stream";

/// Supported MIME types for group images (protocol-level avatars/icons)
///
/// Group images are stored in the group data extension and are protocol-level,
/// so they must be strictly validated. Only safe image formats are allowed.
pub(crate) const GROUP_IMAGE_MIME_TYPES: &[&str] = &[
    "image/png",
    "image/jpeg",
    "image/gif",
    "image/webp",
    "image/bmp",
    "image/x-icon",
    "image/tiff",
    "image/x-farbfeld",
    "image/avif",
    "image/qoi",
];

/// Validate file size against limits
pub(crate) fn validate_file_size(
    data: &[u8],
    options: &MediaProcessingOptions,
) -> Result<(), MediaProcessingError> {
    let max_size = options.max_file_size.unwrap_or(MAX_FILE_SIZE);
    if data.len() > max_size {
        return Err(MediaProcessingError::FileTooLarge {
            size: data.len(),
            max_size,
        });
    }
    Ok(())
}

/// Validate MIME type format and allowlist
///
/// Returns the canonical (trimmed, lowercase, and parameter-stripped) MIME type
/// for consistent use in cryptographic operations and comparisons.
///
/// This is the centralized function for MIME type canonicalization used throughout
/// the image processing system. All MIME type processing should use this function
/// to ensure consistency in encryption keys, AAD construction, and metadata.
///
/// Per MIP-04, this function strips all parameters after the semicolon, returning
/// only the type/subtype portion (e.g., "image/png; charset=utf-8" -> "image/png").
///
/// This function enforces the supported MIME types allowlist, but allows
/// `application/octet-stream` as an escape hatch for applications that need
/// to handle validation themselves.
#[cfg(feature = "mip04")]
pub(crate) fn validate_mime_type(mime_type: &str) -> Result<String, MediaProcessingError> {
    // Normalize the MIME type: trim whitespace and convert to lowercase
    let normalized = mime_type.trim().to_ascii_lowercase();

    // Strip parameters after semicolon per MIP-04 canonicalization requirements
    // Split on ';' and take only the first part (type/subtype)
    let canonical = normalized.split(';').next().unwrap_or(&normalized).trim();

    // Validate MIME type format using canonical version
    if !canonical.contains('/') || canonical.len() > 100 {
        return Err(MediaProcessingError::InvalidMimeType {
            mime_type: mime_type.to_string(),
        });
    }

    // Allow escape hatch for applications that want to handle validation themselves
    if canonical == ESCAPE_HATCH_MIME_TYPE {
        return Ok(canonical.to_string());
    }

    // Enforce allowlist
    if !SUPPORTED_MIME_TYPES.contains(&canonical) {
        return Err(MediaProcessingError::InvalidMimeType {
            mime_type: canonical.to_string(),
        });
    }

    Ok(canonical.to_string())
}

/// Validate MIME type for group images (strict validation, no escape hatch)
///
/// This function validates MIME types for group images stored in the group data extension.
/// Group images are protocol-level avatars/icons and must be strictly validated - only
/// safe image formats are allowed. The escape hatch is not available for group images.
///
/// Returns the canonical (trimmed, lowercase, and parameter-stripped) MIME type.
pub(crate) fn validate_group_image_mime_type(
    mime_type: &str,
) -> Result<String, MediaProcessingError> {
    // Normalize the MIME type: trim whitespace and convert to lowercase
    let normalized = mime_type.trim().to_ascii_lowercase();

    // Strip parameters after semicolon per MIP-04 canonicalization requirements
    // Split on ';' and take only the first part (type/subtype)
    let canonical = normalized.split(';').next().unwrap_or(&normalized).trim();

    // Validate MIME type format using canonical version
    if !canonical.contains('/') || canonical.len() > 100 {
        return Err(MediaProcessingError::InvalidMimeType {
            mime_type: mime_type.to_string(),
        });
    }

    // Enforce strict allowlist for group images (no escape hatch)
    if !GROUP_IMAGE_MIME_TYPES.contains(&canonical) {
        return Err(MediaProcessingError::InvalidMimeType {
            mime_type: canonical.to_string(),
        });
    }

    Ok(canonical.to_string())
}

/// Detect the actual MIME type from image file data
///
/// This function uses the `image` crate to detect the actual format of the image
/// by inspecting the file header/magic bytes, rather than trusting the provided MIME type.
///
/// # Arguments
/// * `data` - The image file data
///
/// # Returns
/// * The detected MIME type in canonical form (lowercase)
///
/// # Errors
/// * `InvalidMimeType` - If the image format cannot be detected
fn detect_mime_type_from_data(data: &[u8]) -> Result<String, MediaProcessingError> {
    let img_reader = ImageReader::new(Cursor::new(data))
        .with_guessed_format()
        .map_err(|e| MediaProcessingError::InvalidMimeType {
            mime_type: format!("Could not detect image format: {}", e),
        })?;

    let format = img_reader
        .format()
        .ok_or_else(|| MediaProcessingError::InvalidMimeType {
            mime_type: "Could not determine image format".to_string(),
        })?;

    // Convert image::ImageFormat to MIME type
    let mime_type = match format {
        image::ImageFormat::Png => "image/png",
        image::ImageFormat::Jpeg => "image/jpeg",
        image::ImageFormat::Gif => "image/gif",
        image::ImageFormat::WebP => "image/webp",
        image::ImageFormat::Bmp => "image/bmp",
        image::ImageFormat::Ico => "image/x-icon",
        image::ImageFormat::Tiff => "image/tiff",
        image::ImageFormat::Tga => "image/x-tga",
        image::ImageFormat::Dds => "image/vnd-ms.dds",
        image::ImageFormat::Hdr => "image/vnd.radiance",
        image::ImageFormat::OpenExr => "image/x-exr",
        image::ImageFormat::Pnm => "image/x-portable-anymap",
        image::ImageFormat::Farbfeld => "image/x-farbfeld",
        image::ImageFormat::Avif => "image/avif",
        image::ImageFormat::Qoi => "image/qoi",
        _ => {
            return Err(MediaProcessingError::InvalidMimeType {
                mime_type: format!("Unsupported image format: {:?}", format),
            });
        }
    };

    Ok(mime_type.to_string())
}

/// Validate that the provided MIME type matches the actual file data
///
/// This function detects the actual image format from the file data and compares
/// it with the provided MIME type. This prevents MIME type confusion attacks where
/// an attacker provides a misleading MIME type.
///
/// # Arguments
/// * `data` - The image file data
/// * `claimed_mime_type` - The MIME type claimed by the application
///
/// # Returns
/// * The canonical MIME type (validated and normalized)
///
/// # Errors
/// * `InvalidMimeType` - If the MIME type format is invalid
/// * `MimeTypeMismatch` - If the claimed MIME type doesn't match the detected format
///
/// # Security
/// This function protects against MIME type confusion attacks by verifying that the
/// claimed MIME type matches the actual file content. Applications cannot lie about
/// the file type.
///
/// Note: If the claimed MIME type is the escape hatch (`application/octet-stream`),
/// this function will skip byte validation and return the escape hatch type.
#[cfg(feature = "mip04")]
pub(crate) fn validate_mime_type_matches_data(
    data: &[u8],
    claimed_mime_type: &str,
) -> Result<String, MediaProcessingError> {
    // First, validate and canonicalize the claimed MIME type
    let canonical_claimed = validate_mime_type(claimed_mime_type)?;

    // If escape hatch is used, skip byte validation (application handles it)
    if canonical_claimed == ESCAPE_HATCH_MIME_TYPE {
        return Ok(canonical_claimed);
    }

    // Detect the actual MIME type from the file data
    let detected_mime_type = detect_mime_type_from_data(data)?;

    // Compare the claimed type with the detected type
    if canonical_claimed != detected_mime_type {
        return Err(MediaProcessingError::MimeTypeMismatch {
            claimed: canonical_claimed,
            detected: detected_mime_type,
        });
    }

    Ok(canonical_claimed)
}

/// Validate that the provided MIME type matches the actual file data for group images
///
/// This function validates group image MIME types with strict validation (no escape hatch).
/// Group images are protocol-level and must be strictly validated.
///
/// # Arguments
/// * `data` - The image file data
/// * `claimed_mime_type` - The MIME type claimed by the application
///
/// # Returns
/// * The canonical MIME type (validated and normalized)
///
/// # Errors
/// * `InvalidMimeType` - If the MIME type format is invalid or not in the group image allowlist
/// * `MimeTypeMismatch` - If the claimed MIME type doesn't match the detected format
pub(crate) fn validate_group_image_mime_type_matches_data(
    data: &[u8],
    claimed_mime_type: &str,
) -> Result<String, MediaProcessingError> {
    // First, validate and canonicalize the claimed MIME type (strict validation)
    let canonical_claimed = validate_group_image_mime_type(claimed_mime_type)?;

    // Detect the actual MIME type from the file data
    let detected_mime_type = detect_mime_type_from_data(data)?;

    // Compare the claimed type with the detected type
    if canonical_claimed != detected_mime_type {
        return Err(MediaProcessingError::MimeTypeMismatch {
            claimed: canonical_claimed,
            detected: detected_mime_type,
        });
    }

    Ok(canonical_claimed)
}

/// Validate filename length and content
#[cfg(feature = "mip04")]
pub(crate) fn validate_filename(filename: &str) -> Result<(), MediaProcessingError> {
    // Validate filename is not empty
    if filename.is_empty() {
        return Err(MediaProcessingError::EmptyFilename);
    }

    // Validate filename length
    if filename.len() > MAX_FILENAME_LENGTH {
        return Err(MediaProcessingError::FilenameTooLong {
            length: filename.len(),
            max_length: MAX_FILENAME_LENGTH,
        });
    }

    // Disallow path separators and control characters
    if filename.contains('/') || filename.contains('\\') || filename.chars().any(|c| c.is_control())
    {
        return Err(MediaProcessingError::InvalidFilename);
    }

    Ok(())
}

/// Validate image dimensions against limits
///
/// This function checks both dimension limits and memory requirements to prevent
/// decompression bombs. Even if individual dimensions are within limits, the total
/// pixel count and estimated memory usage must also be reasonable.
pub(crate) fn validate_image_dimensions(
    width: u32,
    height: u32,
    options: &MediaProcessingOptions,
) -> Result<(), MediaProcessingError> {
    // Check individual dimension limits
    if let Some(max_dim) = options.max_dimension
        && (width > max_dim || height > max_dim)
    {
        return Err(MediaProcessingError::ImageDimensionsTooLarge {
            width,
            height,
            max_dimension: max_dim,
        });
    }

    // Check total pixel count and memory to prevent decompression bombs
    let total_pixels = width as u64 * height as u64;

    // Check pixel count limit first
    if total_pixels > MAX_IMAGE_PIXELS {
        return Err(MediaProcessingError::ImageTooManyPixels {
            total_pixels,
            max_pixels: MAX_IMAGE_PIXELS,
        });
    }

    // Calculate memory with ceiling division to avoid underestimating
    let bytes_per_pixel = 4u64; // RGBA
    let total_bytes = total_pixels * bytes_per_pixel;
    let estimated_mb = total_bytes.div_ceil(1024 * 1024);

    // Check memory limit
    if estimated_mb > MAX_IMAGE_MEMORY_MB {
        return Err(MediaProcessingError::ImageMemoryTooLarge {
            estimated_mb,
            max_mb: MAX_IMAGE_MEMORY_MB,
        });
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use image::{ImageBuffer, Rgb};

    use super::*;

    #[test]
    fn test_validate_file_size() {
        let options = MediaProcessingOptions::validation_only();

        // Test valid size
        let valid_data = vec![0u8; 1000];
        assert!(validate_file_size(&valid_data, &options).is_ok());

        // Test too large
        let large_data = vec![0u8; MAX_FILE_SIZE + 1];
        let result = validate_file_size(&large_data, &options);
        assert!(matches!(
            result,
            Err(MediaProcessingError::FileTooLarge { .. })
        ));

        // Test custom size limit
        let custom_options = MediaProcessingOptions {
            sanitize_exif: false,
            generate_blurhash: false,
            generate_thumbhash: false,
            max_file_size: Some(500),
            ..Default::default()
        };
        let result = validate_file_size(&valid_data, &custom_options);
        assert!(matches!(
            result,
            Err(MediaProcessingError::FileTooLarge { .. })
        ));
    }

    #[test]
    #[cfg(feature = "mip04")]
    fn test_validate_mime_type() {
        // Test valid MIME types return canonical (lowercase) form
        assert_eq!(validate_mime_type("image/jpeg").unwrap(), "image/jpeg");
        assert_eq!(validate_mime_type("video/mp4").unwrap(), "video/mp4");
        assert_eq!(validate_mime_type("audio/wav").unwrap(), "audio/wav");

        // Test canonicalization (uppercase -> lowercase)
        assert_eq!(validate_mime_type("Image/JPEG").unwrap(), "image/jpeg");
        assert_eq!(validate_mime_type("VIDEO/MP4").unwrap(), "video/mp4");

        // Test trimming whitespace
        assert_eq!(validate_mime_type("  image/jpeg  ").unwrap(), "image/jpeg");
        assert_eq!(validate_mime_type("\timage/png\n").unwrap(), "image/png");

        // Test combined normalization
        assert_eq!(validate_mime_type("  Image/WEBP  ").unwrap(), "image/webp");

        // Test parameter stripping (MIP-04 canonicalization)
        assert_eq!(
            validate_mime_type("image/png; charset=utf-8").unwrap(),
            "image/png"
        );
        assert_eq!(
            validate_mime_type("image/jpeg; charset=utf-8; quality=90").unwrap(),
            "image/jpeg"
        );
        assert_eq!(
            validate_mime_type("  image/png ; charset=utf-8  ").unwrap(),
            "image/png"
        );
        assert_eq!(
            validate_mime_type("video/mp4; codecs=\"avc1.42E01E\"").unwrap(),
            "video/mp4"
        );

        // Test invalid format (no slash)
        let result = validate_mime_type("invalid");
        assert!(matches!(
            result,
            Err(MediaProcessingError::InvalidMimeType { .. })
        ));

        // Test too long
        let long_mime = "a".repeat(101);
        let result = validate_mime_type(&long_mime);
        assert!(matches!(
            result,
            Err(MediaProcessingError::InvalidMimeType { .. })
        ));
    }

    #[test]
    #[cfg(feature = "mip04")]
    fn test_validate_filename() {
        // Test valid filename
        assert!(validate_filename("test.jpg").is_ok());
        assert!(validate_filename("my-photo.png").is_ok());

        // Test empty filename
        let result = validate_filename("");
        assert!(matches!(result, Err(MediaProcessingError::EmptyFilename)));

        // Test too long filename
        let long_filename = "a".repeat(MAX_FILENAME_LENGTH + 1);
        let result = validate_filename(&long_filename);
        assert!(matches!(
            result,
            Err(MediaProcessingError::FilenameTooLong { .. })
        ));

        // Test maximum length filename (should be valid)
        let max_filename = "a".repeat(MAX_FILENAME_LENGTH);
        assert!(validate_filename(&max_filename).is_ok());

        // Test invalid characters
        assert!(matches!(
            validate_filename("path/to/file.jpg"),
            Err(MediaProcessingError::InvalidFilename)
        ));
        assert!(matches!(
            validate_filename("path\\to\\file.jpg"),
            Err(MediaProcessingError::InvalidFilename)
        ));
    }

    #[test]
    fn test_validate_image_dimensions() {
        let options = MediaProcessingOptions::validation_only();

        // Test valid dimensions
        assert!(validate_image_dimensions(1920, 1080, &options).is_ok());
        assert!(validate_image_dimensions(800, 600, &options).is_ok());

        // Test dimensions too large
        let result = validate_image_dimensions(20000, 15000, &options);
        assert!(matches!(
            result,
            Err(MediaProcessingError::ImageDimensionsTooLarge { .. })
        ));

        // Test with no dimension limit but still check memory
        let no_limit_options = MediaProcessingOptions {
            sanitize_exif: false,
            generate_blurhash: false,
            generate_thumbhash: false,
            max_dimension: None,
            ..Default::default()
        };
        // 50000 x 40000 = 2 billion pixels, should fail pixel count check
        let result = validate_image_dimensions(50000, 40000, &no_limit_options);
        assert!(matches!(
            result,
            Err(MediaProcessingError::ImageTooManyPixels { .. })
        ));

        // Test reasonable high-res image (12000 x 4000 = 48M pixels, just under 50M limit)
        assert!(validate_image_dimensions(12000, 4000, &no_limit_options).is_ok());

        // Test with custom dimension limit
        let custom_options = MediaProcessingOptions {
            sanitize_exif: false,
            generate_blurhash: false,
            generate_thumbhash: false,
            max_dimension: Some(1024),
            ..Default::default()
        };
        let result = validate_image_dimensions(2048, 1536, &custom_options);
        assert!(matches!(
            result,
            Err(MediaProcessingError::ImageDimensionsTooLarge { .. })
        ));
    }

    #[test]
    fn test_validate_image_dimensions_decompression_bomb_protection() {
        let options = MediaProcessingOptions {
            sanitize_exif: false,
            generate_blurhash: false,
            generate_thumbhash: false,
            max_dimension: None, // No individual dimension limit
            ..Default::default()
        };

        // Test exactly at pixel limit (should pass)
        // sqrt(50M) ≈ 7071, so 7071 x 7071 ≈ 50M pixels
        assert!(validate_image_dimensions(7071, 7071, &options).is_ok());

        // Test just over pixel limit (should fail with TooManyPixels)
        let result = validate_image_dimensions(7100, 7100, &options);
        assert!(matches!(
            result,
            Err(MediaProcessingError::ImageTooManyPixels { .. })
        ));

        // Test extreme decompression bomb attempt
        // 16384 x 16384 = 268M pixels, would be ~1GB RAM
        let result = validate_image_dimensions(16384, 16384, &options);
        assert!(matches!(
            result,
            Err(MediaProcessingError::ImageTooManyPixels { .. })
        ));

        // Test wide panorama (within limits)
        // 10000 x 4000 = 40M pixels, ~160MB
        assert!(validate_image_dimensions(10000, 4000, &options).is_ok());

        // Test tall image (within limits)
        assert!(validate_image_dimensions(4000, 10000, &options).is_ok());
    }

    #[test]
    #[cfg(feature = "mip04")]
    fn test_validate_mime_type_matches_data() {
        // Create a simple PNG image
        let img = ImageBuffer::from_fn(8, 8, |x, y| {
            Rgb([(x * 32) as u8, (y * 32) as u8, ((x + y) * 16) as u8])
        });
        let mut png_data = Vec::new();
        img.write_to(
            &mut std::io::Cursor::new(&mut png_data),
            image::ImageFormat::Png,
        )
        .unwrap();

        // Test matching MIME type
        let result = validate_mime_type_matches_data(&png_data, "image/png");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "image/png");

        // Test matching MIME type with canonicalization
        let result = validate_mime_type_matches_data(&png_data, "Image/PNG");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "image/png");

        // Test matching MIME type with whitespace
        let result = validate_mime_type_matches_data(&png_data, "  image/png  ");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "image/png");

        // Test mismatched MIME type (claiming JPEG but file is PNG)
        let result = validate_mime_type_matches_data(&png_data, "image/jpeg");
        assert!(result.is_err());
        assert!(matches!(
            result,
            Err(MediaProcessingError::MimeTypeMismatch { .. })
        ));

        // Test mismatched MIME type (claiming WebP but file is PNG)
        let result = validate_mime_type_matches_data(&png_data, "image/webp");
        assert!(result.is_err());
        assert!(matches!(
            result,
            Err(MediaProcessingError::MimeTypeMismatch { .. })
        ));

        // Create a JPEG image
        let mut jpeg_data = Vec::new();
        img.write_to(
            &mut std::io::Cursor::new(&mut jpeg_data),
            image::ImageFormat::Jpeg,
        )
        .unwrap();

        // Test matching JPEG
        let result = validate_mime_type_matches_data(&jpeg_data, "image/jpeg");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "image/jpeg");

        // Test mismatched (claiming PNG but file is JPEG)
        let result = validate_mime_type_matches_data(&jpeg_data, "image/png");
        assert!(result.is_err());
        assert!(matches!(
            result,
            Err(MediaProcessingError::MimeTypeMismatch { .. })
        ));
    }

    #[test]
    fn test_detect_mime_type_from_data() {
        // Create test images in different formats
        let img = ImageBuffer::from_fn(8, 8, |x, y| {
            Rgb([(x * 32) as u8, (y * 32) as u8, ((x + y) * 16) as u8])
        });

        // Test PNG detection
        let mut png_data = Vec::new();
        img.write_to(
            &mut std::io::Cursor::new(&mut png_data),
            image::ImageFormat::Png,
        )
        .unwrap();
        let result = detect_mime_type_from_data(&png_data);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "image/png");

        // Test JPEG detection
        let mut jpeg_data = Vec::new();
        img.write_to(
            &mut std::io::Cursor::new(&mut jpeg_data),
            image::ImageFormat::Jpeg,
        )
        .unwrap();
        let result = detect_mime_type_from_data(&jpeg_data);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "image/jpeg");

        // Test invalid data
        let invalid_data = vec![0u8; 100];
        let result = detect_mime_type_from_data(&invalid_data);
        assert!(result.is_err());
    }

    #[test]
    #[cfg(feature = "mip04")]
    fn test_validate_mime_type_parameter_stripping() {
        // Test that parameters are stripped per MIP-04
        assert_eq!(
            validate_mime_type("image/png; charset=utf-8").unwrap(),
            "image/png"
        );
        assert_eq!(
            validate_mime_type("image/jpeg; charset=utf-8; quality=90").unwrap(),
            "image/jpeg"
        );
        assert_eq!(
            validate_mime_type("video/mp4; codecs=\"avc1.42E01E\"").unwrap(),
            "video/mp4"
        );
        assert_eq!(
            validate_mime_type("  image/png ; charset=utf-8  ").unwrap(),
            "image/png"
        );

        // Test that validate_mime_type_matches_data works with parameterized inputs
        let img = ImageBuffer::from_fn(8, 8, |x, y| {
            Rgb([(x * 32) as u8, (y * 32) as u8, ((x + y) * 16) as u8])
        });
        let mut png_data = Vec::new();
        img.write_to(
            &mut std::io::Cursor::new(&mut png_data),
            image::ImageFormat::Png,
        )
        .unwrap();

        // Should work with parameterized MIME type
        let result = validate_mime_type_matches_data(&png_data, "image/png; charset=utf-8");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "image/png");
    }

    #[test]
    #[cfg(feature = "mip04")]
    fn test_validate_mime_type_allowlist_enforcement() {
        // Test supported types
        assert!(validate_mime_type("image/png").is_ok());
        assert!(validate_mime_type("image/jpeg").is_ok());
        assert!(validate_mime_type("video/mp4").is_ok());
        assert!(validate_mime_type("audio/mpeg").is_ok());
        assert!(validate_mime_type("application/pdf").is_ok());
        assert!(validate_mime_type("text/plain").is_ok());

        // Test unsupported types
        assert!(validate_mime_type("application/x-executable").is_err());
        assert!(validate_mime_type("text/html").is_err());
        assert!(validate_mime_type("application/javascript").is_err());
        assert!(validate_mime_type("image/svg+xml").is_err());

        // Test escape hatch (application/octet-stream) - should be allowed
        assert_eq!(
            validate_mime_type("application/octet-stream").unwrap(),
            "application/octet-stream"
        );
        // Test escape hatch with parameters
        assert_eq!(
            validate_mime_type("application/octet-stream; charset=binary").unwrap(),
            "application/octet-stream"
        );
    }

    #[test]
    #[cfg(feature = "mip04")]
    fn test_validate_mime_type_escape_hatch_bypasses_byte_validation() {
        // Create a PNG image
        let img = ImageBuffer::from_fn(8, 8, |x, y| {
            Rgb([(x * 32) as u8, (y * 32) as u8, ((x + y) * 16) as u8])
        });
        let mut png_data = Vec::new();
        img.write_to(
            &mut std::io::Cursor::new(&mut png_data),
            image::ImageFormat::Png,
        )
        .unwrap();

        // Escape hatch should bypass byte validation (application handles it)
        let result = validate_mime_type_matches_data(&png_data, "application/octet-stream");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "application/octet-stream");

        // Regular image types should still validate bytes
        let result = validate_mime_type_matches_data(&png_data, "image/png");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "image/png");

        // Mismatch should still fail for regular types
        let result = validate_mime_type_matches_data(&png_data, "image/jpeg");
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_group_image_mime_type_strict() {
        // Test supported group image types
        assert_eq!(
            validate_group_image_mime_type("image/png").unwrap(),
            "image/png"
        );
        assert_eq!(
            validate_group_image_mime_type("image/jpeg").unwrap(),
            "image/jpeg"
        );
        assert_eq!(
            validate_group_image_mime_type("image/webp").unwrap(),
            "image/webp"
        );

        // Test that non-image types are rejected
        assert!(validate_group_image_mime_type("video/mp4").is_err());
        assert!(validate_group_image_mime_type("audio/mpeg").is_err());
        assert!(validate_group_image_mime_type("application/pdf").is_err());
        assert!(validate_group_image_mime_type("text/plain").is_err());

        // Test that escape hatch is NOT allowed for group images
        assert!(validate_group_image_mime_type("application/octet-stream").is_err());

        // Test that unsupported image types are rejected
        assert!(validate_group_image_mime_type("image/svg+xml").is_err());
    }

    #[test]
    fn test_validate_group_image_mime_type_matches_data() {
        // Create a PNG image
        let img = ImageBuffer::from_fn(8, 8, |x, y| {
            Rgb([(x * 32) as u8, (y * 32) as u8, ((x + y) * 16) as u8])
        });
        let mut png_data = Vec::new();
        img.write_to(
            &mut std::io::Cursor::new(&mut png_data),
            image::ImageFormat::Png,
        )
        .unwrap();

        // Test matching MIME type
        let result = validate_group_image_mime_type_matches_data(&png_data, "image/png");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "image/png");

        // Test mismatched MIME type (claiming JPEG but file is PNG)
        let result = validate_group_image_mime_type_matches_data(&png_data, "image/jpeg");
        assert!(result.is_err());
        assert!(matches!(
            result,
            Err(MediaProcessingError::MimeTypeMismatch { .. })
        ));

        // Test that non-image types are rejected
        let result = validate_group_image_mime_type_matches_data(&png_data, "video/mp4");
        assert!(result.is_err());
        assert!(matches!(
            result,
            Err(MediaProcessingError::InvalidMimeType { .. })
        ));
    }

    #[test]
    #[cfg(feature = "mip04")]
    fn test_validate_mime_type_with_byte_validation() {
        // Test the combination of validate_mime_type and validate_mime_type_matches_data
        // as used in encrypt_for_upload_with_options

        // Test with image type - should validate against file bytes
        let img = ImageBuffer::from_fn(8, 8, |x, y| {
            Rgb([(x * 32) as u8, (y * 32) as u8, ((x + y) * 16) as u8])
        });
        let mut png_data = Vec::new();
        img.write_to(
            &mut std::io::Cursor::new(&mut png_data),
            image::ImageFormat::Png,
        )
        .unwrap();

        // Valid image type matching file bytes
        let canonical = validate_mime_type("image/png").unwrap();
        assert_eq!(canonical, "image/png");
        let result = validate_mime_type_matches_data(&png_data, &canonical);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "image/png");

        // Image type with parameters should work (parameters stripped)
        let canonical = validate_mime_type("image/png; charset=utf-8").unwrap();
        assert_eq!(canonical, "image/png");
        let result = validate_mime_type_matches_data(&png_data, &canonical);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "image/png");

        // Spoofed image type should fail at byte validation
        let canonical = validate_mime_type("image/jpeg").unwrap();
        assert_eq!(canonical, "image/jpeg");
        let result = validate_mime_type_matches_data(&png_data, &canonical);
        assert!(result.is_err());
        assert!(matches!(
            result,
            Err(MediaProcessingError::MimeTypeMismatch { .. })
        ));

        // Unsupported image type should fail at allowlist check
        let result = validate_mime_type("image/svg+xml");
        assert!(result.is_err());
        assert!(matches!(
            result,
            Err(MediaProcessingError::InvalidMimeType { .. })
        ));

        // Test with non-image type - should only check allowlist (no byte validation)
        let canonical = validate_mime_type("video/mp4").unwrap();
        assert_eq!(canonical, "video/mp4");
        // For non-image types, validate_mime_type is sufficient (no byte validation available)

        // Non-image type with parameters should work
        let canonical = validate_mime_type("video/mp4; codecs=\"avc1\"").unwrap();
        assert_eq!(canonical, "video/mp4");

        // Unsupported non-image type should fail
        let result = validate_mime_type("application/x-executable");
        assert!(result.is_err());
        assert!(matches!(
            result,
            Err(MediaProcessingError::InvalidMimeType { .. })
        ));
    }
}