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

Option to add nonce value to style tag of vue3 web components #6530

Closed
landoephan opened this issue Aug 22, 2022 · 1 comment
Closed

Option to add nonce value to style tag of vue3 web components #6530

landoephan opened this issue Aug 22, 2022 · 1 comment
Labels
🍰 p2-nice-to-have Priority 2: this is not breaking anything but nice to have it addressed. has PR A pull request has already been submitted to solve the issue scope: custom elements ✨ feature request New feature or request

Comments

@landoephan
Copy link

What problem does this feature solve?

When I create web components via custom elements in vue3 (as described here: https://vuejs.org/guide/extras/web-components.html#building-custom-elements-with-vue), the styles are loaded directly into the shadow dom with a <style> tag. In order to meet content security policies, I would need to include a nonce value to this style tag when building the web component. Otherwise the styles are rejected with the following error message:

Refused to apply inline style because it violates the following Content Security Policy directive: "style-src 'self' 'nonce-xxx='". Either the 'unsafe-inline' keyword, a hash ('sha256-xxx='), or a nonce ('nonce-...') is required to enable inline execution.

It would be great, if a nonce value could be applied to the style tags while building the

What does the proposed API look like?

Add a nonce parameter to the _applyStyle method in apiCustomElement.ts:

private _applyStyles(styles: string[] | undefined, options?: { nonce: string }) {
    if (styles) {
      styles.forEach(css => {
        const s = document.createElement('style')
        s.textContent = css
        // it is important to add the nonce attribute before it is appended to the DOM
        s.setAttribute('nonce', options?.nonce || '')
        this.shadowRoot!.appendChild(s)
        // record for HMR
        if (__DEV__) {
          ;(this._styles || (this._styles = [])).push(s)
        }
      })
    }
  }
@landoephan landoephan added the ✨ feature request New feature or request label Aug 22, 2022
@haoqunjiang haoqunjiang added the 🍰 p2-nice-to-have Priority 2: this is not breaking anything but nice to have it addressed. label Apr 10, 2023
@haoqunjiang haoqunjiang added the has PR A pull request has already been submitted to solve the issue label Aug 31, 2023
@yyx990803
Copy link
Member

closed via bb4a02a (to be released in 3.5)

@github-actions github-actions bot locked and limited conversation to collaborators Aug 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🍰 p2-nice-to-have Priority 2: this is not breaking anything but nice to have it addressed. has PR A pull request has already been submitted to solve the issue scope: custom elements ✨ feature request New feature or request
Projects
None yet
4 participants