Replies: 5 comments 12 replies
-
Hi @vejja, thank you for opening this discussion. I think moving to |
Beta Was this translation helpful? Give feedback.
-
Thanks @harlan-zw for your answer. From my perspective I can see that you are inserting As for the extra weight and globals, this is obviously an issue. Do you think that having a simple option to deactivate the insertion of the inline event handlers would be an issue? In a Nuxt world, maybe it could then become my own responsibility to bind Let me know if I can help, I can try to contribute but will need a little bit of guidance. |
Beta Was this translation helpful? Give feedback.
-
@harlan-zw : FYI I managed to get a POC running of what I had in mind
Both examples are structured exactly the same way: 1- A 2- A The POC is extremely hacky and probably not stable at all but it works. What I had to do is: I tried to get rid of the However I do believe we could be very close to a solution. I was able to make it 'kind of work' without any change to the codebase, the only annoyance being the console error message about the inline event handler being rejected. |
Beta Was this translation helpful? Give feedback.
-
Ok, thanks |
Beta Was this translation helpful? Give feedback.
-
Hey @harlan-zw Some additional questions though for clarification purposes:
Apologies if my questions are confused (and maybe make no sense in real life), but just trying to make sure I don't mislead our users into a wrong implementation. |
Beta Was this translation helpful? Give feedback.
-
Hi all
useScript
is an absolutely fantastic way to embed 3rd-party external scripts in a Nuxt application.I'm opening this discussion as a contributor to the Nuxt-Security module. We try to contribute to safer Nuxt applications by allowing users to deploy their websites with Strict CSP (Content Security Policy).
CSP Limitations
One thing that gets in our way with
useScript
is the fact that it insertsonload
inline event handlers, as in : https://github.com/unjs/unhead/blob/main/packages/unhead/src/plugins/eventHandlers.ts#L12CSP disallows the insertion of inline event handlers, as explained in MDN docs on CSP
Description of Issue
As a result, when enforcing Strict CSP on a website, the
onload
event managed byuseScript
is blocked :One way to get around this is to allow
unsafe-hashes
on thescript-src-attr
directive, but it is both unsafe (as its name implies), and unpractical (as it requires hashing of all legit inline code at build time).Please note that the other suggestion provided by the error message (which is to use a
nonce
) is not applicable on the client side. Only servers can insert nonces, which de facto prevents hybrid frameworks to implement this solution.Proposal
A better way to solve this issue would be to follow up MDN's recommendation, which is to replace inline event handlers with
addEventListener
calls.For the sake of clarity, I'm copying below the relevant part of the MDN site :
I am wondering whether it would be possible to modify the existing
unhead
code that inserts inline event handlers into the DOM withaddEventListener
bindings on the script tags ?I am not fully able to assess whether this would be feasible, hence opening the discussion here.
Happy to answer any questions about why this would be desirable from a CSP perspective and the technical limitations imposed by CSP on inline scripts.
Cheers
Beta Was this translation helpful? Give feedback.
All reactions