Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 12 additions & 1 deletion src/GameUtils/Types/ImageData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,18 @@ public static ImageData Read(Stream source)

var width = reader.ReadInt32();
var height = reader.ReadInt32();
var data = new Vector4[width * height];
if (width <= 0 || height <= 0)
{
throw new InvalidDataException("Width and height must be positive");
}

long totalPixels = (long)width * height;
if (totalPixels > 67_108_864) // 1 GB max memory allocation (16 bytes per Vector4)
{
throw new InvalidDataException("Image size exceeds maximum allowed limit of 1GB");
}

var data = new Vector4[(int)totalPixels];
for (var i = 0; i < data.Length; i++)
{
data[i] = new Vector4(
Expand Down
34 changes: 34 additions & 0 deletions tests/GameUtils.Tests/Types/ImageDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,40 @@ namespace GameUtils.Tests.Types
[TestClass]
public class ImageDataTests
{
[TestMethod]
public void Read_WithNegativeDimensions_ThrowsInvalidDataException()
{
using var ms = new MemoryStream();
using (var compressor = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionLevel.Optimal, true))
using (var writer = new BinaryWriter(compressor))
{
writer.Write("IMGD"u8);
writer.Write(-1);
writer.Write(10);
}

ms.Position = 0;

Assert.ThrowsExactly<InvalidDataException>(() => ImageData.Read(ms));
}

[TestMethod]
public void Read_WithExcessiveDimensions_ThrowsInvalidDataException()
{
using var ms = new MemoryStream();
using (var compressor = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionLevel.Optimal, true))
using (var writer = new BinaryWriter(compressor))
{
writer.Write("IMGD"u8);
writer.Write(20000);
writer.Write(20000); // 400M pixels > 67.1M limit
}

ms.Position = 0;

Assert.ThrowsExactly<InvalidDataException>(() => ImageData.Read(ms));
}

[TestMethod]
public void Write_WithTraversalPath_ThrowsUnauthorizedAccessException()
{
Expand Down
Loading