feat(app): artifact viewer renders HTML documents inertly (sandboxed iframe on web, script-free web view on mobile)

This commit is contained in:
2026-10-23 15:05:44 +01:00
parent 0fe3bfb5bb
commit e77628ac46
10 changed files with 188 additions and 3 deletions
@@ -0,0 +1,20 @@
import 'package:flutter/widgets.dart';
import 'artifact_html_view_native.dart'
if (dart.library.js_interop) 'artifact_html_view_web.dart'
as impl;
/// Builds the rendered HTML preview; tests inject a plain stand-in so no
/// platform view is created.
typedef ArtifactHtmlBuilder =
Widget Function(BuildContext context, String html);
/// Renders a recorded HTML document inertly: on the web in an iframe whose
/// empty `sandbox` blocks scripts, forms, popups and navigation; on Android and
/// iOS in a web view with JavaScript disabled and navigation refused. The
/// document never runs code with the app's origin or credentials.
Widget defaultArtifactHtml(BuildContext context, String html) => SizedBox(
key: const Key('artifact-html-preview'),
height: MediaQuery.sizeOf(context).height * 0.7,
child: impl.InertHtmlView(html: html),
);
@@ -0,0 +1,36 @@
import 'package:flutter/widgets.dart';
import 'package:webview_flutter/webview_flutter.dart';
/// A web view with JavaScript disabled that refuses every navigation after the
/// recorded document itself loads.
class InertHtmlView extends StatefulWidget {
const InertHtmlView({super.key, required this.html});
final String html;
@override
State<InertHtmlView> createState() => _InertHtmlViewState();
}
class _InertHtmlViewState extends State<InertHtmlView> {
late final WebViewController _controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.disabled)
..setNavigationDelegate(
NavigationDelegate(
onNavigationRequest: (request) =>
request.url.startsWith('about:') || request.url.startsWith('data:')
? NavigationDecision.navigate
: NavigationDecision.prevent,
),
)
..loadHtmlString(widget.html);
@override
void didUpdateWidget(covariant InertHtmlView oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.html != widget.html) _controller.loadHtmlString(widget.html);
}
@override
Widget build(BuildContext context) => WebViewWidget(controller: _controller);
}
@@ -0,0 +1,28 @@
import 'dart:js_interop';
import 'package:flutter/widgets.dart';
import 'package:web/web.dart' as web;
/// A sandboxed iframe: the empty `sandbox` attribute applies every
/// restriction, so the document cannot run script or leave the frame.
class InertHtmlView extends StatelessWidget {
const InertHtmlView({super.key, required this.html});
final String html;
@override
Widget build(BuildContext context) => HtmlElementView.fromTagName(
key: ValueKey<int>(html.hashCode),
tagName: 'iframe',
onElementCreated: (element) {
final frame = element as web.HTMLIFrameElement;
frame.setAttribute('sandbox', '');
frame.srcdoc = html.toJS;
frame.style
..border = 'none'
..width = '100%'
..height = '100%'
..backgroundColor = 'white';
},
);
}
@@ -3,6 +3,7 @@ import 'dart:typed_data';
import 'package:flutter/material.dart';
import '../domain/artifact_preview.dart';
import 'artifact_html_view.dart';
import 'artifact_table_view.dart';
import 'artifact_video_player.dart';
import '../../../ui/text/monospace.dart';
@@ -33,6 +34,7 @@ class ArtifactPreviewBody extends StatelessWidget {
this.bytes,
this.error,
this.videoControllerFactory = defaultArtifactVideoController,
this.htmlBuilder = defaultArtifactHtml,
});
final ArtifactPreviewPlan plan;
@@ -45,6 +47,9 @@ class ArtifactPreviewBody extends StatelessWidget {
/// The video player's controller source; tests inject a fake.
final ArtifactVideoControllerFactory videoControllerFactory;
/// The inert HTML renderer; tests inject a plain stand-in.
final ArtifactHtmlBuilder htmlBuilder;
@override
Widget build(BuildContext context) {
if (plan.kind == ArtifactPreviewKind.unavailable ||
@@ -80,6 +85,8 @@ class ArtifactPreviewBody extends StatelessWidget {
);
case ArtifactPreviewKind.markdown:
return _PreviewPadding(child: markdownBuilder(context, text ?? ''));
case ArtifactPreviewKind.html:
return _PreviewPadding(child: htmlBuilder(context, text ?? ''));
case ArtifactPreviewKind.table:
return ArtifactTableView(text: text ?? '', delimiter: plan.delimiter);
case ArtifactPreviewKind.video: