msoffcrypto-tool icon indicating copy to clipboard operation
msoffcrypto-tool copied to clipboard

struct.error: unpack requires a buffer of 4 bytes

Open piskvorky opened this issue 1 year ago • 2 comments

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/msoffcrypto/format/xls97.py", line 644, in is_encrypted
    if not workbook.has_record(recordNameNum["FilePass"]):
  File "/usr/local/lib/python3.10/dist-packages/msoffcrypto/format/xls97.py", line 418, in has_record
    num, size = unpack("<HH", h)
struct.error: unpack requires a buffer of 4 bytes

Unfortunately I cannot share the file that triggers this exception.

But looking at xls97.py line 418, the code assumes that self.data.read(4) returns either exactly 4 bytes or nothing:

https://github.com/nolze/msoffcrypto-tool/blob/f727b42f8b52053dcb6da9269ad773ea7722ee4f/msoffcrypto/format/xls97.py#L411-L423

I'm not sure what self.data there is but normally read(n) is guaranteed to return at most 4 bytes, not exactly 4 bytes.

piskvorky avatar Sep 26 '23 21:09 piskvorky

Thank you for reporting! This bug doesn't seem easy, and I can't think of a way to debug it without a sample. However, if I figure something out in the future, I will make fixes. Any information is also welcome.

(A quick possible fix would be changing if not h: to if not h or len(h) < 4:, but I'm not sure if it is consistent with the spec.)

nolze avatar Sep 27 '23 11:09 nolze

Thank you.

Yes, if len(h) < 4: might be a solution. Depending on what happens after that return False.

piskvorky avatar Sep 27 '23 12:09 piskvorky