Repository navigation
[web] Harden WebP and GIF header readers against truncated and malformed buffers - #196075
Conversation
…d malformed buffers
…oundaries, plain text frames and full-size color tables
There was a problem hiding this comment.
Code Review
This pull request modifies the image format detector to handle truncated and malformed WebP and GIF buffers gracefully by introducing bounds checks and error handling instead of throwing exceptions. It also exposes the header reader classes as public for testing and adds comprehensive unit tests. Review feedback suggests adding documentation comments to the newly public members of WebpHeaderReader and GifHeaderReader to comply with the style guide, and simplifying the loop in GifHeaderReader.isAnimated by removing a redundant block-skipping check.
…ebpheaderreader-and-gifheaderreader
Every GIF extension is the introducer, the label and data sub-blocks, so skip any extension that is not part of a graphic block instead of only Comment and Application Extensions. An animated GIF with a reserved or unknown extension between frames is detected as animated again, as it was before the bounds checks were added. The frame loop never counted past one frame, so replace it with skipping the first frame and checking whether a second graphic block starts.
mdebbar
left a comment
There was a problem hiding this comment.
LGTM with one final suggestion!
An animated GIF whose graphic control extension declares a block size other than 4 was misparsed and reported as static. Skip the extension the same way as other extensions so its data sub-blocks are walked by their declared sizes.
|
@harryterkelsen I am having my agent give both an adversarial review and a review using personas of different current and previous teammates. It will add comments to this pr. Feel free to ignore it as a human. It is intended to comment for your agent to consume. |
reidbaker-agent
left a comment
There was a problem hiding this comment.
The bounds checks look right to me: every read is guarded, both skip loops always advance or exit, and trailing non-graphic data after one frame no longer counts as animation. A few parts of the PR description no longer match the code after the later commits:
- It says each
isAnimated()is wrapped in anon ArgumentErrorcatch, and lists that catch under the places to focus. There is no try/catch left inimage_format_detector.dart. - It says reserved GIF labels 0xFA–0xFD make the GIF report as static.
_checkForSpecialPurposeBlocknow treats every label other than 0xF9 and 0x01 as a skippable extension, and the tests expectanimatedGiffor 0xFA/0xFD between two frames. - The "Malformed GIF" bullet lists reserved or unknown extension labels as returning
gif. As far as I can tell that only happens now when the extension is truncated.
Could you update the description to match? Inline comments below are smaller suggestions.
Drop the up-front buffer length checks, which repeat the per-read bounds checks, rename the non-graphic extension helpers to match what they match, and use backticks for literals in doc comments.
mdebbar
left a comment
There was a problem hiding this comment.
LGTM!
Can we update the PR description before landing? It still mentions the on ArgumentError catch, and it says reserved labels 0xFA–0xFD make the GIF report as static. Neither matches the code anymore.
|
autosubmit label was removed for flutter/flutter/196075, because - The status or check suite Presubmit has failed. Please fix the issues identified (or deflake) before re-applying this label. |
flutter/flutter@2f09f96...06f32f8 2026-11-18 [email protected] Move macOS desktop builds to ThinLTO (flutter/flutter#196804) 2026-11-18 [email protected] Roll Dart SDK from 5ed55b4b2ebc to 753488ede427 (2 revisions) (flutter/flutter#196893) 2026-11-18 [email protected] Roll Packages from 9af69be to dca2e58 (7 revisions) (flutter/flutter#196892) 2026-11-18 [email protected] Roll Skia from 503a380ebecc to 81ee7327e81c (29 revisions) (flutter/flutter#196889) 2026-11-18 [email protected] Roll Dart SDK from 26e38955d9d4 to 5ed55b4b2ebc (4 revisions) (flutter/flutter#196843) 2026-11-18 [email protected] Fix permissions for action updates (flutter/flutter#196791) 2026-11-18 [email protected] iOS: Apply platform view mutators from FlutterPlatformViewLayer (flutter/flutter#196753) 2026-11-18 [email protected] [flutter_tools] Remove ambient Doctor, FeatureFlags, and device getters (flutter/flutter#196572) 2026-11-18 [email protected] Fix RelativeRect.lerp crash when b is null (flutter/flutter#196655) 2026-11-18 [email protected] [web] Harden WebP and GIF header readers against truncated and malformed buffers (flutter/flutter#196075) 2026-11-17 [email protected] Enable apps to control focus behavior during app lifecycle changes via `FocusLifecyclePolicy` (flutter/flutter#190570) 2026-11-17 [email protected] Don't re-run the code freeze check on label changes (flutter/flutter#196745) 2026-11-17 [email protected] Android: Report FlutterView as a text editor while a text input client is attached (flutter/flutter#193074) 2026-11-17 [email protected] Roll Packages from 4c6a7d3 to 9af69be (5 revisions) (flutter/flutter#196812) 2026-11-17 [email protected] [flutter_tools] Handle target VM filesystem errors in DevFS.create (flutter/flutter#196234) 2026-11-17 [email protected] [flutter_tools] Clarify analytics reporting status when on a feature branch (flutter/flutter#194496) 2026-11-17 [email protected] [web] Recover CanvasKit surfaces from WebGL failures without per-frame null crashes (flutter/flutter#196257) 2026-11-17 [email protected] [devicelab] Trigger navigation tap via Flutter Driver in service_extensions_test (flutter/flutter#194456) If this roll has caused a breakage, revert this CL and set the roller to dry run mode using the controls here: https://autoroll.skia.org/r/flutter-packages Please CC [email protected],[email protected] on the revert to ensure that a human is aware of the problem. To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose To report a problem with the AutoRoller itself, please file a bug: https://issues.skia.org/issues/new?component=1389291&template=1850622 Documentation for the AutoRoller is here: https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Hardens the WebP and GIF header readers that
detectImageTypeuses to sniff animation, so that truncated or malformed image bytes can no longer crash image detection on the web.detectImageTyperuns synchronously on every encoded image before decoding. When a buffer matched the WebP (RIFF....WEBP) or GIF (GIF87a/GIF89a) signature but was cut short (a partial network download, a corrupt asset, a small synthetic buffer), the header readers indexed past the end of theByteDataand threw an unhandledRangeError. In debug/test builds, malformed GIF blocks also tripped bareasserts. Separately, the GIF reader counted a frame even when it failed to parse the graphic block. It also treated any bytes after the first frame (comments, padding, garbage) as a second frame, so some corrupt or trailing-data single-frame GIFs were reported as animated.This PR:
asserts into checks that returnfalse.bool. A frame is only counted after its graphic block parses completely through the data sub-block terminator. The reader only reports animation when that frame is followed by the start of another graphic block (Graphic Control Extension, Plain Text Extension, or Image Descriptor).isAnimated()in a finalon ArgumentErrorcatch (which also coversRangeError), so the readers never throw.WebpHeaderReader/GifHeaderReader, marked@visibleForTesting, so tests can drive them directly with syntheticByteData. They are only reachable through the internal engine library, notdart:ui.Related: #151911 (animated WebP/GIF detection)
Behavioral Changes
detectImageTypethrewRangeError (byteOffset): ....ImageType.webp.detectImageTypethrewRangeError.ImageType.gif.0x2c, data sub-block longer than the remaining bytes):AssertionErrorin debug builds, or a misparse in release.ImageType.gif.ImageType.animatedGif.ImageType.gif.ImageType.gif). Before, these hit anassertin debug builds and in release were skipped as if they were Application Extensions.test_imagescorpus) are classified as before.Where to focus
GifHeaderReader.isAnimated/_skipGraphicBlockinimage_format_detector.dart): animation now means "one fully parsed graphic block, then the start of another." The reader stops at the first bytes of frame 2 and doesn't parse it. So a GIF truncated in the middle of its second frame is still detected as animated, which is intended._maybeSkipSpecialPurposeBlocksreturnsbool): when a comment/application block is truncated or has a reserved label, the read position is mid-block. Every caller then bails out withfalse, so bytes inside the broken block (e.g. a stray0x2c) can't be misread as a frame.on ArgumentErrorcatch is a backstop: every read is already bounds-checked. It catchesArgumentErrorrather thanRangeErrorbecause dart2js surfaces the native JSRangeErrorfromDataViewas a DartArgumentError.engine/src/flutter/lib/web_ui/lib/src/engine/image_format_detector.dart, thenengine/src/flutter/lib/web_ui/test/engine/image_format_detector_test.dart.Pre-launch Checklist
///).If you need help, consider asking for advice on the #hackers-new channel on Discord.
If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance.
Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assistbot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.