Skip to content

feat(flutter): route media and Markdown URLs through the URL security policy - #4120

Draft
nan-yu wants to merge 6 commits into
feat/url-security-dart-corefrom
feat/url-security-flutter
Draft

nan-yu wants to merge 6 commits into
feat/url-security-dart-corefrom
feat/url-security-flutter

Conversation

@nan-yu

@nan-yu nan-yu commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Routes the Flutter basic catalog's media widgets and the Markdown hook through the URL security policy added to a2ui_core in #4114. Part of #4045. Stacked on #4114 (base feat/url-security-dart-core).

Reviewer focus

  1. A2uiMarkdownBuilder gains a fourth argument, A2uiMarkdownUrlSanitizer sanitizeUrl. This breaks existing builders at compile time on purpose: the core can't see the URLs a Markdown package renders, so every builder now has to decide what to do with them. A nullable or optional parameter would let old builders keep loading any URL without notice.
  2. A blocked Image now renders nothing, the empty state the plan asks for, instead of the old broken-image icon. A blocked Video or AudioPlayer gets no URL and shows the idle player it shows for a missing URL. A2uiMediaImage works out its image only when its URL changes, so a large data: image isn't decoded again on every rebuild.
  3. MediaPlayer streams only http(s) URLs. An allowed data: or relative URL shows the failure icon: video_player can't stream the first, and Android's player reads the second as a local file path.
  4. Main's feat(a2ui_flutter): render data: image URLs in Image #3963 added data: support to Image; this PR merges it into one policy-checked A2uiMediaImage (public, exported from both basic catalog libraries) instead of keeping two decoders.

Changes

  1. Image, Video (including posterUrl) and AudioPlayer use the resolved, policy-filtered props. A2uiMediaImage runs the URL baseline again, fetches http(s) URLs with NetworkImage, decodes data: URLs of an image/* type with main's cache-keyed provider (;base64 in any case), and builds its error widget for anything else, such as a relative URL. MediaPlayer also runs the baseline and streams only http(s).
  2. buildText passes the builder a sanitizer bound to the Text's ComponentContext (falling back to the SurfaceScope surface), so warnings name the component and text as the property.
  3. The chat examples (v1.0, v0.9), the explorer and the README run Markdown link targets through navigation and image sources through media, loading images with A2uiMediaImage.
  4. README: the Markdown example and the security section describe the policy; CHANGELOG lists both breaking changes.
  5. Tests: blocked, allowed, rewritten and data: images (main's feat(a2ui_flutter): render data: image URLs in Image #3963 tests kept; its text/plain and MIME-less data: cases are now blocked by the baseline and render nothing); A2uiMediaImage unit tests; blocked, data: and relative video URLs; data: and blocked posters; the Markdown sanitizer under an allowlist policy; the test harness takes a urlSecurityPolicy and collects warnings.

Impact & risks

  1. Breaking for apps with an A2uiMarkdown builder (one-line signature change, shown in the README and CHANGELOG).
  2. Under the default permissive policy, http(s) media behave as before. A relative image URL still shows the broken-image icon without a request.
  3. a2ui_flutter now uses a2ui_core APIs from feat(dart): add URL security policy to a2ui_core #4114 (UrlContext, sanitizeUrl, urlSecurityPolicy), so the release PR needs to raise its a2ui_core constraint to the release that ships them.

Testing

Rebased on the new #4114. Local checks with Flutter stable (Dart 3.14):

  1. a2ui_flutter, example, example_v0_9, a2ui_explorer: flutter analyze --fatal-infos, dart format --set-exit-if-changed and flutter test pass (485 tests in a2ui_flutter).
  2. check_executed_cases.py --harness flutter for the adapter and e2e corpora, and check_feature_coverage.py: clean.
  3. flutter_packages_test.yml dispatched on this branch: green.

🤖 Agent-generated

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@github-project-automation github-project-automation Bot moved this to Todo in A2UI Nov 10, 2026
@nan-yu
nan-yu force-pushed the feat/url-security-flutter branch from 9bdb999 to f8399c0 Compare November 10, 2026 05:45
@nan-yu

nan-yu commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator Author

/gemini review

🤖 Agent-generated

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request integrates URL security policy checks into the basic media and text components of a2ui_flutter, including Image, Video, AudioPlayer, and Markdown renderers. It also introduces a mediaImage helper to handle data: URLs in memory. The feedback suggests wrapping the synchronous base64 decoding in mediaImage in a try-catch block to prevent crashes, simplifying the scheme check using startsWith, and updating the Markdown image builders in the example apps to support data: URLs instead of calling Image.network directly.

Comment on lines +41 to +47
return Image.memory(
data.contentAsBytes(),
fit: fit,
semanticLabel: semanticLabel,
excludeFromSemantics: excludeFromSemantics,
errorBuilder: errorBuilder,
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Calling data.contentAsBytes() synchronously during the widget build phase can throw a FormatException if the base64 payload is malformed. Since this happens before Image.memory is constructed, the error will not be caught by Image.memory's errorBuilder, leading to a synchronous crash/red screen. Wrapping the decoding in a try-catch block and returning the errorBuilder widget on failure ensures robustness.

    try {
      final bytes = data.contentAsBytes();
      return Image.memory(
        bytes,
        fit: fit,
        semanticLabel: semanticLabel,
        excludeFromSemantics: excludeFromSemantics,
        errorBuilder: errorBuilder,
      );
    } catch (error) {
      return Builder(
        builder: (context) => errorBuilder(
          context,
          error is Exception ? error : FormatException(error.toString()),
          null,
        ),
      );
    }

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 62e9a9f. In practice Uri.tryParse already rejects malformed base64 in a data: URL (it returns null, which built the error widget), so I couldn't reproduce a throw. The decode now also catches a FormatException from contentAsBytes() and builds the error widget, so it no longer depends on that parser detail. Added tests: media_image_test.dart (!!!!, truncated base64 and a bare data:) and an Image widget test showing the broken-image icon for data:image/png;base64,!!!!. The Video poster goes through the same widget, so it gets the fix too.

🤖 Agent-generated

bool excludeFromSemantics = false,
required ImageErrorWidgetBuilder errorBuilder,
}) {
if (url.length > 5 && url.substring(0, 5).toLowerCase() == 'data:') {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Checking for a data: URL scheme can be simplified and made more idiomatic using startsWith with a case-insensitive check or by converting to lowercase first. This avoids manual length checks and substring slicing.

Suggested change
if (url.length > 5 && url.substring(0, 5).toLowerCase() == 'data:') {
if (url.toLowerCase().startsWith('data:')) {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 62e9a9f: url.toLowerCase().startsWith('data:'). A bare data: now takes the data path and builds the error widget instead of going to Image.network.

🤖 Agent-generated

Comment on lines +448 to +451
imageBuilder: (uri, title, alt) {
final String? src = sanitizeUrl('$uri', UrlContext.media);
return src == null ? Text(alt ?? '') : Image.network(src);
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The Markdown imageBuilder uses Image.network(src) directly. However, Image.network does not support data: URLs. If the Markdown contains an allowed data: URL image, it will fail to load. Checking if the URL is a data: URL and using Image.memory to decode it ensures that embedded images render correctly.

  imageBuilder: (uri, title, alt) {
    final String? src = sanitizeUrl('$uri', UrlContext.media);
    if (src == null) return Text(alt ?? '');
    if (src.toLowerCase().startsWith('data:')) {
      final data = Uri.tryParse(src)?.data;
      return data == null ? Text(alt ?? '') : Image.memory(data.contentAsBytes());
    }
    return Image.network(src);
  },

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 62e9a9f. Instead of copying the decode logic into each app, the helper is now a public A2uiMediaImage widget exported from basic_catalog.dart and v0_9/basic_catalog.dart. The examples, the explorer and the A2uiMarkdownBuilder doc example use it, so an allowed data: image shows and a malformed one falls back to the alt text.

🤖 Agent-generated

Comment on lines +170 to +173
imageBuilder: (uri, title, alt) {
final String? src = sanitizeUrl('$uri', UrlContext.media);
return src == null ? Text(alt ?? '') : Image.network(src);
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The Markdown imageBuilder uses Image.network(src) directly, which does not support data: URLs. If the Markdown contains an allowed data: URL image, it will fail to load. Checking if the URL is a data: URL and using Image.memory to decode it ensures that embedded images render correctly.

  imageBuilder: (uri, title, alt) {
    final String? src = sanitizeUrl('$uri', UrlContext.media);
    if (src == null) return Text(alt ?? '');
    if (src.toLowerCase().startsWith('data:')) {
      final data = Uri.tryParse(src)?.data;
      return data == null ? Text(alt ?? '') : Image.memory(data.contentAsBytes());
    }
    return Image.network(src);
  },

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 62e9a9f. Instead of copying the decode logic into each app, the helper is now a public A2uiMediaImage widget exported from basic_catalog.dart and v0_9/basic_catalog.dart. The examples, the explorer and the A2uiMarkdownBuilder doc example use it, so an allowed data: image shows and a malformed one falls back to the alt text.

🤖 Agent-generated

Comment on lines +170 to +173
imageBuilder: (uri, title, alt) {
final String? src = sanitizeUrl('$uri', UrlContext.media);
return src == null ? Text(alt ?? '') : Image.network(src);
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The Markdown imageBuilder uses Image.network(src) directly, which does not support data: URLs. If the Markdown contains an allowed data: URL image, it will fail to load. Checking if the URL is a data: URL and using Image.memory to decode it ensures that embedded images render correctly.

  imageBuilder: (uri, title, alt) {
    final String? src = sanitizeUrl('$uri', UrlContext.media);
    if (src == null) return Text(alt ?? '');
    if (src.toLowerCase().startsWith('data:')) {
      final data = Uri.tryParse(src)?.data;
      return data == null ? Text(alt ?? '') : Image.memory(data.contentAsBytes());
    }
    return Image.network(src);
  },

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 62e9a9f. Instead of copying the decode logic into each app, the helper is now a public A2uiMediaImage widget exported from basic_catalog.dart and v0_9/basic_catalog.dart. The examples, the explorer and the A2uiMarkdownBuilder doc example use it, so an allowed data: image shows and a malformed one falls back to the alt text.

🤖 Agent-generated

@nan-yu
nan-yu force-pushed the feat/url-security-flutter branch from 62e9a9f to fef88a9 Compare November 10, 2026 17:13
@nan-yu
nan-yu force-pushed the feat/url-security-dart-core branch 3 times, most recently from 3ce09b4 to c933cdb Compare December 3, 2026 00:51
@nan-yu
nan-yu force-pushed the feat/url-security-flutter branch from fef88a9 to 7b99de7 Compare December 3, 2026 00:52
@nan-yu
nan-yu force-pushed the feat/url-security-dart-core branch from c933cdb to 3a6afec Compare December 3, 2026 01:07
@nan-yu
nan-yu force-pushed the feat/url-security-flutter branch from 7b99de7 to a136f89 Compare December 3, 2026 01:13
@nan-yu
nan-yu force-pushed the feat/url-security-dart-core branch from a8ef288 to 93b4893 Compare December 3, 2026 18:08
@nan-yu
nan-yu force-pushed the feat/url-security-flutter branch 2 times, most recently from 6615542 to 7c9a749 Compare December 3, 2026 19:03
@nan-yu
nan-yu force-pushed the feat/url-security-dart-core branch from 051d74a to 1bc1d4c Compare December 3, 2026 19:11
@nan-yu
nan-yu force-pushed the feat/url-security-flutter branch 2 times, most recently from 39684f3 to 769a42c Compare December 3, 2026 19:26
@nan-yu
nan-yu force-pushed the feat/url-security-dart-core branch from 5eeec2c to 714890c Compare December 3, 2026 19:29
…policy (#4045)

Image, Video and AudioPlayer use the URLs the resolver already checked
against the surface's URL security policy instead of their own http(s)
check. Image decodes an allowed data: URL in memory. A2uiMarkdownBuilder
gets a sanitizer bound to the Text and the surface policy for link targets
and image sources; the examples and explorer use it.
…are A2uiMediaImage (#4045)

Image and the Video poster decode a data: URL during build; a malformed URL
now builds the error widget instead of risking a throw before Image.memory
can report it. The helper becomes the public A2uiMediaImage widget, exported
from both basic catalog libraries, so Markdown image builders in the examples
and the explorer show allowed data: images too.
…Image (#4045)

Main's Image gained data: image support (#3963) while this branch added
A2uiMediaImage for the same job. Merge them into A2uiMediaImage: it fetches
http(s) URLs with NetworkImage, decodes data: URLs of an image/* type with
main's cache-friendly provider, and builds its error widget for any other
URL (such as a relative one) without loading anything. Image, the Video
poster, and the Markdown image builders all use it, so every image URL goes
through the URL security policy first. Main's data: tests are kept; the
non-image and MIME-less data: URLs they used now count as blocked by the
baseline and render nothing.
…kdown images through the policy (#4045)

- MediaPlayer streams only http(s) URLs. A relative URL the policy allows
  reached VideoPlayerController.networkUrl, which Android's player reads as
  a local file path, so an agent could play any readable app file.
- MediaPlayer and A2uiMediaImage also run the URL baseline themselves, so
  one built by hand can't load what the policy never allows.
- A2uiMediaImage reads a ;BASE64 marker in any case, as browsers do.
- The README Markdown example gets an imageBuilder that checks image
  sources, and says that without one flutter_markdown_plus loads images
  unchecked; the description of the default policy is corrected.
- Tests cover the Video poster (data: and blocked) and the new cases.
…rrect the media CHANGELOG (#4045)

A2uiMediaImage checked and decoded its URL on every build, about 130 ms
a frame for a 4 MB data: image, and the Video poster rebuilds on every
player notification. It now works out its image provider when the URL
changes. The CHANGELOG now says a blocked Video or AudioPlayer shows an
idle player, which is what it renders, and the README link snippet uses
Uri.tryParse.
@nan-yu
nan-yu force-pushed the feat/url-security-flutter branch from 769a42c to 40852db Compare December 3, 2026 19:29

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant