haf / DotNetZip.Semverd

Please use System.IO.Compression! A fork of the DotNetZip project without signing with a solution that compiles cleanly. This project aims to follow semver to avoid versioning conflicts. DotNetZip is a FAST, FREE class library and toolset for manipulating zip files. Use VB, C# or any .NET language to easily create, extract, or update zip files.
Other
545 stars 218 forks source link

ZipEntry.Extract: Setting invalid FileAttributes causes exception on unix-based systems #286

Open ir-dev opened 7 months ago

ir-dev commented 7 months ago

Version: DotNetZip 1.16.0

I noticed that when extracting certain zip files (as I later saw zip files with entries with potentially invalid "external file attributes") I was getting an exception:

System.ArgumentException: Invalid File or Directory attributes value. (Parameter 'Attributes')
   at System.IO.FileStatus.SetAttributes(string path, FileAttributes attributes)
   at System.IO.FileSystem.SetAttributes(string fullPath, FileAttributes attributes)
   at Ionic.Zip.ZipEntry.MoveFileInPlace(bool fileExistsBeforeExtraction, string targetFileName, string tmpPath, bool checkLaterForResetDirTimes)
  ..

The issue roots in the following call which will happen at the end of zip extraction process: https://github.com/haf/DotNetZip.Semverd/blob/42a096d455e0eaaa80f1344024df0d36c106de80/src/Zip.Shared/ZipEntry.Extract.cs#L903

I looked into it and saw that i tried to extract ZipEntry with Attribute property value: 0x81000000 which obviously wouldn't work here as the System.IO.FileAttributes doesn't even have such a bit flag defined.

For me, the strange thing about it is that the same extraction with the same data previously ran on a Win system without any problems, whereas now I use Linux on amd64 arch. Hence, I think the BCL for System.IO.FileStatus behaves differently in the corresponding runtime version between these operating systems, in the sense that the flag enum is validated on Unix-based systems, while invalid values being probably ignored on Win.

However, I think it doesn't matter if the FileAttributes are invalid or even if the BCL there has an inconsistency, in my opinion it should actually never throw in this place while setting file attributes. If they were invalid, I would assume that the library is simply using default values for the attributes or even not setting them at all. Eventually, I think this approach will be better, especially considering that other zip libraries ignore setting the file attributes anyway.

Add.: Temporary workaround or even to-go solution for this issue will be to only set ZipEntry Attributes property to valid FileAttributes with valid bit flags specified in the enum otherwise setting the prop to FileAttributes.None.