Skip to content

fix: delete staged xlsx when encrypted stream write fails - #1147

Closed
kalayciburak wants to merge 1 commit into
apache:mainfrom
kalayciburak:fix/gh-1137-temp-xlsx-leak
Closed

kalayciburak wants to merge 1 commit into
apache:mainfrom
kalayciburak:fix/gh-1137-temp-xlsx-leak

Conversation

@kalayciburak

Copy link
Copy Markdown

Purpose of the pull request

Closed: #1137

A password-protected xlsx written to an OutputStream is staged in a temp file by doOutputStreamEncrypt07(). If workbook.write fails and the following close() succeeds, that temp file was never deleted.

What's changed?

After the stream is closed, delete the staged file when write() did not succeed. The success path is unchanged: the file is still removed after encryption. Delete happens only after close(), so the unlink is not attempted while the stream still holds the file.

Checklist

  • I have read the Contributor Guide.
  • I have written the necessary doc or comment.
  • I have added the necessary unit tests and all cases have passed.

Test plan

  • JAVA_HOME=17 ./mvnw -pl fesod-sheet -Dtest=WriteContextImplEncryptTempFileTest -Dsurefire.failIfNoSpecifiedTests=false -Dmaven.test.skip=false test — 1/0. Before the fix the same test left the staged xlsx.
  • JAVA_HOME=17 ./mvnw -pl fesod-sheet -Dtest=WriteContextImplEncryptTempFileTest,EncryptDataTest -Dsurefire.failIfNoSpecifiedTests=false -Dmaven.test.skip=false test — EncryptDataTest 4/0, no failures.
  • ./mvnw -pl fesod-sheet spotless:check — clean.

A password-protected xlsx written to a stream is staged in a temp file.
If workbook.write fails and close succeeds, that file was left behind.

Closed: apache#1137
Signed-off-by: kalayciburak <kalayciburak1996@gmail.com>
@nkuprins

Copy link
Copy Markdown
Contributor

Hi @kalayciburak, thanks for your interest in this! 🙂

I'd ticked "willing to submit a PR" on #1137 and was waiting for a maintainer to confirm the bug and the proposed fix before opening one.

On the implementation:

  • This PR adds a wrote flag and a third delete on top of the existing nested catch, so the temp file is now cleaned up in three places.
  • The fix proposed in the issue replaces all of that with a single outer finally and one delete, and it behaves the same in every failure case.

Would you mind closing this so the fix can go ahead once the issue and fix are confirmed?

Tip

A small tip for next time: if an issue has "willing to submit a PR" ticked, a quick comment asking the author first saves duplicate work for both of us. Contributions are very welcome, and I'd be glad to see more from you! 🙏

@kalayciburak

Copy link
Copy Markdown
Author

sorry, missed the willing-to-submit flag on #1137. your finally approach is cleaner than the extra wrote flag. closing this so you can open yours once the issue is confirmed.

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.

[Bug] xlsx stream write leaves temp file

2 participants