我正在开发一个 Magento 2 站点,该站点使用自定义扩展来向 Magento 的 CSP 添加白名单。我遇到了以下错误的问题:“Content-Security-Policy 指令‘frame-ancestors’不支持源表达式‘‘unsafe-inline’’”
此问题的根源是以下文件:https://translate.googleapis.com/element/TE_20210503_00/e/js/element/element_main.js
问题是,我已将此网站列入框架祖先的白名单,但它仍然被阻止。这是我迄今为止的政策:
<policy id="frame-ancestors">
<values>
<value id="google-apis" type="host">*.googleapis.com</value>
</values>
</policy>
这与我对所有其他策略的格式相同,并且所有这些策略都已正确列入白名单。这是唯一没有受到影响的。
我按照本教程制作了我的 CSP 扩展,以供参考:https://magento.stackexchange.com/a/312350/73083
我不确定我做错了什么,这是在将 CSP 添加到站点之前需要解决的最后一个问题。
在 2.4.3 版本 Magento 文档中提到过,这仍然是 Magento 的一个已知问题:https://devdocs.magento.com/guides/v2.4/release-notes/open-source-2-4 -3.html#已知问题。所以,我们能做的就是暂时解决时间问题。
解决方案是创建自己的自定义模块来扩展 Magento_Csp 模块。在etc/config.xml文件中,我们要修改frame-ancestor策略并将其设置为0。
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<csp>
<policies>
<storefront>
<frame-ancestors>
<inline>0</inline>
</frame-ancestors>
</storefront>
<admin>
<frame-ancestors>
<inline>0</inline>
</frame-ancestors>
</admin>
</policies>
</csp>
</default>
然后运行:
bin/magento s:up
bin/magento s:s:d -f
bin/magento c:f
这将有助于它成为一个有效的解决方案。
快乐编码!!
frame-ancestors
指令不支持'unsafe-inline'
标记。此令牌仅受 script-src
、style-src
和 defaulr-src
指令支持。如果您收到错误:“Content-Security-Policy 指令 'frame-ancestors' 不支持源表达式 ''unsafe-inline''”,这意味着您有一条规则:
frame-ancestors ... 'unsafe-inline' ...;
在 CSP 中。frame-ancestors
frame-src
而不是 <frame src='https://accounts.googleapis.com/auth/...'
指令。因为 frame-ancestors *.googleapis.com
控制将您的网站嵌入到 *.googleapis.com
网页中。而 frame-src
控制哪些网站可以嵌入您的页面。
INSERT INTO `core_config_data` (`scope`, `scope_id`, `path`, `value`)
VALUES
('default', 0, 'csp/policies/admin/frame-ancestors/inline', '0'),
('default', 0, 'csp/policies/storefront/frame-ancestors/inline', '0');