Skip to content

Commit dafdda9

Browse files
authored
Merge PR #420: Throw NotSupportedException in ZipFile.Add when trying to add AES entry
1 parent ba7c716 commit dafdda9

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,6 +1850,9 @@ public void Add(ZipEntry entry)
18501850
/// <param name="dataSource">The source of the data for this entry.</param>
18511851
/// <param name="entry">The entry to add.</param>
18521852
/// <remarks>This can be used to add file entries with a custom data source.</remarks>
1853+
/// <exception cref="NotSupportedException">
1854+
/// The encryption method specified in <paramref name="entry"/> is unsupported.
1855+
/// </exception>
18531856
public void Add(IStaticDataSource dataSource, ZipEntry entry)
18541857
{
18551858
if (entry == null)
@@ -1862,6 +1865,13 @@ public void Add(IStaticDataSource dataSource, ZipEntry entry)
18621865
throw new ArgumentNullException(nameof(dataSource));
18631866
}
18641867

1868+
// We don't currently support adding entries with AES encryption, so throw
1869+
// up front instead of failing or falling back to ZipCrypto later on
1870+
if (entry.AESKeySize > 0)
1871+
{
1872+
throw new NotSupportedException("Creation of AES encrypted entries is not supported");
1873+
}
1874+
18651875
CheckUpdating();
18661876

18671877
AddUpdate(new ZipUpdate(dataSource, entry));

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,5 +1547,27 @@ public void HostSystemPersistedFromZipFile()
15471547
}
15481548
}
15491549
}
1550+
1551+
/// <summary>
1552+
/// Refs https://github.com/icsharpcode/SharpZipLib/issues/385
1553+
/// Trying to add an AES Encrypted entry to ZipFile should throw as it isn't supported
1554+
/// </summary>
1555+
[Test]
1556+
[Category("Zip")]
1557+
public void AddingAnAESEncryptedEntryShouldThrow()
1558+
{
1559+
var memStream = new MemoryStream();
1560+
using (ZipFile zof = new ZipFile(memStream))
1561+
{
1562+
var entry = new ZipEntry("test")
1563+
{
1564+
AESKeySize = 256
1565+
};
1566+
1567+
zof.BeginUpdate();
1568+
var exception = Assert.Throws<NotSupportedException>(() => zof.Add(new StringMemoryDataSource("foo"), entry));
1569+
Assert.That(exception.Message, Is.EqualTo("Creation of AES encrypted entries is not supported"));
1570+
}
1571+
}
15501572
}
15511573
}

0 commit comments

Comments
 (0)