Skip to content

xlsb: return an error on unchecked indexes in malformed files - #800

Open
hsdfat wants to merge 2 commits into
tafia:masterfrom
hsdfat:xlsb-unchecked-indexes
Open

hsdfat wants to merge 2 commits into
tafia:masterfrom
hsdfat:xlsb-unchecked-indexes

Conversation

@hsdfat

@hsdfat hsdfat commented Nov 13, 2026

Copy link
Copy Markdown

Summary

Fixed issue where three malformed xlsb inputs caused a panic instead of an error. Each one now returns XlsbError::Unrecognized, the variant the BrtName fix in #761 uses:

  • A BrtCellIsst record whose shared string index is past the shared strings table, or in a workbook with no shared strings part (self.strings[isst]).
  • A sheet whose relationship id is not in workbook.bin.rels (relationships[&*relid]).
  • A BrtWsDim record shorter than 16 bytes (&buf[..16]). The check is on the record length and not on the buffer, because the buffer also held the skipped BrtBeginSheet/BrtWsProp records and can be longer than the record.

Related issue

Fixes #799.

Testing

Three test files under tests/, one per case, and a test for each in tests/test.rs. Without the change each test panics at the line named in the issue; with it they pass.

The files are hand-edited, since the reporter's seed files are not posted yet. I took tests/unknown_record_before_names.xlsb, removed its unknown workbook record, and changed one thing per file:

  • shared_string_index_out_of_range.xlsb: added a BrtRowHdr and a BrtCellIsst with index 5 to the sheet. The workbook has no sharedStrings.bin.
  • sheet_relationship_not_found.xlsb: the BrtBundleSh relationship id is rId9 instead of rId1.
  • short_ws_dim_record.xlsb: the BrtWsDim record is 8 bytes instead of 16.

If the reporter's files show a different shape for any of these, I am happy to swap them in.

Checklist

  • cargo test --all-features passes.
  • cargo fmt -- --check passes.
  • cargo clippy --all-targets --all-features -- -Dwarnings passes.
  • New or changed behavior is covered by a test.
  • Code and tests follow CONTRIBUTING.md.

Other information

Not run locally: cargo +1.88 check, the wasm32-unknown-unknown check and typos, as I do not have those installed. The change only uses slice::get and Option::ok_or.

Fixed issue where three malformed xlsb inputs caused a panic instead of
an error: a BrtCellIsst record whose shared string index is past the
shared strings table, a sheet whose relationship id is not in
workbook.bin.rels, and a BrtWsDim record shorter than 16 bytes.

Fixes tafia#799.
Comment thread src/xlsb/cells_reader.rs Outdated
],
&mut buf,
)?;
// The buffer can be longer than the record, since it also held the

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is confusing. Maybe something like:

  // The buffer is reused for each record and is never shrunk, so it can be
  // longer than this record. Check the record length instead.

Comment thread src/xlsb/mod.rs Outdated
Comment on lines +312 to +318
let target =
relationships
.get(&*relid)
.ok_or_else(|| XlsbError::Unrecognized {
typ: "sheet relationship id",
val: relid.to_string(),
})?;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be better as:

let Some(target) = relationships.get(&*relid) else { return Err(...) };

Comment thread src/xlsb/cells_reader.rs Outdated
// BrtCellIsst
let isst = read_usize(&self.buf[8..12]);
DataRef::SharedString(&self.strings[isst])
let string = self.strings.get(isst).ok_or(XlsbError::Unrecognized {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think .ok_or() will allocate every time. This would be better as ok_or_else():

  let string = self.strings.get(isst).ok_or_else(|| XlsbError::Unrecognized {
      typ: "shared string index",
      val: isst.to_string(),
  })?;

Or as an "let else":

  let Some(string) = self.strings.get(isst) else {
      return Err(XlsbError::Unrecognized {
          typ: "shared string index",
          val: isst.to_string(),
      });
  };
  DataRef::SharedString(string)

@jmcnamara

Copy link
Copy Markdown
Collaborator

Thanks for the fixes. There are other similar issues in the code that you could also fix in passing (after double checking):

  • The BrtBundleSh arm, next to fix 2: read_u32(&buf[8..len]) and &buf[12..12 + rel_len]. rel_len comes straight from the file, so a bad value panics just as easily as a bad relationship id.
  • BrtWbProp: buf[0].
  • BrtCellIsst: the index read read_usize(&self.buf[8..12]), and the other fixed offsets in the cell reader.

Apply the review suggestions and check the length of BrtWbProp,
BrtBundleSh, the cell records and the formula records before they are
indexed. The record buffer is reused and never shrunk, so a short cell
record was read with the bytes of an earlier record.
@hsdfat

hsdfat commented Nov 13, 2026 •

Copy link
Copy Markdown
Author

Thanks for the review. Changes are in 6fb85db:

  • Comment on the BrtWsDim check reworded as suggested.
  • The relationship lookup and the shared string lookup now use let ... else, so nothing is allocated on the success path.
  • BrtWbProp: an empty record is an error.
  • BrtBundleSh: the record must hold the 12 fixed bytes, and the relationship id must end inside the record.
  • next_cell: the fixed-size part of each cell record is checked against the record length before it is read. The buffer is reused and never shrunk, so a short record did not always panic here: it was read with the bytes of an earlier record. BrtCellSt is now also read only up to the end of its record.
  • next_formula: the cch and cce lengths in BrtFmlaString, BrtFmlaNum, BrtFmlaBool and BrtFmlaError are checked against the record.

Four new fixtures and tests cover an empty BrtWbProp, a short BrtBundleSh, a short BrtCellIsst and a short BrtFmlaNum. Without the source changes three of them panic and the short cell test fails. cargo test, cargo fmt --check and cargo clippy --all-targets -- -D warnings pass locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs work for merge The PR needs some rework or clarification. No suitable for merge, yet. next release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

xlsb: panics on unchecked indexes (shared string index, sheet relationship, short BrtWsDim)

2 participants