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

Remove in-line script from header #1004

Merged
merged 4 commits into from
Dec 1, 2024
Merged

Conversation

davidgs
Copy link
Contributor

@davidgs davidgs commented Oct 21, 2024

Issue

An in-line script is added to the header, which cannot be executed if the Content-Security policy is anything but unsafe-inline. unsafe-inline should never be allowed.

This PR adds the script to the main application.js script, which is hashed, and so removes the issue.

fixes #1002

Description

Removes the in-line script

<script>
      theme = localStorage.getItem('theme-scheme') || localStorage.getItem('darkmode:color-scheme') || 'light';
      if (theme == 'system') {
        if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
          theme = 'dark';
        } else {
          theme = 'light';
        }
      }
      document.documentElement.setAttribute('data-theme', theme);
    </script>

from baseof.html and adds it to the `application.js script.

Test Evidence

prior to change:

davidgs.com/:68 Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' https://app.posthog.com/ *.googletagmanager.com https://cdn.userfront.com https://commento.davidgs.com:8088  *.unpkg.com apis.google.com *.googleapis.com cdn.polyfill.io https://buttons.github.io  cdn.jsdelivr.net *.zencdn.net https://cdnjs.cloudflare.com https://*.google-analytics.com https://*.statcounter.com". Either the 'unsafe-inline' keyword, a hash ('sha256-WiE2LPSnZlTiP9NnrQN14OnMKI2ild8fGH0n+PhofS0='), or a nonce ('nonce-...') is required to enable inline execution.

After change: no error.

Copy link

netlify bot commented Oct 21, 2024

Deploy Preview for toha-ci ready!

Name Link
🔨 Latest commit 0c816bc
🔍 Latest deploy log https://app.netlify.com/sites/toha-ci/deploys/674c6cbc45d9620008a3c59c
😎 Deploy Preview https://deploy-preview-1004--toha-ci.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@joyao joyao added the bug-fix Bug fixes label Oct 21, 2024
@hossainemruz
Copy link
Member

Hi @davidgs! Thank you for the PR. However, this inline script is necessary. Otherwise, when you are in dark mode and click on a new page, it will flash white background and then turn into dark mode again.

@hossainemruz hossainemruz changed the title remove in-line script from header Remove in-line script from header Nov 30, 2024
@hossainemruz
Copy link
Member

@davidgs Found a solution. We can set the script in-line with integrity hash. I have updated the PR. Please, check and let me know if that works for you.

@hossainemruz hossainemruz enabled auto-merge (squash) December 1, 2024 14:31
@hossainemruz hossainemruz disabled auto-merge December 1, 2024 14:33
@hossainemruz hossainemruz added the enhancement New feature or request label Dec 1, 2024
@hossainemruz hossainemruz merged commit 4e4acdb into hugo-toha:main Dec 1, 2024
11 of 12 checks passed
@davidgs
Copy link
Contributor Author

davidgs commented Dec 1, 2024

That's great news, thanks!

@hossainemruz hossainemruz removed the bug-fix Bug fixes label Jan 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

script in based.html can't be executed
3 participants