Skip to content

Conversation

@allisonlarson
Copy link
Member

When untarring a tarball, attempt to create files initially with the
umask file permissions and delete the file if it fails to copy
correctly. This prevents a partially written file from remaining on disk
with permissions that didn't include the umask.

Adds a unit test and a invalid tarball (created by truncating tarball containing a large
file generated from /dev/urandom), which fails to decompress but errors
partially written.

PCI review checklist

  • I have documented a clear reason for, and description of, the change I am making.

  • If applicable, I've documented a plan to revert these changes if they require more than reverting the pull request.

  • If applicable, I've documented the impact of any changes to security controls.

    Examples of changes to security controls include using new access control methods, adding or removing logging pipelines, etc.

@allisonlarson allisonlarson requested a review from a team as a code owner October 22, 2025 17:44
When untarring a tarball, attempt to create files initially with the
umask file permissions and delete the file if it fails to copy
correctly. This prevents a paritally written file from remaining on disk
with permissions that didn't include the umask.

Adds a unit test and a invalid tarball (created by truncating a large
file generated from /dev/urandom), which fails to decompress but errors
partially written.
Copy link
Collaborator

@dduzgun-security dduzgun-security left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for the PR 🙏

CreatorHead
CreatorHead previously approved these changes Oct 23, 2025
Copy link
Contributor

@CreatorHead CreatorHead left a comment

Choose a reason for hiding this comment

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

Lgtm

@dduzgun-security
Copy link
Collaborator

Looks like the Windows unit test in the CI are not happy with the setuid2 bits added in the tests here.

We should consider adding a case if runtime.GOOS = "windows" for thisTestDecompressTarPermissionsFailed to skip it and add another test for Windows only TestDecompressTarPermissionsWindows which starts with if runtime.GOOS != "windows"

@allisonlarson
Copy link
Member Author

allisonlarson commented Oct 23, 2025

@CreatorHead @dduzgun-security I was able to get the tests to pass on windows by explicitly closing the file (whoops!). I'd like to avoid splitting the tests since we expect the same behavior regardless of the underlying OS. Ready for another look!

Copy link
Collaborator

@dduzgun-security dduzgun-security left a comment

Choose a reason for hiding this comment

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

🚀

@allisonlarson allisonlarson changed the title Handle failed decompressed files Remove failed decompressed files Oct 23, 2025
Copy link
Member

@jrasell jrasell left a comment

Choose a reason for hiding this comment

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

LGTM.

Minor comments about duplicate close calls to the file handler.

_, err = io.Copy(dstF, src)
if err != nil {
// Close & remove the file in case of partial write
_ = dstF.Close()
Copy link
Member

Choose a reason for hiding this comment

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

We already call close in a deferred function on line 42, so do we need this? In any case, having a second call won't cause any errors as we are throwing away the returned error.

Copy link
Member Author

Choose a reason for hiding this comment

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

The windows test case would fail here because the file was reported as being in use at the time of removal. Adding in the explicit file closed fixed it and allowed the file to be removed, so it seems like windows has a different requirement/handling that needs the file to be explicitly closed before removing it. I figured it's better to close the file an extra time as opposed to trying to check.

count, err := Copy(ctx, dstF, srcF)
if err != nil {
// Close & remove the file in case of partial write
_ = dstF.Close()
Copy link
Member

Choose a reason for hiding this comment

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

We already call close in a deferred function on line 84, so do we need this? In any case, having a second call won't cause any errors as we are throwing away the returned error.

Copy link
Member Author

Choose a reason for hiding this comment

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

Same reasoning as above, keeping the way these are handled in sync.

@dduzgun-security
Copy link
Collaborator

@CreatorHead Can you Squash and merge the PR when you get some time please? I think we don't have the permissions.

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.

4 participants