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
// Copyright 2026 Encypher Corporation
// SPDX-License-Identifier: Apache-2.0
//! Deterministic robustness tests for the format parsers (adversarial input).
//!
//! Adversarial input is the #1 C2PA attack surface: a validator must never
//! panic, hang, or over-allocate on malformed assets. These tests feed a large
//! corpus of pseudo-random and structurally-hostile byte strings to every
//! `extract_manifest` path and assert the parser returns `Ok`/`Err` without
//! panicking. They run on stable Rust in CI; `fuzz/` holds the cargo-fuzz
//! targets for deeper coverage-guided runs.
use crate::c2pa_formats::{extract_manifest, AssetFormat};
/// A small deterministic PRNG (xorshift64*) — no external dependency, fully
/// reproducible so a failure is debuggable.
struct Rng(u64);
impl Rng {
fn next(&mut self) -> u64 {
let mut x = self.0;
x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
self.0 = x;
x.wrapping_mul(0x2545F4914F6CDD1D)
}
fn bytes(&mut self, len: usize) -> Vec<u8> {
let mut v = Vec::with_capacity(len);
while v.len() < len {
v.extend_from_slice(&self.next().to_le_bytes());
}
v.truncate(len);
v
}
}
const FORMATS: &[AssetFormat] = &[
AssetFormat::Jpeg,
AssetFormat::Png,
AssetFormat::Bmff,
AssetFormat::Riff,
AssetFormat::Tiff,
AssetFormat::Gif,
AssetFormat::Svg,
AssetFormat::Pdf,
AssetFormat::Zip,
AssetFormat::Id3,
AssetFormat::Flac,
AssetFormat::Ogg,
AssetFormat::Font,
AssetFormat::Jxl,
AssetFormat::Midi,
AssetFormat::Ttml,
AssetFormat::Onnx,
AssetFormat::SafeTensors,
AssetFormat::Parquet,
AssetFormat::BlockFact,
AssetFormat::Warc,
AssetFormat::AppendOnlyText {
flavor: crate::c2pa_formats::AppendOnlyFlavor::PlainText,
},
AssetFormat::AppendOnlyText {
flavor: crate::c2pa_formats::AppendOnlyFlavor::JsonLines,
},
];
/// Format magic prefixes — exercises the "looks valid then turns hostile" path,
/// which is where length/offset parsing bugs hide.
const MAGICS: &[&[u8]] = &[
&[0xFF, 0xD8, 0xFF], // JPEG SOI
b"\x89PNG\r\n\x1a\n", // PNG
b"RIFF", // RIFF
b"II*\x00", // TIFF LE
b"MM\x00*", // TIFF BE
b"II+\x00\x08\x00\x00\x00", // BigTIFF LE
b"MM\x00+\x00\x08\x00\x00", // BigTIFF BE
b"GIF89a", // GIF
b"<?xml version=\"1.0\"?><svg", // SVG
b"%PDF-1.7", // PDF
b"PK\x03\x04", // ZIP
b"ID3\x04\x00", // ID3
b"fLaC", // FLAC
b"OggS", // Ogg
b"\x00\x01\x00\x00", // SFNT/OTF
b"\x00\x00\x00\x0cJXL \r\n\x87\n", // JXL
b"\x00\x00\x00\x18ftypisom", // BMFF
b"MThd\x00\x00\x00\x06\x00\x01", // MIDI
b"<tt:tt xmlns:tt=\"http://www.w3.org/ns/ttml\"><tt:head>", // TTML
b"\x08\x09\x72\x02", // ONNX (ir_version, then field 14)
b"\x40\x00\x00\x00\x00\x00\x00\x00{\"__metadata__\":{", // SafeTensors
b"PAR1", // Parquet
b"\xfa\x49\x41\x00\x00\x00\x00\x02{}", // BlockFact
b"WARC/1.1\r\n", // WARC
b"-----BEGIN C2PA CHECKPOINT----- ", // append-only text
];
#[test]
fn parsers_never_panic_on_random_bytes() {
let mut rng = Rng(0x9E3779B97F4A7C15);
for &fmt in FORMATS {
for _ in 0..200 {
let len = (rng.next() % 4096) as usize;
let data = rng.bytes(len);
// Must not panic; result is irrelevant.
let _ = extract_manifest(fmt, &data);
}
}
}
#[test]
fn parsers_never_panic_on_magic_prefixed_garbage() {
let mut rng = Rng(0xDEADBEEFCAFEF00D);
for &fmt in FORMATS {
for magic in MAGICS {
for _ in 0..50 {
let mut data = magic.to_vec();
let tail = (rng.next() % 2048) as usize;
data.extend(rng.bytes(tail));
let _ = extract_manifest(fmt, &data);
}
}
}
}
#[test]
fn parsers_never_panic_on_hostile_lengths() {
// Boxes/chunks that declare enormous lengths must be rejected, not OOM.
let cases: &[Vec<u8>] = &[
// BMFF box with size 0xFFFFFFFF and no body.
vec![0xFF, 0xFF, 0xFF, 0xFF, b'u', b'u', b'i', b'd'],
// BMFF 64-bit largesize escape with absurd size.
vec![
0, 0, 0, 1, b'm', b'd', b'a', b't', 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
],
// PNG chunk claiming 4GiB length.
{
let mut v = b"\x89PNG\r\n\x1a\n".to_vec();
v.extend_from_slice(&[0xFF, 0xFF, 0xFF, 0xFF]); // length
v.extend_from_slice(b"caBX");
v
},
// RIFF claiming huge chunk size.
{
let mut v = b"RIFF".to_vec();
v.extend_from_slice(&[0xFF, 0xFF, 0xFF, 0xFF]);
v.extend_from_slice(b"WEBP");
v
},
// JPEG APP11 segment with max length and no body.
vec![0xFF, 0xD8, 0xFF, 0xEB, 0xFF, 0xFF],
// BigTIFF IFD declaring a 64-bit entry count no file can hold.
{
let mut v = b"II+\x00\x08\x00\x00\x00".to_vec();
v.extend_from_slice(&16u64.to_le_bytes()); // first IFD
v.extend_from_slice(&u64::MAX.to_le_bytes()); // entry count
v
},
// BigTIFF chain pointing an IFD back at the fixed header.
{
let mut v = b"MM\x00+\x00\x08\x00\x00".to_vec();
v.extend_from_slice(&8u64.to_be_bytes());
v.extend_from_slice(&1u64.to_be_bytes());
v
},
// Deeply truncated everything.
vec![],
vec![0x00],
];
for &fmt in FORMATS {
for c in cases {
let _ = extract_manifest(fmt, c);
}
}
}