Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tiff: Add maximum IFD threshold #1892

Merged
merged 2 commits into from
Dec 13, 2021
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
12 changes: 10 additions & 2 deletions src/ImageSharp/Formats/Tiff/Ifd/DirectoryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ internal class DirectoryReader

private uint nextIfdOffset;

private const int DirectoryMax = 65534;

// used for sequential read big values (actual for multiframe big files)
// todo: different tags can link to the same data (stream offset) - investigate
private readonly SortedList<uint, Action> lazyLoaders = new SortedList<uint, Action>(new DuplicateKeyComparer<uint>());
private readonly SortedList<uint, Action> lazyLoaders = new(new DuplicateKeyComparer<uint>());

public DirectoryReader(Stream stream) => this.stream = stream;

Expand Down Expand Up @@ -48,7 +50,8 @@ private static ByteOrder ReadByteOrder(Stream stream)
{
return ByteOrder.LittleEndian;
}
else if (headerBytes[0] == TiffConstants.ByteOrderBigEndian && headerBytes[1] == TiffConstants.ByteOrderBigEndian)

if (headerBytes[0] == TiffConstants.ByteOrderBigEndian && headerBytes[1] == TiffConstants.ByteOrderBigEndian)
{
return ByteOrder.BigEndian;
}
Expand All @@ -67,6 +70,11 @@ private IEnumerable<ExifProfile> ReadIfds()
this.nextIfdOffset = reader.NextIfdOffset;

readers.Add(reader);

if (readers.Count >= DirectoryMax)
{
TiffThrowHelper.ThrowImageFormatException("TIFF image contains too many directories");
}
}

// Sequential reading big values.
Expand Down
2 changes: 1 addition & 1 deletion src/ImageSharp/Formats/Tiff/Ifd/EntryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public EntryReader(Stream stream, ByteOrder byteOrder, uint ifdOffset, SortedLis
this.lazyLoaders = lazyLoaders;
}

public List<IExifValue> Values { get; } = new List<IExifValue>();
public List<IExifValue> Values { get; } = new();

public uint NextIfdOffset { get; private set; }

Expand Down
12 changes: 12 additions & 0 deletions tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,18 @@ public void TiffDecoder_CanDecode_PackBitsCompressed<TPixel>(TestImageProvider<T
public void TiffDecoder_CanDecode_JpegCompressed<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel> => TestTiffDecoder(provider, useExactComparer: false);

// https://github.com/SixLabors/ImageSharp/issues/1891
[Theory]
[WithFile(Issues1891, PixelTypes.Rgba32)]
public void TiffDecoder_ThrowsException_WithTooManyDirectories<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel> => Assert.Throws<ImageFormatException>(
() =>
{
using (provider.GetImage(TiffDecoder))
{
}
});

[Theory]
[WithFileCollection(nameof(MultiframeTestImages), PixelTypes.Rgba32)]
public void DecodeMultiframe<TPixel>(TestImageProvider<TPixel> provider)
Expand Down
1 change: 1 addition & 0 deletions tests/ImageSharp.Tests/TestImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,7 @@ public static class Tiff
public const string Flower32BitGrayPredictorLittleEndian = "Tiff/flower-minisblack-32_lsb_deflate_predictor.tiff";

public const string Issues1716Rgb161616BitLittleEndian = "Tiff/Issues/Issue1716.tiff";
public const string Issues1891 = "Tiff/Issues/Issue1891.tiff";

public const string SmallRgbDeflate = "Tiff/rgb_small_deflate.tiff";
public const string SmallRgbLzw = "Tiff/rgb_small_lzw.tiff";
Expand Down
3 changes: 3 additions & 0 deletions tests/Images/Input/Tiff/Issues/Issue1891.tiff
Git LFS file not shown