http - CORP 使用 CSP 沙箱集阻止明显同源请求

标签 http content-security-policy cross-origin-embedder-policy cross-origin-resource-policy cross-origin-opener-policy

想象一个网站有两个文档:index.htmltest.jpg,都位于根目录。 index.html 具有以下内容。

<!DOCTYPE html>
<html lang=en>
        <head>
                <meta charset=utf-8>
                <meta content="text/html; charset=utf-8" http-equiv=content-type>
                <title>Test</title>
        </head>
        <body>
                <p>Hello.</p>
                <img src="/test.jpg">
        </body>
</html>

如果没有安全 header ,则效果很好。但是,在 index.html 上使用这些 header :

Cross-Origin-Embedder-Policy: require-corp
Cross-Origin-Opener-Policy: same-origin

以及 test.jpg 上的此 header :

Cross-Origin-Resource-Policy: same-origin

test.jpg 将不再由 Chrome 98 加载。它在控制台中报告 net::ERR_BLOCKED_BY_RESPONSE.NotSameOrigin 200

将以下 header 添加到 index.html 也会导致该图像被 Firefox 97 拦截:

Content-Security-Policy: default-src 'none'; style-src 'self'; img-src 'self'; prefetch-src 'self'; frame-ancestors 'none'; form-action 'none'; upgrade-insecure-requests; block-all-mixed-content; disown-opener; sandbox; base-uri 'none'

(我知道有很多指令,但我试图将它们分成重要的部分,但无法弄清楚。)

这里发生了什么? https://example.comhttps://example.com/test.jpg 是同一个来源,不是吗?显式导航到 https://example.com/index.html 也会阻止 test.jpg 的加载。我对同源的理解缺少什么?为什么 Chrome 和 Firefox 的处理方式存在差异?为什么添加 CSP 会导致 Firefox 开始阻止对 test.jpg 的请求?

如果我错过了什么,这里是每个请求的完整 header 集(来自 cURL):

> GET / HTTP/2
> Host: example.com
> user-agent: curl/7.77.0
> accept: */*
>
< HTTP/2 200
< date: Wed, 16 Feb 2022 04:56:43 GMT
< server: Apache
< via: e3s
< last-modified: Wed, 16 Feb 2022 04:43:18 GMT
< content-length: 226
< x-content-type-options: nosniff
< referrer-policy: same-origin
< content-security-policy: default-src 'none'; style-src 'self'; img-src 'self'; prefetch-src 'self'; frame-ancestors 'none'; form-action 'none'; upgrade-insecure-requests; block-all-mixed-content; disown-opener; sandbox; base-uri 'none'
< cross-origin-embedder-policy: require-corp
< cross-origin-opener-policy: same-origin
< etag: "e2-5d81b49ddfad8"
< accept-ranges: bytes
< vary: Accept-Encoding
< content-type: text/html; charset=UTF-8
<
> GET /test.jpg HTTP/2
> Host: example.com
> user-agent: curl/7.77.0
> accept: */*
>
< HTTP/2 200
< date: Wed, 16 Feb 2022 04:52:19 GMT
< server: Apache
< x-content-type-options: nosniff
< referrer-policy: same-origin
< cross-origin-resource-policy: same-origin
< last-modified: Wed, 16 Feb 2022 04:22:59 GMT
< etag: "18692-5d81b013bbe82"
< accept-ranges: bytes
< content-length: 99986
< cache-control: public, max-age=31557600, immutable
< age: 336
< via: e2s
< content-type: image/jpeg
<

最佳答案

我没有尝试重现,但从阅读本文来看,当您对文档进行沙箱处理时,Firefox 会开始阻止,这意味着它具有不透明的来源,因此图像将显示跨源。

对于 Chrome,沙箱是否也以某种方式发挥作用?

关于http - CORP 使用 CSP 沙箱集阻止明显同源请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71136656/

相关文章:

http - Varnish 抛出 503 - 'http format error' 以获得 301 后端响应

content-security-policy - 具有内联样式的 svg 的内容安全策略

azure-ad-b2c - 登录 Azure B2C 页面上的内容安全策略错误

javascript - 如何使我的网站与即将到来的 SharedArrayBuffer 跨域隔离更改兼容

javascript - 在跨源隔离站点中使用 Youtube iframe api

javascript - react 错误 : "SharedArrayBuffer is not defined" in Firefox

rest - 是否可以执行 put-redirect-get 操作?

http - Go lang 捕获带超时的重定向 url 和状态代码

http - 在 Go 中,如何有效地将流式 http 响应主体写入文件中的查找位置?

ios - 内容安全策略破坏了 iOS 客户端的站点