Repository navigation
Conversation
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
9bdb999 to
f8399c0
Compare
|
/gemini review 🤖 Agent-generated |
There was a problem hiding this comment.
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.
| return Image.memory( | ||
| data.contentAsBytes(), | ||
| fit: fit, | ||
| semanticLabel: semanticLabel, | ||
| excludeFromSemantics: excludeFromSemantics, | ||
| errorBuilder: errorBuilder, | ||
| ); |
There was a problem hiding this comment.
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,
),
);
}There was a problem hiding this comment.
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:') { |
There was a problem hiding this comment.
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.
| if (url.length > 5 && url.substring(0, 5).toLowerCase() == 'data:') { | |
| if (url.toLowerCase().startsWith('data:')) { |
There was a problem hiding this comment.
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
| imageBuilder: (uri, title, alt) { | ||
| final String? src = sanitizeUrl('$uri', UrlContext.media); | ||
| return src == null ? Text(alt ?? '') : Image.network(src); | ||
| }, |
There was a problem hiding this comment.
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);
},There was a problem hiding this comment.
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
| imageBuilder: (uri, title, alt) { | ||
| final String? src = sanitizeUrl('$uri', UrlContext.media); | ||
| return src == null ? Text(alt ?? '') : Image.network(src); | ||
| }, |
There was a problem hiding this comment.
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);
},There was a problem hiding this comment.
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
| imageBuilder: (uri, title, alt) { | ||
| final String? src = sanitizeUrl('$uri', UrlContext.media); | ||
| return src == null ? Text(alt ?? '') : Image.network(src); | ||
| }, |
There was a problem hiding this comment.
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);
},There was a problem hiding this comment.
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
62e9a9f to
fef88a9
Compare
3ce09b4 to
c933cdb
Compare
fef88a9 to
7b99de7
Compare
c933cdb to
3a6afec
Compare
7b99de7 to
a136f89
Compare
a8ef288 to
93b4893
Compare
6615542 to
7c9a749
Compare
051d74a to
1bc1d4c
Compare
39684f3 to
769a42c
Compare
5eeec2c to
714890c
Compare
…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.
769a42c to
40852db
Compare
Summary
Routes the Flutter basic catalog's media widgets and the Markdown hook through the URL security policy added to
a2ui_corein #4114. Part of #4045. Stacked on #4114 (basefeat/url-security-dart-core).Reviewer focus
A2uiMarkdownBuildergains 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.Imagenow renders nothing, the empty state the plan asks for, instead of the old broken-image icon. A blockedVideoorAudioPlayergets no URL and shows the idle player it shows for a missing URL.A2uiMediaImageworks out its image only when its URL changes, so a largedata:image isn't decoded again on every rebuild.MediaPlayerstreams onlyhttp(s)URLs. An alloweddata:or relative URL shows the failure icon:video_playercan't stream the first, and Android's player reads the second as a local file path.data:support toImage; this PR merges it into one policy-checkedA2uiMediaImage(public, exported from both basic catalog libraries) instead of keeping two decoders.Changes
Image,Video(includingposterUrl) andAudioPlayeruse the resolved, policy-filtered props.A2uiMediaImageruns the URL baseline again, fetcheshttp(s)URLs withNetworkImage, decodesdata:URLs of animage/*type with main's cache-keyed provider (;base64in any case), and builds its error widget for anything else, such as a relative URL.MediaPlayeralso runs the baseline and streams onlyhttp(s).buildTextpasses the builder a sanitizer bound to the Text'sComponentContext(falling back to theSurfaceScopesurface), so warnings name the component andtextas the property.navigationand image sources throughmedia, loading images withA2uiMediaImage.data:images (main's feat(a2ui_flutter): render data: image URLs in Image #3963 tests kept; itstext/plainand MIME-lessdata:cases are now blocked by the baseline and render nothing);A2uiMediaImageunit tests; blocked,data:and relative video URLs;data:and blocked posters; the Markdown sanitizer under an allowlist policy; the test harness takes aurlSecurityPolicyand collects warnings.Impact & risks
A2uiMarkdownbuilder (one-line signature change, shown in the README and CHANGELOG).http(s)media behave as before. A relative image URL still shows the broken-image icon without a request.a2ui_flutternow usesa2ui_coreAPIs from feat(dart): add URL security policy to a2ui_core #4114 (UrlContext,sanitizeUrl,urlSecurityPolicy), so the release PR needs to raise itsa2ui_coreconstraint to the release that ships them.Testing
Rebased on the new #4114. Local checks with Flutter stable (Dart 3.14):
a2ui_flutter,example,example_v0_9,a2ui_explorer:flutter analyze --fatal-infos,dart format --set-exit-if-changedandflutter testpass (485 tests ina2ui_flutter).check_executed_cases.py --harness flutterfor the adapter and e2e corpora, andcheck_feature_coverage.py: clean.flutter_packages_test.ymldispatched on this branch: green.🤖 Agent-generated