在 Firefox 上获取 JSON 时,favicon.ico 被 CSP 阻止

dav*_*hew 5 java firefox spring spring-mvc content-security-policy

我构建了一个简单的 Spring Boot Rest 控制器,它除了返回自定义 Java 对象 - 数据之外什么也不做。一切都编译并正常运行。当我从端点获取数据时,我得到了预期的数据。

\n

然而,当在 Firefox 上使用“ Inspect Element ”查看底层时,我看到由于内容安全策略 (CSP)导致的错误。内容安全策略错误如下:

\n

错误消息的屏幕截图

\n

“内容安全策略:页面\xe2\x80\x99s 设置阻止加载 http://localhost:8081/favicon.ico (\xe2\x80\x9cdefault-src\xe2\x80\x9d) 上的资源。”

\n

我尝试了几种解决方案,但均无济于事。

\n
    \n
  • 我尝试通过 application.properties 禁用该图标,但这似乎没有任何效果。
  • \n
  • 我创建了一个名为“favicon.ico”的图标并将其放置在正确的目录中。令人烦恼的是,这个页面仍然抛出错误,同时我的所有其他页面都开始获取图标。
  • \n
  • 我尝试了许多标头排列,包括将 Content-Security-Policy 标头设置为默认 src self。没有一个起作用,尽管这可能是问题的根源,因为似乎有很多我没有完全掌握的活动部件。
  • \n
  • 我尝试为“/favicon.ico”创建一个 GET 端点,但这似乎根本没有完成任何事情。\n
      \n
    • 此时我已将图标添加到我的目录中,因此当我尝试访问端点时,它只是向我发送了图标的图像,该图标也显示在浏览器顶部的选项卡中,并且没有错误在日志中。
    • \n
    \n
  • \n
  • 我试图摆弄 WebSecurityConfigurerAdapter,但这很快就失控了,坦率地说,其中很多都没有意义。
  • \n
\n

这是我的文件。

\n

应用程序属性 = application.properties

\n
spring.mvc.favicon.enabled=false\n\n
Run Code Online (Sandbox Code Playgroud)\n

主文件 - DemoApplication

\n
import org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\n\n@SpringBootApplication\npublic class DemoApplication {\n\n   public static void main(String[] args) {\n      System.getProperties().put( "server.port", 8081);\n      SpringApplication.run(DemoApplication.class, args);\n   }\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n

休息控制器=数据控制器

\n
import org.springframework.web.bind.annotation.GetMapping;\nimport org.springframework.web.bind.annotation.RestController;\n\n@RestController\npublic class DataController\n{\n\n   @GetMapping("/data")\n   public Data data()\n   {\n   \n      return new Data(123, "abc");\n   \n   }\n  \n}\n
Run Code Online (Sandbox Code Playgroud)\n

返回类型 = 数据

\n
public class Data\n{\n\n    private final long id;\n    private final String data;\n\n    public Data(long id, String data) { this.data = data; }\n\n    public long getId() { return this.id; }\n    public String getData() { return this.data; }\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n

dav*_*hew 7

经过一番调试后,我发现该问题似乎是Firefox 特有的,并且仅适用于返回 JSON 对象的端点

例如,如果我构建了一个仅返回字符串的端点,则 Firefox 将返回该字符串,并且图标将位于顶部的选项卡中。

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DataController
{

   @GetMapping("/data")
   public Data data()
   {
   
      return new Data(123, "abc");
   
   }
  
   @GetMapping("/abc123")
   public String abc123()
   {
   
      return "abc123";
   
   }
   
}
Run Code Online (Sandbox Code Playgroud)

有错误的 JSON 响应类型的屏幕截图 没有错误的字符串响应类型的屏幕截图

我运行了比这更多的示例,但这演示了 String 作为响应类型如何在 Firefox 上不会引发错误,但 JSON 作为响应类型会引发错误。

在其他浏览器上尝试这两个端点时,JSON 或 String 似乎都没有错误。

然后我意识到 - 与其他浏览器不同,Firefox 有一个内置的JSON Viewer。这意味着,如果整个页面接收到的数据纯粹是 JSON,那么 Firefox 将以完全不同的方式呈现该页面。Firefox 将使用一些奇特的 UI 来组织 JSON,以便于阅读/解析,而不是仅仅将纯 JSON 字符串吐出到页面上。

因此,作为最终测试,我关闭了 JSON Viewer,然后重新加载页面 - 一切都按预期工作。

没有错误的 JSON 响应类型的屏幕截图

我还要补充一点 -我真的不认为这是 Firefox 的一个错误- @granty 指出Mozilla 自己正在调查这个问题。这只发生在整个响应都是 JSON 的端点上。

如果从大局来看,返回纯 JSON 的端点实际上只是数据流,而不是由普通用户使用。那么你真的需要一个图标吗?哈哈,可能不是。

  • Firefox BUG:[favicon.ico 被 JSON 查看器中的内容安全策略阻止](https://bugzilla.mozilla.org/show_bug.cgi?id=1698115) (3认同)