Merge pull request #557 from haranishi/fix/pdf-inflate-limit

This commit is contained in:
Claude
2026-10-10 12:51:15 +00:00
3 changed files with 331 additions and 54 deletions
+176 -1
View File
@@ -147,7 +147,182 @@ fn eps_postscript_subset() {
fn filters_decode() {
assert_eq!(object::ascii85(b"<~87cURD]i,\"Ebo80~>"), b"Hello World!".to_vec());
let z = miniz_oxide::deflate::compress_to_vec_zlib(b"abc", 6);
assert_eq!(object::inflate(&z).unwrap(), b"abc");
assert_eq!(object::inflate(&z, 1 << 20).unwrap(), b"abc");
}
#[test]
fn flate_bomb_is_bounded() {
// A decompression bomb in miniature: 16 MiB of zeros deflate to about 16 KB.
let zeros = vec![0u8; 16 << 20];
for bomb in [miniz_oxide::deflate::compress_to_vec_zlib(&zeros, 6), miniz_oxide::deflate::compress_to_vec(&zeros, 6)] {
assert!(bomb.len() < 64 << 10);
// Past the limit, as zlib and as raw deflate, it is undecodable: not inflated in full
// and not salvaged as a truncated stream.
assert_eq!(object::inflate(&bomb, 1 << 20).map(|v| v.len()), None);
assert_eq!(object::inflate(&bomb, 32 << 20).map(|v| v.len()), Some(16 << 20));
}
}
#[test]
fn truncated_flate_keeps_what_inflates() {
let mut x = 1u32;
let data: Vec<u8> = (0..20_000)
.map(|_| {
x = x.wrapping_mul(1_103_515_245).wrapping_add(12_345);
(x >> 16) as u8
})
.collect();
let z = miniz_oxide::deflate::compress_to_vec_zlib(&data, 6);
let part = object::inflate(&z[..z.len() / 2], 1 << 20).unwrap();
assert!(!part.is_empty() && part.len() < data.len());
assert_eq!(part, data[..part.len()]);
}
#[test]
fn salvage_buffer_is_bounded_by_the_limit() {
// Small inputs keep eight times their size and 1 KiB.
assert_eq!(object::salvage_len(0, 1 << 20), 1024);
assert_eq!(object::salvage_len(10_000, 1 << 20), 81_024);
// Large ones stop at the limit (`1 << 28` fits a 32-bit `usize` too), without overflowing.
assert_eq!(object::salvage_len(1 << 28, 1 << 20), 1 << 20);
assert_eq!(object::salvage_len(usize::MAX, 1 << 20), 1 << 20);
}
#[test]
fn nested_bomb_in_the_second_flate_filter_is_refused() {
// `/Filter [/FlateDecode /FlateDecode]`: the first filter inflates, within the limit, to a zlib
// bomb (padded to just under the limit) that the second would inflate to 16 MiB. The second
// stops at the limit, so the stream is undecodable.
const MAX: usize = 1 << 20;
let flate = object::Obj::Name("FlateDecode".into());
let chain: object::Dict = [("Filter".to_string(), object::Obj::Array(vec![flate.clone(), flate]))].into_iter().collect();
let file = object::File::parse(b"");
let mut nested = miniz_oxide::deflate::compress_to_vec_zlib(&vec![0u8; 16 << 20], 6);
nested.resize(MAX - 1, 0);
let raw = miniz_oxide::deflate::compress_to_vec_zlib(&nested, 6);
assert_eq!(object::inflate(&raw, MAX).map(|v| v.len()), Some(MAX - 1));
assert_eq!(object::decode_stream_within(&file, &chain, &raw, MAX).map(|v| v.len()), None);
}
fn bounded_filter(dict: &str, raw: &[u8], max: usize) -> Option<Vec<u8>> {
let object::Obj::Dict(d) = object::Lexer::new(dict.as_bytes(), 0).next().unwrap() else { panic!("not a dictionary") };
object::decode_stream_within(&object::File::parse(b""), &d, raw, max)
}
#[test]
fn predictor_huge_columns_decode_to_nothing() {
// A row of a petabyte used to be allocated before looking at the data (an allocation
// failure aborts the process, so this test took the whole test binary down).
let raw = miniz_oxide::deflate::compress_to_vec_zlib(&[0, 42], 6);
let dict = "<< /Filter /FlateDecode /DecodeParms << /Predictor 12 /Columns 1000000000000000 >> >>";
assert_eq!(bounded_filter(dict, &raw, 16), Some(vec![]));
}
#[test]
fn predictor_overflow_and_empty_rows_decode_to_nothing() {
let raw = miniz_oxide::deflate::compress_to_vec_zlib(&[0, 42], 6);
for parms in [
format!("/Columns {} /Colors 2 /BitsPerComponent 8", usize::MAX),
format!("/Columns 1 /Colors {} /BitsPerComponent 8", usize::MAX),
"/Columns 0".into(),
// A row is two bytes plus its tag byte: longer than the two bytes of data.
"/Columns 2".into(),
] {
assert_eq!(bounded_filter(&format!("<< /Filter /FlateDecode /DecodeParms << /Predictor 12 {parms} >> >>"), &raw, 16), Some(vec![]));
}
}
#[test]
fn run_length_chain_is_bounded() {
// Each stage expands repetitions of 0x81, which is itself an RL repeat code.
let raw = [0x81, 0x81, 128];
let chain = "<< /Filter [/RunLengthDecode /RL /RunLengthDecode] >>";
assert_eq!(bounded_filter(chain, &raw, 128).map(|v| v.len()), None);
assert_eq!(bounded_filter("<< /Filter /RL >>", &raw, 128), Some(vec![0x81; 128]));
// An over-limit intermediate stage must fail even if the next stage shrinks it.
assert_eq!(bounded_filter("<< /Filter [/RL /AHx] >>", &raw, 127), None);
}
fn lzw_codes(codes: impl IntoIterator<Item = u16>, early: bool) -> Vec<u8> {
let mut w = crate::ccitt::tests::W::default();
let (mut bits, mut entries, mut prev) = (9, 258, false);
for code in codes {
w.put(code, bits);
match code {
256 => (bits, entries, prev) = (9, 258, false),
257 => break,
_ => {
if prev && entries < 4096 {
entries += 1;
}
prev = true;
if entries + usize::from(early) >= (1 << bits) && bits < 12 {
bits += 1;
}
}
}
}
w.bytes
}
#[test]
fn lzw_long_run_is_bounded_and_full_table_decodes() {
// Grow strings of A until the 12-bit table is full, then send literal A codes.
// Both old and new implementations use well under 100 MB for this fixture.
for early in [false, true] {
let raw = lzw_codes([256, 65].into_iter().chain(258..=4095).chain(std::iter::repeat_n(65, 62_500)).chain([257]), early);
assert!((95_000..105_000).contains(&raw.len()));
let dict = format!("<< /Filter /LZWDecode /DecodeParms << /EarlyChange {} >> >>", usize::from(early));
assert_eq!(bounded_filter(&dict, &raw, 1 << 20).map(|v| v.len()), None);
let expected = 3839 * 3840 / 2 + 62_500;
assert_eq!(bounded_filter(&dict, &raw, expected), Some(vec![b'A'; expected]));
// Clear restores the initial dictionary and code width after saturation.
let cleared = lzw_codes([256, 65].into_iter().chain(258..=4095).chain([256, 66, 257]), early);
let mut expected = vec![b'A'; 3839 * 3840 / 2];
expected.push(b'B');
assert_eq!(bounded_filter(&dict, &cleared, expected.len()), Some(expected));
}
}
#[test]
fn ccitt_declared_and_unknown_rows_are_bounded() {
// Eight white Group 4 rows. The old decoder pads declared missing rows to white.
assert_eq!(bounded_filter("<< /Filter /CCF /DecodeParms << /K -1 /Columns 64 /Rows 1024 >> >>", &[0xFF], 16), None);
assert_eq!(bounded_filter("<< /Filter /CCF /DecodeParms << /K -1 /Columns 64 >> >>", &[0xFF], 16), None);
assert_eq!(bounded_filter("<< /Filter /CCF /DecodeParms << /K -1 /Columns 8 /Rows 8 >> >>", &[0xFF], 8), Some(vec![0xFF; 8]));
// Padding below the limit remains unchanged.
assert_eq!(bounded_filter("<< /Filter /CCF /DecodeParms << /K -1 /Columns 8 /Rows 16 >> >>", &[0xFF], 16), Some(vec![0xFF; 16]));
// Without `/Rows` the size is never computed up front: 2^20 rows of 4096 bytes overflow a
// 32-bit `usize`, but eight rows of this width decode on every target.
assert_eq!(bounded_filter("<< /Filter /CCF /DecodeParms << /K -1 /Columns 32768 >> >>", &[0xFF], 32768), Some(vec![0xFF; 32768]));
}
#[test]
fn ascii85_and_hex_outputs_are_bounded() {
assert_eq!(bounded_filter("<< /Filter /A85 >>", b"zz", 7), None);
assert_eq!(bounded_filter("<< /Filter /A85 >>", b"zz", 8), Some(vec![0; 8]));
assert_eq!(bounded_filter("<< /Filter /A85 >>", b"87cURD]i,\"Ebo80", 12), Some(b"Hello World!".to_vec()));
assert_eq!(bounded_filter("<< /Filter /A85 >>", b"87cURD]i,\"Ebo80", 11), None);
assert_eq!(bounded_filter("<< /Filter /AHx >>", b"4142434>", 3), None);
assert_eq!(bounded_filter("<< /Filter /AHx >>", b"4142434>", 4), Some(b"ABC@".to_vec()));
}
#[test]
fn predictors_and_normal_lzw_decode_within_limit() {
for predictor in [1, 2, 12] {
let (data, expected) = if predictor == 12 { (vec![0, 10, 20, 2, 1, 2], vec![10, 20, 11, 22]) } else { (vec![10, 20], vec![10, 20]) };
let raw = miniz_oxide::deflate::compress_to_vec_zlib(&data, 6);
let dict = format!("<< /Filter /FlateDecode /DecodeParms << /Predictor {predictor} /Columns 2 >> >>");
assert_eq!(bounded_filter(&dict, &raw, data.len()), Some(expected));
}
for early in [false, true] {
let raw = lzw_codes([256, 65, 66, 258, 257], early);
assert_eq!(bounded_filter("<< /Filter /LZW >>", &raw, 4), Some(b"ABAB".to_vec()));
assert_eq!(bounded_filter("<< /Filter /LZW >>", &raw, 3), None);
}
assert_eq!(bounded_filter("<< /Filter /RL >>", &[2, b'a', b'b', b'c', 254, b'd', 128], 6), Some(b"abcddd".to_vec()));
assert_eq!(bounded_filter("<< /Filter /RL >>", &[2, b'a', b'b', b'c', 128], 2), None);
assert_eq!(bounded_filter("<< /Filter /RL >>", &[128], 0), Some(vec![]));
}
// ------------------------------------------------------------------ M13.6: text, images,