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

Conversation

Frank3K
Copy link
Contributor

@Frank3K Frank3K commented Jul 28, 2024

Description

Since noopener is used to open URLs, there is no way to determine if the browser has succesfully opened a given URL (MDN). Therefore success is now assumed for supported schemes.

Fixes flutter/flutter#139783.

Implementation largely follows suggestions by @ditman written down in a comment.

The only exception is that launchUrl does not returns a hard-coded true, but a canLaunch(url). That way false is returned for invalid schemes (e.g. javascript:, as is checked in an integration test).

Testing

Sample application

The following application can be used to validate the fix. Note that the print is executed using the current version of url_launcher_web (2.3.1) and that it does not execute when the fix from this PR is used.

main.dart

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

final Uri _url = Uri.parse('https://flutter.dev');

void main() => runApp(
      const MaterialApp(
        home: Material(
          child: Center(
            child: ElevatedButton(
              onPressed: _launchUrl,
              child: Text('Show Flutter homepage'),
            ),
          ),
        ),
      ),
    );

Future<void> _launchUrl() async {
  if (!await launchUrl(_url)) {
    // ignore: avoid_print
    print('Failed opening link.');
  }
}

Pre-launch Checklist

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

Since noopener is used to open URLs, there is no way to determine if the browser has succesfully opened a given URL. Therefore success is now assumed for supported schemes.

Fixes flutter/flutter#139783.
@Frank3K Frank3K requested a review from ditman as a code owner July 28, 2024 13:17
@flutter-dashboard

This comment was marked as resolved.

Copy link
Member

@ditman ditman left a comment

Choose a reason for hiding this comment

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

Any chance of adding a test? Most of the tests of this package are in the example/integration_test directory.

@@ -109,7 +110,8 @@ class UrlLauncherPlugin extends UrlLauncherPlatform {
@override
Future<bool> launchUrl(String url, LaunchOptions options) async {
final String? windowName = options.webOnlyWindowName;
return openNewWindow(url, webOnlyWindowName: windowName) != null;
openNewWindow(url, webOnlyWindowName: windowName);
return canLaunch(url);
Copy link
Member

Choose a reason for hiding this comment

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

This should just return true;, no need to call canLaunch or anything else.

(Question: if canLaunch returns false, does it prevent launchUrl from actually attempting to open a browser tab with a bogus URL? I don't think it would, right?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Returning true will break the url_launcher_web_test test.

I'll update the code and the test.

Question: if canLaunch returns false, does it prevent launchUrl from actually attempting to open a browser tab with a bogus URL? I don't think it would, right?

In the current setup it won't. If we would want that, we could do a canLaunch earlier and bail-out. But I agree returning true is better; if the user of this package wants such behaviour he can do so himself by calling canLaunch first in his own code.

Copy link
Member

Choose a reason for hiding this comment

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

canLaunch is not super valuable on the web, the browser doesn't know if it can handle a protocol and will just attempt to open anything, I think

Copy link
Member

@ditman ditman Aug 3, 2024

Choose a reason for hiding this comment

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

@Frank3K how about:

  • html.Window? openNewWindow(String url, {String? webOnlyWindowName}) becomes:
    • bool openNewWindow(String url, {String? webOnlyWindowName})

If it does NOT call window.open -> return false
If it calls window.open -> return true (regardless of what window.open returns)

Then the implementation of launchUrl can be just:

return openNewWindow...

That way we can still return false if the user really tries to mess up (like opening a disallowed protocol).

(I can incorporate those changes to this PR myself, if you think they make sense)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea 👍 . I applied the proposed changes and added a small test.

This PR is ready for re-review.

@Frank3K
Copy link
Contributor Author

Frank3K commented Aug 1, 2024

Thanks for your review.

Any chance of adding a test? Most of the tests of this package are in the example/integration_test directory.

I have checked the tests and they are already quite extensive. With the proposed return true change, I could add a test with a bogus scheme to validate that it always returns true. Would that suffice or can you indicate what additional tests would be of value?

@ditman
Copy link
Member

ditman commented Aug 5, 2024

Woops, dart format is complaining, I'll get that fixed.

@ditman ditman changed the title [url_launcher] Prevent launchUrl from incorrectly returning false on web [url_launcher] launchUrl always returns true for valid schemes on the web. Aug 5, 2024
@ditman
Copy link
Member

ditman commented Aug 5, 2024

The flutter-dashboard flagged this as "not having tests" earlier, but it does now: opening a bogus scheme returns true, regardless of what the browser does.

Copy link
Member

@ditman ditman left a comment

Choose a reason for hiding this comment

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

LGTM!

@ditman ditman added the autosubmit Merge PR when tree becomes green via auto submit App label Aug 6, 2024
@auto-submit auto-submit bot merged commit 4354f65 into flutter:main Aug 6, 2024
76 checks passed
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Aug 7, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Aug 7, 2024
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request Aug 7, 2024
flutter/packages@551bde5...5cc0a01

2024-08-07 stuartmorgan@google.com [ci] Update repository for the release of Flutter 3.24 (flutter/packages#7331)
2024-08-07 engine-flutter-autoroll@skia.org Roll Flutter (stable) from b0850be to 80c2e84 (1397 revisions) (flutter/packages#7322)
2024-08-07 49699333+dependabot[bot]@users.noreply.github.com Bump actions/upload-artifact from 4.3.5 to 4.3.6 (flutter/packages#7330)
2024-08-07 49699333+dependabot[bot]@users.noreply.github.com [pigeon]: Bump io.mockk:mockk from 1.13.7 to 1.13.12 in /packages/pigeon/platform_tests/test_plugin/android (flutter/packages#7189)
2024-08-07 joonas.kerttula@codemate.com [google_maps_flutter] Marker clustering support (flutter/packages#4319)
2024-08-07 49699333+dependabot[bot]@users.noreply.github.com Bump github/codeql-action from 3.25.15 to 3.26.0 (flutter/packages#7329)
2024-08-07 engine-flutter-autoroll@skia.org Manual roll Flutter from 1dd7141 to 0a7f8af (23 revisions) (flutter/packages#7328)
2024-08-06 magder@google.com [image_picker_ios] Update image picker UI test query for iOS 18 (flutter/packages#7325)
2024-08-06 joonas.kerttula@codemate.com [google_maps_flutter] Add marker clustering support - iOS implementation (flutter/packages#6186)
2024-08-06 8014077+Frank3K@users.noreply.github.com [url_launcher] launchUrl always returns true for valid schemes on the web. (flutter/packages#7229)
2024-08-06 rexios@rexios.dev [google_maps_flutter] Add heatmap support (flutter/packages#3257)
2024-08-06 stuartmorgan@google.com [local_auth] Endorse macOS (flutter/packages#7274)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC flutter-ecosystem@google.com,rmistry@google.com on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
TytaniumDev pushed a commit to TytaniumDev/flutter that referenced this pull request Aug 7, 2024
flutter/packages@551bde5...5cc0a01

2024-08-07 stuartmorgan@google.com [ci] Update repository for the release of Flutter 3.24 (flutter/packages#7331)
2024-08-07 engine-flutter-autoroll@skia.org Roll Flutter (stable) from b0850be to 80c2e84 (1397 revisions) (flutter/packages#7322)
2024-08-07 49699333+dependabot[bot]@users.noreply.github.com Bump actions/upload-artifact from 4.3.5 to 4.3.6 (flutter/packages#7330)
2024-08-07 49699333+dependabot[bot]@users.noreply.github.com [pigeon]: Bump io.mockk:mockk from 1.13.7 to 1.13.12 in /packages/pigeon/platform_tests/test_plugin/android (flutter/packages#7189)
2024-08-07 joonas.kerttula@codemate.com [google_maps_flutter] Marker clustering support (flutter/packages#4319)
2024-08-07 49699333+dependabot[bot]@users.noreply.github.com Bump github/codeql-action from 3.25.15 to 3.26.0 (flutter/packages#7329)
2024-08-07 engine-flutter-autoroll@skia.org Manual roll Flutter from 1dd7141 to 0a7f8af (23 revisions) (flutter/packages#7328)
2024-08-06 magder@google.com [image_picker_ios] Update image picker UI test query for iOS 18 (flutter/packages#7325)
2024-08-06 joonas.kerttula@codemate.com [google_maps_flutter] Add marker clustering support - iOS implementation (flutter/packages#6186)
2024-08-06 8014077+Frank3K@users.noreply.github.com [url_launcher] launchUrl always returns true for valid schemes on the web. (flutter/packages#7229)
2024-08-06 rexios@rexios.dev [google_maps_flutter] Add heatmap support (flutter/packages#3257)
2024-08-06 stuartmorgan@google.com [local_auth] Endorse macOS (flutter/packages#7274)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC flutter-ecosystem@google.com,rmistry@google.com on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
DBowen33 pushed a commit to DBowen33/flutter that referenced this pull request Aug 16, 2024
flutter/packages@551bde5...5cc0a01

2024-08-07 stuartmorgan@google.com [ci] Update repository for the release of Flutter 3.24 (flutter/packages#7331)
2024-08-07 engine-flutter-autoroll@skia.org Roll Flutter (stable) from b0850be to 80c2e84 (1397 revisions) (flutter/packages#7322)
2024-08-07 49699333+dependabot[bot]@users.noreply.github.com Bump actions/upload-artifact from 4.3.5 to 4.3.6 (flutter/packages#7330)
2024-08-07 49699333+dependabot[bot]@users.noreply.github.com [pigeon]: Bump io.mockk:mockk from 1.13.7 to 1.13.12 in /packages/pigeon/platform_tests/test_plugin/android (flutter/packages#7189)
2024-08-07 joonas.kerttula@codemate.com [google_maps_flutter] Marker clustering support (flutter/packages#4319)
2024-08-07 49699333+dependabot[bot]@users.noreply.github.com Bump github/codeql-action from 3.25.15 to 3.26.0 (flutter/packages#7329)
2024-08-07 engine-flutter-autoroll@skia.org Manual roll Flutter from 1dd7141 to 0a7f8af (23 revisions) (flutter/packages#7328)
2024-08-06 magder@google.com [image_picker_ios] Update image picker UI test query for iOS 18 (flutter/packages#7325)
2024-08-06 joonas.kerttula@codemate.com [google_maps_flutter] Add marker clustering support - iOS implementation (flutter/packages#6186)
2024-08-06 8014077+Frank3K@users.noreply.github.com [url_launcher] launchUrl always returns true for valid schemes on the web. (flutter/packages#7229)
2024-08-06 rexios@rexios.dev [google_maps_flutter] Add heatmap support (flutter/packages#3257)
2024-08-06 stuartmorgan@google.com [local_auth] Endorse macOS (flutter/packages#7274)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC flutter-ecosystem@google.com,rmistry@google.com on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Buchimi pushed a commit to Buchimi/flutter that referenced this pull request Sep 2, 2024
flutter/packages@551bde5...5cc0a01

2024-08-07 stuartmorgan@google.com [ci] Update repository for the release of Flutter 3.24 (flutter/packages#7331)
2024-08-07 engine-flutter-autoroll@skia.org Roll Flutter (stable) from b0850be to 80c2e84 (1397 revisions) (flutter/packages#7322)
2024-08-07 49699333+dependabot[bot]@users.noreply.github.com Bump actions/upload-artifact from 4.3.5 to 4.3.6 (flutter/packages#7330)
2024-08-07 49699333+dependabot[bot]@users.noreply.github.com [pigeon]: Bump io.mockk:mockk from 1.13.7 to 1.13.12 in /packages/pigeon/platform_tests/test_plugin/android (flutter/packages#7189)
2024-08-07 joonas.kerttula@codemate.com [google_maps_flutter] Marker clustering support (flutter/packages#4319)
2024-08-07 49699333+dependabot[bot]@users.noreply.github.com Bump github/codeql-action from 3.25.15 to 3.26.0 (flutter/packages#7329)
2024-08-07 engine-flutter-autoroll@skia.org Manual roll Flutter from 1dd7141 to 0a7f8af (23 revisions) (flutter/packages#7328)
2024-08-06 magder@google.com [image_picker_ios] Update image picker UI test query for iOS 18 (flutter/packages#7325)
2024-08-06 joonas.kerttula@codemate.com [google_maps_flutter] Add marker clustering support - iOS implementation (flutter/packages#6186)
2024-08-06 8014077+Frank3K@users.noreply.github.com [url_launcher] launchUrl always returns true for valid schemes on the web. (flutter/packages#7229)
2024-08-06 rexios@rexios.dev [google_maps_flutter] Add heatmap support (flutter/packages#3257)
2024-08-06 stuartmorgan@google.com [local_auth] Endorse macOS (flutter/packages#7274)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC flutter-ecosystem@google.com,rmistry@google.com on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App p: url_launcher platform-web
Projects
None yet
Development

Successfully merging this pull request may close these issues.

launchUrl returns false even though new tab opens successfully
3 participants