Azure / Azure/static-web-apps

SWA API Proxy is not passing X-Forwarded-Host to linked ContainerApp when proxying a direct URL

Open
#1,039 4 comments 1 reaction 0 assignees View on GitHub
Dominant language
No language data
Stars
346
Forks
67
PR merge metrics
No merged PRs in 30d

Description

I have a static web app that is API linked to an Azure Container App which is hosting an ASP.NET Core web app.

When I redirect to an authentiction route in my app like https://[swacustomdomain]/api/auth/login/[provider] it routes to the backend correctly, but because there is no X-FORWARDED-HOST it is using is my container app host, not my SWA host for redirects so I end up with a redirect_url in the authentication request to the external provider (e.g. AAD) that is using the container app host not the SWA host as well as any other redirects I make.

If I then, in my ASP.NET app do the following, passing in the headers 'x-appservice-proto' and 'disguised-host' (this is what I can see in the request headers from logging them out in the container app):

```
builder.Services.Configure(options =>
{
// If we have specific headers configured, user them
var config = builder.Configuration.GetSection("ForwardHeaderNames");

var protocolForwardHeader = config["Protocol"];
if (!string.IsNullOrEmpty(protocolForwardHeader))
{
options.ForwardedProtoHeaderName= protocolForwardHeader;
}

var hostForwardHeader = config["Host"];
if (!string.IsNullOrEmpty(hostForwardHeader))
{
options.ForwardedHostHeaderName = hostForwardHeader;
}

options.ForwardedHeaders = ForwardedHeaders.XForwardedHost | ForwardedHeaders.XForwardedProto;
options.ForwardLimit = 2;
options.KnownNetworks.Clear();
options.KnownProxies.Clear();
});
```

I then get the correct redirect_uri passed out to the external provider, but all the cookies the authentication process is setting or I am trying to set, pook to be being set for the container app host, not the SWA host.

```
System.Exception: An error was encountered while handling the remote login.
---] System.Exception: Correlation failed.
--- End of inner exception stack trace ---
at Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler`1.HandleRequestAsync()
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Serilog.AspNetCore.RequestLoggingMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
```

**Expected behavior**

This is all seeming to be much harder that I would expect to get it to work.

The expected behaviour is that the X-FORWARDED-HOST header is passed through to be used correctly by ASP.NET Core

**Additional context**

Container App Log Stream:
```
2023-01-16T22:19:28.805733753Z 2023-01-16 22:19:28.805 Debug - Connection id "0HMNO1PL2F40U" accepted.
2023-01-16T22:19:28.806111385Z 2023-01-16 22:19:28.805 Debug - Connection id "0HMNO1PL2F40U" started.
2023-01-16T22:19:28.806532959Z 2023-01-16 22:19:28.806 Information - Request starting HTTP/1.1 GET http://[ca_url].azurecontainerapps.io/api/auth/login/Microsoft - 0
2023-01-16T22:19:28.806549450Z 2023-01-16 22:19:28.806 Debug - No response compression available, the Accept-Encoding header is missing or invalid.
2023-01-16T22:19:28.806556252Z 2023-01-16 22:19:28.806 Debug - The request path /api/auth/login/Microsoft does not match a supported file type
2023-01-16T22:19:28.806560440Z 2023-01-16 22:19:28.806 Debug - 1 candidate(s) found for the request path '/api/auth/login/Microsoft'
2023-01-16T22:19:28.807508144Z 2023-01-16 22:19:28.806 Debug - Endpoint 'ai.App.Api.Endpoints.Endpoints.AuthEndpoints.ExternalLogin (ai.app.api.endpoints)' with route pattern 'api/auth/login/{provider}' is valid for the request path '/api/auth/login/Microsoft'
2023-01-16T22:19:28.808093222Z 2023-01-16 22:19:28.806 Debug - Request matched endpoint 'ai.App.Api.Endpoints.Endpoints.AuthEndpoints.ExternalLogin (ai.app.api.endpoints)'
2023-01-16T22:19:28.808362533Z 2023-01-16 22:19:28.806 Debug - Static files was skipped as the request already matched an endpoint.
2023-01-16T22:19:28.808881448Z 2023-01-16 22:19:28.807 Debug - AuthenticationScheme: Cookies was not authenticated.
2023-01-16T22:19:28.809150378Z 2023-01-16 22:19:28.807 Information - Executing endpoint 'ai.App.Api.Endpoints.Endpoints.AuthEndpoints.ExternalLogin (ai.app.api.endpoints)'
2023-01-16T22:19:28.809169814Z 2023-01-16 22:19:28.807 Information - Route matched with {action = "ExternalLogin", controller = "AuthEndpoints"}. Executing controller action with signature Microsoft.AspNetCore.Http.IResult ExternalLogin(System.String) on controller ai.App.Api.Endpoints.Endpoints.AuthEndpoints (ai.app.api.endpoints).
2023-01-16T22:19:28.809175425Z 2023-01-16 22:19:28.807 Debug - Execution plan of authorization filters (in the following order): ["None"]
2023-01-16T22:19:28.809179553Z 2023-01-16 22:19:28.807 Debug - Execution plan of resource filters (in the following order): ["None"]
2023-01-16T22:19:28.809184251Z 2023-01-16 22:19:28.807 Debug - Execution plan of action filters (in the following order): ["Microsoft.AspNetCore.Mvc.ModelBinding.UnsupportedContentTypeFilter (Order: -3000)", "Microsoft.AspNetCore.Mvc.Infrastructure.ModelStateInvalidFilter (Order: -2000)"]
2023-01-16T22:19:28.809188249Z 2023-01-16 22:19:28.807 Debug - Execution plan of exception filters (in the following order): ["None"]
2023-01-16T22:19:28.809198498Z 2023-01-16 22:19:28.807 Debug - Execution plan of result filters (in the following order): ["Microsoft.AspNetCore.Mvc.Infrastructure.ClientErrorResultFilter (Order: -2000)"]
2023-01-16T22:19:28.809202946Z 2023-01-16 22:19:28.807 Debug - Executing controller factory for controller ai.App.Api.Endpoints.Endpoints.AuthEndpoints (ai.app.api.endpoints)
2023-01-16T22:19:28.809206513Z 2023-01-16 22:19:28.807 Debug - Executed controller factory for controller ai.App.Api.Endpoints.Endpoints.AuthEndpoints (ai.app.api.endpoints)
2023-01-16T22:19:28.809210400Z 2023-01-16 22:19:28.807 Debug - Attempting to bind parameter 'provider' of type 'System.String' ...
2023-01-16T22:19:28.809214227Z 2023-01-16 22:19:28.807 Debug - Attempting to bind parameter 'provider' of type 'System.String' using the name 'provider' in request data ...
2023-01-16T22:19:28.809218615Z 2023-01-16 22:19:28.807 Debug - Done attempting to bind parameter 'provider' of type 'System.String'.
2023-01-16T22:19:28.809222292Z 2023-01-16 22:19:28.807 Debug - Done attempting to bind parameter 'provider' of type 'System.String'.
2023-01-16T22:19:28.809226710Z 2023-01-16 22:19:28.807 Debug - Attempting to validate the bound parameter 'provider' of type 'System.String' ...
2023-01-16T22:19:28.809230617Z 2023-01-16 22:19:28.807 Debug - Done attempting to validate the bound parameter 'provider' of type 'System.String'.
2023-01-16T22:19:28.809234364Z 2023-01-16 22:19:28.807 Information - Executing action method ai.App.Api.Endpoints.Endpoints.AuthEndpoints.ExternalLogin (ai.app.api.endpoints) - Validation state: Valid
2023-01-16T22:19:28.809238542Z 2023-01-16 22:19:28.807 Debug - [ExternalLogin] ] Client URL: https://[swa_url]
2023-01-16T22:19:28.809242570Z 2023-01-16 22:19:28.807 Debug - [ExternalLogin] ] Redirect Url: https://[swa_url]/api/auth/signin/Microsoft
2023-01-16T22:19:28.809247369Z 2023-01-16 22:19:28.807 Information - Executed action method ai.App.Api.Endpoints.Endpoints.AuthEndpoints.ExternalLogin (ai.app.api.endpoints), returned result Microsoft.AspNetCore.Mvc.HttpActionResult in 0.0442ms.
2023-01-16T22:19:28.809251476Z 2023-01-16 22:19:28.807 Information - Executing ChallengeResult with authentication schemes (["Microsoft"]).
2023-01-16T22:19:28.809674583Z 2023-01-16 22:19:28.808 Debug - HandleChallenge with Location: https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=cb85a04d-1754-4a21-8396-565f42abf4dd&response_type=code&redirect_uri=https%3A%2F%2F[SWAHOST]%2Fapi%2Fauth%2Fsignin-microsoft&scope=https%3A%2F%2Fgraph.microsoft.com%2Fuser.read&code_challenge=pfmxggldRgT7nqbge88f-bwm3ivUjiSrdw9SiFAnl0E&code_challenge_method=S256&state=CfDJ8PThRPk0fLVHnuW-wqx7dL51GAQpCn35ld7D4LOeDMSVBR-kCPPsbeAjs4F3vEZ-33mU8ESHaGpSt2bcldcNbpj4fIOmhe1jCoefdBhhRu6FjXIvU7MlazbfJZ2RZg2dhZDM2rgCu30ay1VVE0JH_o935u7rNwkftp8lvhx4Bl-rqBEeG-ALDmFmrpPAORzRKM7lSZ48hKltr-Q0hcN_yVJwy9sf-l_4UGOcr2Lf8glu2CHKNRoGk6AtDY3I56xaCmAj58HEwbx2Y1Gr1BzxW2fIm7B5GwVrF7_xALGnm6pDLXN-smCQeizlnWPLWYE80zFnZgMShPnMXagkKRllo-CCPCGZtFHhXOH-z5TiF848; and Set-Cookie: .AspNetCore.Correlation.LCbKxzwLbPD39bXbCXm3xnRD4SSpJ9oeQZhdWZRd7nw=N; expires=Mon, 16 Jan 2023 22:34:28 GMT; path=/api/auth/signin-microsoft; secure; samesite=none; httponly.
2023-01-16T22:19:28.809970003Z 2023-01-16 22:19:28.808 Information - AuthenticationScheme: Microsoft was challenged.
2023-01-16T22:19:28.809977196Z 2023-01-16 22:19:28.808 Information - Executed action ai.App.Api.Endpoints.Endpoints.AuthEndpoints.ExternalLogin (ai.app.api.endpoints) in 0.9118ms
2023-01-16T22:19:28.809981183Z 2023-01-16 22:19:28.808 Information - Executed endpoint 'ai.App.Api.Endpoints.Endpoints.AuthEndpoints.ExternalLogin (ai.app.api.endpoints)'
2023-01-16T22:19:28.809985181Z 2023-01-16 22:19:28.808 Information - HTTP GET /api/auth/login/Microsoft responded 302 in 2.1439 ms
2023-01-16T22:19:28.809988277Z 2023-01-16 22:19:28.808 Debug - Connection id "0HMNO1PL2F40U" completed keep alive response.
2023-01-16T22:19:28.809992194Z 2023-01-16 22:19:28.808 Information - Request finished HTTP/1.1 GET http://[ca_url].azurecontainerapps.io/api/auth/login/Microsoft - 0 - 302 0 - 2.4075ms
[removed for brevity]
2023-01-16T22:19:50.543569987Z 2023-01-16 22:19:50.543 Information - Request starting HTTP/1.1 GET http://[ca_url].azurecontainerapps.io/api/auth/signin-microsoft?code=M.R3_BAY.34fe2b73-b897-6cf5-1ccb-9c04eefa7749&state=CfDJ8PThRPk0fLVHnuW-wqx7dL51GAQpCn35ld7D4LOeDMSVBR-kCPPsbeAjs4F3vEZ-33mU8ESHaGpSt2bcldcNbpj4fIOmhe1jCoefdBhhRu6FjXIvU7MlazbfJZ2RZg2dhZDM2rgCu30ay1VVE0JH_o935u7rNwkftp8lvhx4Bl-rqBEeG-ALDmFmrpPAORzRKM7lSZ48hKltr-Q0hcN_yVJwy9sf-l_4UGOcr2Lf8glu2CHKNRoGk6AtDY3I56xaCmAj58HEwbx2Y1Gr1BzxW2fIm7B5GwVrF7_xALGnm6pDLXN-smCQeizlnWPLWYE80zFnZgMShPnMXagkKRllo-CCPCGZtFHhXOH-z5TiF848 - 0
2023-01-16T22:19:50.543997332Z 2023-01-16 22:19:50.543 Debug - No response compression available, the Accept-Encoding header is missing or invalid.
2023-01-16T22:19:50.544267475Z 2023-01-16 22:19:50.543 Debug - The request path /api/auth/signin-microsoft does not match a supported file type
2023-01-16T22:19:50.544558176Z 2023-01-16 22:19:50.543 Debug - No candidates found for the request path '/api/auth/signin-microsoft'
2023-01-16T22:19:50.544857192Z 2023-01-16 22:19:50.543 Debug - Request did not match any endpoints
2023-01-16T22:19:50.545132133Z 2023-01-16 22:19:50.543 Debug - The request path does not match the path filter
2023-01-16T22:19:50.546238681Z 2023-01-16 22:19:50.546 Warning - '.AspNetCore.Correlation.LCbKxzwLbPD39bXbCXm3xnRD4SSpJ9oeQZhdWZRd7nw' cookie not found.
2023-01-16T22:19:50.546254521Z 2023-01-16 22:19:50.546 Information - Error from RemoteAuthentication: Correlation failed..
2023-01-16T22:19:50.547234233Z 2023-01-16 22:19:50.546 Error - HTTP GET /api/auth/signin-microsoft responded 500 in 2.9298 ms
2023-01-16T22:19:50.547249441Z System.Exception: An error was encountered while handling the remote login.
2023-01-16T22:19:50.547254861Z ---] System.Exception: Correlation failed.
2023-01-16T22:19:50.547258859Z --- End of inner exception stack trace ---
2023-01-16T22:19:50.547262435Z at Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler`1.HandleRequestAsync()
2023-01-16T22:19:50.547266352Z at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
2023-01-16T22:19:50.547270200Z at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
2023-01-16T22:19:50.547297400Z at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
2023-01-16T22:19:50.547302359Z at Serilog.AspNetCore.RequestLoggingMiddleware.Invoke(HttpContext httpContext)
2023-01-16T22:19:50.547307219Z at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
2023-01-16T22:19:50.547310935Z 2023-01-16 22:19:50.546 Error - An unhandled exception has occurred while executing the request.
2023-01-16T22:19:50.547314673Z System.Exception: An error was encountered while handling the remote login.
2023-01-16T22:19:50.547318329Z ---] System.Exception: Correlation failed.
2023-01-16T22:19:50.547321796Z --- End of inner exception stack trace ---
2023-01-16T22:19:50.547325252Z at Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler`1.HandleRequestAsync()
2023-01-16T22:19:50.547328749Z at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
2023-01-16T22:19:50.547332526Z at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
2023-01-16T22:19:50.547336173Z at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
2023-01-16T22:19:50.547341152Z at Serilog.AspNetCore.RequestLoggingMiddleware.Invoke(HttpContext httpContext)
2023-01-16T22:19:50.547344778Z at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
2023-01-16T22:19:50.548970910Z 2023-01-16 22:19:50.548 Debug - Connection id "0HMNO1PL2F40U" completed keep alive response.
2023-01-16T22:19:50.548984896Z 2023-01-16 22:19:50.548 Information - Request finished HTTP/1.1 GET http://[ca_url].azurecontainerapps.io/api/auth/signin-microsoft?code=M.R3_BAY.34fe2b73-b897-6cf5-1ccb-9c04eefa7749&state=CfDJ8PThRPk0fLVHnuW-wqx7dL51GAQpCn35ld7D4LOeDMSVBR-kCPPsbeAjs4F3vEZ-33mU8ESHaGpSt2bcldcNbpj4fIOmhe1jCoefdBhhRu6FjXIvU7MlazbfJZ2RZg2dhZDM2rgCu30ay1VVE0JH_o935u7rNwkftp8lvhx4Bl-rqBEeG-ALDmFmrpPAORzRKM7lSZ48hKltr-Q0hcN_yVJwy9sf-l_4UGOcr2Lf8glu2CHKNRoGk6AtDY3I56xaCmAj58HEwbx2Y1Gr1BzxW2fIm7B5GwVrF7_xALGnm6pDLXN-smCQeizlnWPLWYE80zFnZgMShPnMXagkKRllo-CCPCGZtFHhXOH-z5TiF848 - 0 - 500 - text/html;+charset=utf-8 5.0435ms

```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.