首页 > 解决方案 > 同源请求会导致“访问控制允许源不匹配”错误,尽管源当然匹配。注意:具有带“沙盒”的 CSP 策略

问题描述

当CORS 及其网页的 URL 使用完全相同的 URL 时,我的 Firefox 开发控制台中仍然会收到相同的错误消息

浏览器控制台消息是:

Cross-Origin Request Blocked: \
  The Same Origin Policy disallows reading the remote resource \
  at https://egbert.net/fonts/fontawesome-webfont.woff2?v=4.7.0. \
  (Reason: CORS header ‘Access-Control-Allow-Origin’ does not \
  match ‘https://egbert.net’).

标头设置,lighttpd 服务器

Access-Control-Allow-Origin: https://egbert.net
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Headers: DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range
Content-Security-Policy: \
    default-src 'none'; \
    base-uri 'none'; \
    script-src 'strict-dynamic'; \
    object-src 'none'; \
    style-src 'self'; \
    img-src https://egbert.net/favicon.ico https://egbert.net/images/ https://egbert.net/blog/articles/*/images/*.png data:; \
    media-src https://egbert.net/media/ data:; \
    frame-src https://egbert.net/frames/; \
    frame-ancestors 'self'; \
    worker-src 'self'; \
    child-src https://egbert.net/frames/; \
    font-src https://egbert.net/fonts/; \
    connect-src 'self' https://egbert.net/; \
    form-action 'none'; \
    require-trusted-types-for; \
    trusted-types template; \
    sandbox; \
    report-uri https://ssoseo1.report-uri.com/r/d/csp/enforce; \
    report-to endpoint-1; \
    upgrade-insecure-requests; \
    block-all-mixed-content;
Feature-Policy: accelerometer 'none'; camera 'none'; fullscreen 'self'; geolocation 'none'; gyroscope 'none'; magnetometer 'none'; microphone 'none'; midi 'none'; notifications 'none'; payment 'none'; push 'none'; sync-xhr 'none'; speaker 'none'; usb 'none'; vibrate 'none';
Referrer-Policy: no-referrer

HTML 设置

  <link rel="stylesheet" href="https://egbert.net/css/m-dark.compiled.css">

CSS 路径

 */@font-face {
 font-family:'FontAwesome';
 src:url('../fonts/fontawesome-webfont.eot?v=4.7.0');
 src:url('../fonts/fontawesome-webfont.eot?#iefix&v=4.7.0') format('embedded-opentype'),
 url('../fonts/fontawesome-webfont.woff2?v=4.7.0') format('woff2'),
 url('../fonts/fontawesome-webfont.woff?v=4.7.0') format('woff'),
 url('../fonts/fontawesome-webfont.ttf?v=4.7.0') format('truetype'),
 url('../fonts/fontawesome-webfont.svg?v=4.7.0#fontawesomeregular') format('svg');
 font-weight:normal;
 font-style:normal
}

我确保所有字体文件都可以通过同一浏览器在单独的选项卡中下载。

更奇怪的是,错误信息暗示着“远程资源”。它们是完全相同的 URL。

没有加载插件,这是 Firefox v73.0.1 的安全模式。

更新 1

/@font-face当我用绝对目录路径替换 CSS 中的相对路径('../fonts')时,它没有改变任何东西。

更新 2

当我将方案和域( https://egbert.net/)添加到CSS 的绝对目录路径之前,它没有改变任何东西,/@font-face以获得完整的 URL 路径。

这与以下问题不同:

标签: firefoxcorssandboxcontent-security-policy

解决方案


在 CSP 策略中替换sandboxsandbox allow-same-origin将解决问题。

解释:

问题描述的 CORS 问题最终是由浏览器将原始值设置为null. 因此,即使Access-Control-Allow-Origin响应标头设置为通常预期的原始值(与浏览器地址栏中显示的 URL 匹配),它实际上也不匹配,因为浏览器已将源设置为null.

所以你最终会遇到一个悖论:文档似乎与它自己的来源不匹配。

https://stackoverflow.com/a/42242802/441757的答案概述了浏览器将原点设置为null. 问题中案件的具体原因来自https://html.spec.whatwg.org/multipage/#attr-iframe-sandbox的要求,如果sandbox设置:

内容被视为来自唯一来源,表单、脚本和各种可能令人讨厌的 API 被禁用,链接被阻止针对其他浏览上下文,并且插件受到保护。allow-same-origin关键字导致内容被视为来自其真实来源,而不是强制其成为唯一来源。

所以底线是:无论何时指定sandbox,在大多数情况下,您都希望使用allow-same-origin包含的关键字来指定它 - 以防止出现令人惊讶且难以解决的问题/副作用,例如问题中描述的 CORS 问题。


推荐阅读