mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-11-18 18:32:34 +00:00
Fix release review findings in RAR, GZip, and split ZIP parsing
This commit is contained in:
@@ -37,6 +37,26 @@ internal static class ZipArchiveVolumeFactory
|
||||
return item;
|
||||
}
|
||||
|
||||
// Generated suffixes are lowercase, but split archives can use any on-disk casing.
|
||||
// Prefer the exact path above and never choose arbitrarily between fallback matches.
|
||||
if (item is not null && item.Directory!.Exists)
|
||||
{
|
||||
FileInfo? match = null;
|
||||
foreach (var file in item.Directory!.EnumerateFiles())
|
||||
{
|
||||
if (!string.Equals(file.Name, item.Name, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (match is not null)
|
||||
{
|
||||
throw new InvalidFormatException($"Ambiguous ZIP volume name: {item.Name}");
|
||||
}
|
||||
match = file;
|
||||
}
|
||||
return match;
|
||||
}
|
||||
|
||||
return null; //no more items
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +64,11 @@ internal sealed partial class GZipFilePart
|
||||
return;
|
||||
}
|
||||
|
||||
if (n != 10)
|
||||
if (
|
||||
!await _stream
|
||||
.ReadFullyAsync(header, n, header.Length - n, cancellationToken)
|
||||
.ConfigureAwait(false)
|
||||
)
|
||||
{
|
||||
throw new ZlibException("Not a valid GZIP stream.");
|
||||
}
|
||||
@@ -80,7 +84,10 @@ internal sealed partial class GZipFilePart
|
||||
{
|
||||
// read and discard extra field
|
||||
var lengthField = new byte[2];
|
||||
_ = await _stream.ReadAsync(lengthField, 0, 2, cancellationToken).ConfigureAwait(false);
|
||||
if (!await _stream.ReadFullyAsync(lengthField, cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
throw new ZlibException("Unexpected end-of-file reading GZIP header.");
|
||||
}
|
||||
|
||||
var extraLength = lengthField[0] + (lengthField[1] * 256);
|
||||
var extra = new byte[extraLength];
|
||||
|
||||
@@ -94,7 +94,7 @@ internal sealed partial class GZipFilePart : FilePart
|
||||
return;
|
||||
}
|
||||
|
||||
if (n != 10)
|
||||
if (!_stream.ReadFully(header.Slice(n)))
|
||||
{
|
||||
throw new ZlibException("Not a valid GZIP stream.");
|
||||
}
|
||||
@@ -109,7 +109,10 @@ internal sealed partial class GZipFilePart : FilePart
|
||||
if ((header[3] & 0x04) == 0x04)
|
||||
{
|
||||
// read and discard extra field
|
||||
n = _stream.Read(header.Slice(0, 2)); // 2-byte length field
|
||||
if (!_stream.ReadFully(header.Slice(0, 2)))
|
||||
{
|
||||
throw new ZlibException("Unexpected end-of-file reading GZIP header.");
|
||||
}
|
||||
|
||||
var extraLength = header[0] + (header[1] * 256);
|
||||
var extra = new byte[extraLength];
|
||||
|
||||
@@ -53,7 +53,7 @@ internal static class EncryptionFlagsV5
|
||||
internal static class EncryptionConstV5
|
||||
{
|
||||
public const int VERSION = 0;
|
||||
public const uint CRYPT5_KDF_LG2_COUNT_MAX = 0x24;
|
||||
public const uint CRYPT5_KDF_LG2_COUNT_MAX = 24;
|
||||
public const int SIZE_SALT30 = 0x08;
|
||||
public const int SIZE_SALT50 = 0x10;
|
||||
public const int SIZE_INITV = 0x10;
|
||||
|
||||
@@ -24,7 +24,7 @@ internal class Rar5CryptoInfo
|
||||
encryptionFlags,
|
||||
EncryptionFlagsV5.CHFL_CRYPT_PSWCHECK
|
||||
);
|
||||
cryptoInfo.LG2Count = reader.ReadRarVIntByte(1);
|
||||
cryptoInfo.LG2Count = reader.ReadByte();
|
||||
|
||||
if (cryptoInfo.LG2Count > EncryptionConstV5.CRYPT5_KDF_LG2_COUNT_MAX)
|
||||
{
|
||||
@@ -78,10 +78,9 @@ internal class Rar5CryptoInfo
|
||||
encryptionFlags,
|
||||
EncryptionFlagsV5.CHFL_CRYPT_PSWCHECK
|
||||
);
|
||||
cryptoInfo.LG2Count = (int)
|
||||
await reader
|
||||
.ReadRarVIntUInt32Async(cancellationToken: CancellationToken.None)
|
||||
.ConfigureAwait(false);
|
||||
cryptoInfo.LG2Count = await reader
|
||||
.ReadByteAsync(CancellationToken.None)
|
||||
.ConfigureAwait(false);
|
||||
if (cryptoInfo.LG2Count > EncryptionConstV5.CRYPT5_KDF_LG2_COUNT_MAX)
|
||||
{
|
||||
throw new CryptographicException($"Unsupported LG2 count of {cryptoInfo.LG2Count}.");
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using SharpCompress.Common.Rar.Headers;
|
||||
|
||||
namespace SharpCompress.Common.Rar;
|
||||
|
||||
@@ -12,6 +13,12 @@ internal sealed class Rar5KeyCache
|
||||
|
||||
internal List<byte[]> GetKeys(string password, byte[] salt, int lg2Count)
|
||||
{
|
||||
// Metadata can be mutated after parsing; validate before C# masks the shift count.
|
||||
if (lg2Count < 0 || lg2Count > EncryptionConstV5.CRYPT5_KDF_LG2_COUNT_MAX)
|
||||
{
|
||||
throw new CryptographicException($"Unsupported LG2 count of {lg2Count}.");
|
||||
}
|
||||
|
||||
lock (entries)
|
||||
{
|
||||
foreach (var (cachedPassword, cachedSalt, cachedCount, cachedKeys) in entries)
|
||||
|
||||
@@ -40,8 +40,11 @@ internal sealed class RarCryptoWrapper : Stream
|
||||
Span<byte> cipherText = stackalloc byte[16];
|
||||
for (var i = 0; i < alignedSize / 16; i++)
|
||||
{
|
||||
//long ax = System.currentTimeMillis();
|
||||
_actualStream.Read(cipherText);
|
||||
if (!_actualStream.ReadFully(cipherText))
|
||||
{
|
||||
// Preserve the synchronous API's malformed-archive exception type.
|
||||
throw new InvalidFormatException("Unexpected end of encrypted stream");
|
||||
}
|
||||
|
||||
var readBytes = _rijndael.ProcessBlock(cipherText);
|
||||
foreach (var readByte in readBytes)
|
||||
@@ -49,11 +52,11 @@ internal sealed class RarCryptoWrapper : Stream
|
||||
_data.Enqueue(readByte);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
buffer[offset + i] = _data.Dequeue();
|
||||
}
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
buffer[offset + i] = _data.Dequeue();
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
@@ -341,18 +341,9 @@ internal class ZlibBaseStream : Stream, IStreamStack
|
||||
// Make sure we have read to the end of the stream
|
||||
_z.InputBuffer.AsSpan(_z.NextIn, _z.AvailableBytesIn).CopyTo(trailer);
|
||||
var bytesNeeded = 8 - _z.AvailableBytesIn;
|
||||
var bytesRead = _stream.Read(
|
||||
trailer.Slice(_z.AvailableBytesIn, bytesNeeded)
|
||||
);
|
||||
if (bytesNeeded != bytesRead)
|
||||
if (!_stream.ReadFully(trailer.Slice(_z.AvailableBytesIn, bytesNeeded)))
|
||||
{
|
||||
throw new ZlibException(
|
||||
String.Format(
|
||||
Constants.DefaultCultureInfo,
|
||||
"Protocol error. AvailableBytesIn={0}, expected 8",
|
||||
_z.AvailableBytesIn + bytesRead
|
||||
)
|
||||
);
|
||||
throw new ZlibException("Unexpected end-of-file reading GZIP trailer.");
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -491,9 +482,19 @@ internal class ZlibBaseStream : Stream, IStreamStack
|
||||
// Make sure we have read to the end of the stream
|
||||
_z.InputBuffer.AsSpan(_z.NextIn, _z.AvailableBytesIn).CopyTo(trailer);
|
||||
var bytesNeeded = 8 - _z.AvailableBytesIn;
|
||||
var bytesRead = await _stream
|
||||
.ReadAsync(trailer, _z.AvailableBytesIn, bytesNeeded, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
if (
|
||||
!await _stream
|
||||
.ReadFullyAsync(
|
||||
trailer,
|
||||
_z.AvailableBytesIn,
|
||||
bytesNeeded,
|
||||
cancellationToken
|
||||
)
|
||||
.ConfigureAwait(false)
|
||||
)
|
||||
{
|
||||
throw new ZlibException("Unexpected end-of-file reading GZIP trailer.");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -736,7 +737,7 @@ internal class ZlibBaseStream : Stream, IStreamStack
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (n != 10)
|
||||
if (!_stream.ReadFully(header.Slice(n)))
|
||||
{
|
||||
throw new ZlibException("Not a valid GZIP stream.");
|
||||
}
|
||||
@@ -748,21 +749,23 @@ internal class ZlibBaseStream : Stream, IStreamStack
|
||||
|
||||
var timet = BinaryPrimitives.ReadInt32LittleEndian(header.Slice(4));
|
||||
_GzipMtime = TarHeader.EPOCH.AddSeconds(timet);
|
||||
totalBytesRead += n;
|
||||
totalBytesRead += header.Length;
|
||||
if ((header[3] & 0x04) == 0x04)
|
||||
{
|
||||
// read and discard extra field
|
||||
n = _stream.Read(header.Slice(0, 2)); // 2-byte length field
|
||||
totalBytesRead += n;
|
||||
|
||||
var extraLength = header[0] + (header[1] * 256);
|
||||
var extra = new byte[extraLength];
|
||||
n = _stream.Read(extra, 0, extra.Length);
|
||||
if (n != extraLength)
|
||||
if (!_stream.ReadFully(header.Slice(0, 2)))
|
||||
{
|
||||
throw new ZlibException("Unexpected end-of-file reading GZIP header.");
|
||||
}
|
||||
totalBytesRead += n;
|
||||
totalBytesRead += 2;
|
||||
|
||||
var extraLength = header[0] + (header[1] * 256);
|
||||
var extra = new byte[extraLength];
|
||||
if (!_stream.ReadFully(extra))
|
||||
{
|
||||
throw new ZlibException("Unexpected end-of-file reading GZIP header.");
|
||||
}
|
||||
totalBytesRead += extraLength;
|
||||
}
|
||||
if ((header[3] & 0x08) == 0x08)
|
||||
{
|
||||
@@ -796,7 +799,11 @@ internal class ZlibBaseStream : Stream, IStreamStack
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (n != 10)
|
||||
if (
|
||||
!await _stream
|
||||
.ReadFullyAsync(header, n, header.Length - n, cancellationToken)
|
||||
.ConfigureAwait(false)
|
||||
)
|
||||
{
|
||||
throw new ZlibException("Not a valid GZIP stream.");
|
||||
}
|
||||
@@ -808,23 +815,25 @@ internal class ZlibBaseStream : Stream, IStreamStack
|
||||
|
||||
var timet = BinaryPrimitives.ReadInt32LittleEndian(header.AsSpan(4));
|
||||
_GzipMtime = TarHeader.EPOCH.AddSeconds(timet);
|
||||
totalBytesRead += n;
|
||||
totalBytesRead += header.Length;
|
||||
if ((header[3] & 0x04) == 0x04)
|
||||
{
|
||||
// read and discard extra field
|
||||
n = await _stream.ReadAsync(header, 0, 2, cancellationToken).ConfigureAwait(false); // 2-byte length field
|
||||
totalBytesRead += n;
|
||||
|
||||
var extraLength = header[0] + (header[1] * 256);
|
||||
var extra = new byte[extraLength];
|
||||
n = await _stream
|
||||
.ReadAsync(extra, 0, extra.Length, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
if (n != extraLength)
|
||||
if (
|
||||
!await _stream.ReadFullyAsync(header, 0, 2, cancellationToken).ConfigureAwait(false)
|
||||
)
|
||||
{
|
||||
throw new ZlibException("Unexpected end-of-file reading GZIP header.");
|
||||
}
|
||||
totalBytesRead += n;
|
||||
totalBytesRead += 2;
|
||||
|
||||
var extraLength = header[0] + (header[1] * 256);
|
||||
var extra = new byte[extraLength];
|
||||
if (!await _stream.ReadFullyAsync(extra, cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
throw new ZlibException("Unexpected end-of-file reading GZIP header.");
|
||||
}
|
||||
totalBytesRead += extraLength;
|
||||
}
|
||||
if ((header[3] & 0x08) == 0x08)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user