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

X-Forwarded-Host header missing in dotnet 8 isolated #10253

Open
paulverbeke opened this issue Jun 27, 2024 · 2 comments
Open

X-Forwarded-Host header missing in dotnet 8 isolated #10253

paulverbeke opened this issue Jun 27, 2024 · 2 comments
Assignees

Comments

@paulverbeke
Copy link

paulverbeke commented Jun 27, 2024

The X-Forwarded-Host header provided by Azure Front Door is missing in a fresh new function in dotnet 8 isolated.
Is working fine in dotnet 6 in-process but stops working when migrating to 8 isolated.

Investigative information

Please provide the following:

  • Timestamp:
  • Function App version: 4
  • Function App name:
  • Function name(s) (as appropriate):
  • Invocation ID:
  • Region: France Central

Repro steps

  1. create an Azure Function in dotnet 8 isolated
  2. in a function testforwardheader, output the value of the header X-Forwarded-Host req.Headers.TryGetValue("X-Forwarded-Host", out StringValues value)
  3. publish the function behind a Front Door with redirect rules to this app from the url subdomain.myfrontdoordomain.com/myfunction.
  4. call the function using subdomain.myfrontdoordomain.com/myfunction/api/testforwardheader

Expected behavior

in step 4 you should get the host requested by client, so subdomain.myfrontdoordomain.com

Actual behavior

in step 4 you get an empty string

Known workarounds

Someone found a workaround. They were able to find and restore the value from the bindingContext.BindingData using custom middleware.

Workaround
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Middleware;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;

var builder = new HostBuilder();

var host = builder
    .ConfigureFunctionsWebApplication(
        app =>
        {
            app.UseMiddleware<ForwardedForHeaderMiddleware>();
        })
    .Build();

await host.RunAsync();

public class ForwardedForHeaderMiddleware : IFunctionsWorkerMiddleware
{
    public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
    {
        await CorrectForwardedForAsync(context);

        await next(context);
    }

    private static async Task CorrectForwardedForAsync(FunctionContext context)
    {
        var val = GetForwardedFor(context);
        var req = await context.GetHttpRequestDataAsync();
        req?.Headers.TryAddWithoutValidation(ForwardedHeadersDefaults.XForwardedForHeaderName, val);
    }

    private static string? GetForwardedFor(FunctionContext context)
    {
        var hdr = context.BindingContext.BindingData["Headers"];
        dynamic obj = JsonConvert.DeserializeObject(hdr.ToString());

        var val = obj[ForwardedHeadersDefaults.XForwardedForHeaderName]?.Value as string;
        return val;
    }
}

Related information

written in C#

Someone else had this problem

I know that the Front Door will send X-Forwarded-Host and X-Forwarded-Proto which we were able to use with the older Swashbuckle library which we used for In-Process Function Apps.

@fabiocav
Copy link
Member

@paulverbeke are you using the ASP.NET Core integration for your tests? In the HTTP proxying mode, all headers should be forwarded and will be processed by the worker.

@paulverbeke
Copy link
Author

paulverbeke commented Jul 1, 2024

@fabiocav thanks for getting back quickly. Yes we are using ASP.NET Core integration. But I couldn't find anything online regarding "HTTP proxying mode". Is there something I need to enable or specify somewhere ?

EDIT: I just tested the starting Azure Functions template without ASP.NET Core integration (with HttpRequestData/HttpResponseData) and the header is available.
Is it expected behavior that ASP.NET Core integration is causing header to be dropped ? If so why the default Visual Studio template is showing an example function with ASP.NET Core integration without this drawback be documented somewhere ?

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

No branches or pull requests

2 participants