Skip to content

CSP nonce cached on pages when disabling unsafe-inline #38615

@Dnd-Coch

Description

@Dnd-Coch

Preconditions

  • Magento 2.4.7
  • Custom configuration as following in etc/config.xml:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
       <csp>
           <policies>
               <storefront>
                   <scripts>
                       <inline>0</inline>
                   </scripts>
               </storefront>
           </policies>
       </csp>
    </default>
</config>

Steps to reproduce

  1. Add above content in the etc/config.xml of a custom module
  2. Clear the cache
  3. Load the site homepage in your browser
  4. Inspect the Content-Security-Policy-Report-Only HTTP header of the page

Expected result

  • The unsafe-inline expression is not present in the script-src policy
  • Hashes of the inline scripts of the page are present in the script-src policy

Actual result

  • The unsafe-inline expression is not present in the script-src policy
  • A generated nonce is present in the script-src policy instead of hashes
  • The nonce is cached by FPC

As it is mentioned in this architecture document, the nonce approach is incompatible with Magento's page cache.

So the fact that nonce takes precedence over hashes in this method \Magento\Csp\Helper\InlineUtil::processTag is a problem for merchants who want to remove the unsafe-inline expression of their policies to improve overall security.

Nonce should be generated only for non-cacheable pages.

Examples

See \Magento\Csp\Helper\InlineUtil::processTag method.

Proposed solution

No response

Release note

No response

Triage and priority

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

Metadata

Metadata

Labels

Area: FrameworkComponent: CspIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedPriority: P2A defect with this priority could have functionality issues which are not to expectations.Progress: doneReported on 2.4.7Indicates original Magento version for the Issue report.Reproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branchTriage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject it

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions