craft-xlsx: review fixes: styles caps are too large, differential format strings are charged

- A styles table past 65,536 entries, or a gradient past 1,024 stops, is past
  a fixed cap: Error::TooLarge, as the error's contract and the other caps
  have it, not a damaged part that is skipped with a warning.
- A differential format's font name and number format are charged to
  binary_text, as the other string-bearing style records are.
This commit is contained in:
kiwi4boy committed 2026-11-14 12:40:55 -05:00
1 parent 14e516edf4
commit 88287a6b46
3 files changed
+41 -23

No files matched your search

+16 -15
View File
@@ -83,21 +83,22 @@ for new pictures (`vm = i + 1`) and the `XLDAPR` dynamic-array metadata (`cm = 1
The parts this crate writes use the transitional namespaces; a strict file's kept parts stay as
they were. A binary workbook is not written.
A binary workbook gives the same `Workbook`, strings, rows and merged cells, and the same
`Styles`: number formats, fonts, fills, borders, cell and cell style formats, cell styles,
differential formats and the indexed colours, as Excel writes them in `styles.xml` (default fill
colours and empty settings left out, a cell style format's `apply*="0"`, a number format with a
newer locale tag in its Excel 2007 spelling, as `formatCode` has it). Custom table styles and
recent colours are not read; a damaged styles part is a warning and no styles. Its cell formulas
are decoded from their tokens into the text an `.xlsx` file holds (`_xlfn.` and `_xlpm.` names as
written, apart from the four ISO/IEC 29500 functions `.xlsx` names bare, no implicit `@`, the
spaces and line breaks typed in the formula, numbers as Excel spells them), with
`FormulaKind::Shared` or `FormulaKind::Array` and their ranges, and `BrtCellMeta` as the cell
metadata index. Every cell of a shared formula gets its own text, decoded at the cell, since the
file keeps the tokens once. A formula whose decoding needs something the reader does not read
keeps only its saved result, with a warning saying why: user defined names, table references,
other workbooks, Python code stored outside the cell, data tables. The XLSB function table
(`xlsb/ftab.rs`) and the expression printer are this crate's own, apart from `craft-xls`'s.
A binary workbook gives the same `Workbook`, strings, rows and merged cells, and the same `Styles`:
number formats, fonts, fills, borders, cell and cell style formats, cell styles, differential
formats and the indexed colours, as Excel writes them in `styles.xml` (default fill colours and
empty settings left out, a cell style format's `apply*="0"`, a number format with a newer locale tag
in its Excel 2007 spelling, as `formatCode` has it). Custom table styles and recent colours are not
read; a damaged styles part is a warning and no styles, while one past a cap (65,536 entries in a
table, 1,024 gradient stops) or past `binary_text` is too large. Its cell formulas are decoded from
their tokens into the text an `.xlsx` file holds (`_xlfn.` and `_xlpm.` names as written, apart from
the four ISO/IEC 29500 functions `.xlsx` names bare, no implicit `@`, the spaces and line breaks
typed in the formula, numbers as Excel spells them), with `FormulaKind::Shared` or
`FormulaKind::Array` and their ranges, and `BrtCellMeta` as the cell metadata index. Every cell of a
shared formula gets its own text, decoded at the cell, since the file keeps the tokens once. A
formula whose decoding needs something the reader does not read keeps only its saved result, with a
warning saying why: user defined names, table references, other workbooks, Python code stored
outside the cell, data tables. The XLSB function table (`xlsb/ftab.rs`) and the expression printer
are this crate's own, apart from `craft-xls`'s.
## Hostile input
+11 -4
View File
@@ -176,7 +176,7 @@ fn fill(b: &mut Bytes<'_>) -> Result<Fill, Error> {
let count = b.u32()?;
if fls == 0x28 {
if count > MAX_STOPS {
return Err(invalid("too many gradient stops"));
return Err(Error::TooLarge(format!("more than {MAX_STOPS} gradient stops")));
}
for _ in 0..count {
let c = color(b)?.unwrap_or_default();
@@ -330,7 +330,7 @@ fn dxf(b: &mut Bytes<'_>) -> Result<Dxf, Error> {
0x04 => {
let g = gradient_fill.as_mut().ok_or_else(|| invalid("gradient stop without a gradient"))?;
if g.stops.len() >= MAX_STOPS as usize {
return Err(invalid("too many gradient stops"));
return Err(Error::TooLarge(format!("more than {MAX_STOPS} gradient stops")));
}
p.u16()?;
let position = p.f64()?;
@@ -418,7 +418,7 @@ pub(crate) fn styles(bytes: &[u8], limits: &Limits, budget: &Budget, warnings: &
_ => 0,
};
if entries >= MAX_ENTRIES {
return Err(invalid(format!("more than {MAX_ENTRIES} entries in a styles table")));
return Err(Error::TooLarge(format!("more than {MAX_ENTRIES} entries in a styles table")));
}
match id {
279 => {
@@ -451,7 +451,14 @@ pub(crate) fn styles(bytes: &[u8], limits: &Limits, budget: &Budget, warnings: &
budget.text(c.name.len(), limits)?;
s.cell_styles.push(c);
}
507 => s.dxfs.push(dxf(&mut b)?),
507 => {
let d = dxf(&mut b)?;
// What it keeps of its strings, after a later property has replaced an earlier.
let font = d.font.as_ref().and_then(|f| f.name.as_ref()).map_or(0, String::len);
let format = d.num_fmt.as_ref().and_then(|n| n.code.as_ref()).map_or(0, String::len);
budget.text(font + format, limits)?;
s.dxfs.push(d);
}
// BrtBeginIndexedColors, BrtEndIndexedColors.
565 => palette = true,
566 => palette = false,
+14 -4
View File
@@ -1196,11 +1196,11 @@ fn damaged_styles_are_a_warning_and_the_workbook_opens() {
assert!(warnings.iter().any(|w| w.contains("look/styles.bin")), "{why}: {warnings:?}");
assert_eq!(read(&bytes).unwrap().sheets[0].value(CellRef::new(0, 0)), Value::Number(1.0), "{why}");
}
// A table past 65,536 entries is refused before it is held.
// A table past 65,536 entries is past a fixed cap: refused before it is held, as too large.
let xfs = brt_xf(0, 0, 0, 0, 0, 0, 0, 0x1010, 0).repeat(65_537);
let (styles, warnings) = styles_of(&styled_file(&with(&[&begin(617, 65_537)[..], &xfs, &record(618, &[])].concat()), STYLES_TYPES));
assert!(styles.is_none());
assert!(warnings.iter().any(|w| w.contains("65536 entries")), "{warnings:?}");
let bytes = styled_file(&with(&[&begin(617, 65_537)[..], &xfs, &record(618, &[])].concat()), STYLES_TYPES);
let mut p = Package::open(&bytes, &Limits::default()).unwrap();
assert!(matches!(p.styles(), Err(Error::TooLarge(m)) if m.contains("65536 entries")));
// Custom table styles are not read, and say so.
let table_styles = [body, &record(508, &[&1u32.to_le_bytes()[..], &wide("Probe Table"), &wide("")].concat()), &end].concat();
let (styles, warnings) = styles_of(&styled_file(&table_styles, STYLES_TYPES));
@@ -1339,3 +1339,13 @@ fn a_spent_formula_allowance_skips_decoding() {
assert!(warned(&wb, "more formula text than the limits allow"), "{:?}", wb.warnings);
assert!(!warned(&wb, "unknown formula token"), "{:?}", wb.warnings);
}
#[test]
fn differential_format_strings_are_charged_to_the_text_allowance() {
// A DXF's font name and number format are text the styles part keeps, charged like a font's.
let dxf = |name: &str| [record(278, &[]), begin(505, 1), brt_dxf(&[prop(0x18, &short(name))]), record(506, &[]), record(279, &[])].concat();
let limits = Limits { binary_text: 100, ..Limits::default() };
let open = |part: &[u8]| Package::open(&styled_file(part, STYLES_TYPES), &limits).unwrap().styles();
assert!(open(&dxf("Arial")).is_ok_and(|s| s.is_some_and(|s| s.dxfs.len() == 1)));
assert!(matches!(open(&dxf(&"x".repeat(200))), Err(Error::TooLarge(_))));
}