Skip to content

Commit 8f36397

Browse files
CopilotJerryNixon
andcommitted
Fix Location header to respect X-Forwarded-Proto and X-Forwarded-Host headers
Updated SqlMutationEngine.cs and SqlResponseHelpers.cs to use the SqlPaginationUtil.ResolveRequestScheme and ResolveRequestHost methods when constructing Location headers. This ensures that when behind a reverse proxy (like Azure API Management), the Location header uses the original client request scheme (HTTPS) instead of the internal connection scheme (HTTP). Co-authored-by: JerryNixon <1749983+JerryNixon@users.noreply.github.com>
1 parent b9eea70 commit 8f36397

3 files changed

Lines changed: 14 additions & 8 deletions

File tree

src/Core/Resolvers/SqlMutationEngine.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,12 @@ await queryExecutor.ExecuteQueryAsync(
397397
case EntityActionOperation.Insert:
398398

399399
HttpContext httpContext = GetHttpContext();
400+
// Use scheme/host from X-Forwarded-* headers if present, else fallback to request values
401+
string scheme = SqlPaginationUtil.ResolveRequestScheme(httpContext.Request);
402+
string host = SqlPaginationUtil.ResolveRequestHost(httpContext.Request);
400403
string locationHeaderURL = UriHelper.BuildAbsolute(
401-
scheme: httpContext.Request.Scheme,
402-
host: httpContext.Request.Host,
404+
scheme: scheme,
405+
host: new HostString(host),
403406
pathBase: GetBaseRouteFromConfig(_runtimeConfigProvider.GetConfig()),
404407
path: httpContext.Request.Path);
405408

src/Core/Resolvers/SqlPaginationUtil.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ public static string FormatQueryString(NameValueCollection? queryStringParameter
756756
/// <param name="req">The HTTP request.</param>
757757
/// <returns>The scheme string ("http" or "https").</returns>
758758
/// <exception cref="DataApiBuilderException">Thrown when client explicitly sets an invalid scheme.</exception>
759-
private static string ResolveRequestScheme(HttpRequest req)
759+
internal static string ResolveRequestScheme(HttpRequest req)
760760
{
761761
string? rawScheme = req.Headers["X-Forwarded-Proto"].FirstOrDefault();
762762
string? normalized = rawScheme?.Trim().ToLowerInvariant();
@@ -780,7 +780,7 @@ private static string ResolveRequestScheme(HttpRequest req)
780780
/// <param name="req">The HTTP request.</param>
781781
/// <returns>The host string.</returns>
782782
/// <exception cref="DataApiBuilderException">Thrown when client explicitly sets an invalid host.</exception>
783-
private static string ResolveRequestHost(HttpRequest req)
783+
internal static string ResolveRequestHost(HttpRequest req)
784784
{
785785
string? rawHost = req.Headers["X-Forwarded-Host"].FirstOrDefault();
786786
string? trimmed = rawHost?.Trim();
@@ -803,7 +803,7 @@ private static string ResolveRequestHost(HttpRequest req)
803803
/// </summary>
804804
/// <param name="scheme">Scheme, e.g., "http" or "https".</param>
805805
/// <returns>True if valid, otherwise false.</returns>
806-
private static bool IsValidScheme(string? scheme)
806+
internal static bool IsValidScheme(string? scheme)
807807
{
808808
return scheme is "http" or "https";
809809
}
@@ -813,7 +813,7 @@ private static bool IsValidScheme(string? scheme)
813813
/// </summary>
814814
/// <param name="host">The host name (with optional port).</param>
815815
/// <returns>True if valid, otherwise false.</returns>
816-
private static bool IsValidHost(string? host)
816+
internal static bool IsValidHost(string? host)
817817
{
818818
if (string.IsNullOrWhiteSpace(host))
819819
{

src/Core/Resolvers/SqlResponseHelpers.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,12 @@ HttpContext httpContext
381381
// The third part is the computed primary key route.
382382
if (operationType is EntityActionOperation.Insert && !string.IsNullOrEmpty(primaryKeyRoute))
383383
{
384+
// Use scheme/host from X-Forwarded-* headers if present, else fallback to request values
385+
string scheme = SqlPaginationUtil.ResolveRequestScheme(httpContext.Request);
386+
string host = SqlPaginationUtil.ResolveRequestHost(httpContext.Request);
384387
locationHeaderURL = UriHelper.BuildAbsolute(
385-
scheme: httpContext.Request.Scheme,
386-
host: httpContext.Request.Host,
388+
scheme: scheme,
389+
host: new HostString(host),
387390
pathBase: baseRoute,
388391
path: httpContext.Request.Path);
389392

0 commit comments

Comments
 (0)