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

Incorrect hashes generated causing CSP violations #126

Open
4 of 9 tasks
warnyul opened this issue Nov 29, 2024 · 3 comments
Open
4 of 9 tasks

Incorrect hashes generated causing CSP violations #126

warnyul opened this issue Nov 29, 2024 · 3 comments

Comments

@warnyul
Copy link

warnyul commented Nov 29, 2024

Description

The CspHtmlWebpackPlugin generates incorrect hashes, causing issues when trying to use the generated CSP.

I have created a GitHub repository to reproduce the issue and deployed the site for reference:

My observations

If I copy the content of the <style> or <script> tags from the generated index.html into an online CSP hash generator, I get different hash values. When I manually replace the generated hashes in index.html with these correct values, my site works as expected without CSP violations.

I could not prove it but maybe the issue is related to #111

What type of issue is this? (place an x in one of the [ ])

  • bug
  • enhancement (feature request)
  • question
  • documentation related
  • testing related
  • discussion

Requirements (place an x in each of the [ ])

  • I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • I've read and agree to the Code of Conduct.
  • I've searched for any related issues and avoided creating a duplicate issue.

Bug Report

Reproducible in:

slackhq/csp-html-webpack-plugin version: 5.1.0

node version: 22.10.0

OS version(s): macOS Sequoia 15.01, Ubuntu 24.04

Steps to reproduce:

  1. Clone the reproduction repository: git clone https://github.com/warnyul/bvarga.dev.git
  2. Install dependencies: npm install
  3. Build the project: npm run build
  4. Open index.html in dist folder using Safari or Google Chrome.

Expected result:

The hashes generated by the CspHtmlWebpackPlugin should correctly match the content of the built files.

Actual result:

The hashes generated by the plugin do not match the content of the files, causing CSP violations in the browser when accessing the deployed site. Manually generated hashes work correctly when applied.

@warnyul warnyul changed the title Incorrect hashes henerated by CspHtmlWebpackPlugin causing CSP violations Incorrect hashes generated by CspHtmlWebpackPlugin causing CSP violations Nov 29, 2024
@warnyul warnyul changed the title Incorrect hashes generated by CspHtmlWebpackPlugin causing CSP violations Incorrect hashes generated causing CSP violations Nov 29, 2024
@warnyul
Copy link
Author

warnyul commented Dec 1, 2024

I investigated further and logged the generated hashes in the hash method. I discovered that the processCsp function is called twice during the build. The logs show that the correct hashes are generated, but they appear after the ones currently included in the index.html. Based on this, I think this issue is not related to #111 .

@warnyul
Copy link
Author

warnyul commented Dec 3, 2024

It seems that removing the following SCSS definitions resolves the issue. With these definitions removed, the CSP plugin and the inline CSS plugin use the same CSS, resulting correct hashes:

> * {
   // style definition
}

@warnyul
Copy link
Author

warnyul commented Dec 3, 2024

I think I’ve identified the root cause of the CSP violations. It’s related to cheerio when _useHtmlParser2 is set to true. In this mode, cheerio modifies the content of <style> and <script> tags by replacing special characters like <, >, and & with their HTML entities (&lt;, &gt;, &amp;).

This transformation causes a mismatch between the original tag content (used to generate the CSP hash) and the altered content in the output, resulting in CSP violations. We’ll need to address this to ensure the CSP matches the actual content of the tags.

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

No branches or pull requests

1 participant