Skip to content
Closed
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
5 changes: 4 additions & 1 deletion app/lib/pages/apps/markdown_viewer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ class _MarkdownViewerState extends State<MarkdownViewer> {
} else {
href += '?uid=${SharedPreferencesUtil().uid}';
}
launchUrl(Uri.parse(href));
final uri = Uri.tryParse(href);
if (uri != null && (uri.scheme == 'http' || uri.scheme == 'https')) {
launchUrl(uri);
}
Comment on lines +70 to +73
Copy link
Contributor

Choose a reason for hiding this comment

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

high

While this change correctly prevents the crash by validating the URL scheme, the way the uid query parameter is added on the preceding lines is fragile and can lead to broken URLs. Modifying the URL via string concatenation doesn't account for URL fragments (#).

For example, a URL like http://example.com/page#section would be incorrectly transformed into http://example.com/page#section?uid=..., which is an invalid URI structure.

A more robust approach is to parse the URL into a Uri object first, and then use its methods to add query parameters. This ensures that all parts of the URL (including fragments) are handled correctly.

Consider refactoring the onTapLink handler like this:

onTapLink: (text, href, title) {
  if (href == null) return;

  var uri = Uri.tryParse(href);
  if (uri == null) return;

  // Only handle http and https schemes.
  if (uri.scheme == 'http' || uri.scheme == 'https') {
    // Add uid query parameter robustly.
    final newQueryParameters = Map<String, dynamic>.from(uri.queryParameters);
    newQueryParameters['uid'] = SharedPreferencesUtil().uid;
    final newUri = uri.replace(queryParameters: newQueryParameters);
    launchUrl(newUri);
  }
}

This would make the link handling logic safer and prevent future issues.

}
},
),
Expand Down
5 changes: 4 additions & 1 deletion app/lib/pages/chat/widgets/markdown_message_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ Widget getMarkdownWidget(BuildContext context, String message, {Function(String)
),
onTapLink: (text, href, title) {
if (href != null) {
launchUrl(Uri.parse(href));
final uri = Uri.tryParse(href);
if (uri != null && (uri.scheme == 'http' || uri.scheme == 'https')) {
launchUrl(uri);
}
}
},
);
Expand Down