Skip to content

TarArchive.WriteEntryCore() throws UnauthorizedAccessException if user does not have write permissions in application directory #644

Closed

Description

@crusopaul

Steps to reproduce

  1. Create & build an application that will create a user-specified tar file with ASCII translation turned on (utilizing TarEntry.WriteEntry())
  2. Move the application and SharpZipLib dll into a testing folder
  3. Create a test file in the testing folder (to be tar'd in step 5)
  4. Remove the tester's write permissions from the testing folder
  5. Run the application, with the current working directory set to the testing folder, to create a tar file in a folder that the tester has write permissions on

Expected behavior

A tar file should be created in the folder that the tester has write permissions on / supplied to the application.

Actual behavior

UnauthorizedAccessException is thrown as the tester does not have write permissions in the working directory for the application.

Version of SharpZipLib

1.3.2

Obtained from (only keep the relevant lines)

  • Package installed using NuGet

TLDR

Ran into a permissions issue after updating from 1.2.0 to 1.3.2. Looks like issues #537 and #543 were related to the change that caused it. I am hoping that the last two sentences in the following comment can be implemented (at least for TarArchive.WriteEntryCore()): #537 (comment)

Activity

  1. piksel commented on Jun 30, 2021

    @piksel
    Member

    So, if I understand this correctly, you are trying to create an archive in

    /tmp/target/output.tar
    

    by adding a file from

    /tmp/source/sourcefile.txt
    

    but you get a UnauthorizedAccessException because of no write access to /tmp/source/, even though the output should be in /tmp/target/?
    You also use /tmp/source/ as the CWD, is that essential?
    If so, it looks like the temporary file is created in CWD instead of the target directory for some reason...

    Also, are you using TarOutputStream or TarArchive? A short repro snippet would be helpful. It kind of sound like you are crossing the Create/Update code paths somehow...

  2. piksel commented on Jun 30, 2021

    @piksel
    Member

    Never mind, found the bug. Path.GetTempFileName() returns a file path including temp path, but Path.GetRandomFileName() returns just the file path. And if no absolute path is given, that is of course interpreted as CWD.

  3. crusopaul commented on Jun 30, 2021

    @crusopaul
    Author

    Oh wow, thank you for the quick response and change! That is all correct. In fact, I would not have noticed this if the CWD was not the same as the read-only directory.

    That said, it seems like the change that addresses this issue is reintroducing the concern mentioned in #537 and #543. Maybe I should open a new issue for this, but I ultimately would want to avoid an unnecessary temporary file from being created at all - be it in the CWD or a user's temp directory.

    Specific to tar creation: is there a need for a temporary translated file to be created, or is this done out of convenience? I am not seeing a necessity, but want to check before I pester further / work on a PR 🙂

  4. piksel commented on Jun 30, 2021

    @piksel
    Member

    Sort of. The reason for doing it at that step is that the size of the file needs to be calculated, and in doing so you are already creating the translated version of the file, so instead of doing that once more in the next step it just uses a temporary file.
    The main reason for doing it this way is probably because of memory concerns. For a smaller file it wouldn't be noticeable to do the conversion twice, nor to store the result in a MemoryStream. But what about a multi-GiB file? Where do you draw the line?
    Except for that, it should be fairly easy to do. Write into a MemoryStream and use that instead of the FileStream when writing the contents a couple of lines further down... Or do the translation twice. That's slower, but you don't have to consider the memory usage.

  5. crusopaul commented on Jun 30, 2021

    @crusopaul
    Author

    Gotcha. I think I'd want to attempt a change that does not utilize a MemoryStream. Like you hinted, a MemoryStream introduces the issue that a larger file could exhaust memory resources and make the tar creation impossible. Performing the translation twice is slow and therefore not ideal, but would not make tar creation impossible in the case of a larger file or less memory. In other words, I would argue that a performance bottleneck is preferable to a restriction.

    If there are no qualms with that approach, I'll go ahead and work on a PR this weekend. Would you want to see a new issue for this or would the PR be sufficient?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions