mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-02-03 21:23:38 +00:00
Entry is trying to write a file outside of the destination #679
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @crystalgreen on GitHub (Mar 21, 2025).
Originally assigned to: @Copilot on GitHub.
I get the above mentioned error with this code
when running as Windows Service and the LocalSystem account. I use pkgWorkDir that is constructed starting with
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData).On my system, this returns
C:\Windows\system32\config\systemprofile\AppData\Local.The code throws the error
As one can see, there is a difference between System32 and system32.
The lib code here uses
StringComparison.Ordinalto compare the strings.My issue could be fixed, if - under Windows OS - we use
StringComparison.OrdinalIgnoreCase.@adamhathcock commented on GitHub (Mar 21, 2025):
This isn't an obvious fix because other OSes use case sensitive file systems. I guess the addition of an option based on OS is relevant
@Morilli commented on GitHub (Mar 21, 2025):
I can't reproduce this with a simple example zip containing only one entry.
Logging the relevant paths on extract results in:
As you can see, all of these use lowercase
system32and still work. Could you provide an example zip and full example code that reproduces this issue?@DoeWayne commented on GitHub (Mar 24, 2025):
This problem do not happen on each windows system. I got three environments and the problem only exist on one system. So there would be no specific example code which could be provided. The best would be as suggested by @adamhathcock to add an extra option or flag which can be set.
@adamhathcock commented on GitHub (Mar 24, 2025):
or just write your own extraction code....the write to destination is just a shortcut method.
@crystalgreen commented on GitHub (Mar 24, 2025):
I extract a 7z archive and the USAGE doc indicates it is more efficient to use
ExtractAllEntriesorWriteAllToDirectory. Is there another efficient way with more control over extraction?@adamhathcock commented on GitHub (Mar 24, 2025):
look and see what those methods do....they get the entries and stream copy basically
@Morilli commented on GitHub (Mar 24, 2025):
What makes you so sure that the string comparison is the issue then? The fact that this code does work on some systems, even if the paths have casing different from what exists on the disk makes me believe that case differences are not the problem here.
Perhaps if you could compile the library while adding logs to the failing code it would help in tracking down the cause:
@DoeWayne commented on GitHub (Mar 24, 2025):
We did this already:
Entry is trying to write a file outside of the destination directory:
fullDestinationDirectoryPath:
C:\Windows\system32\config\systemprofile\AppData\Local\AppPkgs\XL9j\7OLtRyxbLNavRSbmtk3UQ_tmp
destinationFileName:
C:\Windows\System32\config\systemprofile\AppData\Local\AppPkgs\XL9j\7OLtRyxbLNavRSbmtk3UQ_tmp\js\7cf1.js
@Morilli commented on GitHub (Mar 24, 2025):
Something weird must be going on then because the only operations performed on the path are
Path.Combinewhich doesn't even touch the paths andPath.GetFullPathwhich should generally also not modify the paths passed to it, however I could see some weird things happening if windows' short paths are used somehow.Using case-insensitive comparison should definitely not be the fix here in any case as most file systems are case sensitive, including NTFS.
@DoeWayne commented on GitHub (Mar 25, 2025):
Here some more test we did
So it looks like that the
is changing it. We created a small example and if we run it in that environment it failed as well. But as mentioned not in all environments:
Failed on Windows 2016 1607 Build 14393.7876
@adamhathcock commented on GitHub (Mar 25, 2025):
Odd...doing a search and the AI says the method will "normalize" casing but haven't found anywhere saying it yet
@jdpurcell commented on GitHub (Mar 25, 2025):
Tilde in the filename seems to trigger this behavior, As @Morilli suspected it's due to short path (8.3) handling, but it's not even a short name really, it happens with any tilde.
@jdpurcell commented on GitHub (Mar 26, 2025):
As for a workaround you could try this before calling
WriteAllToDirectory:@Morilli commented on GitHub (Mar 26, 2025):
This unfortunately appears to be a badly documented quirk of
Path.GetFullPath: The documentation statesIf you pass in a short file name, it is expanded to a long file name, however~that aren't short paths are also expanded, and~are never expanded, even though they should beSo in effect, the function may call
GetLongPathNameWon (part) of the passed in path, which may change casing (of part of the path). In my opinion this is a bug in the .NET runtime library.@crystalgreen commented on GitHub (Mar 27, 2025):
@jdpurcell Your workaround looks promising, thanks.
This issue can be closed unless you consider adding the workaround to the lib.