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

Different behavior with CSP and HTML imports #616

Open
lmuntaner opened this issue Oct 22, 2022 · 0 comments
Open

Different behavior with CSP and HTML imports #616

lmuntaner opened this issue Oct 22, 2022 · 0 comments

Comments

@lmuntaner
Copy link

Hi,

I found a different behavior in how Chrome, Safari and Firefox interact with HTML Modules and CSP.

Maybe someone can help me better understand it.

We had an inline script importing a module:

<script type="module">
  import start from './start.js';
  start();
</script>

We used sha256 hashing for our CSP tag:

<meta http-equiv="Content-Security-Policy" content="
  script-src 'unsafe-eval' 'unsafe-inline' https: 'strict-dynamic' 'sha256-GD2MyhNzRFjmxD4jAUnvYIt90C1er46lTotfFdaZ3lg='
">

I found three different behaviors:

Safari:

It works.

Chrome:

Refused to load the script 'http://127.0.0.1:8080/start.js' because it violates the following Content Security Policy directive

Yet, if we preload the module start.js it works:

<link rel="modulepreload" href="./start.js">

Firefox:

It doesn't work in any case.

We had to do a workaround:

We moved the inline script to a file:

// main.js
import start from './start.js';
start();

Then we loaded it dynamically in the html:

<script>
   const loader = document.createElement("script");
  loader.type = "module";
  loader.src = "./main.js";
  document.head.appendChild(loader);
</script>

We had to change the sha256 of the CSP to the new inline script.

Any ideas of why this different behavior?

Thanks!!

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