bigskysoftware / htmx

</> htmx - high power tools for HTML
https://htmx.org
Other
38.19k stars 1.3k forks source link

CSP issue with IndicatorStyles - needs a htmx.config.inlineStyleNonce config option #1062

Open Kimotu opened 2 years ago

Kimotu commented 2 years ago

Please add config option htmx.config.inlineStyleNonce:'' and extend function insertIndicatorStyles to include:

"<style nonce=\"" + htmx.config.inlineStyleNonce + "\">\
                      ." + htmx.config.indicatorClass + "{opacity:0;transition: opacity 200ms ease-in;}\
                      ." + htmx.config.requestClass + " ." + htmx.config.indicatorClass + "{opacity:1}\
                      ." + htmx.config.requestClass + "." + htmx.config.indicatorClass + "{opacity:1}\
                    </style>");

Tested it in a locale modified installation. Works.

jyrimatti commented 1 year ago

I also ran into this issue. For example when a recommended Content Security Policy is used: <meta http-equiv="Content-Security-Policy" content="default-src 'self'" /> no runtime style tags are allows to be inserted to the document (without a known hash/nonce).

This behaviour can be disabled by using <meta name="htmx-config" content='{ "includeIndicatorStyles": false }' /> but it feels kind of awkward to add this everywhere.

Is there a reason why this style is added programmatically, instead of providing a separate htmx.css file that the users could include if needed?

terrablue commented 1 year ago

Would you accept a PR here? I needed to add style-src 'unsafe-inline'; to my CSP to temporarily resolve this issue, but it loosens up the default security. I'd go with either of the proposed solutions here, though I'd prefer the CSS solution.