Skip to content

Commit 83c80d2

Browse files
Numpsypiksel
andauthored
PR #333: Handle unsupported compression methods in ZipInputStream better
* Change ZipInputStream to use its own IsEntryCompressionMethodSupported function rather than the one in ZipEntry. * Unit test for ZipInputStream.CanDecompressEntry being false for AES encrypted entries. * Fix comment typo Co-authored-by: nils måsén <[email protected]>
1 parent c557266 commit 83c80d2

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/ICSharpCode.SharpZipLib/Zip/ZipInputStream.cs

+18-2
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,26 @@ public bool CanDecompressEntry
131131
{
132132
get
133133
{
134-
return (entry != null) && entry.CanDecompress;
134+
return (entry != null) && IsEntryCompressionMethodSupported(entry) && entry.CanDecompress;
135135
}
136136
}
137137

138+
/// <summary>
139+
/// Is the compression method for the specified entry supported?
140+
/// </summary>
141+
/// <remarks>
142+
/// Uses entry.CompressionMethodForHeader so that entries of type WinZipAES will be rejected.
143+
/// </remarks>
144+
/// <param name="entry">the entry to check.</param>
145+
/// <returns>true if the compression methiod is supported, false if not.</returns>
146+
private static bool IsEntryCompressionMethodSupported(ZipEntry entry)
147+
{
148+
var entryCompressionMethod = entry.CompressionMethodForHeader;
149+
150+
return entryCompressionMethod == CompressionMethod.Deflated ||
151+
entryCompressionMethod == CompressionMethod.Stored;
152+
}
153+
138154
/// <summary>
139155
/// Advances to the next entry in the archive
140156
/// </summary>
@@ -271,7 +287,7 @@ public ZipEntry GetNextEntry()
271287
}
272288

273289
// Determine how to handle reading of data if this is attempted.
274-
if (entry.IsCompressionMethodSupported())
290+
if (IsEntryCompressionMethodSupported(entry))
275291
{
276292
internalReader = new ReadDataHandler(InitialRead);
277293
}

test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEncryptionHandling.cs

+34
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,40 @@ public void ZipFileAESReadWithEmptyPassword()
437437
}
438438
}
439439

440+
/// <summary>
441+
/// ZipInputStream can't decrypt AES encrypted entries, but it should report that to the caller
442+
/// rather than just failing.
443+
/// </summary>
444+
[Test]
445+
[Category("Zip")]
446+
public void ZipinputStreamShouldGracefullyFailWithAESStreams()
447+
{
448+
string password = "password";
449+
450+
using (var memoryStream = new MemoryStream())
451+
{
452+
// Try to create a zip stream
453+
WriteEncryptedZipToStream(memoryStream, password, 256);
454+
455+
// reset
456+
memoryStream.Seek(0, SeekOrigin.Begin);
457+
458+
// Try to read
459+
using (var inputStream = new ZipInputStream(memoryStream))
460+
{
461+
inputStream.Password = password;
462+
var entry = inputStream.GetNextEntry();
463+
Assert.That(entry.AESKeySize, Is.EqualTo(256), "Test entry should be AES256 encrypted.");
464+
465+
// CanDecompressEntry should be false.
466+
Assert.That(inputStream.CanDecompressEntry, Is.False, "CanDecompressEntry should be false for AES encrypted entries");
467+
468+
// Should throw on read.
469+
Assert.Throws<ZipException>(() => inputStream.ReadByte());
470+
}
471+
}
472+
}
473+
440474
private static readonly string[] possible7zPaths = new[] {
441475
// Check in PATH
442476
"7z", "7za",

0 commit comments

Comments
 (0)