Browse Source

Ignore invalid EXIF or XMP chunks

pull/2077/head
Brian Popow 5 years ago
parent
commit
d22e50c0d0
  1. 12
      src/ImageSharp/Formats/Webp/WebpDecoderCore.cs
  2. 19
      tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs

12
src/ImageSharp/Formats/Webp/WebpDecoderCore.cs

@ -465,22 +465,18 @@ namespace SixLabors.ImageSharp.Formats.Webp
/// <param name="features">The webp image features.</param> /// <param name="features">The webp image features.</param>
private void ParseOptionalExtendedChunks(WebpChunkType chunkType, WebpFeatures features) private void ParseOptionalExtendedChunks(WebpChunkType chunkType, WebpFeatures features)
{ {
int bytesRead;
switch (chunkType) switch (chunkType)
{ {
case WebpChunkType.Iccp: case WebpChunkType.Iccp:
this.ReadIccProfile(); this.ReadIccProfile();
break; break;
case WebpChunkType.Exif: case WebpChunkType.Exif:
this.ReadExifProfile(); this.ReadExifProfile();
break; break;
case WebpChunkType.Xmp: case WebpChunkType.Xmp:
this.ReadXmpProfile(); this.ReadXmpProfile();
break; break;
case WebpChunkType.Animation: case WebpChunkType.Animation:
@ -492,7 +488,7 @@ namespace SixLabors.ImageSharp.Formats.Webp
features.AlphaChunkHeader = (byte)this.currentStream.ReadByte(); features.AlphaChunkHeader = (byte)this.currentStream.ReadByte();
int alphaDataSize = (int)(alphaChunkSize - 1); int alphaDataSize = (int)(alphaChunkSize - 1);
features.AlphaData = this.memoryAllocator.Allocate<byte>(alphaDataSize); features.AlphaData = this.memoryAllocator.Allocate<byte>(alphaDataSize);
bytesRead = this.currentStream.Read(features.AlphaData.Memory.Span, 0, alphaDataSize); int bytesRead = this.currentStream.Read(features.AlphaData.Memory.Span, 0, alphaDataSize);
if (bytesRead != alphaDataSize) if (bytesRead != alphaDataSize)
{ {
WebpThrowHelper.ThrowInvalidImageContentException("Not enough data to read the alpha chunk"); WebpThrowHelper.ThrowInvalidImageContentException("Not enough data to read the alpha chunk");
@ -556,7 +552,8 @@ namespace SixLabors.ImageSharp.Formats.Webp
int bytesRead = this.currentStream.Read(exifData, 0, (int)exifChunkSize); int bytesRead = this.currentStream.Read(exifData, 0, (int)exifChunkSize);
if (bytesRead != exifChunkSize) if (bytesRead != exifChunkSize)
{ {
WebpThrowHelper.ThrowInvalidImageContentException("Not enough data to read the exif chunk"); // Ignore invalid chunk.
return;
} }
var profile = new ExifProfile(exifData); var profile = new ExifProfile(exifData);
@ -580,7 +577,8 @@ namespace SixLabors.ImageSharp.Formats.Webp
int bytesRead = this.currentStream.Read(xmpData, 0, (int)xmpChunkSize); int bytesRead = this.currentStream.Read(xmpData, 0, (int)xmpChunkSize);
if (bytesRead != xmpChunkSize) if (bytesRead != xmpChunkSize)
{ {
WebpThrowHelper.ThrowInvalidImageContentException("Not enough data to read the xmp chunk"); // Ignore invalid chunk.
return;
} }
var profile = new XmpProfile(xmpData); var profile = new XmpProfile(xmpData);

19
tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs

@ -1,6 +1,7 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System;
using System.IO; using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
using SixLabors.ImageSharp.Formats.Webp; using SixLabors.ImageSharp.Formats.Webp;
@ -153,14 +154,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp
[Theory] [Theory]
[WithFile(TestImages.Webp.Lossy.WithExifNotEnoughData, PixelTypes.Rgba32)] [WithFile(TestImages.Webp.Lossy.WithExifNotEnoughData, PixelTypes.Rgba32)]
public void WebpDecoder_ThrowInvalidImageContentException_OnWithInvalidExifData<TPixel>(TestImageProvider<TPixel> provider) public void WebpDecoder_IgnoresInvalidExifChunk<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel> => where TPixel : unmanaged, IPixel<TPixel>
Assert.Throws<InvalidImageContentException>( {
() => Exception ex = Record.Exception(() =>
{ {
using (provider.GetImage(WebpDecoder)) using Image<TPixel> image = provider.GetImage();
{ });
} Assert.Null(ex);
}); }
} }
} }

Loading…
Cancel
Save