Azure / Azure/azure-functions-host

X-Forwarded-Host header missing in dotnet 8 isolated

Open
#10,253 7 comments 5 reactions 1 assignee Claimed by @fabiocav View on GitHub
Dominant language
C#
Stars
2k
Forks
482
Avg merge
2d 12h
Merged PRs (30d)
38

Description

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

```csharp
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();
})
.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](https://github.com/Azure/azure-functions-openapi-extension/issues/634)

> 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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.