配置 "RequireHttpsAttribute" 过滤器以考虑 X-Forwarded-Proto
Configuring "RequireHttpsAttribute" filter to account for X-Forwarded-Proto
我正在 AWS elastic bean 上托管一个 ASP.NET 框架应用程序,并且我正在使用负载均衡器。
使用我当前的配置要求 https 导致无限循环,因为负载平衡器使用 http 与服务器通信。为了实现我的过滤功能,我需要考虑 X-Forwarded-Proto Header,根据我的理解,它包含原始请求协议。
我知道出了什么问题,但我不明白如何使用此属性来确保安全连接。
这是我当前的配置:
public class RequireHttpsAttribute : AuthorizationFilterAttribute {
public int Port { get; set; }
public RequireHttpsAttribute()
{
Port = 443;
}
public override void OnAuthorization(HttpActionContext actionContext)
{
var request = actionContext.Request;
if (request.RequestUri.Scheme != Uri.UriSchemeHttps)
{
var response = new HttpResponseMessage();
if (request.Method == HttpMethod.Get || request.Method == HttpMethod.Head)
{
var uri = new UriBuilder(request.RequestUri);
uri.Scheme = Uri.UriSchemeHttps;
uri.Port = this.Port;
response.StatusCode = HttpStatusCode.Found;
response.Headers.Location = uri.Uri;
}
else
{
response.StatusCode = HttpStatusCode.Forbidden;
}
actionContext.Response = response;
}
else
{
base.OnAuthorization(actionContext);
}
}
}
此过滤器配置已添加到我的 WebApiConfig。
我的问题是:
如何检查此过滤器中的 X-Forwarded-Proto?
如果您的负载均衡器正在为您处理 SSL,您会放弃对它们的限制(仅限 SSL)——正如您所注意到的,您的应用程序将始终检测http:80
(不是 https:443
)。
Do not use RequireHttpsAttribute on Web APIs that receive sensitive
information. RequireHttpsAttribute uses HTTP status codes to redirect
browsers from HTTP to HTTPS. API clients may not understand or obey
redirects from HTTP to HTTPS. Such clients may send information over
HTTP. Web APIs should either:
- Not listen on HTTP.
- Close the connection with status code 400 (Bad> Request) and not serve the request.
文档中的注释说 API 客户 may/may 不遵循您的重定向响应,因此建议您响应一些错误 (也不是完全没有)- 你应该检查 http
header 的地方。在你的情况下,你不能做第一个,因为你的应用程序将始终获得 HTTP:80(因此只有 http
header check/filter)。
在 MVC 站点中,您可以执行路由重定向(Url 重写等),但这是可行的,因为客户端是浏览器(并且知道如何处理 redirect
)。然而,API 客户端可以是 任何东西(不仅仅是浏览器)。
Update/clarification:
当然这很重要,因此您应该 仍然过滤由您的负载均衡器转发的 http header
,表明原始请求是否通过 https
或不遵循文档中的建议 - 使用适当的 http
错误代码进行响应(注释建议 400 Bad Request
)。
您不能做的是检查原始请求的实际 scheme/protocol,因为您的负载均衡器将始终转发 http:80
到您的应用程序。
TLDR:您根据负载均衡器转发的 http header 信息进行限制。
我正在 AWS elastic bean 上托管一个 ASP.NET 框架应用程序,并且我正在使用负载均衡器。
使用我当前的配置要求 https 导致无限循环,因为负载平衡器使用 http 与服务器通信。为了实现我的过滤功能,我需要考虑 X-Forwarded-Proto Header,根据我的理解,它包含原始请求协议。 我知道出了什么问题,但我不明白如何使用此属性来确保安全连接。
这是我当前的配置:
public class RequireHttpsAttribute : AuthorizationFilterAttribute {
public int Port { get; set; }
public RequireHttpsAttribute()
{
Port = 443;
}
public override void OnAuthorization(HttpActionContext actionContext)
{
var request = actionContext.Request;
if (request.RequestUri.Scheme != Uri.UriSchemeHttps)
{
var response = new HttpResponseMessage();
if (request.Method == HttpMethod.Get || request.Method == HttpMethod.Head)
{
var uri = new UriBuilder(request.RequestUri);
uri.Scheme = Uri.UriSchemeHttps;
uri.Port = this.Port;
response.StatusCode = HttpStatusCode.Found;
response.Headers.Location = uri.Uri;
}
else
{
response.StatusCode = HttpStatusCode.Forbidden;
}
actionContext.Response = response;
}
else
{
base.OnAuthorization(actionContext);
}
}
}
此过滤器配置已添加到我的 WebApiConfig。
我的问题是: 如何检查此过滤器中的 X-Forwarded-Proto?
如果您的负载均衡器正在为您处理 SSL,您会放弃对它们的限制(仅限 SSL)——正如您所注意到的,您的应用程序将始终检测http:80
(不是 https:443
)。
Do not use RequireHttpsAttribute on Web APIs that receive sensitive information. RequireHttpsAttribute uses HTTP status codes to redirect browsers from HTTP to HTTPS. API clients may not understand or obey redirects from HTTP to HTTPS. Such clients may send information over HTTP. Web APIs should either:
- Not listen on HTTP.
- Close the connection with status code 400 (Bad> Request) and not serve the request.
文档中的注释说 API 客户 may/may 不遵循您的重定向响应,因此建议您响应一些错误 (也不是完全没有)- 你应该检查 http
header 的地方。在你的情况下,你不能做第一个,因为你的应用程序将始终获得 HTTP:80(因此只有 http
header check/filter)。
在 MVC 站点中,您可以执行路由重定向(Url 重写等),但这是可行的,因为客户端是浏览器(并且知道如何处理 redirect
)。然而,API 客户端可以是 任何东西(不仅仅是浏览器)。
Update/clarification:
当然这很重要,因此您应该 仍然过滤由您的负载均衡器转发的 http header
,表明原始请求是否通过 https
或不遵循文档中的建议 - 使用适当的 http
错误代码进行响应(注释建议 400 Bad Request
)。
您不能做的是检查原始请求的实际 scheme/protocol,因为您的负载均衡器将始终转发 http:80
到您的应用程序。
TLDR:您根据负载均衡器转发的 http header 信息进行限制。