Skip to content

Adding CSP support to the inline google consent mode script, while ke…#32

Merged
sprankhub merged 1 commit intocustomgento:masterfrom
baldwin-agency:csp-compatibility
Mar 3, 2025
Merged

Adding CSP support to the inline google consent mode script, while ke…#32
sprankhub merged 1 commit intocustomgento:masterfrom
baldwin-agency:csp-compatibility

Conversation

@hostep
Copy link
Contributor

@hostep hostep commented Feb 28, 2025

…eping in mind compatibility with Magento 2.3.x and 2.4.x

I noticed while installing this module in a Magento 2.4.7-p4 shop that we got an error on the checkout in the browser console that this inline google consent mode script is being blocked by the Content-Security-Policy checks in the browser.
We need to add a nonce to the script to allow CSP checks to not block this script.

I've created this PR to fix that.

Good to know:

  • The SecureHtmlRenderer class doesn't exist in Magento 2.3.x, and your composer.json indicates that this module can still be installed on Magento 2.3.x shops, so I'm first checking if that class exists before trying to use it. Otherwise I'm just outputting the script as-is. Read more about it here
  • I'm explicitly not using heredoc syntax for putting the script into a string, because of a known bug with html minification in older version of Magento that can't work with it. Read more about it here

I also cleaned up some code styling/formatting, let me know if this doesn't follow the standards used in this module and if it should be reworked in some way.

…eping in mind compatibility with Magento 2.3.x and 2.4.x
@sprankhub
Copy link
Member

Thanks a lot for your contribution!

Honestly, I'd also be fine with ditching Magento 2.3 support. So if you don't need it and would be willing to, feel free to update the dependencies and the code. If you don't want to, I can also take over :)

@hostep
Copy link
Contributor Author

hostep commented Mar 3, 2025

Hi Simon

I'll leave it up to you to deal with removing Magento 2.3 support.
Or just merge the PR and keep both 2.3 and 2.4 support for now until it becomes harder to maintain at some point and then remove it.

Thanks!

@sprankhub sprankhub merged commit a59b52d into customgento:master Mar 3, 2025
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants