Skip to content

Prevents a crash when a data link reads a damaged spreadsheet - #3465

Closed
travisjhughes wants to merge 2 commits into
HakanSeven12:mainfrom
travisjhughes:workbook-read-panic
Closed

travisjhughes wants to merge 2 commits into
HakanSeven12:mainfrom
travisjhughes:workbook-read-panic

Conversation

@travisjhughes

@travisjhughes travisjhughes commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Change summary

A data link, the Table dialog and a paste read spreadsheet files with calamine. A damaged .xls file makes calamine panic, and these reads run in message handlers on the UI thread, so the app stops.

With this change each spreadsheet read runs under catch_unwind. A panic comes back as the error of the read (the spreadsheet reader stopped: <panic text>), which the callers already show.

A file of 512 bytes is enough: the .xls signature and a header whose directory starts in a sector that the sector table does not have.

What specifically changed

Two commits: the fix, then its tests.

  • src/app/annotation_data.rs:
    • workbook_read(read) calls read inside catch_unwind and makes an error from a panic. A wasm build aborts on a panic, so there it only calls read.
    • The five places that open a workbook run inside it: workbook_metadata (a file on disk), workbook_metadata_bytes, workbook_first_sheet_rows, and the two branches of read_tabular_file (a picked file in memory, a file on disk). The open and the read of the sheets are both inside, because calamine reads some parts only when a sheet is asked for.
  • Tests in the same file: 5 new.

Why

  • The panic is real. In calamine 0.36.1 (the version in Cargo.lock) and in 0.38.0 (the newest), Sectors::get_chain (src/cfb.rs) follows the sector chain with fats[sector_id as usize] and does not check the index. A file with an empty or short sector table panics there: index out of bounds.
  • It reaches the UI thread. The callers are message handlers: the Table dialog (on_table_insert_apply), the Data Link Manager (on_data_link_insert), the refresh of a linked table (src/app/commands/table_cells.rs) and the paste of a workbook (src/app/shortcuts.rs).
  • The file comes from other people: a linked spreadsheet is an external file next to the drawing.
  • RUSTSEC-2026-0436 is an advisory for this parser (panic and unbounded allocation on malformed input, repaired in 0.38.0). The update to 0.38.0 is a separate change; this panic is still there after it.

Testing

On main 0ca03459b (calamine 0.36.1). I also ran the tests with calamine 0.38.0: the same results, and without the guard the same panic at cfb.rs:344.

  • New tests (5), with a damaged file that the test builds (the header, then one empty sector):
    • in memory: the sheet list and the first sheet's rows are errors that start with the spreadsheet reader stopped: ; the header with nothing after it gives the same;
    • on disk: workbook_metadata and read_tabular_file are errors;
    • a picked file (its bytes registered under its name): read_tabular_file is an error;
    • a file cut in the middle of the header is an error (it was one before; calamine refuses it);
    • a read that does not panic gives its own value and its own error.
  • The tests fail without the fix. With catch_unwind removed from workbook_read, the three tests with the damaged file fail (the panic leaves the test).
  • Before and after (cargo test --workspace --locked --no-fail-fast, cargo check --workspace --all-targets, clippy, the wasm32 check, rustfmt on the changed files, the golden file): 7,729 passed and 1 failed before; 7,735 passed and 0 failed after. The 5 new tests are the only other difference. One test of main, picklocked_off_makes_locked_layer_objects_unselectable, failed in the run of main and passed in the run with this change; it also passed in three other runs on the same main, so it is not stable on main and not from this change. Warnings, the wasm32 check, clippy, rustfmt and the golden file are the same.
  • Fault injection with cargo mutants on the diff: 27 mutants, 26 caught, 1 not viable, 0 missed.

Not covered

  • An unbounded allocation or a stack overflow in the reader. catch_unwind does not stop those. The update of calamine to 0.38.0 repairs the allocation cases of the advisory.
  • The cause in calamine. The index is not checked in 0.38.0. The calamine project has a report of it (its issue 764), and its master branch (93e2e6b) gives an error for the damaged file of the tests. No release has that repair yet.
  • Reads on a worker. The reads stay on the UI thread, so a large or slow file still blocks the window.
  • The other formats (.xlsx, .xlsb, .ods): they go through the same guard, but I made no damaged file for them.
  • The web build: a panic aborts there.

Impact to Contributors

A new place that opens a workbook with calamine: do it inside workbook_read.

Co-Authored-By: Claude

calamine panics on a damaged .xls file: Sectors::get_chain follows the
sector chain with an index that it does not check. The data link, the
Table dialog and a paste read workbooks in message handlers on the UI
thread, so the panic stops the app. Each of the five places that open
a workbook now runs inside workbook_read, which gives a panic back as
the read's error.

Co-Authored-By: Claude
The tests build a damaged .xls file: the header, with a directory
that starts in a sector which the empty sector table does not have.
The sheet list and the rows are errors for the file in memory, on
disk and as a picked file. A read that does not panic gives its own
result.

Co-Authored-By: Claude

Copy link
Copy Markdown
Owner

Thanks! I merged #3466 (calamine 0.38.0) but I'm closing this one:

  • On desktop the app does not stop on this panic. The Table dialog, the Data Link Manager, refresh, paste and alias Import all run inside update_guarded, which catches it and rolls the command back.
  • The panic itself is in calamine (cfb.rs get_chain). The fix is a calamine release that carries it. Wrapping the reader in catch_unwind would only work around the bug.
  • The new test also registers a picked source and never removes it, so it leaks into other tests.

Generated by Claude Code

@travisjhughes
travisjhughes deleted the workbook-read-panic branch November 23, 2026 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants