Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[url_launcher] Error handling when URL cannot be parsed with Uri.parse #4365

Merged
merged 8 commits into from
Sep 23, 2021

Conversation

Baw-Appie
Copy link
Contributor

@Baw-Appie Baw-Appie commented Sep 21, 2021

image

Uri.parse cannot parse some app scheme URLs.
Use try-catch to handle errors.

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the relevant style guides and ran the auto-formatter. (Note that unlike the flutter/flutter repo, the flutter/plugins repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the plugin surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy.
  • I updated CHANGELOG.md to add a description of the change.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@stuartmorgan-g
Copy link
Contributor

Thanks for the submission!

Uri.parse cannot parse some app scheme URLs.

Please file an issue describing this problem in detail, with a specific example. The PR as submitted is not correct, since it will always catch the deliberately thrown exception, so we need to better understand what the problem is to figure out how to move forward.

@Baw-Appie
Copy link
Contributor Author

There is an issue with Uri.parse not correctly parsing URLs containing ":"

For example, Microsoft Remote Desktop uses a custom app scheme URL like the one below, but Uri.parse throws an FormatException error.

rdp://full address=s:example.com:3389&username=s:Administrator

Dart recognizes everything after the ":" as a port, but it seems to be giving an error because it's a string.

Should I file an issue to dart-lang/sdk instead of submitting this PR?

@stuartmorgan-g
Copy link
Contributor

Should I file an issue to dart-lang/sdk instead of submitting this PR?

It turns out that's not actually a valid URI per RFC 3986, which is what Uri uses, so it's not actually a Dart bug. Given that, a more targeted version of the change you've proposed here is reasonable.

Copy link
Contributor

@stuartmorgan-g stuartmorgan-g left a comment

Choose a reason for hiding this comment

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

In addition to the specific comment below, this will need the rest of the items on the PR checklist addressed. Most importantly, this needs a unit test that validates behavior using a sample RDP URL.

message: 'To use webview or safariVC, you need to pass'
'in a web URL. This $urlString is not a web URL.');
}
} catch (e) {}
Copy link
Contributor

Choose a reason for hiding this comment

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

This should only be wrapping parse, not everything (especially not the throw) and should only catch the specific exception it is intended to handle.

@stuartmorgan-g
Copy link
Contributor

rdp://full address=s:example.com:3389&username=s:Administrator

When you write the test, please properly encode the space (see https://docs.microsoft.com/en-us/windows-server/remote/remote-desktop-services/clients/remote-desktop-uri for example); as written this is invalid for reasons unrelated to the actual problem with rdp: URLs.

Copy link
Contributor

@stuartmorgan-g stuartmorgan-g left a comment

Choose a reason for hiding this comment

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

Small nits, but otherwise looks good!

try {
final Uri url = Uri.parse(urlString.trimLeft());
isWebURL = url.scheme == 'http' || url.scheme == 'https';
} on FormatException catch (_) {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, you can just use Uri.tryParse instead of this, and simplify the code. I forgot to see if there was a try version of this API before my last review comments.

  final Uri? url = Uri.tryParse(urlString.trimLeft());
  final bool isWebURL = url != null && (url.scheme == 'http' || url.scheme == 'https');

@@ -281,6 +281,42 @@ void main() {
await launchResult;
expect(binding.renderView.automaticSystemUiAdjustment, true);
});

test('open remote desktop url', () async {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's call this 'open non-parseable URL' so it's clear what the real purpose of this test is.

isTrue);
});

test('cannot open remote desktop url with forceSafariVC: true', () async {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here and below; s/remove desktop/non-parseable/

Copy link
Contributor

@stuartmorgan-g stuartmorgan-g left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@stuartmorgan-g stuartmorgan-g added the waiting for tree to go green (Use "autosubmit") This PR is approved and tested, but waiting for the tree to be green to land. label Sep 23, 2021
@fluttergithubbot fluttergithubbot merged commit 8c5f0f0 into flutter:master Sep 23, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Sep 23, 2021
amantoux pushed a commit to amantoux/plugins that referenced this pull request Sep 27, 2021
NickalasB added a commit to NickalasB/plugins that referenced this pull request Sep 27, 2021
* master: (51 commits)
  [webview_flutter] Update version number app_facing package (flutter#4375)
  [webview_flutter] Adjust integration test domains (flutter#4383)
  Remove some trivial custom analysis options files (flutter#4379)
  [google_maps_flutter_platfomr_interface] Add Marker drag events (flutter#2653)
  [flutter_plugin_tools] Improve version check error handling (flutter#4376)
  [flutter_plugin_tools] Allow overriding breaking change check (flutter#4369)
  [url_launcher] Error handling when URL cannot be parsed with Uri.parse (flutter#4365)
  [webview_flutter] Migrate main package to fully federated architecture. (flutter#4366)
  [google_sign_in] Bump minimum Flutter version and iOS deployment target (flutter#4334)
  Add false secret lists, and enforce ordering (flutter#4372)
  [camera_web] Update usage documentation (flutter#4371)
  [video_player] VTT Support (flutter#2878)
  Require authors file (flutter#4367)
  [flutter_plugin_tools] Fix federated safety check (flutter#4368)
  [webview_flutter] Extract WKWebView implementation into a separate package (flutter#4345)
  [webview_flutter] Extract Android implementation into a separate package (flutter#4343)
  [in_app_purchase] Ensure the `introductoryPriceMicros` field is populated correctly. (flutter#4364)
  [flutter_plugin_tools] Add a federated PR safety check (flutter#4329)
  [camera] Add web support (flutter#4240)
  [webview_flutter] Bump minimum Flutter version and iOS deployment target (flutter#4361)
  ...

# Conflicts:
#	packages/webview_flutter/webview_flutter/lib/platform_interface.dart
#	packages/webview_flutter/webview_flutter/lib/src/webview_method_channel.dart
#	packages/webview_flutter/webview_flutter/lib/webview_flutter.dart
KyleFin pushed a commit to KyleFin/plugins that referenced this pull request Dec 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes p: url_launcher waiting for tree to go green (Use "autosubmit") This PR is approved and tested, but waiting for the tree to be green to land.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants