Magento 2中的内容安全策略

大家好!

当然,您已经安装了Magento 2.3.5,并在浏览器控制台中找到了

[仅报告]之类的东西,拒绝加载脚本'***',因为它违反了以下“内容安全策略”指令:“ script-src *”。请注意,未明确设置“ script-src-elem”,因此将“ script-src”用作后备。



我会告诉你减价怎么办

介绍


从版本2.3.5开始,一个名为Magento_CSP的模块出现在Magento中。他负责内容安全策略,实际上,添加了标头Content-Security-Policy,或者添加了Content-Security-Policy-Report-Only。与往常一样,在提高“修复”版本时,按时且很期望:)我们已经写了



什么内容安全政策以及在Habré上吃什么

标头Content-Security-PolicyContent-Security-Policy-Report-Only的选择分别取决于配置文件vendor / magento / module-csp / etc / config.xml,分别针对管理部分。

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <csp>
            <mode>
                <storefront>
                    <report_only>1</report_only>
                </storefront>
                <admin>
                    <report_only>1</report_only>
                </admin>
            </mode>
        </csp>
    </default>
</config>

在“ report_uri”指令的同一位置,可以设置报告的URL,但是由于 它不在那儿,然后chrome毫不留情地用控制台缺席的消息来变红。



示例错误文字
The Content Security Policy 'font-src 'self' 'unsafe-inline'; form-action secure.authorize.net test.authorize.net geostag.cardinalcommerce.com geo.cardinalcommerce.com 1eafstag.cardinalcommerce.com 1eaf.cardinalcommerce.com centinelapistag.cardinalcommerce.com centinelapi.cardinalcommerce.com 'self' 'unsafe-inline'; frame-ancestors 'self' 'unsafe-inline'; frame-src secure.authorize.net test.authorize.net geostag.cardinalcommerce.com geo.cardinalcommerce.com 1eafstag.cardinalcommerce.com 1eaf.cardinalcommerce.com centinelapistag.cardinalcommerce.com centinelapi.cardinalcommerce.com www.paypal.com www.sandbox.paypal.com 'self' 'unsafe-inline'; img-src widgets.magentocommerce.com www.googleadservices.com www.google-analytics.com t.paypal.com www.paypal.com www.paypalobjects.com fpdbs.paypal.com fpdbs.sandbox.paypal.com *.vimeocdn.com s.ytimg.com 'self' 'unsafe-inline'; script-src assets.adobedtm.com secure.authorize.net test.authorize.net geostag.cardinalcommerce.com 1eafstag.cardinalcommerce.com geoapi.cardinalcommerce.com 1eafapi.cardinalcommerce.com songbird.cardinalcommerce.com includestest.ccdc02.com js.authorize.net jstest.authorize.net www.googleadservices.com www.google-analytics.com www.paypal.com www.sandbox.paypal.com www.paypalobjects.com t.paypal.com js.braintreegateway.com s.ytimg.com video.google.com vimeo.com www.vimeo.com cdn-scripts.signifyd.com www.youtube.com 'self' 'unsafe-inline' 'unsafe-eval'; style-src getfirebug.com 'self' 'unsafe-inline'; object-src 'self' 'unsafe-inline'; media-src 'self' 'unsafe-inline'; manifest-src 'self' 'unsafe-inline'; connect-src geostag.cardinalcommerce.com geo.cardinalcommerce.com 1eafstag.cardinalcommerce.com 1eaf.cardinalcommerce.com centinelapistag.cardinalcommerce.com centinelapi.cardinalcommerce.com 'self' 'unsafe-inline'; child-src 'self' 'unsafe-inline'; default-src 'self' 'unsafe-inline' 'unsafe-eval'; base-uri 'self' 'unsafe-inline';' was delivered in report-only mode, but does not specify a 'report-uri'; the policy will have no effect. Please either add a 'report-uri' directive, or deliver the policy via the 'Content-Security-Policy' header.

如何将您的网址添加到策略中?


在/ etc / module文件夹的根目录中创建一个名为csp_whitelist.xml和内容的文件

<?xml version="1.0" encoding="UTF-8"?>
<csp_whitelist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Csp:etc/csp_whitelist.xsd">
    <policies>
        <policy id="POLICY_ID">
            <values>
                <value id="VALUE_ID" type="TYPE" algorithm="ALGORITHM">SOME DOMAIN</value>
            </values>
        </policy>
    </policies>
</csp_whitelist>

其中POLICY_ID为以下之一:

  • default-src
  • 脚本源
  • 对象源
  • 样式源
  • img-src
  • 媒体源
  • 帧源
  • 字体源
  • 连接源

VALUE_ID-一个任意的唯一名称TYPE-
类型,可以采用哈希值
ALGORITHM-哈希算法(TYPE = hash),例如sha256

让我们来看一下Magento本身集成测试示例

<?xml version="1.0"?>
<!--
/**
 * Copyright  Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<csp_whitelist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Csp/etc/csp_whitelist.xsd">
    <policies>
        <policy id="object-src">
            <values>
                <value id="mage-base" type="host">https://magento.com</value>
                <value id="hash" type="hash" algorithm="sha256">B2yPHKaXnvFWtRChIbabYmUBFZdVfKKXHbWtWidDVF8=</value>
                <value id="hash2" type="hash" algorithm="sha256">B3yPHKaXnvFWtRChIbabYmUBFZdVfKKXHbWtWidDVF8=</value>
            </values>
        </policy>
        <policy id="media-src">
            <values>
                <value id="mage-base" type="host">https://magento.com</value>
                <value id="devdocs-base" type="host">https://devdocs.magento.com</value>
            </values>
        </policy>
    </policies>
</csp_whitelist>

All Articles