dotnet / Open-XML-SDK

Open XML SDK by Microsoft
https://www.nuget.org/packages/DocumentFormat.OpenXml/
MIT License
4k stars 544 forks source link

OpenXmlValidator breaks digital signatures. #1358

Open petarpetrovt opened 1 year ago

petarpetrovt commented 1 year ago

Describe the bug Validating a SpreadsheetDocument using OpenXmlValidator breaks digital signatures.

Screenshots styles workbook

To Reproduce

using var document = SpreadsheetDocument.Open("valid.xlsx", isEditable: true);
var validator = new OpenXmlValidator();
// This line breaks the signatures
validator.Validate(document);
document.Dispose();

Steps to reproduce the behavior: Create .xlsx file using Microsoft Excel and add digital signature or signature line and sign it.

Observed behavior Some parts (styles.xml and workbook.xml) get modified, which breaks digital signatures.

Expected behavior No changes should happen when simply validating a package.

Desktop (please complete the following information):

Additional context When the document is loaded with the isEditable:false option, the issue does not occur.

twsouthwick commented 1 year ago

Is there a scenario where you need to pass isEditable:true but don't want to change it?

We can investigate what is changing here, but if the flag is being passed as isEditable=true, then you're opting into things potentially changing, which would break any digital signature. It may be an interesting exercise to understand what is changing, but I'm not sure this is unexpected given that you're opting into things changing.

petarpetrovt commented 1 year ago

I am appending a co-signature or prolonging the previous signature and aiming to authenticate the package prior to saving it. The addition or extension of signatures does not invalidate the preceding signature.

tomjebo commented 1 year ago

@petarpetrovt In case this is still blocking you... As a workaround, you could make the changes, clone the changed package, validate the clone and then save the original. I tried this approach and it appears to work. After validation, the clone does have some minor changes but the original is intact.