Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
2 changes: 1 addition & 1 deletion src/SharpCompress/Common/GZip/GZipFilePart.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private async ValueTask ReadAndValidateGzipHeaderAsync(
var lengthField = new byte[2];
_ = await _stream.ReadAsync(lengthField, 0, 2, cancellationToken).ConfigureAwait(false);

var extraLength = (short)(lengthField[0] + (lengthField[1] * 256));
var extraLength = lengthField[0] + (lengthField[1] * 256);
var extra = new byte[extraLength];

if (!await _stream.ReadFullyAsync(extra, cancellationToken).ConfigureAwait(false))
Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Common/GZip/GZipFilePart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private void ReadAndValidateGzipHeader()
// read and discard extra field
n = _stream.Read(header.Slice(0, 2)); // 2-byte length field

var extraLength = (short)(header[0] + (header[1] * 256));
var extraLength = header[0] + (header[1] * 256);
var extra = new byte[extraLength];

if (!_stream.ReadFully(extra))
Expand Down
7 changes: 7 additions & 0 deletions src/SharpCompress/Common/SevenZip/ArchiveReader.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ public async ValueTask<ArchiveDatabase> ReadDatabaseAsync(
throw new ArchiveOperationException("nextHeaderOffset is invalid");
}

// Validate the physical header range before allocating attacker-controlled sizes.
// Subtract the validated offset instead of adding offset and size to avoid overflow.
if (nextHeaderSize > _streamEnding - db._startPositionAfterHeader - nextHeaderOffset)
{
throw new IncompleteArchiveException("Next header extends beyond the end of stream.");
}

_stream.Seek(nextHeaderOffset, SeekOrigin.Current);

var header = new byte[nextHeaderSize];
Expand Down
7 changes: 7 additions & 0 deletions src/SharpCompress/Common/SevenZip/ArchiveReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,13 @@ public ArchiveDatabase ReadDatabase(IPasswordProvider pass)
throw new ArchiveOperationException("nextHeaderOffset is invalid");
}

// Validate the physical header range before allocating attacker-controlled sizes.
// Subtract the validated offset instead of adding offset and size to avoid overflow.
if (nextHeaderSize > _streamEnding - db._startPositionAfterHeader - nextHeaderOffset)
{
throw new IncompleteArchiveException("Next header extends beyond the end of stream.");
}

_stream.Seek(nextHeaderOffset, SeekOrigin.Current);

var header = new byte[nextHeaderSize];
Expand Down
96 changes: 93 additions & 3 deletions tests/SharpCompress.Test/GZip/GZipExtraFieldTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AwesomeAssertions;
using SharpCompress.Archives.GZip;
using SharpCompress.Readers.GZip;
using SharpCompress.Test.Mocks;
using Xunit;
using SharpCompressionMode = SharpCompress.Compressors.CompressionMode;
Expand Down Expand Up @@ -40,7 +44,7 @@
)
{
using var rawInput = new MemoryStream(CreateGZipWithExtraField(extraLength));
await using var input = new AsyncOnlyStream(rawInput, disposeStream: false);

Check failure on line 47 in tests/SharpCompress.Test/GZip/GZipExtraFieldTests.cs

View workflow job for this annotation

GitHub Actions / build-and-publish (windows-latest)

'AsyncOnlyStream': type used in an asynchronous using statement must implement 'System.IAsyncDisposable' or implement a suitable 'DisposeAsync' method. Did you mean 'using' rather than 'await using'?

Check failure on line 47 in tests/SharpCompress.Test/GZip/GZipExtraFieldTests.cs

View workflow job for this annotation

GitHub Actions / build-and-publish (windows-latest)

'AsyncOnlyStream': type used in an asynchronous using statement must implement 'System.IAsyncDisposable' or implement a suitable 'DisposeAsync' method. Did you mean 'using' rather than 'await using'?
await using var gzip = new SharpCompress.Compressors.Deflate.GZipStream(
input,
SharpCompressionMode.Decompress
Expand All @@ -52,7 +56,79 @@
_ = output.ToArray().Should().Equal(EXPECTED_PAYLOAD);
}

private static byte[] CreateGZipWithExtraField(int extraLength)
[Fact]
public void GZipReader_Read_WithLargeExtraFieldAndName_ShouldReturnEntry()
{
using var input = new MemoryStream(CreateGZipWithExtraField(65535, "payload.bin"));
using var reader = GZipReader.OpenReader(input);

Assert.True(reader.MoveToNextEntry());
Assert.Equal("payload.bin", reader.Entry.Key);

using var output = new MemoryStream();
reader.WriteEntryTo(output);

_ = output.ToArray().Should().Equal(EXPECTED_PAYLOAD);
}

[Fact]
public async ValueTask GZipReader_ReadAsync_WithLargeExtraFieldAndName_ShouldReturnEntry()
{
await using var input = new AsyncOnlyStream(

Check failure on line 77 in tests/SharpCompress.Test/GZip/GZipExtraFieldTests.cs

View workflow job for this annotation

GitHub Actions / build-and-publish (windows-latest)

'AsyncOnlyStream': type used in an asynchronous using statement must implement 'System.IAsyncDisposable' or implement a suitable 'DisposeAsync' method. Did you mean 'using' rather than 'await using'?

Check failure on line 77 in tests/SharpCompress.Test/GZip/GZipExtraFieldTests.cs

View workflow job for this annotation

GitHub Actions / build-and-publish (windows-latest)

'AsyncOnlyStream': type used in an asynchronous using statement must implement 'System.IAsyncDisposable' or implement a suitable 'DisposeAsync' method. Did you mean 'using' rather than 'await using'?
new MemoryStream(CreateGZipWithExtraField(65535, "payload.bin")),
disposeStream: false
);
await using var reader = await GZipReader.OpenAsyncReader(input);

Assert.True(await reader.MoveToNextEntryAsync());
Assert.Equal("payload.bin", reader.Entry.Key);

using var output = new MemoryStream();
await reader.WriteEntryToAsync(output);

_ = output.ToArray().Should().Equal(EXPECTED_PAYLOAD);
}

[Fact]
public void GZipArchive_Read_WithLargeExtraFieldAndName_ShouldReturnEntry()
{
using var input = new MemoryStream(CreateGZipWithExtraField(65535, "payload.bin"));
using var archive = GZipArchive.OpenArchive(input);
var entry = archive.Entries.Single();

Assert.Equal("payload.bin", entry.Key);
using var entryStream = entry.OpenEntryStream();
using var output = new MemoryStream();
entryStream.CopyTo(output);

_ = output.ToArray().Should().Equal(EXPECTED_PAYLOAD);
}

[Fact]
public async ValueTask GZipArchive_ReadAsync_WithLargeExtraFieldAndName_ShouldReturnEntry()
{
await using var input = new AsyncOnlyStream(

Check failure on line 110 in tests/SharpCompress.Test/GZip/GZipExtraFieldTests.cs

View workflow job for this annotation

GitHub Actions / build-and-publish (windows-latest)

'AsyncOnlyStream': type used in an asynchronous using statement must implement 'System.IAsyncDisposable' or implement a suitable 'DisposeAsync' method. Did you mean 'using' rather than 'await using'?

Check failure on line 110 in tests/SharpCompress.Test/GZip/GZipExtraFieldTests.cs

View workflow job for this annotation

GitHub Actions / build-and-publish (windows-latest)

'AsyncOnlyStream': type used in an asynchronous using statement must implement 'System.IAsyncDisposable' or implement a suitable 'DisposeAsync' method. Did you mean 'using' rather than 'await using'?
new MemoryStream(CreateGZipWithExtraField(65535, "payload.bin")),
disposeStream: false
);
await using var archive = await GZipArchive.OpenAsyncArchive(input);
var entry = await archive.EntriesAsync.FirstAsync();

Assert.Equal("payload.bin", entry.Key);
#if NETFRAMEWORK
using (var entryStream = await entry.OpenEntryStreamAsync())
#else
await using (var entryStream = await entry.OpenEntryStreamAsync())
#endif
{
using var output = new MemoryStream();
await entryStream.CopyToAsync(output);

_ = output.ToArray().Should().Equal(EXPECTED_PAYLOAD);
}
}

private static byte[] CreateGZipWithExtraField(int extraLength, string? fileName = null)
{
using var compressed = new MemoryStream();
using (
Expand All @@ -67,12 +143,26 @@
}

var original = compressed.ToArray();
var result = new byte[original.Length + extraLength + 2];
var fileNameBytes = fileName is null
? []
: Encoding.GetEncoding("iso-8859-1").GetBytes(fileName + "\0");
var result = new byte[original.Length + extraLength + 2 + fileNameBytes.Length];
Buffer.BlockCopy(original, 0, result, 0, 10);
result[3] |= 0x04;
if (fileNameBytes.Length > 0)
{
result[3] |= 0x08;
}
result[10] = (byte)extraLength;
result[11] = (byte)(extraLength >> 8);
Buffer.BlockCopy(original, 10, result, 12 + extraLength, original.Length - 10);
Buffer.BlockCopy(fileNameBytes, 0, result, 12 + extraLength, fileNameBytes.Length);
Buffer.BlockCopy(
original,
10,
result,
12 + extraLength + fileNameBytes.Length,
original.Length - 10
);
return result;
}
}
128 changes: 128 additions & 0 deletions tests/SharpCompress.Test/SevenZip/SevenZipHeaderValidationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using SharpCompress.Archives;
using SharpCompress.Common;
using SharpCompress.Common.SevenZip;
using SharpCompress.Crypto;
using SharpCompress.Test.Mocks;
using Xunit;

namespace SharpCompress.Test.SevenZip;

public class SevenZipHeaderValidationTests
{
[Theory]
[InlineData(0, 64 * 1024 * 1024, 0)]
[InlineData(0, 256 * 1024 * 1024, 0)]
[InlineData(0, 2, 1)]
[InlineData(1, 2, 2)]
[InlineData(2, 1, 2)]
public void SevenZipArchive_TruncatedNextHeader_ThrowsBeforeReadingHeader(
int offset,
int size,
int available
)
{
using var stream = CreateArchive(offset, size, available);
using var archive = ArchiveFactory.OpenArchive(stream);

var exception = Assert.Throws<IncompleteArchiveException>(() => archive.Entries.ToList());

Assert.Equal("Next header extends beyond the end of stream.", exception.Message);
Assert.Equal(32L, stream.Position);
Assert.True(stream.CanRead);
}

[Theory]
[InlineData(0, 64 * 1024 * 1024, 0)]
[InlineData(0, 256 * 1024 * 1024, 0)]
[InlineData(0, 2, 1)]
[InlineData(1, 2, 2)]
[InlineData(2, 1, 2)]
public async Task SevenZipArchive_TruncatedNextHeader_AsyncThrowsBeforeReadingHeader(
int offset,
int size,
int available
)
{
using var stream = CreateArchive(offset, size, available);
using var asyncStream = new AsyncOnlyStream(stream, disposeStream: false);
await using var archive = await ArchiveFactory.OpenAsyncArchive(
asyncStream,
cancellationToken: CancellationToken.None
);

var exception = await Assert.ThrowsAsync<IncompleteArchiveException>(async () =>
{
await foreach (
var entry in archive.EntriesAsync.WithCancellation(CancellationToken.None)
) { }
});

Assert.Equal("Next header extends beyond the end of stream.", exception.Message);
Assert.Equal(32L, stream.Position);
Assert.True(stream.CanRead);
}

[Theory]
[InlineData(0, 0, 0)]
[InlineData(0, 2, 2)]
[InlineData(3, 2, 5)]
public void SevenZipArchive_NextHeaderAtEnd_IsValid(int offset, int size, int available)
{
using var stream = CreateArchive(offset, size, available);
using var archive = ArchiveFactory.OpenArchive(stream);

Assert.Empty(archive.Entries);
}

[Theory]
[InlineData(0, 0, 0)]
[InlineData(0, 2, 2)]
[InlineData(3, 2, 5)]
public async Task SevenZipArchive_NextHeaderAtEnd_AsyncIsValid(
int offset,
int size,
int available
)
{
using var stream = CreateArchive(offset, size, available);
using var asyncStream = new AsyncOnlyStream(stream, disposeStream: false);
await using var archive = await ArchiveFactory.OpenAsyncArchive(
asyncStream,
cancellationToken: CancellationToken.None
);

var count = 0;
await foreach (var entry in archive.EntriesAsync.WithCancellation(CancellationToken.None))
{
count++;
}

Assert.Equal(0, count);
}

private static MemoryStream CreateArchive(int offset, int size, int available)
{
// A minimal uncompressed next header: kHeader followed by kEnd.
byte[] header = [1, 0];
var crc = Crc32Stream.Compute(
Crc32Stream.DEFAULT_POLYNOMIAL,
Crc32Stream.DEFAULT_SEED,
header
);
var stream = new MemoryStream();
SevenZipSignatureHeaderWriter.WriteFinal(stream, (ulong)offset, (ulong)size, crc);
stream.SetLength(32 + available);
if (available >= offset + header.Length)
{
stream.Position = 32 + offset;
stream.Write(header, 0, header.Length);
}

stream.Position = 0;
return stream;
}
}
Loading