File tree 2 files changed +52
-2
lines changed
src/ICSharpCode.SharpZipLib/Zip
test/ICSharpCode.SharpZipLib.Tests/Zip
2 files changed +52
-2
lines changed Original file line number Diff line number Diff line change @@ -131,10 +131,26 @@ public bool CanDecompressEntry
131
131
{
132
132
get
133
133
{
134
- return ( entry != null ) && entry . CanDecompress ;
134
+ return ( entry != null ) && IsEntryCompressionMethodSupported ( entry ) && entry . CanDecompress ;
135
135
}
136
136
}
137
137
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
+
138
154
/// <summary>
139
155
/// Advances to the next entry in the archive
140
156
/// </summary>
@@ -271,7 +287,7 @@ public ZipEntry GetNextEntry()
271
287
}
272
288
273
289
// Determine how to handle reading of data if this is attempted.
274
- if ( entry . IsCompressionMethodSupported ( ) )
290
+ if ( IsEntryCompressionMethodSupported ( entry ) )
275
291
{
276
292
internalReader = new ReadDataHandler ( InitialRead ) ;
277
293
}
Original file line number Diff line number Diff line change @@ -437,6 +437,40 @@ public void ZipFileAESReadWithEmptyPassword()
437
437
}
438
438
}
439
439
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
+
440
474
private static readonly string [ ] possible7zPaths = new [ ] {
441
475
// Check in PATH
442
476
"7z" , "7za" ,
You can’t perform that action at this time.
0 commit comments