From 8f8ed7d3650aad4c5cd8258cd678bd06486d5921 Mon Sep 17 00:00:00 2001 From: Baw Appie Date: Tue, 21 Sep 2021 17:24:48 +0900 Subject: [PATCH 1/8] [url_launcher] Error handling when URL cannot be parsed with Uri.parse --- .../url_launcher/lib/url_launcher.dart | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/url_launcher/url_launcher/lib/url_launcher.dart b/packages/url_launcher/url_launcher/lib/url_launcher.dart index 239e3c46c480..4001cd621fd1 100644 --- a/packages/url_launcher/url_launcher/lib/url_launcher.dart +++ b/packages/url_launcher/url_launcher/lib/url_launcher.dart @@ -71,14 +71,18 @@ Future launch( Brightness? statusBarBrightness, String? webOnlyWindowName, }) async { - final Uri url = Uri.parse(urlString.trimLeft()); - final bool isWebURL = url.scheme == 'http' || url.scheme == 'https'; - if ((forceSafariVC == true || forceWebView == true) && !isWebURL) { - throw PlatformException( - code: 'NOT_A_WEB_SCHEME', - message: 'To use webview or safariVC, you need to pass' - 'in a web URL. This $urlString is not a web URL.'); - } + bool isWebURL = false; + try { + final Uri url = Uri.parse(urlString.trimLeft()); + + isWebURL = url.scheme == 'http' || url.scheme == 'https'; + if ((forceSafariVC == true || forceWebView == true) && !isWebURL) { + throw PlatformException( + code: 'NOT_A_WEB_SCHEME', + message: 'To use webview or safariVC, you need to pass' + 'in a web URL. This $urlString is not a web URL.'); + } + } catch (e) {} /// [true] so that ui is automatically computed if [statusBarBrightness] is set. bool previousAutomaticSystemUiAdjustment = true; From 7f897254f92ee6e3a11ae603bf754f3f69c27b3f Mon Sep 17 00:00:00 2001 From: Baw-Appie Date: Wed, 22 Sep 2021 12:21:13 +0900 Subject: [PATCH 2/8] [url_launcher] Add tests for Microsoft Remote Desktop URL --- .../url_launcher/test/url_launcher_test.dart | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/packages/url_launcher/url_launcher/test/url_launcher_test.dart b/packages/url_launcher/url_launcher/test/url_launcher_test.dart index 04f727a57746..f3954f8dc5b7 100644 --- a/packages/url_launcher/url_launcher/test/url_launcher_test.dart +++ b/packages/url_launcher/url_launcher/test/url_launcher_test.dart @@ -281,6 +281,39 @@ void main() { await launchResult; expect(binding.renderView.automaticSystemUiAdjustment, true); }); + + test('open remote desktop url', () async { + mock + ..setLaunchExpectations( + url: 'rdp://full%20address=s:mypc:3389&audiomode=i:2&disable%20themes=i:1', + useSafariVC: false, + useWebView: false, + enableJavaScript: false, + enableDomStorage: false, + universalLinksOnly: false, + headers: {}, + webOnlyWindowName: null, + ) + ..setResponse(true); + expect(await launch('rdp://full%20address=s:mypc:3389&audiomode=i:2&disable%20themes=i:1'), + isTrue); + }); + + test('cannot open remote desktop url with forceSafariVC: true', () async { + expect( + () async => await launch( + 'rdp://full%20address=s:mypc:3389&audiomode=i:2&disable%20themes=i:1', + forceSafariVC: true), + throwsA(isA())); + }); + + test('cannot open remote desktop url with forceWebView: true', () async { + expect( + () async => await launch( + 'rdp://full%20address=s:mypc:3389&audiomode=i:2&disable%20themes=i:1', + forceWebView: true), + throwsA(isA())); + }); }); } From 6a7b2725fd275edd7913e5a186a22edcd222eb6f Mon Sep 17 00:00:00 2001 From: Baw-Appie Date: Wed, 22 Sep 2021 12:23:13 +0900 Subject: [PATCH 3/8] [url_launcher] Fix try block to catch only FormatException --- .../url_launcher/lib/url_launcher.dart | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/url_launcher/url_launcher/lib/url_launcher.dart b/packages/url_launcher/url_launcher/lib/url_launcher.dart index 4001cd621fd1..ecf0389467b1 100644 --- a/packages/url_launcher/url_launcher/lib/url_launcher.dart +++ b/packages/url_launcher/url_launcher/lib/url_launcher.dart @@ -74,15 +74,15 @@ Future launch( bool isWebURL = false; try { final Uri url = Uri.parse(urlString.trimLeft()); - isWebURL = url.scheme == 'http' || url.scheme == 'https'; - if ((forceSafariVC == true || forceWebView == true) && !isWebURL) { - throw PlatformException( - code: 'NOT_A_WEB_SCHEME', - message: 'To use webview or safariVC, you need to pass' - 'in a web URL. This $urlString is not a web URL.'); - } - } catch (e) {} + } on FormatException {} + + if ((forceSafariVC == true || forceWebView == true) && !isWebURL) { + throw PlatformException( + code: 'NOT_A_WEB_SCHEME', + message: 'To use webview or safariVC, you need to pass' + 'in a web URL. This $urlString is not a web URL.'); + } /// [true] so that ui is automatically computed if [statusBarBrightness] is set. bool previousAutomaticSystemUiAdjustment = true; From 2f7d78611666ac2b37333a2835e3c0163c79293c Mon Sep 17 00:00:00 2001 From: Baw-Appie Date: Wed, 22 Sep 2021 12:43:08 +0900 Subject: [PATCH 4/8] [url_launcher] Formatting test codes --- .../url_launcher/test/url_launcher_test.dart | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/url_launcher/url_launcher/test/url_launcher_test.dart b/packages/url_launcher/url_launcher/test/url_launcher_test.dart index f3954f8dc5b7..90560b3b216c 100644 --- a/packages/url_launcher/url_launcher/test/url_launcher_test.dart +++ b/packages/url_launcher/url_launcher/test/url_launcher_test.dart @@ -281,11 +281,12 @@ void main() { await launchResult; expect(binding.renderView.automaticSystemUiAdjustment, true); }); - + test('open remote desktop url', () async { mock ..setLaunchExpectations( - url: 'rdp://full%20address=s:mypc:3389&audiomode=i:2&disable%20themes=i:1', + url: + 'rdp://full%20address=s:mypc:3389&audiomode=i:2&disable%20themes=i:1', useSafariVC: false, useWebView: false, enableJavaScript: false, @@ -295,7 +296,9 @@ void main() { webOnlyWindowName: null, ) ..setResponse(true); - expect(await launch('rdp://full%20address=s:mypc:3389&audiomode=i:2&disable%20themes=i:1'), + expect( + await launch( + 'rdp://full%20address=s:mypc:3389&audiomode=i:2&disable%20themes=i:1'), isTrue); }); From 4da3eb7a666455f1e4554d377e383c628a85f0cd Mon Sep 17 00:00:00 2001 From: Baw-Appie Date: Wed, 22 Sep 2021 12:44:20 +0900 Subject: [PATCH 5/8] [url_launcher] Update version to 6.0.12 --- packages/url_launcher/url_launcher/CHANGELOG.md | 4 ++++ packages/url_launcher/url_launcher/pubspec.yaml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/url_launcher/url_launcher/CHANGELOG.md b/packages/url_launcher/url_launcher/CHANGELOG.md index 4b52a8d46f8b..fff325e08915 100644 --- a/packages/url_launcher/url_launcher/CHANGELOG.md +++ b/packages/url_launcher/url_launcher/CHANGELOG.md @@ -1,3 +1,7 @@ +## 6.0.12 + +* Fixed an error where 'launch' method of url_launcher would cause an error if the provided URL was not valid by RFC 3986. + ## 6.0.11 * Update minimum Flutter SDK to 2.5 and iOS deployment target to 9.0. diff --git a/packages/url_launcher/url_launcher/pubspec.yaml b/packages/url_launcher/url_launcher/pubspec.yaml index c90d2feb08f4..8edb9e21c535 100644 --- a/packages/url_launcher/url_launcher/pubspec.yaml +++ b/packages/url_launcher/url_launcher/pubspec.yaml @@ -3,7 +3,7 @@ description: Flutter plugin for launching a URL. Supports web, phone, SMS, and email schemes. repository: https://github.com/flutter/plugins/tree/master/packages/url_launcher/url_launcher issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+url_launcher%22 -version: 6.0.11 +version: 6.0.12 environment: sdk: ">=2.14.0 <3.0.0" From de9b86b4abde53089c2141c601388affda0f9061 Mon Sep 17 00:00:00 2001 From: Baw-Appie Date: Wed, 22 Sep 2021 12:58:14 +0900 Subject: [PATCH 6/8] [url_launcher] Fix empty_catches warning --- packages/url_launcher/url_launcher/lib/url_launcher.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/url_launcher/url_launcher/lib/url_launcher.dart b/packages/url_launcher/url_launcher/lib/url_launcher.dart index ecf0389467b1..5320ab31b1a9 100644 --- a/packages/url_launcher/url_launcher/lib/url_launcher.dart +++ b/packages/url_launcher/url_launcher/lib/url_launcher.dart @@ -75,8 +75,8 @@ Future launch( try { final Uri url = Uri.parse(urlString.trimLeft()); isWebURL = url.scheme == 'http' || url.scheme == 'https'; - } on FormatException {} - + } on FormatException catch (_) {} + if ((forceSafariVC == true || forceWebView == true) && !isWebURL) { throw PlatformException( code: 'NOT_A_WEB_SCHEME', From 0bde82de47fa3571937160075db013983f44def6 Mon Sep 17 00:00:00 2001 From: Baw-Appie Date: Thu, 23 Sep 2021 08:56:10 +0900 Subject: [PATCH 7/8] [url_launcher] Use Uri.tryParse instead of try-catch --- packages/url_launcher/url_launcher/lib/url_launcher.dart | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/url_launcher/url_launcher/lib/url_launcher.dart b/packages/url_launcher/url_launcher/lib/url_launcher.dart index 5320ab31b1a9..300f96f4a179 100644 --- a/packages/url_launcher/url_launcher/lib/url_launcher.dart +++ b/packages/url_launcher/url_launcher/lib/url_launcher.dart @@ -71,11 +71,9 @@ Future launch( Brightness? statusBarBrightness, String? webOnlyWindowName, }) async { - bool isWebURL = false; - try { - final Uri url = Uri.parse(urlString.trimLeft()); - isWebURL = url.scheme == 'http' || url.scheme == 'https'; - } on FormatException catch (_) {} + final Uri? url = Uri.tryParse(urlString.trimLeft()); + final bool isWebURL = + url != null && (url.scheme == 'http' || url.scheme == 'https'); if ((forceSafariVC == true || forceWebView == true) && !isWebURL) { throw PlatformException( From 3751fe9064a2a3d5472a27e6aa15cabff3223b5f Mon Sep 17 00:00:00 2001 From: Baw-Appie Date: Thu, 23 Sep 2021 08:56:30 +0900 Subject: [PATCH 8/8] [url_launcher] Update tests name --- .../url_launcher/url_launcher/test/url_launcher_test.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/url_launcher/url_launcher/test/url_launcher_test.dart b/packages/url_launcher/url_launcher/test/url_launcher_test.dart index 90560b3b216c..a038746d6bec 100644 --- a/packages/url_launcher/url_launcher/test/url_launcher_test.dart +++ b/packages/url_launcher/url_launcher/test/url_launcher_test.dart @@ -282,7 +282,7 @@ void main() { expect(binding.renderView.automaticSystemUiAdjustment, true); }); - test('open remote desktop url', () async { + test('open non-parseable url', () async { mock ..setLaunchExpectations( url: @@ -302,7 +302,7 @@ void main() { isTrue); }); - test('cannot open remote desktop url with forceSafariVC: true', () async { + test('cannot open non-parseable url with forceSafariVC: true', () async { expect( () async => await launch( 'rdp://full%20address=s:mypc:3389&audiomode=i:2&disable%20themes=i:1', @@ -310,7 +310,7 @@ void main() { throwsA(isA())); }); - test('cannot open remote desktop url with forceWebView: true', () async { + test('cannot open non-parseable url with forceWebView: true', () async { expect( () async => await launch( 'rdp://full%20address=s:mypc:3389&audiomode=i:2&disable%20themes=i:1',