Skip to content

Added CSP support for inline scripts.#108

Open
hostep wants to merge 1 commit intotrustpilot:masterfrom
baldwin-agency:add-inline-scripts-csp-support
Open

Added CSP support for inline scripts.#108
hostep wants to merge 1 commit intotrustpilot:masterfrom
baldwin-agency:add-inline-scripts-csp-support

Conversation

@hostep
Copy link

@hostep hostep commented Jul 4, 2025

Ticket 🎟

#107

What ⁉️

Adds CSP (Content-Security-Policy) support to the frontend inline scripts in this module, so those aren't being blocked on the checkout in modern Magento versions.

Additional cleanup:

  • Proper type annotation in the files touched.
  • Extra escaping for output to JS code (the code that returns json encoded data is not needed to be escaped, but other stuff is)

Why ⁉️

Magento started blocking inline javascript scripts many months ago on the checkout.

How ⁉️

Using the SecureHtmlRenderer (introduced in Magento 2.4.0) we can output the inline javascript with nonces so they no longer violate the CSP policies.
I took extra precaution because the composer.json file of this module allows the module to still be installed on Magento versions older then 2.4.0, so I'm first checking if SecureHtmlRenderer exists before trying to use it so the module should (untested) be backwards compatible still with older versions of Magento as well.

How to review and test 📱

Open a modern Magento shop with this module installed, especially the checkout and see if no inline scripts are being blocked anymore after changes from this PR. And all scripts keep working as expected.
Open an older version of a Magento shop (2.3.7-p4 for example) with this module installed and see if all scripts keep working as expected.

Checklist ✅

  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing tests are passing locally

@hostep hostep mentioned this pull request Jul 4, 2025
@ssx
Copy link

ssx commented Jul 7, 2025

@hostep given how unreliable TP are at responding here, I may fork this and get all these recent PRs into it.

@itaymesh
Copy link

@ssx, did you fork and merge those PRs?

@ssx
Copy link

ssx commented Jul 14, 2025

@ssx, did you fork and merge those PRs?

it's on my list to do this week!

@itaymesh
Copy link

itaymesh commented Jul 14, 2025 via email

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.

4 participants