Skip to content

VP8X reserved bytes use Position += 3 instead of Write, causing 3-byte truncation in alpha WebP images #3035

@jrlost

Description

@jrlost

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am running the latest version of ImageSharp
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • I have searched open and closed issues to ensure it has not already been reported

ImageSharp version

3.1.12

Other ImageSharp packages and versions

Seems to have been introduced around v3.1.0

Environment (Operating system, version and so on)

Windows and Linux

.NET Framework version

net8

Description

Some webp images with an alpha channel result in a broken image. I've included an example of an image as well as the resulting encoded result.

This was the result of using the following encoder:

new WebpEncoder {
	Quality = 70,
	TransparentColorMode = TransparentColorMode.Preserve
}

The issue appears to be here: https://github.com/SixLabors/ImageSharp/blob/main/src/ImageSharp/Formats/Webp/Chunks/WebpVp8X.cs#L126

specifically

        stream.Position += 3; // Reserved bytes

The stream position is being advanced 3 bytes but the stream is not actually being written to. This appears to result in the RIFF size and the corresponding VP8 chunk to be 3 bytes too large.

Changing this stream.Position += 3; to instead write zeros to advance the stream

Span<byte> reserved = stackalloc byte[3];
stream.Write(reserved);

seems to resolve the issue.

I am currently running a private fork for this to get us moving forward but would rather someone with more experience with webp review this finding.

Steps to Reproduce

Try to do a simple encoding of the original.webp provided that preserves the alpha channel.
Using something like

new WebpEncoder {
	Quality = 70,
	TransparentColorMode = TransparentColorMode.Preserve
}

Compare the results

original.webp (valid)

Property Value
File size 9484 bytes
Declared size 9476 + 8 = 9484 bytes
VP8X chunk 10 bytes (metadata)
ALPH chunk 3483 bytes (alpha/transparency)
VP8 chunk 5954 bytes (image data)

after-encoding.webp (corrupted)

Property Value
File size 9371 bytes
Declared size 9366 + 8 = 9374 bytes
VP8X chunk 10 bytes
ALPH chunk 3483 bytes
VP8 chunk Declared 5844 bytes, but only 5841 bytes present

Images

Image
Image

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions