我可以使用 CSP 来限制对 https: 和 'self' 的请求吗?

Can I use CSP to limit requests to both https: AND 'self'?

我最初在我的 CSP header 中有这个:default-src 'self';,它将源限制在我的域中,但它没有说明 scheme/protocol。据我所知,如果页面本身是通过 http 加载的,这将允许通过 http 加载源。我正在使用 HSTS 和 CDN 重定向来尝试将所有流量保持在 https 上,但无论如何我都想解决这个问题。 (事实上​​ ,我试图让 this header checker 给我所有的绿色标记。) 编辑:当使用 'self' 时,srcclr 会很高兴地给出所有的绿色标记。我只需要修正一些拼写错误。

无论如何,我尝试将 CSP 指令更改为 default-src https: 'self';,但添加方案似乎允许从 any 域加载资源,只要因为它通过 https。即,如果任一规则(https 或 'self')匹配,则请求被允许。我通过在 chrome 开发工具中使用 URL 来测试这个。

有没有办法表示:

  1. 所有资源都应该通过https加载;和
  2. 资源只能来自原域名?

类似于 default-src https://'self';,但我不认为这是有效的,是吗?

一种解决方案是对域名进行硬编码,但如果可能的话,我想使用 'self'

根据https://developer.mozilla.org/en-US/docs/Web/Security/CSP/CSP_policy_directives'self'内容源关键词:

Refers to the origin from which the protected document is being served, including the same URL scheme and port number.

如果您通过 https 提供网页,则自己仅允许对同一域的 https 请求。

CSP2 规范 http://www.w3.org/TR/CSP2/#match-source-expression 说了同样的话:

If the source expression is a case insensitive match for 'self' (including the quotation marks), then:

Return does match if the origin of url matches the origin of protected resource’s URL.