dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.98k stars 1.71k forks source link

Space character in Webview's Source causes net::ERR_FILE_NOT_FOUND error #23767

Open CostasAthan opened 1 month ago

CostasAthan commented 1 month ago

Description

In a .NET 8 Maui project that targets Android API 34, I'm using a Webview with a Source defined by a variable: myWebView.Source = "https://example.com/?q=" + myVariable

If there is one or more leading or random spaces (%20) in myVariable, then I get a net::ERR_FILE_NOT_FOUND error:

Webpage not available

The webpage at file:///android_asset/https://example.com/?q=%20%20Test could not be loaded because:

net::ERR_FILE_NOT_FOUND

If there are only trailing spaces, there is no problem and the site loads fine. Of course the same is true if there are no spaces at all.

The URL opens without issues in Xamarin.Forms Webview and in every major web browser, so definitely that's a MAUI related bug.

Steps to Reproduce

A leading or random (not trailing) space (%20) in the query string of any URL passed to the Source property of a Webview will recreate the problem.

Link to public reproduction project repository

No response

Version with bug

Unknown/Other

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

I was not able test on other platforms

Affected platform versions

Android 34

Did you find any workaround?

No response

Relevant log output

No response

github-actions[bot] commented 1 month ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

CostasAthan commented 1 month ago

Here you go: https://github.com/CostasAthan/MauiWebViewSpaceBug

What I found out is that string concatenation creates the problem.

If I alter the code and I give a hard coded query then the WebView loads the site: myWebView.Source = "https://example.com/?q=Test Test"

If I get the query string from the Entry then the issue arises: myWebView.Source = "https://example.com/?q=" + query.Text, with query.Text being equal with Test Test.

In Xamarin.Forms the concatenation worked OK.

ninachen03 commented 1 month ago

I can repro this issue at Android platform on the latest 17.11.0 Preview 5.0 (8.0.70 & 8.0.61) image

jfversluis commented 1 month ago

Looks like we need to apply some URL encoding on our side to make this easier?

CostasAthan commented 1 month ago

@jfversluis

As far as I understand that's done automatically.

If you type "Test Test" in the Entry and hit the "Search" button, then the error you are going to get gives the following URL:

file:///android_asset/https://example.com/?q=Test%20Test

I guess the "%20" in the URL of the error message means the URL encoding has already been applied.

jfversluis commented 1 month ago

Oh I see, so it seems to add the file:///android_asset in front there? is that the problem here?

Also it seems you forgot to add the code that works 😄

CostasAthan commented 1 month ago

I guess it probably is. Isn't file:///android_asset used only for local resources? If yes, then it shouldn't be there when trying to access a webpage.

As for the code it works, I tried to recreate that version, but for some reason I failed. I'm not sure what changed. As far as I remember I achieved to load the website if I didn't use concatenation. I can't do it anymore, so for now there is no code that works.

kubaflo commented 1 month ago

@jfversluis here it is: https://github.com/dotnet/maui/pull/24002 The question remains whether devs should handle it on their side or maybe MAUI should make this easier like in the PR I created 🤔

CostasAthan commented 1 week ago

@jfversluis here it is: #24002 The question remains whether devs should handle it on their side or maybe MAUI should make this easier like in the PR I created 🤔

Probably MAUI should handle it. Xamarin.Forms used to do it, so I don't see why MAUI shouldn't.

Furthermore, the error shown is misleading:

file:///android_asset/https://example.com/?q=Test%20Test

The "%20%" character in the error message makes it to appear that URL encoding has already been applied automatically and the space character has been already substituted.