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

[Android] Encoding webview's source #24002

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

kubaflo
Copy link
Contributor

@kubaflo kubaflo commented Aug 4, 2024

Issues Fixed

Fixes #23767
Fixes #23690

Before After
Screen.Recording.2024-08-04.at.14.06.51.mov
Screen.Recording.2024-08-04.at.14.04.00.mov

Copy link
Contributor

Hey there @kubaflo! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@jfversluis
Copy link
Member

This just fixes the spaces, there is another issue for square brackets. Should we just use the UriEncode method to catch it all?

@kubaflo
Copy link
Contributor Author

kubaflo commented Aug 4, 2024

@jfversluis
The problem with Uri encode: string? encodedUrl = HttpUtility.UrlEncode(url);
https://www.google.com/search?q=test 123 becomes https%3a%2f%2fwww.google.com%2fsearch%3fq%3dtest+123 and Uri.IsWellFormedUriString validates it as a bad URL :/

@jfversluis
Copy link
Member

Oh yeah that makes sense. Hmmm I do wonder why this worked in Xamarin.Forms though and not here. Same with #23690

@kubaflo
Copy link
Contributor Author

kubaflo commented Aug 6, 2024

@jfversluis
It looks like Xamarin didn't have this logic:

if (url != null && !url.StartsWith('/') && !Uri.IsWellFormedUriString(encodedUrl, UriKind.Absolute))
{
    // URLs like "index.html" can't possibly load, so try "file:///android_asset/index.html"
    url = AssetBaseUrl + url;
} 

void LoadUrl(string url, bool fireNavigatingCanceled)

@@ -48,7 +48,8 @@ void LoadUrl(string url, bool fireNavigatingCanceled)
if (!fireNavigatingCanceled || !SendNavigatingCanceled(url))
{
_eventState = WebNavigationEvent.NewPage;
if (url != null && !url.StartsWith('/') && !Uri.IsWellFormedUriString(url, UriKind.Absolute))
string? spacesEscapedUrl = url?.Replace(" ","%20", StringComparison.InvariantCulture);
Copy link
Member

Choose a reason for hiding this comment

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

Is there no Uri.EncodeSomething that is safer? What about other "special" characters in the uri?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is a bit problematic: #24002 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

Can we only encode whatever comes after the base URL? So only the query string basically?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Imo, great idea!

Co-Authored-By: Gerald Versluis <gerald.versluis@microsoft.com>
@kubaflo kubaflo changed the title [Android] Space character in Webview's Source [Android] Encoding webview's source Sep 5, 2024
@David-Ernstsson
Copy link

David-Ernstsson commented Oct 7, 2024

Any progress on this? Not sure if I have same problem since a single bracket or space works fine when escaped.

HOWEVER using a space AND a german special character creates error shown above. See sample code:

        var webView = new WebView();
        var text = Uri.EscapeDataString("sehr schön");
        var source = $"https://translate.google.com/?sl=de&tl=en&op=translate&text={text}";

        webView.Source = new UrlWebViewSource { Url = source };

@jsuarezruiz
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants