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.45k stars 9.29k forks source link

Putting csp_whitelist.xml in theme does not work and creates intermittent issue #38933

Open Jakhotiya opened 1 month ago

Jakhotiya commented 1 month ago

Preconditions and environment

Steps to reproduce

Expected result

Domains listed in your custom csp_whitelist.xml under theme should have reflected on frontend

Actual result

If admin is hit first after cache flush, domains under your csp_whitelist.xml under theme are not seen on frontend.

Additional information

This bug happens because contents of csp_whitelist.xml in theme are stored in global cache. Lets see how this works right now. CSP cache is maintained area wise. For example, you will find 3 files in your cache

var/cache/mage--8/mage---06a_FRONTEND__CSP_WHITELIST_CONFIG
var/cache/mage--2/mage---06a_GLOBAL__CSP_WHITELIST_CONFIG
var/cache/mage--a/mage---06a_ADMINHTML__CSP_WHITELIST_CONFIG

Now the bug happens because of app/code/Magento/Csp/Model/Collector/CspWhitelistXml/FileResolver.php

public function get($filename, $scope)
    {
         $configs = $this->moduleFileResolver->get($filename, $scope);
        if ($scope === 'global') {
            $files = [];
            $theme = $this->theme;
            while ($theme) {
                /** @var CustomizationInterface $info */
                $info = $this->themeInfoFactory->create(['theme' => $theme]);
                $file = $info->getThemeFilesPath() .'/etc/' .$filename;
                if ($this->rootDir->isExist($file)) {
                    $files[] = $file;
                }
                $theme = $theme->getParentTheme();
            }
            $configs = $this->iteratorFactory->create(
                ['paths' => array_reverse($files), 'existingIterator' => $configs]
            );
        }

        return $configs;
    }

if ($scope === 'global') leads to csp_whitelist config in theme being put in global cache. After cache flush if admin request lands on server, $theme variable is set to Magento/backend . In this case, your file in frontend theme wont be read. Global cache is then populated according to Magento/backend

The behavior completely depends on which area request lands first, frontend or admin. This leads to poor developer experience too.

Release note

No response

Triage and priority

m2-assistant[bot] commented 1 month ago

Hi @Jakhotiya. 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. To deploy vanilla Magento instance on our environment, Add a comment to the issue: