Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[url_launcher] launchUrl always returns true for valid schemes on the web. #7229

Merged
merged 6 commits into from
Aug 6, 2024
Merged
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
4 changes: 4 additions & 0 deletions packages/url_launcher/url_launcher_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.3.3

* Changes `launchUrl` so it always returns `true`, except for disallowed URL schemes.

## 2.3.2

* Adds support for `web: ^1.0.0`.
Expand Down
12 changes: 11 additions & 1 deletion packages/url_launcher/url_launcher_web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,14 @@ In such cases, you can use the `webOnlyWindowName` parameter, setting it to
`_self`, to open the URL within the current tab. Another approach is to ensure
that the `uri` is synchronously ready.

Read more: MDN > [Transient activation](https://developer.mozilla.org/en-US/docs/Glossary/Transient_activation).
Read more: MDN > [Transient activation](https://developer.mozilla.org/en-US/docs/Glossary/Transient_activation).

### Method `launchUrl` always returns `true` for allowed schemes

The `launchUrl` method always returns `true` on the web platform for allowed
schemes. This is because URLs are opened in a new window using the `noopener`
window feature. When the `noopener` feature is used, the browser does not
return any information that can be used to determine if the link was
successfully opened.

Read more: MDN > [window.open](https://developer.mozilla.org/en-US/docs/Web/API/Window/open#noopener).
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ void main() {
(WidgetTester _) async {
expect(plugin.launch('javascript:alert("1")'), completion(isFalse));
});

testWidgets('launching a unknown sceheme returns true',
(WidgetTester _) async {
expect(
plugin.launch(
'foo:bar',
),
completion(isTrue));
});
});

group('openNewWindow', () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,30 @@ class UrlLauncherPlugin extends UrlLauncherPlatform {

/// Opens the given [url] in the specified [webOnlyWindowName].
///
/// Returns the newly created window.
/// Always returns `true`, except for disallowed schemes. Because `noopener`
/// is used as a window feature, it can not be detected if the window was
/// opened successfully.
/// See https://html.spec.whatwg.org/multipage/nav-history-apis.html#window-open-steps.
@visibleForTesting
html.Window? openNewWindow(String url, {String? webOnlyWindowName}) {
bool openNewWindow(String url, {String? webOnlyWindowName}) {
final String? scheme = _getUrlScheme(url);
// Actively disallow opening some schemes, like javascript.
// See https://github.com/flutter/flutter/issues/136657
if (_isDisallowedScheme(scheme)) {
if (kDebugMode) {
print('Disallowed URL with scheme: $scheme');
}
return null;
return false;
}
// Some schemes need to be opened on the _top window context on Safari.
// See https://github.com/flutter/flutter/issues/51461
final String target = webOnlyWindowName ??
((_isSafari && _isSafariTargetTopScheme(scheme)) ? '_top' : '');

// ignore: unsafe_html
return _window.open(url, target, 'noopener,noreferrer');
_window.open(url, target, 'noopener,noreferrer');

return true;
}

@override
Expand All @@ -109,7 +114,7 @@ class UrlLauncherPlugin extends UrlLauncherPlatform {
@override
Future<bool> launchUrl(String url, LaunchOptions options) async {
final String? windowName = options.webOnlyWindowName;
return openNewWindow(url, webOnlyWindowName: windowName) != null;
return openNewWindow(url, webOnlyWindowName: windowName);
}

@override
Expand Down
2 changes: 1 addition & 1 deletion packages/url_launcher/url_launcher_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: url_launcher_web
description: Web platform implementation of url_launcher
repository: https://github.com/flutter/packages/tree/main/packages/url_launcher/url_launcher_web
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+url_launcher%22
version: 2.3.2
version: 2.3.3

environment:
sdk: ^3.3.0
Expand Down