veracode-platform 0.7.11

A comprehensive Rust client library for the Veracode platform (Applications, Identity, Pipeline Scan, Sandbox)
Documentation
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
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
//! JSON validation utilities to prevent `DoS` attacks
//!
//! This module provides functions to validate JSON structure before deserialization,
//! preventing Denial of Service attacks through deeply nested JSON structures.

use log::warn;
use serde_json::Value;

/// Maximum allowed JSON nesting depth
///
/// This limit prevents `DoS` attacks via deeply nested `JSON` that can cause:
/// - Stack overflow
/// - Excessive memory consumption
/// - CPU exhaustion during parsing
///
/// A depth of 32 is sufficient for legitimate API responses while protecting
/// against malicious payloads. Most real-world APIs use <10 levels of nesting.
pub const MAX_JSON_DEPTH: usize = 32;

/// Validate JSON nesting depth to prevent `DoS` attacks
///
/// # Arguments
///
/// * `json_str` - The JSON string to validate
/// * `max_depth` - Maximum allowed nesting depth (use `MAX_JSON_DEPTH` for default)
///
/// # Returns
///
/// * `Ok(())` if the JSON is valid and within depth limits
/// * `Err(String)` with error message if validation fails
///
/// # Examples
///
/// ```
/// use veracode_platform::json_validator::{validate_json_depth, MAX_JSON_DEPTH};
///
/// // Valid JSON within depth limit
/// let json = r#"{"user": {"profile": {"settings": {"theme": "dark"}}}}"#;
/// assert!(validate_json_depth(json, MAX_JSON_DEPTH).is_ok());
///
/// // Deeply nested JSON should be rejected
/// let deep_json = (0..50).fold(String::from("{\"a\":"), |acc, _| acc + "{\"a\":")
///     + &(0..50).map(|_| "}").collect::<String>();
/// assert!(validate_json_depth(&deep_json, MAX_JSON_DEPTH).is_err());
/// ```
///
/// # Security
///
/// This function protects against:
/// - Stack overflow from recursive parsing
/// - CPU exhaustion from excessive nesting
/// - Memory exhaustion from deeply nested structures
///
/// # Errors
///
/// Returns an error if the JSON is invalid or exceeds the maximum nesting depth.
/// Error messages are sanitized to avoid information disclosure, with detailed
/// errors logged internally for debugging.
pub fn validate_json_depth(json_str: &str, max_depth: usize) -> Result<(), String> {
    // First, try to parse the JSON
    let value: Value = serde_json::from_str(json_str).map_err(|e| {
        // Log detailed parse error for debugging (internal only)
        warn!("JSON parse error: {}", e);
        // Return sanitized error to caller (may be exposed to users)
        "Invalid JSON format".to_string()
    })?;

    // Calculate the actual nesting depth
    let depth = calculate_depth(&value);

    if depth > max_depth {
        // Log detailed depth information for debugging (internal only)
        warn!(
            "JSON depth validation failed: depth {} exceeds maximum {}",
            depth, max_depth
        );
        // Return sanitized error to caller (may be exposed to users)
        return Err("JSON structure too deeply nested".to_string());
    }

    Ok(())
}

/// Calculate the maximum nesting depth of a JSON value
///
/// # Arguments
///
/// * `value` - The JSON value to analyze
///
/// # Returns
///
/// The maximum nesting depth (0 for scalars, 1+ for nested structures)
///
/// # Security
///
/// Uses bounded recursion to prevent stack overflow. Stops early if depth exceeds `MAX_JSON_DEPTH`.
fn calculate_depth(value: &Value) -> usize {
    calculate_depth_limited(value, 0)
}

/// Calculate depth with recursion limit to prevent stack overflow
///
/// Stops recursion early once `MAX_JSON_DEPTH` is exceeded, preventing stack overflow
/// on maliciously deep JSON (e.g., 10,000+ nesting levels).
fn calculate_depth_limited(value: &Value, current_depth: usize) -> usize {
    // Early termination: stop recursing if we've exceeded the limit
    // We don't need exact depth if it's already > MAX_JSON_DEPTH
    if current_depth > MAX_JSON_DEPTH {
        return current_depth;
    }

    match value {
        Value::Array(arr) => {
            if arr.is_empty() {
                1
            } else {
                let max_child = arr
                    .iter()
                    .map(|v| calculate_depth_limited(v, current_depth.saturating_add(1)))
                    .max()
                    .unwrap_or(0);
                1_usize.saturating_add(max_child)
            }
        }
        Value::Object(obj) => {
            if obj.is_empty() {
                1
            } else {
                let max_child = obj
                    .values()
                    .map(|v| calculate_depth_limited(v, current_depth.saturating_add(1)))
                    .max()
                    .unwrap_or(0);
                1_usize.saturating_add(max_child)
            }
        }
        // Scalars have depth 0
        Value::Null | Value::Bool(_) | Value::Number(_) | Value::String(_) => 0,
    }
}

#[cfg(test)]
#[allow(clippy::expect_used)]
mod tests {
    use super::*;

    #[test]
    fn test_calculate_depth_scalar() {
        let value = serde_json::json!("test");
        assert_eq!(calculate_depth(&value), 0);

        let value = serde_json::json!(42);
        assert_eq!(calculate_depth(&value), 0);

        let value = serde_json::json!(true);
        assert_eq!(calculate_depth(&value), 0);

        let value = serde_json::json!(null);
        assert_eq!(calculate_depth(&value), 0);
    }

    #[test]
    fn test_calculate_depth_simple_object() {
        let value = serde_json::json!({"key": "value"});
        assert_eq!(calculate_depth(&value), 1);
    }

    #[test]
    fn test_calculate_depth_simple_array() {
        let value = serde_json::json!([1, 2, 3]);
        assert_eq!(calculate_depth(&value), 1);
    }

    #[test]
    fn test_calculate_depth_nested_object() {
        let value = serde_json::json!({
            "user": {
                "profile": {
                    "settings": {
                        "theme": "dark"
                    }
                }
            }
        });
        assert_eq!(calculate_depth(&value), 4);
    }

    #[test]
    fn test_calculate_depth_nested_array() {
        let value = serde_json::json!([[[1, 2], [3, 4]]]);
        assert_eq!(calculate_depth(&value), 3);
    }

    #[test]
    fn test_calculate_depth_mixed() {
        let value = serde_json::json!({
            "data": [
                {"nested": [1, 2, 3]},
                {"nested": [4, 5, 6]}
            ]
        });
        // Depth: root object (1) + array (1) + inner objects (1) + inner arrays (1) = 4
        assert_eq!(calculate_depth(&value), 4);
    }

    #[test]
    fn test_calculate_depth_empty_structures() {
        let value = serde_json::json!({});
        assert_eq!(calculate_depth(&value), 1);

        let value = serde_json::json!([]);
        assert_eq!(calculate_depth(&value), 1);
    }

    #[test]
    fn test_validate_json_depth_valid() {
        let json = r#"{"user": {"profile": {"name": "test"}}}"#;
        assert!(validate_json_depth(json, MAX_JSON_DEPTH).is_ok());
    }

    #[test]
    fn test_validate_json_depth_at_limit() {
        // Create JSON at exactly MAX_JSON_DEPTH
        let mut json = String::from("{");
        for i in 0..MAX_JSON_DEPTH - 1 {
            json.push_str(&format!("\"level{}\":{{", i));
        }
        json.push_str("\"value\":42");
        json.push_str(&"}".repeat(MAX_JSON_DEPTH));

        assert!(validate_json_depth(&json, MAX_JSON_DEPTH).is_ok());
    }

    #[test]
    fn test_validate_json_depth_exceeds_limit() {
        // Create JSON that exceeds MAX_JSON_DEPTH
        let mut json = String::from("{");
        for i in 0..MAX_JSON_DEPTH + 5 {
            json.push_str(&format!("\"level{}\":{{", i));
        }
        json.push_str("\"value\":42");
        json.push_str(&"}".repeat(MAX_JSON_DEPTH + 6));

        let result = validate_json_depth(&json, MAX_JSON_DEPTH);
        assert!(result.is_err());
        assert!(
            result
                .expect_err("should fail on deeply nested json")
                .contains("too deeply nested")
        );
    }

    #[test]
    fn test_validate_json_depth_invalid_json() {
        let json = r#"{"invalid": json}"#;
        let result = validate_json_depth(json, MAX_JSON_DEPTH);
        assert!(result.is_err());
        assert!(
            result
                .expect_err("should fail on invalid json")
                .contains("Invalid JSON format")
        );
    }

    #[test]
    fn test_validate_json_depth_deeply_nested_array() {
        // Create deeply nested array
        let mut json = String::new();
        for _ in 0..50 {
            json.push('[');
        }
        json.push_str("42");
        for _ in 0..50 {
            json.push(']');
        }

        let result = validate_json_depth(&json, MAX_JSON_DEPTH);
        assert!(result.is_err());
        assert!(
            result
                .expect_err("should fail on deeply nested json")
                .contains("too deeply nested")
        );
    }

    #[test]
    fn test_validate_json_depth_custom_limit() {
        let json = r#"{"a": {"b": {"c": {"d": "value"}}}}"#;

        // Should pass with limit 5
        assert!(validate_json_depth(json, 5).is_ok());

        // Should fail with limit 3
        let result = validate_json_depth(json, 3);
        assert!(result.is_err());
    }

    #[test]
    fn test_realistic_api_response() {
        // Simulate a realistic API response with moderate nesting
        let json = r#"{
            "_embedded": {
                "applications": [
                    {
                        "id": 123,
                        "profile": {
                            "name": "TestApp",
                            "settings": {
                                "scan": {
                                    "enabled": true
                                }
                            }
                        }
                    }
                ]
            }
        }"#;

        assert!(validate_json_depth(json, MAX_JSON_DEPTH).is_ok());
    }

    #[test]
    fn test_dos_payload_detection() {
        // Test case similar to what a fuzzer might generate
        // This creates a very deeply nested structure that could cause DoS
        let depth = 100;
        let mut json = String::new();

        // Create nested objects
        for i in 0..depth {
            json.push_str(&format!("{{\"level_{}\":", i));
        }
        json.push_str("null");
        for _ in 0..depth {
            json.push('}');
        }

        let result = validate_json_depth(&json, MAX_JSON_DEPTH);
        assert!(result.is_err());
        assert!(
            result
                .expect_err("should fail on deeply nested json")
                .contains("too deeply nested")
        );
    }
}

// ============================================================================
// TIER 1: PROPERTY-BASED SECURITY TESTS (Fast, High ROI)
// ============================================================================
//
// These tests use proptest to validate security properties against adversarial
// inputs. They run 1000 test cases in normal mode and 10 under Miri for UB detection.

#[cfg(test)]
#[allow(clippy::expect_used)]
mod proptest_security {
    use super::*;
    use proptest::prelude::*;

    // ============================================================================
    // SECURITY TEST: JSON Depth Validation with Adversarial Inputs
    // ============================================================================

    proptest! {
        #![proptest_config(ProptestConfig {
            cases: if cfg!(miri) { 5 } else { 1000 },
            failure_persistence: None,
            .. ProptestConfig::default()
        })]

        /// Property: Valid JSON within depth limits must always succeed
        /// Tests that legitimate JSON structures are never incorrectly rejected
        #[test]
        fn proptest_valid_json_within_limits_succeeds(
            depth in 1usize..=MAX_JSON_DEPTH,
        ) {
            // Create JSON with exactly 'depth' nesting levels
            let mut json = String::new();
            for i in 0..depth {
                json.push_str(&format!("{{\"level{}\":", i));
            }
            json.push_str("\"value\"");
            json.push_str(&"}".repeat(depth));

            let result = validate_json_depth(&json, MAX_JSON_DEPTH);
            prop_assert!(result.is_ok(), "Valid JSON at depth {} should succeed", depth);
        }

        /// Property: JSON exceeding depth limits must always fail
        /// Tests that deeply nested JSON is correctly rejected
        #[test]
        fn proptest_deeply_nested_json_rejected(
            excess_depth in 1usize..=50,
        ) {
            let depth = MAX_JSON_DEPTH.saturating_add(excess_depth);

            // Create JSON that exceeds MAX_JSON_DEPTH
            let mut json = String::new();
            for i in 0..depth {
                json.push_str(&format!("{{\"level{}\":", i));
            }
            json.push_str("null");
            json.push_str(&"}".repeat(depth));

            let result = validate_json_depth(&json, MAX_JSON_DEPTH);
            prop_assert!(result.is_err(), "JSON at depth {} should be rejected", depth);

            // Error can be either from depth validation or from serde_json's recursion limit
            if let Err(msg) = result {
                prop_assert!(
                    msg.contains("too deeply nested") || msg == "Invalid JSON format",
                    "Error message should indicate rejection: {}", msg
                );
            }
        }

        /// Property: Invalid JSON must return parse error, never panic
        /// Tests that malformed JSON is handled gracefully
        #[test]
        fn proptest_invalid_json_returns_error(
            garbage in ".*{0,200}",
        ) {
            // Most random strings are not valid JSON
            let result = validate_json_depth(&garbage, MAX_JSON_DEPTH);

            // Either valid JSON or error, never panic
            match result {
                Ok(_) => {
                    // If it succeeded, must be valid JSON
                    prop_assert!(serde_json::from_str::<Value>(&garbage).is_ok());
                },
                Err(msg) => {
                    // Error should be sanitized
                    prop_assert!(
                        msg == "Invalid JSON format" || msg.contains("too deeply nested"),
                        "Error message should be sanitized"
                    );
                }
            }
        }

        /// Property: Empty and whitespace-only JSON must be handled
        /// Tests edge cases with minimal input
        #[test]
        fn proptest_empty_and_whitespace_json(
            whitespace in "\\s{0,100}",
        ) {
            let result = validate_json_depth(&whitespace, MAX_JSON_DEPTH);

            // Empty/whitespace is invalid JSON, should return error
            match result {
                Ok(_) => {
                    // Only succeeds if it's valid JSON (unlikely with just whitespace)
                    prop_assert!(serde_json::from_str::<Value>(&whitespace).is_ok());
                },
                Err(msg) => {
                    prop_assert_eq!(msg, "Invalid JSON format");
                }
            }
        }

        /// Property: Custom depth limits must be respected
        /// Tests that the max_depth parameter is correctly enforced
        #[test]
        fn proptest_custom_depth_limit_enforced(
            max_depth in 5usize..=MAX_JSON_DEPTH,
            test_depth in 1usize..=MAX_JSON_DEPTH,
        ) {
            // Only test within MAX_JSON_DEPTH to avoid serde_json's own limits
            // Create JSON with test_depth nesting
            let mut json = String::new();
            for i in 0..test_depth {
                json.push_str(&format!("{{\"d{}\":", i));
            }
            json.push('0');
            json.push_str(&"}".repeat(test_depth));

            let result = validate_json_depth(&json, max_depth);

            // Property: validation result should match depth comparison
            if test_depth <= max_depth {
                prop_assert!(result.is_ok(),
                    "JSON depth {} should pass with limit {}", test_depth, max_depth);
            } else {
                prop_assert!(result.is_err(),
                    "JSON depth {} should fail with limit {}", test_depth, max_depth);
            }
        }
    }

    // ============================================================================
    // SECURITY TEST: String Handling and Injection Attacks
    // ============================================================================

    proptest! {
        #![proptest_config(ProptestConfig {
            cases: if cfg!(miri) { 5 } else { 1000 },
            failure_persistence: None,
            .. ProptestConfig::default()
        })]

        /// Property: Special characters in JSON strings must be handled safely
        /// Tests against JSON injection and escape sequence attacks
        #[test]
        fn proptest_special_characters_in_strings(
            special_chars in r#"[<>'"&\x00-\x1f\x7f\\]{0,100}"#,
        ) {
            let json = serde_json::json!({
                "payload": special_chars,
                "nested": {
                    "value": special_chars
                }
            }).to_string();

            // Property 1: Must not panic on special characters
            let result = validate_json_depth(&json, MAX_JSON_DEPTH);
            prop_assert!(result.is_ok());

            // Property 2: Parse result must preserve the data
            let parsed: Value = serde_json::from_str(&json)
                .expect("serde_json should handle its own output");
            prop_assert_eq!(parsed.get("payload").and_then(|v| v.as_str()), Some(special_chars.as_str()));
        }

        /// Property: Control characters must be properly escaped
        /// Tests that control characters don't break JSON parsing
        #[test]
        fn proptest_control_characters_safe(
            // Test various control characters that could cause issues
            control_char in prop::sample::select(vec![
                '\0', '\t', '\n', '\r', '\x01', '\x02', '\x08', '\x0c', '\x1f', '\x7f'
            ]),
        ) {
            let payload = format!("test{}value", control_char);
            let json = serde_json::json!({
                "data": payload
            }).to_string();

            // Must successfully validate and parse
            let result = validate_json_depth(&json, MAX_JSON_DEPTH);
            prop_assert!(result.is_ok());
        }

        /// Property: Extremely long strings must not cause buffer overflows
        /// Tests memory safety with large string values
        #[test]
        fn proptest_large_strings_safe(
            length in 0usize..=10000,
        ) {
            let large_string = "A".repeat(length);
            let json = serde_json::json!({
                "large_field": large_string
            }).to_string();

            // Property 1: Must not panic on large strings
            let result = validate_json_depth(&json, MAX_JSON_DEPTH);
            prop_assert!(result.is_ok());

            // Property 2: Depth should be 1 (just the object)
            let parsed: Value = serde_json::from_str(&json).expect("JSON parsing should succeed");
            prop_assert_eq!(calculate_depth(&parsed), 1);
        }

        /// Property: Unicode edge cases must be handled correctly
        /// Tests UTF-8 boundary handling and multi-byte characters
        #[test]
        fn proptest_unicode_handling(
            // Test various Unicode ranges including emojis and special scripts
            unicode_str in "[\\p{L}\\p{N}\\p{S}\\p{M}]{0,200}",
        ) {
            let json = serde_json::json!({
                "unicode": unicode_str,
                "nested": {
                    "more_unicode": unicode_str
                }
            }).to_string();

            // Property 1: Must handle Unicode correctly
            let result = validate_json_depth(&json, MAX_JSON_DEPTH);
            prop_assert!(result.is_ok());

            // Property 2: Unicode should be preserved
            let parsed: Value = serde_json::from_str(&json).expect("JSON parsing should succeed");
            prop_assert_eq!(parsed.get("unicode").and_then(|v| v.as_str()), Some(unicode_str.as_str()));
        }

        /// Property: Path traversal sequences in JSON values must be safely contained
        /// Tests that path traversal strings don't affect JSON validation
        #[test]
        fn proptest_path_traversal_sequences_safe(
            // Test common path traversal patterns
            traversal in prop::sample::select(vec![
                "../", "..\\", "../../", "../../../etc/passwd",
                "....//", "..\\..\\", "/etc/passwd", "C:\\Windows\\System32",
                "%2e%2e%2f", "%2e%2e/", "..%2f", "..%5c"
            ]),
        ) {
            let json = serde_json::json!({
                "filename": traversal,
                "path": traversal
            }).to_string();

            // Property: Path traversal sequences are just string data in JSON
            // They should not affect validation or cause any special behavior
            let result = validate_json_depth(&json, MAX_JSON_DEPTH);
            prop_assert!(result.is_ok());

            // Data should be preserved as-is
            let parsed: Value = serde_json::from_str(&json).expect("JSON parsing should succeed");
            prop_assert_eq!(parsed.get("filename").and_then(|v| v.as_str()), Some(traversal));
        }

        /// Property: Null byte injection attempts must be handled safely
        /// Tests that null bytes in JSON strings don't cause truncation
        #[test]
        fn proptest_null_byte_injection_safe(
            prefix in "[a-zA-Z0-9]{0,50}",
            suffix in "[a-zA-Z0-9]{0,50}",
        ) {
            // Create a string with null byte
            let payload = format!("{}\0{}", prefix, suffix);
            let json = serde_json::json!({
                "payload": payload
            }).to_string();

            // Property: Null bytes should be properly escaped in JSON
            let result = validate_json_depth(&json, MAX_JSON_DEPTH);
            prop_assert!(result.is_ok());

            // Verify the null byte was preserved through encoding/decoding
            let parsed: Value = serde_json::from_str(&json).expect("JSON parsing should succeed");
            if let Some(s) = parsed.get("payload").and_then(|v| v.as_str()) {
                // The null byte should be present in the decoded string
                let expected_without_null = format!("{}{}", prefix, suffix);
                prop_assert!(s.contains('\0') || s == expected_without_null);
            }
        }
    }

    // ============================================================================
    // SECURITY TEST: Array and Object Boundary Conditions
    // ============================================================================

    proptest! {
        #![proptest_config(ProptestConfig {
            cases: if cfg!(miri) { 5 } else { 1000 },
            failure_persistence: None,
            .. ProptestConfig::default()
        })]

        /// Property: Large arrays must not cause memory exhaustion
        /// Tests that large (but shallow) arrays are handled efficiently
        #[test]
        fn proptest_large_arrays_safe(
            size in 0usize..=1000,
        ) {
            let array: Vec<i32> = (0..size).map(|i| i32::try_from(i).unwrap_or(0)).collect();
            let json = serde_json::json!({
                "large_array": array
            }).to_string();

            // Property 1: Must not panic on large arrays
            let result = validate_json_depth(&json, MAX_JSON_DEPTH);
            prop_assert!(result.is_ok());

            // Property 2: Depth should be 2 (object + array)
            let parsed: Value = serde_json::from_str(&json).expect("JSON parsing should succeed");
            prop_assert_eq!(calculate_depth(&parsed), 2);
        }

        /// Property: Large objects must not cause memory exhaustion
        /// Tests that objects with many keys are handled efficiently
        #[test]
        fn proptest_large_objects_safe(
            key_count in 0usize..=500,
        ) {
            // Create object with key_count keys
            let mut obj = serde_json::Map::new();
            for i in 0..key_count {
                obj.insert(format!("key_{}", i), Value::from(i));
            }
            let json = Value::Object(obj).to_string();

            // Property 1: Must not panic on large objects
            let result = validate_json_depth(&json, MAX_JSON_DEPTH);
            prop_assert!(result.is_ok());

            // Property 2: Depth should be 1 (flat object)
            let parsed: Value = serde_json::from_str(&json).expect("JSON parsing should succeed");
            prop_assert_eq!(calculate_depth(&parsed), 1);
        }

        /// Property: Empty arrays and objects must be handled correctly
        /// Tests edge cases with zero elements
        #[test]
        fn proptest_empty_structures_depth(
            nest_level in 0usize..=10,
        ) {
            // Create nested empty objects
            let mut json = String::new();
            for _ in 0..nest_level {
                json.push_str("{\"empty\":");
            }
            json.push_str("{}");
            json.push_str(&"}".repeat(nest_level));

            let result = validate_json_depth(&json, MAX_JSON_DEPTH);
            prop_assert!(result.is_ok());

            let parsed: Value = serde_json::from_str(&json).expect("JSON parsing should succeed");
            let depth = calculate_depth(&parsed);
            // Empty objects still count toward depth
            prop_assert_eq!(depth, nest_level.saturating_add(1));
        }

        /// Property: Mixed nesting (arrays and objects) must be calculated correctly
        /// Tests depth calculation with alternating structures
        #[test]
        fn proptest_mixed_nesting_depth_calculation(
            depth in 1usize..=10,
        ) {
            // Create simple mixed nesting with arrays and objects
            let mut json = String::new();
            for _ in 0..depth {
                json.push_str("[{\"x\":");
            }
            json.push_str("null");
            json.push_str(&"}]".repeat(depth));

            let result = validate_json_depth(&json, MAX_JSON_DEPTH);
            prop_assert!(result.is_ok(), "JSON construction should be valid");

            let parsed: Value = serde_json::from_str(&json)
                .expect("JSON should parse correctly");
            let calculated_depth = calculate_depth(&parsed);
            // Each iteration adds both array and object, so depth = 2*depth
            prop_assert!(calculated_depth >= depth,
                "Calculated depth {} should be >= nesting levels {}",
                calculated_depth, depth);
        }
    }

    // ============================================================================
    // SECURITY TEST: Numeric Edge Cases and Integer Overflow
    // ============================================================================

    proptest! {
        #![proptest_config(ProptestConfig {
            cases: if cfg!(miri) { 5 } else { 1000 },
            failure_persistence: None,
            .. ProptestConfig::default()
        })]

        /// Property: Integer overflow in depth calculation must be prevented
        /// Tests that saturating_add prevents overflow panics
        #[test]
        fn proptest_depth_calculation_no_overflow(
            // Test with realistic depths that shouldn't overflow
            depth in 0usize..=200,
        ) {
            // Create JSON with specified depth
            let mut json = String::new();
            for i in 0..depth {
                json.push_str(&format!("{{\"{}\":", i));
            }
            json.push_str("42");
            json.push_str(&"}".repeat(depth));

            // Property: Must never panic on overflow
            // Early termination at MAX_JSON_DEPTH prevents excessive recursion
            let result = validate_json_depth(&json, MAX_JSON_DEPTH);

            if depth <= MAX_JSON_DEPTH {
                prop_assert!(result.is_ok());
            } else {
                prop_assert!(result.is_err());
            }
        }

        /// Property: Extreme numeric values in JSON must be handled
        /// Tests that large numbers don't cause issues
        #[test]
        fn proptest_extreme_numeric_values(
            value in prop::num::i64::ANY,
        ) {
            let json = serde_json::json!({
                "number": value,
                "nested": {
                    "another_number": value
                }
            }).to_string();

            // Property: Large numbers should not affect depth validation
            let result = validate_json_depth(&json, MAX_JSON_DEPTH);
            prop_assert!(result.is_ok());

            let parsed: Value = serde_json::from_str(&json).expect("JSON parsing should succeed");
            prop_assert_eq!(calculate_depth(&parsed), 2);
        }

        /// Property: Boolean and null values must not affect depth calculation
        /// Tests that scalar values are correctly identified as depth 0
        #[test]
        fn proptest_scalar_values_depth_zero(
            bool_val in any::<bool>(),
        ) {
            // Test various scalar types
            let null_value = Value::Null;
            let bool_value = Value::Bool(bool_val);
            let num_value = Value::from(42);
            let str_value = Value::from("test");

            prop_assert_eq!(calculate_depth(&null_value), 0);
            prop_assert_eq!(calculate_depth(&bool_value), 0);
            prop_assert_eq!(calculate_depth(&num_value), 0);
            prop_assert_eq!(calculate_depth(&str_value), 0);
        }

        /// Property: Very deep recursion must be bounded
        /// Tests that calculate_depth_limited provides early termination
        #[test]
        fn proptest_recursion_bounded(
            depth in (MAX_JSON_DEPTH + 1)..=60,
        ) {
            // Create JSON deeper than MAX_JSON_DEPTH
            let mut json = String::new();
            for i in 0..depth {
                json.push_str(&format!("[{{\"{}\":", i));
            }
            json.push('0');
            json.push_str(&"}]".repeat(depth));

            // Property: Should reject without stack overflow
            // Early termination prevents excessive recursion
            let result = validate_json_depth(&json, MAX_JSON_DEPTH);
            prop_assert!(result.is_err());

            // Note: serde_json has its own recursion limit (~128 levels)
            // For very deep JSON, serde_json may fail before our validation
            // Either error is acceptable - both protect against DoS
            if let Ok(parsed) = serde_json::from_str::<Value>(&json) {
                let calculated = calculate_depth(&parsed);
                // If parsing succeeded, depth calculation should detect the issue
                prop_assert!(calculated > MAX_JSON_DEPTH);
            }
        }
    }

    // ============================================================================
    // SECURITY TEST: DoS Attack Vectors
    // ============================================================================

    proptest! {
        #![proptest_config(ProptestConfig {
            cases: if cfg!(miri) { 5 } else { 500 },  // Fewer cases due to complexity
            failure_persistence: None,
            .. ProptestConfig::default()
        })]

        /// Property: Exponentially wide JSON must not cause memory exhaustion
        /// Tests the "billion laughs" style attack adapted for JSON
        #[test]
        fn proptest_dos_exponential_width(
            width in 1usize..=20,
            depth in 1usize..=4,
        ) {
            // Create JSON with exponentially increasing width
            // Each level has 'width' children
            fn create_wide_json(width: usize, depth: usize) -> String {
                if depth == 0 {
                    return "42".to_string();
                }

                let mut json = String::from("[");
                for i in 0..width {
                    if i > 0 {
                        json.push(',');
                    }
                    json.push_str(&create_wide_json(width, depth.saturating_sub(1)));
                }
                json.push(']');
                json
            }

            let json = create_wide_json(width, depth);

            // Property: Must handle or reject gracefully, never crash
            let result = validate_json_depth(&json, MAX_JSON_DEPTH);

            if depth <= MAX_JSON_DEPTH {
                prop_assert!(result.is_ok() || result.is_err()); // Either is fine
            } else {
                prop_assert!(result.is_err());
            }
        }

        /// Property: Repeated deep nesting with different patterns
        /// Tests that various nesting styles are consistently handled
        #[test]
        fn proptest_dos_varied_nesting_patterns(
            depth in 30usize..=MAX_JSON_DEPTH + 20,
            pattern in prop::sample::select(vec!["array", "object", "mixed"]),
        ) {
            let json = match pattern {
                "array" => {
                    let mut s = String::new();
                    for _ in 0..depth {
                        s.push('[');
                    }
                    s.push_str("null");
                    s.push_str(&"]".repeat(depth));
                    s
                },
                "object" => {
                    let mut s = String::new();
                    for i in 0..depth {
                        s.push_str(&format!("{{\"k{}\":", i));
                    }
                    s.push_str("null");
                    s.push_str(&"}".repeat(depth));
                    s
                },
                _ => { // "mixed"
                    let mut s = String::new();
                    for i in 0..depth {
                        if i % 2 == 0 {
                            s.push('[');
                        } else {
                            s.push_str("{\"x\":");
                        }
                    }
                    s.push_str("null");
                    for i in (0..depth).rev() {
                        if i % 2 == 0 {
                            s.push(']');
                        } else {
                            s.push('}');
                        }
                    }
                    s
                }
            };

            let result = validate_json_depth(&json, MAX_JSON_DEPTH);

            if depth <= MAX_JSON_DEPTH {
                prop_assert!(result.is_ok());
            } else {
                prop_assert!(result.is_err());
            }
        }

        /// Property: Malformed JSON with unbalanced brackets must fail gracefully
        /// Tests that parser errors are caught and sanitized
        #[test]
        fn proptest_malformed_json_graceful_failure(
            open_brackets in 0usize..=50,
            close_brackets in 0usize..=50,
        ) {
            // Create intentionally malformed JSON
            let mut json = String::new();
            json.push_str(&"[".repeat(open_brackets));
            json.push_str("null");
            json.push_str(&"]".repeat(close_brackets));

            let result = validate_json_depth(&json, MAX_JSON_DEPTH);

            // Property: Either valid (if balanced) or returns sanitized error
            match result {
                Ok(_) => {
                    // If successful, brackets must be balanced
                    prop_assert_eq!(open_brackets, close_brackets);
                },
                Err(msg) => {
                    // Error must be sanitized
                    prop_assert!(
                        msg == "Invalid JSON format" || msg.contains("too deeply nested")
                    );
                }
            }
        }
    }
}