【问题标题】:Should UseHsts apply headers to static files added via UseStaticFilesUseHsts 是否应该将标头应用于通过 UseStaticFiles 添加的静态文件
【发布时间】:2021-10-13 14:58:52
【问题描述】:

我们正在运行一个 .net 5 网络应用程序。

我们在启动时在我们的应用程序Configure() 中同时使用UseStaticFilesUseHsts

我可以访问我的静态文件。

我所有的 API/动态 Web 端点都有 HSTS 标头。

我的静态文件没有 HSTS 标头。

我们在 SPA 应用程序中使用了一些静态 HTML 文件,并且我们运行的笔测试在扫描时会触发它的漏洞。

如何配置我的静态文件以使用 HSTS 标头。

// Simplified example
public void Configure(
    IApplicationBuilder app,
    IHostApplicationLifetime appLifetime
)
{
    app.UseStaticFiles();
    app.UseHsts();
    app.UseRouting();
    app.UseCors();
    app.UseAuthentication();

    app.UseEndpoints(endpoints => {
        endpoints.MapControllers();
        endpoints.MapHealthChecks("/healthcheck");
    });
}

基于@gjhommersom awnser 的更新

像这样配置应用程序,而不是将 cors 添加到标题中。

但是仍然缺少严格的传输安全标头:(

// Simplified example
public void Configure(
    IApplicationBuilder app,
    IHostApplicationLifetime appLifetime
)
{
    app.UseHsts();
    app.UseRouting();
    app.UseCors();
    app.UseAuthentication();

    app.UseEndpoints(endpoints => {
        endpoints.MapControllers();
        endpoints.MapHealthChecks("/healthcheck");
    });
    app.UseStaticFiles();
}

【问题讨论】:

  • app.UseHsts();后面加app.UseHttpsRedirection();?我发现this high-vote answer 说使用hsts 使http 请求自动切换到https。所以我认为您需要添加该代码行。

标签: c# .net asp.net-core .net-5 hsts


【解决方案1】:

据我所知,设置应用程序的顺序很重要。你有没有试过在UseStaticFiles()之前打电话给UseHsts()

原因是静态文件中间件在 hsts 中间件可以进行重定向之前返回一个文件。

类似问题:Why does order between UseStaticFiles and UseDefaultFiles matter?

【讨论】:

  • 一个可靠的想法,但是改变它并不能解决问题,我更新了我的问题以包含你的想法。
猜你喜欢
  • 1970-01-01
  • 2014-07-21
  • 2017-05-31
  • 2013-12-15
  • 2021-03-26
  • 2013-03-20
  • 2011-08-30
  • 1970-01-01
  • 2013-07-04
相关资源
最近更新 更多