Skip to content
New issue

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

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

Already on GitHub? # to your account

Don't attempt to navigate to invalid URLs #163

Merged
merged 1 commit into from
Jul 21, 2021

Conversation

ghiculescu
Copy link
Contributor

@ghiculescu ghiculescu commented Mar 29, 2021

Currently Turbo Native will attempt to navigate to any link, even if it's not a valid URL. This can cause the app to crash (here) if you click on a link like sms:+55555555?body=hi, since URL(location) doesn't recognise sms: links as valid.

To properly handle this you should implement this code. But if you forget to, or if someone working on the web app ships a new link without you knowing about it, you don't want this to cause a scenario where clicking a link can crash the native app.

This PR avoids that, by not navigating to a link that can't be parsed to a URL by default. Now, rather than crashing the app, the link does nothing. Still not great, but not as bad.

If you've overridden shouldNavigateTo this shouldn't change anything for you.

Currently Turbo Native will attempt to navigate to any link, even if it's not a valid URL. This can cause the app to crash if you click on a link like `sms:+55555555?body=hi`, since `URL(location)` doesn't recognise `sms:` links as valid.

To properly handle this you should implement [this code](https://github.com/hotwired/turbo-android/blob/main/demo/src/main/kotlin/dev/hotwire/turbo/demo/base/NavDestination.kt#L18..L57). But if you forget to, or if someone working on the web app ships a new link without you knowing about it, you don't want this to cause a scenario where clicking a link can crash the native app.

This PR avoids that, by not navigating to a link that can't be parsed to a URL by default. Now, rather than crashing the app, the link does nothing. Still not great, but not as bad.

If you've overridden `shouldNavigateTo` this shouldn't change anything for you.
@@ -127,7 +128,12 @@ interface TurboNavDestination {
* Turbo navigation flow).
*/
fun shouldNavigateTo(newLocation: String): Boolean {
return true
return try {
URL(newLocation)
Copy link
Contributor Author

@ghiculescu ghiculescu Mar 29, 2021

Choose a reason for hiding this comment

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

@jayohms
Copy link
Collaborator

jayohms commented Jul 21, 2021

This is great, thank you!

@jayohms jayohms merged commit 6c1ea13 into hotwired:main Jul 21, 2021
@ghiculescu ghiculescu deleted the crash-non-url branch July 21, 2021 16:31
ghiculescu added a commit to ghiculescu/turbo-android that referenced this pull request Feb 6, 2022
Since hotwired#163, the base implementation of `shouldNavigateTo` is useful because it checks for invalid URLs. So if you override this method we should still recommend you call `super` to get the base implementation.

While here I turned the `when` into an `if` per [this](https://kotlinlang.org/docs/coding-conventions.html#if-versus-when), but I'm certainly not a Kotlin expert and happy to change it back if you prefer.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants