Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strict CSP issue with onclick #3431

Open
ganeddact opened this issue Nov 1, 2024 · 4 comments
Open

Strict CSP issue with onclick #3431

ganeddact opened this issue Nov 1, 2024 · 4 comments
Assignees
Labels

Comments

@ganeddact
Copy link

Preconditions

Apply a strict CSP, not allowing inline event handlers

Magento Version : 2.4.6-p8

ElasticSuite Version : 2.11.9.2

Environment : developer

Third party modules :

Steps to reproduce

  1. Have a multiselect filter attribute in a category page
  2. Select one checkbox
  3. Check the console

Expected result

  1. Filter should apply

Actual result

  1. No filtering applies
  2. Console log reports "Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src .."

Looks like in vendor/smile/elasticsuite/src/module-elasticsuite-catalog/view/frontend/web/template/attribute-filter.html on line 15 there is an onclick event that does not sit well with this strict CSP.
Unrelated to the above, there is another one in line 28 in vendor/smile/elasticsuite/src/module-elasticsuite-catalog/view/frontend/templates/layer/filter/default.phtml

Inline onclicks are generally considered bad practice, and should be replaced by event handlers.

@vahonc
Copy link
Collaborator

vahonc commented Nov 8, 2024

Hello @ganeddact,

I agree that Inline 'onclicks' are generally bad practices and should be replaced by event handlers. I will try to add some fixes for these.

But what is really strange for me is that you pointed out that after clicking on one checkbox of the multi-select filter attribute in a category page, no filtering applies, and you see the Console log report "Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src." I could not reproduce this on my test environment with fresh Magento 2.4.6-p8 EE and ES 2.11.9.2. I don't see errors in the Console log report, and the filters that I select are applied.

Screenshot from 2024-11-08 14-28-15

Therefore, I'm not sure that the not applying filters issue is linked to using inline 'onclicks', especially when it targets smth with directive: "script-src.", of course I could be wrong. And I just want to know if there are no other things that could effect on this. Maybe you could add more details about your environment and your issue (attributes configuration, etc.), screenshots are welcome, which help to understand where exactly the problem is described by your issue.

BR,
Vadym

vahonc added a commit to vahonc/elasticsuite that referenced this issue Nov 8, 2024
@ganeddact
Copy link
Author

ganeddact commented Nov 8, 2024

Hi @vahonc , thanks for your reply.
As I stated in my precondition, the problem arise when applying a strict CSP, not allowing inline event handlers by adding specific policies in a config.xml file of a module, like:

<policies>
    <storefront>
        <scripts>
            <policy_id>script-src</policy_id>
            <self>1</self>
            <inline>0</inline>
            <eval>1</eval>
            <dynamic>0</dynamic>
        </scripts>
    </storefront>
</policies>

With default CSP rules that ship with Magento, it would not show up and in fact in your test did not emerge. But if a store is set up for stricter content security policies, this may happen.

@vahonc
Copy link
Collaborator

vahonc commented Nov 8, 2024

@ganeddact, thanks for this clarification.

Could you test my fix on your environment? I will test it with your additional info on my side also.

BR,
Vadym

@ganeddact
Copy link
Author

I've applied your change and it works like a charm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants