Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_file/tests/hostile.rs

10.3 KiB, 17 runs

created by r1403719401:77595, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! Input built to hurt a reader: each case must end in an `Outcome`, quickly and in bounded memory,
2//! never in an abort, an exhausted heap or a link followed out of the tree. Each case is its own
3//! test, so each shows red or green on its own; the DOCX nesting case, which aborts the binary
4//! before its fix, is in `hostile_docx.rs`.
5
6use oxedyne_fe2o3_file::{
7 office::{
8 odf,
9 pptx,
10 deck::Deck,
11 sheet::{
12 Book,
13 Cell,
14 Sheet,
15 },
16 xlsx,
17 },
18 walk::{
19 git_global_excludes,
20 Disk,
21 Walk,
22 WalkEnd,
23 },
24 zip::{
25 Method,
26 Zip,
27 },
28};
29
30use oxedyne_fe2o3_core::prelude::*;
31use oxedyne_fe2o3_text::doc::markdown;
32
33use std::{
34 fs,
35 path::PathBuf,
36 time::{
37 Duration,
38 Instant,
39 },
40};
41
42// fixed: generous for a loaded build machine; a reader that allocates the bomb takes minutes or dies
43const FAST: Duration = Duration::from_secs(10);
44
45const NS_ODS: &str = "xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \
46 xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\" \
47 xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"";
48
49/// A real package from the crate's own writer, with one part replaced by the crafted one.
50fn package(bytes: Vec<u8>, part: &str, xml: String) -> Outcome<Vec<u8>> {
51 let mut zip = res!(Zip::read(bytes));
52 assert!(zip.has(part), "the writer names no {}", part);
53 zip.set(part, xml.into_bytes(), Method::Deflate);
54 zip.write()
55}
56
57fn one_cell_book() -> Book {
58 let mut sheet = Sheet::new("Sheet1");
59 sheet.rows.push(vec![Cell::text("x")]);
60 let mut book = Book::new();
61 book.sheets.push(sheet);
62 book
63}
64
65fn ods(rows: &str) -> Outcome<Vec<u8>> {
66 let bytes = res!(odf::sheet::write(&one_cell_book()));
67 package(bytes, "content.xml", fmt!(
68 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><office:document-content {}><office:body>\
69 <office:spreadsheet><table:table table:name=\"Sheet1\">{}</table:table></office:spreadsheet>\
70 </office:body></office:document-content>", NS_ODS, rows))
71}
72
73fn xlsx_of(rows: &str) -> Outcome<Vec<u8>> {
74 let bytes = res!(xlsx::write(&one_cell_book()));
75 package(bytes, "xl/worksheets/sheet1.xml", fmt!(
76 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><worksheet \
77 xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\"><sheetData>{}</sheetData>\
78 </worksheet>", rows))
79}
80
81/// A non-empty ODS cell repeated `n` times across.
82fn ods_cells(n: usize) -> String {
83 fmt!("<table:table-cell table:number-columns-repeated=\"{}\" office:value-type=\"string\">\
84 <text:p>x</text:p></table:table-cell>", n)
85}
86
87/// One xlsx row per line, each holding a single cell in the last column, XFD.
88fn xlsx_far_rows(n: usize) -> String {
89 let mut s = String::new();
90 for r in 1..=n {
91 s.push_str(&fmt!("<row r=\"{0}\"><c r=\"XFD{0}\" t=\"inlineStr\"><is><t>x</t></is></c></row>", r));
92 }
93 s
94}
95
96fn refused<T>(out: Outcome<T>, what: &str, began: Instant) {
97 match out {
98 Ok(_) => panic!("{}: read whole, when it should be refused for its cell budget", what),
99 Err(e) => {
100 let msg = fmt!("{}", e);
101 assert!(msg.contains("cells"), "{}: refused, but not for its cell budget: {}", what, msg);
102 }
103 }
104 assert!(began.elapsed() < FAST, "{}: refused only after {:?}", what, began.elapsed());
105}
106
107/// A package with one part's text patched, every `from` made `to`.
108fn patched(bytes: Vec<u8>, part: &str, from: &str, to: &str) -> Outcome<Vec<u8>> {
109 let zip = res!(Zip::read(bytes.clone()));
110 let text = res!(String::from_utf8(res!(zip.content(part))), Decode, String);
111 assert!(text.contains(from), "{} holds no {:?}", part, from);
112 package(bytes, part, text.replace(from, to))
113}
114
115/// A walk of `root` with no budget pressure, its kept entries' paths.
116fn walked(root: &PathBuf) -> Outcome<Vec<String>> {
117 let mut found = Vec::new();
118 let end = res!(Walk::new(100).run(&mut Disk::new(root), |e| {
119 found.push(String::from_utf8_lossy(&e.path).into_owned());
120 Ok(true)
121 }));
122 assert_eq!(end, WalkEnd::Complete);
123 Ok(found)
124}
125
126/// A fresh directory of this name under the test target's scratch.
127fn scratch(name: &str) -> Outcome<PathBuf> {
128 let dir = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join(name);
129 let _ = fs::remove_dir_all(&dir);
130 res!(fs::create_dir_all(&dir));
131 Ok(dir)
132}
133
134#[test]
135fn ods_interior_gap_keeps_rows_in_place() -> Outcome<()> {
136 // Five thousand empty rows, then one cell: it is on row 5,001, not pulled up to 4,097.
137 let bytes = res!(ods(&fmt!(
138 "<table:table-row table:number-rows-repeated=\"5000\"><table:table-cell/></table:table-row>\
139 <table:table-row>{}</table:table-row>\
140 <table:table-row table:number-rows-repeated=\"1048000\"><table:table-cell/></table:table-row>",
141 ods_cells(1))));
142 let book = res!(odf::sheet::read(&bytes)).book;
143 assert_eq!(book.sheets[0].rows.len(), 5001, "the gap moved the row after it");
144 assert_eq!(book.sheets[0].rows[5000].len(), 1);
145 Ok(())
146}
147
148#[test]
149fn ods_repeat_bomb_refused_by_cell_budget() -> Outcome<()> {
150 // About 4.5 million cells from a few hundred bytes, then the full 16 billion of one row element.
151 for (what, rows) in [
152 ("ods 4096 x 1100", fmt!("<table:table-row table:number-rows-repeated=\"4096\">{}\
153 </table:table-row>", ods_cells(1100))),
154 ("ods 1e6 x 16384", fmt!("<table:table-row table:number-rows-repeated=\"1000000\">{}\
155 </table:table-row>", ods_cells(16_384))),
156 ] {
157 let bytes = res!(ods(&rows));
158 let began = Instant::now();
159 refused(odf::sheet::read(&bytes), what, began);
160 }
161 Ok(())
162}
163
164#[test]
165fn xlsx_far_cells_refused_by_cell_budget() -> Outcome<()> {
166 // Each row's one cell at XFD costs a whole row of empty cells before it.
167 for (what, n) in [("xlsx 300 rows at XFD", 300), ("xlsx 100000 rows at XFD", 100_000)] {
168 let bytes = res!(xlsx_of(&xlsx_far_rows(n)));
169 let began = Instant::now();
170 refused(xlsx::read(&bytes), what, began);
171 }
172 Ok(())
173}
174
175#[test]
176fn linked_ignore_file_not_followed() -> Outcome<()> {
177 let base = res!(scratch("fe2o3_file_hostile_link"));
178 let root = base.join("tree");
179 res!(fs::create_dir_all(&root));
180 res!(fs::write(base.join("outside"), "*.txt\n"));
181 res!(fs::write(root.join("kept.txt"), ""));
182 res!(std::os::unix::fs::symlink(base.join("outside"), root.join(".gitignore")));
183 let found = res!(walked(&root));
184 assert!(found.contains(&"kept.txt".to_string()),
185 "the linked ignore file was followed out of the tree: {:?}", found);
186 let _ = fs::remove_dir_all(&base);
187 Ok(())
188}
189
190#[test]
191fn oversize_ignore_file_not_read() -> Outcome<()> {
192 let root = res!(scratch("fe2o3_file_hostile_big"));
193 // Two MiB of comment, then a rule: past a one MiB cap the rule is never reached.
194 let mut big = "#".repeat(2 * 1024 * 1024);
195 big.push_str("\n*.txt\n");
196 res!(fs::write(root.join(".gitignore"), big));
197 res!(fs::write(root.join("kept.txt"), ""));
198 let found = res!(walked(&root));
199 assert!(found.contains(&"kept.txt".to_string()),
200 "an ignore file past the cap was read whole: {:?}", found);
201 let _ = fs::remove_dir_all(&root);
202 Ok(())
203}
204
205#[test]
206fn global_excludes_device_and_oversize_not_read() -> Outcome<()> {
207 // The only test here that touches the environment, so no other test can see it changed.
208 let dir = res!(scratch("fe2o3_file_hostile_global"));
209 let mut big = "#".repeat(2 * 1024 * 1024);
210 big.push_str("\n*.txt\n");
211 res!(fs::write(dir.join("big"), big));
212 for (what, file) in [("two MiB", dir.join("big")), ("a device", PathBuf::from("/dev/zero"))] {
213 let config = dir.join("config");
214 res!(fs::write(&config, fmt!("[core]\n\texcludesFile = {}\n", file.display())));
215 std::env::set_var("GIT_CONFIG_GLOBAL", &config);
216 let began = Instant::now();
217 let got = res!(git_global_excludes());
218 assert!(got.is_none(), "global excludes of {} were read, {} bytes",
219 what, got.map(|b| b.len()).unwrap_or(0));
220 assert!(began.elapsed() < FAST, "global excludes of {} took {:?}", what, began.elapsed());
221 }
222 std::env::remove_var("GIT_CONFIG_GLOBAL");
223 let _ = fs::remove_dir_all(&dir);
224 Ok(())
225}
226
227#[test]
228fn pptx_bullet_level_clamped() -> Outcome<()> {
229 let doc = res!(markdown::parse("# Slide\n\n- a\n - b\n"));
230 let (bytes, _) = res!(pptx::write(&Deck::from_doc(&doc)));
231 let bytes = res!(patched(bytes, "ppt/slides/slide1.xml", "lvl=\"1\"", "lvl=\"4000000000\""));
232 let deck = res!(pptx::read(&bytes)).deck;
233 let deepest = deck.slides.iter().flat_map(|s| s.bullets.iter().map(|b| b.level)).max().unwrap_or(0);
234 assert!(deepest <= oxedyne_fe2o3_file::office::deck::MAX_LEVEL,
235 "a bullet read at level {}, past the deck's deepest", deepest);
236 Ok(())
237}
238
239fn refused_for_bytes<T>(out: Outcome<T>, what: &str, began: Instant) {
240 match out {
241 Ok(_) => panic!("{}: read whole, when it should be refused for the bytes it holds", what),
242 Err(e) => {
243 let msg = fmt!("{}", e);
244 assert!(msg.contains("sheet_bytes"), "{}: refused, but not for its byte budget: {}", what, msg);
245 }
246 }
247 assert!(began.elapsed() < FAST, "{}: refused only after {:?}", what, began.elapsed());
248}
249
250#[test]
251fn ods_repeated_long_text_refused_by_byte_budget() -> Outcome<()> {
252 // A 16 KiB cell repeated across a row is 16,384 cells, well inside the cell budget, holding
253 // 256 MiB; the same cell in a one-cell row repeated down is the row loop's copy of it.
254 let text = "x".repeat(16_384);
255 let cell = |n: usize| fmt!("<table:table-cell table:number-columns-repeated=\"{}\" \
256 office:value-type=\"string\"><text:p>{}</text:p></table:table-cell>", n, text);
257 for (what, rows) in [
258 ("ods 16 KiB x 16384 across", fmt!("<table:table-row>{}</table:table-row>", cell(16_384))),
259 ("ods 16 KiB x 16384 down", fmt!("<table:table-row table:number-rows-repeated=\"16384\">{}\
260 </table:table-row>", cell(1))),
261 ] {
262 let bytes = res!(ods(&rows));
263 assert!(bytes.len() < 4096, "{}: the bomb is {} bytes", what, bytes.len());
264 let began = Instant::now();
265 refused_for_bytes(odf::sheet::read(&bytes), what, began);
266 }
267 Ok(())
268}
269
270#[test]
271fn xlsx_shared_long_string_refused_by_byte_budget() -> Outcome<()> {
272 // One 16 KiB shared string named by 16,384 cells, each read as its own copy.
273 let mut sheet = Sheet::new("Sheet1");
274 sheet.rows.push(vec![Cell::text(&"x".repeat(16_384))]);
275 let mut book = Book::new();
276 book.sheets.push(sheet);
277 let bytes = res!(xlsx::write(&book));
278 let mut rows = String::new();
279 for r in 1..=16_384 {
280 rows.push_str(&fmt!("<row r=\"{0}\"><c r=\"A{0}\" t=\"s\"><v>0</v></c></row>", r));
281 }
282 let bytes = res!(package(bytes, "xl/worksheets/sheet1.xml", fmt!(
283 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><worksheet \
284 xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\"><sheetData>{}</sheetData>\
285 </worksheet>", rows)));
286 let began = Instant::now();
287 refused_for_bytes(xlsx::read(&bytes), "xlsx shared 16 KiB x 16384", began);
288 Ok(())
289}