Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added test for extracting zip files with invalid characters in Windows #67332

Merged
merged 23 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
56a49cb
Added test for extracting zip files with invalid characters in Windows
Danyy427 Mar 30, 2022
682dbe3
Removed faulty test case
Danyy427 Mar 30, 2022
ebac56d
Removed redundant test cases, Added contents of the InvalidWindowsFil…
Danyy427 Mar 30, 2022
feac6a7
Replaced "+"s to Path.Combine for forming the test file path.
Danyy427 Mar 30, 2022
b8afbef
Update src/libraries/System.IO.Compression.ZipFile/tests/ZipFile.Extr…
danmoseley Mar 30, 2022
1e9ba60
Added code to use ValidFullName across several files
Danyy427 Mar 31, 2022
d97c6a2
Merge branch 'Danyy427-ZipFile-Test' of https://github.com/Danyy427/r…
Danyy427 Mar 31, 2022
eeac178
Removed weird character
Danyy427 Mar 31, 2022
6fbf14d
Added Sanitization
Danyy427 Apr 1, 2022
a36d344
Fixed Typo
Danyy427 Apr 1, 2022
f55c3a0
Fixed wrong if statement
Danyy427 Apr 2, 2022
db5102b
Fixed wrong filename in test
Danyy427 Apr 2, 2022
dc7a5fa
Added sanitization as a method
Danyy427 Apr 2, 2022
af95adb
Merge branch 'Danyy427-ZipFile-Test' of https://github.com/Danyy427/r…
Danyy427 Apr 2, 2022
46b335b
Fixed tests that didn't pass
Danyy427 Apr 8, 2022
6867258
Cleaned up code
Danyy427 Apr 8, 2022
389289a
Removed unnecessary tests
Danyy427 Apr 8, 2022
482bddc
Change agnostic TFM back to Windows
danmoseley Apr 10, 2022
d9b7f9a
spacing
danmoseley Apr 10, 2022
f8d75fc
Changed default target framework to Unix
Danyy427 Apr 11, 2022
034466f
Defaulted framework to unix
Danyy427 Apr 11, 2022
3046526
Removed Browser
Danyy427 Apr 11, 2022
90b2e00
Update src/libraries/System.IO.Compression.ZipFile/tests/ZipFile.Crea…
danmoseley Apr 11, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TargetFrameworks>$(NetCoreAppCurrent)-Unix;$(NetCoreAppCurrent)-Browser;$(NetCoreAppCurrent)</TargetFrameworks>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-Browser;$(NetCoreAppCurrent)-Unix</TargetFrameworks>
danmoseley marked this conversation as resolved.
Show resolved Hide resolved
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
Expand All @@ -16,6 +16,10 @@
<Compile Include="$(CommonPath)System\IO\PathInternal.CaseSensitivity.cs"
Link="Common\System\IO\PathInternal.CaseSensitivity.cs" />
</ItemGroup>
<!-- Windows specific files -->
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'windows'">
<Compile Include="System\IO\Compression\ZipFileValidName_Windows.cs" />
</ItemGroup>
<!-- Unix specific files -->
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'Unix' or '$(TargetPlatformIdentifier)' == 'Browser'">
<Compile Include="System\IO\Compression\ZipFileExtensions.ZipArchive.Create.Unix.cs" />
Expand All @@ -30,6 +34,7 @@
Link="Common\Interop\Unix\System.Native\Interop.FChMod.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Stat.cs"
Link="Common\Interop\Unix\System.Native\Interop.Stat.cs" />
<Compile Include="System\IO\Compression\ZipFileValidFullName_Unix.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.IO.Compression" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ internal static void ExtractRelativeToDirectory(this ZipArchiveEntry source!!, s
if (!destinationDirectoryFullPath.EndsWith(Path.DirectorySeparatorChar))
destinationDirectoryFullPath += Path.DirectorySeparatorChar;

string fileDestinationPath = Path.GetFullPath(Path.Combine(destinationDirectoryFullPath, source.FullName));
string fileDestinationPath = Path.GetFullPath(Path.Combine(destinationDirectoryFullPath, SanitizeZipFilePath(source.FullName)));

if (!fileDestinationPath.StartsWith(destinationDirectoryFullPath, PathInternal.StringComparison))
throw new IOException(SR.IO_ExtractingResultsInOutside);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text;

namespace System.IO.Compression
{
public static partial class ZipFileExtensions
{
internal static string SanitizeZipFilePath(string zipPath)
{
return zipPath.Replace('\0', '_');
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.


using System.Text;

namespace System.IO.Compression
{
public static partial class ZipFileExtensions
{
internal static string SanitizeZipFilePath(string zipPath)
{
StringBuilder builder = new StringBuilder(zipPath);
for (int i = 0; i < zipPath.Length; i++)
{
if (((int)builder[i] >= 0 && (int)builder[i] < 32) ||
builder[i] == '?' || builder[i] == ':' ||
builder[i] == '*' || builder[i] == '"' ||
builder[i] == '<' || builder[i] == '>' ||
builder[i] == '|')
{
builder[i] = '_';
}
}
return builder.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public void InvalidDates()
FileInfo fileWithBadDate = new FileInfo(GetTestFilePath());
fileWithBadDate.Create().Dispose();
fileWithBadDate.LastWriteTimeUtc = new DateTime(1970, 1, 1, 1, 1, 1);

danmoseley marked this conversation as resolved.
Show resolved Hide resolved
string archivePath = GetTestFilePath();
using (FileStream output = File.Open(archivePath, FileMode.Create))
using (ZipArchive archive = new ZipArchive(output, ZipArchiveMode.Create))
Expand Down Expand Up @@ -344,19 +344,6 @@ public void ReadStreamOps()
}
}

/// <summary>
/// This test ensures that a zipfile with path names that are invalid to this OS will throw errors
/// when an attempt is made to extract them.
/// </summary>
[Theory]
[InlineData("NullCharFileName_FromWindows")]
[InlineData("NullCharFileName_FromUnix")]
[PlatformSpecific(TestPlatforms.AnyUnix)] // Checks Unix-specific invalid file path
public void Unix_ZipWithInvalidFileNames_ThrowsArgumentException(string zipName)
{
Assert.Throws<ArgumentException>(() => ZipFile.ExtractToDirectory(compat(zipName) + ".zip", GetTestFilePath()));
}

[Fact]
public void UpdateReadTwice()
{
Expand Down Expand Up @@ -415,24 +402,6 @@ public async Task UpdateAddFile()
}
}

/// <summary>
/// This test ensures that a zipfile with path names that are invalid to this OS will throw errors
/// when an attempt is made to extract them.
/// </summary>
[Theory]
[InlineData("WindowsInvalid_FromUnix", null)]
[InlineData("WindowsInvalid_FromWindows", null)]
[InlineData("NullCharFileName_FromWindows", "path")]
[InlineData("NullCharFileName_FromUnix", "path")]
[PlatformSpecific(TestPlatforms.Windows)] // Checks Windows-specific invalid file path
public void Windows_ZipWithInvalidFileNames_ThrowsException(string zipName, string paramName)
{
if (paramName == null)
Assert.Throws<IOException>(() => ZipFile.ExtractToDirectory(compat(zipName) + ".zip", GetTestFilePath()));
else
AssertExtensions.Throws<ArgumentException>(paramName, null, () => ZipFile.ExtractToDirectory(compat(zipName) + ".zip", GetTestFilePath()));
}

private static async Task UpdateArchive(ZipArchive archive, string installFile, string entryName)
{
string fileName = installFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,12 @@ public void ExtractOutOfRoot(string entryName)
[InlineData("NullCharFileName_FromWindows")]
[InlineData("NullCharFileName_FromUnix")]
[PlatformSpecific(TestPlatforms.AnyUnix)] // Checks Unix-specific invalid file path
public void Unix_ZipWithInvalidFileNames_ThrowsArgumentException(string zipName)
public void Unix_ZipWithInvalidFileNames(string zipName)
{
AssertExtensions.Throws<ArgumentException>("path", () => ZipFile.ExtractToDirectory(compat(zipName) + ".zip", GetTestFilePath()));
var testDirectory = GetTestFilePath();
danmoseley marked this conversation as resolved.
Show resolved Hide resolved
ZipFile.ExtractToDirectory(compat(zipName) + ".zip", testDirectory);

Assert.True(File.Exists(Path.Combine(testDirectory, "a_6b6d")));
}

[Theory]
Expand All @@ -90,30 +93,47 @@ public void Unix_ZipWithOSSpecificFileNames(string zipName, string fileName)
Assert.Equal(fileName, Path.GetFileName(results[0]));
}

/// <summary>
/// This test ensures that a zipfile with path names that are invalid to this OS will throw errors
/// when an attempt is made to extract them.
/// </summary>
[Theory]
[InlineData("NullCharFileName_FromWindows")]
[InlineData("NullCharFileName_FromUnix")]
[PlatformSpecific(TestPlatforms.Windows)] // Checks Windows-specific invalid file path
public void Windows_ZipWithInvalidFileNames_ThrowsArgumentException(string zipName)
{
AssertExtensions.Throws<ArgumentException>("path", null, () => ZipFile.ExtractToDirectory(compat(zipName) + ".zip", GetTestFilePath()));
}

/// <summary>
/// This test ensures that a zipfile with path names that are invalid to this OS will throw errors
/// when an attempt is made to extract them.
/// This test checks whether or not ZipFile.ExtractToDirectory() is capable of handling filenames
/// which contain invalid path characters in Windows.
/// Archive: InvalidWindowsFileNameChars.zip
/// Test/
/// Test/normalText.txt
/// Test"<>|^A^B^C^D^E^F^G^H^I^J^K^L^M^N^O^P^Q^R^S^T^U^V^W^X^Y^Z^[^\^]^^^_/
/// Test"<>|^A^B^C^D^E^F^G^H^I^J^K^L^M^N^O^P^Q^R^S^T^U^V^W^X^Y^Z^[^\^]^^^_/TestText1"<>|^A^B^C^D^E^F^G^H^I^J^K^L^M^N^O^P^Q^R^S^T^U^V^W^X^Y^Z^[^\^]^^^_.txt
/// TestEmpty/
/// TestText"<>|^A^B^C^D^E^F^G^H^I^J^K^L^M^N^O^P^Q^R^S^T^U^V^W^X^Y^Z^[^\^]^^^_.txt
/// </summary>
[Theory]
[InlineData("WindowsInvalid_FromUnix")]
[InlineData("WindowsInvalid_FromWindows")]
[PlatformSpecific(TestPlatforms.Windows)] // Checks Windows-specific invalid file path
public void Windows_ZipWithInvalidFileNames_ThrowsIOException(string zipName)
[Fact]
[PlatformSpecific(TestPlatforms.Windows)]
public void Windows_ZipWithInvalidFileNames()
{
AssertExtensions.Throws<IOException>(() => ZipFile.ExtractToDirectory(compat(zipName) + ".zip", GetTestFilePath()));

var testDirectory = GetTestFilePath();
ZipFile.ExtractToDirectory(compat("InvalidWindowsFileNameChars.zip"), testDirectory);
CheckExists(testDirectory, "TestText______________________________________.txt");
CheckExists(testDirectory, "Test______________________________________/TestText1______________________________________.txt");
CheckExists(testDirectory, "Test/normalText.txt");

ZipFile.ExtractToDirectory(compat("NullCharFileName_FromWindows.zip"), testDirectory);
CheckExists(testDirectory, "a_6b6d");

ZipFile.ExtractToDirectory(compat("NullCharFileName_FromUnix.zip"), testDirectory);
CheckExists(testDirectory, "a_6b6d");

ZipFile.ExtractToDirectory(compat("WindowsInvalid_FromUnix.zip"), testDirectory);
CheckExists(testDirectory, "aa_b_d");

ZipFile.ExtractToDirectory(compat("WindowsInvalid_FromWindows.zip"), testDirectory);
CheckExists(testDirectory, "aa_b_d");

void CheckExists(string testDirectory, string file)
{
string path = Path.Combine(testDirectory, file);
Assert.True(File.Exists(path));
File.Delete(path);
}
}

[Theory]
Expand Down