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

InvalidOperationException occurs on onsubmit event when using NavigateTo method with non-ASCII characters on Blazor SSR render mode. #52438

Closed
1 task done
runceel opened this issue Nov 29, 2023 · 5 comments · Fixed by #53623
Assignees
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug.
Milestone

Comments

@runceel
Copy link

runceel commented Nov 29, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

InvalidOperationException occurs on onsubmit event when using NavigateTo method with non-ASCII characters on Blazor SSR render mode. Only ASCII case works fine.

// OK
NavigationManager.NavigateTo("navtarget?text=HelloWorld");

// Error
NavigationManager.NavigateTo("navtarget?text=😀");

The error page:
image

The stack trace:

InvalidOperationException: Invalid non-ASCII or control character in header: 0xD83D
Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.ThrowInvalidHeaderCharacter(char ch)
Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.ValidateHeaderValueCharacters(string headerCharacters, bool requireAscii)
Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.ValidateHeaderValueCharacters(string headerName, StringValues headerValues, Func<string, Encoding> encodingSelector)
Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpResponseHeaders.Microsoft.AspNetCore.Http.IHeaderDictionary.set_Location(StringValues value)
Microsoft.AspNetCore.Components.Endpoints.EndpointHtmlRenderer.HandleNavigationException(HttpContext httpContext, NavigationException navigationException)
Microsoft.AspNetCore.Components.Endpoints.RazorComponentEndpointInvoker.RenderComponentCore(HttpContext context)
Microsoft.AspNetCore.Components.Endpoints.RazorComponentEndpointInvoker.RenderComponentCore(HttpContext context)
Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext+<>c+<<InvokeAsync>b__10_0>d.MoveNext()
Microsoft.AspNetCore.Antiforgery.Internal.AntiforgeryMiddleware.InvokeAwaited(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

Expected Behavior

Navigating works without error.

Steps To Reproduce

Scratch

  1. Create a Blazor Web App project.
  • Authentication type: None
  • Interactive render mode: None
  • Interactivity location: Per page/component
  • Uncheck Include sample pages
    image
  1. Edit Components/Pages/Home.razor like below:
@page "/"
@inject NavigationManager NavigationManager

<PageTitle>Home</PageTitle>

<form method="post" @formname="form1" @onsubmit="Navigate">
    <AntiforgeryToken />
    <input type="submit" />
</form>

@code {
    private void Navigate() => 
        NavigationManager.NavigateTo("somepage?text=😀");
}
  1. Launch the project.
  2. Click the submit button.

Use a repro project

I created a sample project for repro.
https://github.com/runceel/NoAsciiError

  1. Clone the repository.
  2. Open NoAsciiError.sln
  3. Launch the project
  4. Click NavigationManager.NavigateTo("navtarget?text=😀") button.

Exceptions (if any)

No response

.NET Version

8.0.100

Anything else?

I faced the error when implement a search page like Bing and Google.
Those pages have a textbox and a submit button. And then search result page has search key words in query parameters.
If search key worlds are non ASCII characters such as Emoji and Japanese and other languages, the error occurs.
I guess it is a serious blocker to use Blazor SSR mode.

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Nov 29, 2023
@runceel
Copy link
Author

runceel commented Nov 29, 2023

I tested it on additional platforms.
The result is:

Server OK/NG Comment
Kestrel NG Local dev env.
IIS Express OK Local dev env.
App Service on Windows NG No error. But 😀 was replaced to ??.
App Service on Linux NG

@javiercn
Copy link
Member

@runceel thanks for contacting us.

Special characters need to be encoded, as they are not allowed in the Header. Likely related to #4919

@runceel
Copy link
Author

runceel commented Nov 29, 2023

@javiercn
Even if I escape the URL like following code, same error occured.

    private void NavigateToWithNonASCIIParameter() => 
        NavigationManager.NavigateTo($"navtarget?text={Uri.EscapeDataString("😀")}");

@runceel
Copy link
Author

runceel commented Nov 29, 2023

When I noticed this error, I used NavigationManager.NavigateTo(NavigationManager.GetUriWithQueryParameter("text", "😀"));.
In GetUriWithQueryParameter, all parameters were escaped. But the error occured.

@runceel
Copy link
Author

runceel commented Nov 30, 2023

@javiercn I apologize for the consecutive replies.
I tested it on ASP.NET Core Razor Pages, and same error occured when non-encoded URL, and then encoded URL case works fine.

@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<form method="post">
    <input type="submit" />
</form>
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace WebApplication29.Pages;
public class IndexModel : PageModel
{
    private readonly ILogger<IndexModel> _logger;

    public IndexModel(ILogger<IndexModel> logger)
    {
        _logger = logger;
    }

    public void OnGet()
    {

    }

    public IActionResult OnPost()
    {
        // NG
        // return Redirect("next?text=😀");

        // OK
        return Redirect($"next?text={Uri.EscapeDataString("😀")}");
    }
}

However, this issue occurs even if I use encoded URL. There is not workaround. So I guess it is blazor specific issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug.
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

5 participants
@runceel @javiercn @MackinnonBuck @mkArtakMSFT and others