1

I have update my store to Magento version 2.4.6-p6
Aaaand there is an issue with newly added restricted CSP policy on checkout page.

I have a bunch inline scripts that are added via admin panel (Content->Design->Configuration->Theme->Html head). Most of them are being blocked by CSP

What is the safest way to fix this besides moving scripts to the .phtml and adding safe render?

Magento documentation is dead on this topic. Which makes me very frustrated. https://developer.adobe.com/commerce/php/development/security/content-security-policies

P.s. I don't want any strange workarounds that allows unsafe inline scripts globally or on whole checkout page. Because that smells like unsafe option.

EDIT Update For example this script was marked as blocked.

<script type="text/javascript">
  zE('webWidget', 'setLocale', 'NB');
</script>

3 Answers 3

2

In Magento 2.4.6-p6, Inline scripts on the checkout page are deprecated. You can resolve this by placing your script in a phtml file. This approach ensures that the script is whitelisted by the Content Security Policy (CSP), thereby fixing the error.

Here's an example script to include in your phtml file, which will be whitelisted by CSP. You can use a similar method for other inline scripts:

<?php
$scriptString = <<<script
require(['jquery'],function($){
    $(document).ready(function() {
        zE('webWidget', 'setLocale', 'NB');
    });
});
script;
?>
<?= /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false) ?>

Place this code in your phtml file, and it should resolve csp the error.

4
  • Yeah, I was afraid that I will need to put them to the phtml :/ I have used this method on other scripts. I thought maybe Magento has something more useful to solve it rather than having to move them to phtml. Something like {{nonce}} to add to the script would be nice. Doable but I thought magento has something and Im just missing it.
    – Macas
    Commented Jul 25 at 14:32
  • 1
    This code is also adding the nonce to the scripts. Still need to review how can we handle it form Admin Dashboard. Commented Jul 26 at 5:38
  • I meant add {{nonce}} in the admin. I have used that secure render and it does wonders.
    – Macas
    Commented Jul 26 at 6:51
  • In the end I had to move my inline scripts phtml. This was most safest option I had. Since this answer is closely relates to my solution and has an example I mark this as an answer.
    – Macas
    Commented Sep 4 at 7:28
0

The description is not really dead about this topic at all.

All you have to do is to define your scripts in your custom csp_whitelist.xml

Then define anything you want and need in there

For exemple

<?xml version = "1.0"?>
<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="script-src">
            <values>
                <value id="cloudflare" type="host">*.cloudflare.com</value>
                <value id="twitter.com" type="host">*.twitter.com</value>
                <value id="google-analytics" type="host">*.google-analytics.com</value>
                <value id="google" type="host">*.google.com</value>
                <value id="twimg" type="host">*.twimg.com</value>
                <value id="gstatic" type="host">*.gstatic.com</value>
                <value id="trustedshops" type="host">*.trustedshops.com</value>
                <value id="usercentrics" type="host">*.usercentrics.eu</value>
                <value id="fontawesome" type="host">*.fontawesome.com</value>
                <value id="cookiebotScript" type="host">*.cookiebot.com</value>
                <value id="bing" type="host">*.bing.com</value>
                <value id="pinimg" type="host">*.pinimg.com</value>
                <value id="dwin1" type="host">*.dwin1.com</value>
                <value id="criteo" type="host">*.criteo.com</value>
                <value id="clarityOne" type="host">https://www.clarity.ms/tag/uet/25149270</value>
                <value id="clarityTwo" type="host">https://www.clarity.ms/eus-f/s/0.6.37/clarity.js</value>
                <value id="adservicegoogle" type="host">*.googleadservices.com</value>
                <value id="googleadservicescript" type="host">https://www.googleadservices.com/pagead/conversion_async.js</value>
                <value id="googleadsDoubleclick" type="host">*.g.doubleclick.net</value>
                <value id="datatables" type="host">*.datatables.net</value>
                <value id="colissimows" type="host">https://ws.colissimo.fr</value>
            </values>
        </policy>

You can link domain names or you can link direct scripts links

There you have an exemple for scripts but you can do that with everything, fonts, images etc etc

4
  • Thanks for an answer, but I meant scripts that doesnt have hosts. Simple js that does smth. I will add update to the question. For example zendesk script.
    – Macas
    Commented Jul 24 at 15:16
  • can you provide the exact message of block you have ? I suspect your zE method to still make a call external...if there is no externall call it doesn't make sense that it's blocked
    – Claims
    Commented Jul 24 at 15:36
  • I have added an example to a question itself with an update. Also Grazitti Dev posted that there is nothing to do for now. Leaving this question open until either I will find some time to get write something and share code or someone else does.
    – Macas
    Commented Jul 25 at 14:34
  • @Macas Hey, Does your inline scripts have external domain or cross origin ? if yes than may be you need to whitelist that script domain in csp_whitelist.xml Commented Jul 26 at 12:43
0

If you don't want to update all your inline scripts to use the secure renderer, you can disable the new checkout CSP settings inside one of your module's config.xml files:

<?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_checkout_index_index>
                    <scripts>
                        <inline>1</inline>
                        <event_handlers>0</event_handlers>
                    </scripts>
                </storefront_checkout_index_index>
            </policies>
        </csp>
    </default>
</config>

This will make your website slightly less secure, but it's a lot less work if you have a ton of scripts that need updating.

1
  • I dont recoment this to run on prod env as this makes inline scripts not safe. Injected script via cms block that exists on checkout wont be blocked. Secure rendering is the way in my optinion - see answer to this question
    – Macas
    Commented Sep 4 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.