magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.55k stars 9.32k forks source link

Error 500 CSP #39192

Closed marctbb closed 2 weeks ago

marctbb commented 1 month ago

Preconditions and environment

Steps to reproduce

I'am currently managing a Magento 2.4.7 instance on an Apache server, and we have encountered issues related to Content Security Policies (CSP) after updates and the installation of third-party plugins. The problem manifests as CSP errors in the console, which we have addressed by adding specific directives to our custom whitelist located in app/code. Additionally, these third-party plugins have added their own CSP rules in their respective whitelists.

Over time, we’ve cleaned our custom whitelist to remove directives already included by the third-party plugins. However, we’ve now reached a point where we cannot reduce the whitelist further, as the remaining entries in our custom CSP whitelist are unique across the codebase. Removing them would result in console CSP errors.

The core issue is that Apache imposes a limit on the number of CSP directives. When this limit is exceeded, we encounter 500 server errors, rendering both the frontend and backend of Magento unusable.

It is possible to remove csp in the whitelists of third party plugins and thus reduce the number of csp and therefore the frontend and backend are shown. But this means that there is a possibility of errors appearing in the console and having the problem in future updates.

It should be noted that it is only reproducible if the Magento is on a server, it does not work on localhost.

Expected result

The expected result is that magento will detect the csp's that are added to the head and if it finds that they are duplicated in several whitelists it will only add or use one of those. With this you would be able to see both the forntend and the backend without any problem.

Actual result

The current result is as I said, we get error 500 in both the frontend and the backend and it is not possible to work. Is there a way for magento to handle csp correctly?

Additional information

I found this, maybe it helps to understand the issue: https://konkretor.com/2022/07/07/apache-ah01070-error-parsing-script-headers/

Release note

No response

Triage and priority

m2-assistant[bot] commented 1 month ago

Hi @marctbb. Thank you for your report. To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce.

m2-assistant[bot] commented 1 month ago

Hi @engcom-Hotel. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-Hotel commented 1 month ago

Hello @marctbb,

Thanks for the report and collaboration!

We have tried to reproduce the issue in the latest development i.e. 2.4-develop, and Magento has already removed the duplicated CSPs from the header from different modules/extensions. To reproduce the issue we have followed the below steps:

  1. Create 2 custom modules.
  2. Add csp_whitelist.xml in both the modules.
  3. Add some duplicated and unique CSPs in both csp_whitelist.xml
  4. Enable modules and run bin/magento s:up
  5. After loading the page, it shows only the unique CSP in the response header.

It seems the issue is with the Apache's header limit, and not with the Magento core.

Let us know if we missed anything.

Thanks

hostep commented 1 month ago

You could also give https://github.com/basecom/magento2-csp-split-header/ a try, to see if that module improves the situation

engcom-Hotel commented 2 weeks ago

Dear @marctbb,

We've observed that there haven't been any updates on this issue for quite some time. Therefore, we're assuming it's been resolved and will be closing it. Feel free to open a new ticket or reopen this one if you require further assistance.

Regards