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

CSP support for vite's injectClientScript #201

Closed
meck93 opened this issue Jan 2, 2025 · 0 comments · Fixed by #202
Closed

CSP support for vite's injectClientScript #201

meck93 opened this issue Jan 2, 2025 · 0 comments · Fixed by #202

Comments

@meck93
Copy link
Contributor

meck93 commented Jan 2, 2025

What is the feature you are proposing?

First of all, thanks a lot for the the work on Hono and HonoX. It is awesome!

While experimenting with HonoX and the secure-headers middleware, I've noticed a little issue with the DEV setup. The dev-server.ts has an option (activated by default) to inject the client script (injectClientScript) which does the following:

if (
  options?.injectClientScript !== false &&
  response.headers.get('content-type')?.match(/^text\/html/)
) {
  const script = '<script>import("/@vite/client")</script>'
  return injectStringToResponse(response, script)
}
return response

This currently results in a CSP violation as the nonce is not set on the script tag.

The following change should do the trick:

if (
  options?.injectClientScript !== false &&
  response.headers.get('content-type')?.match(/^text\/html/)
) {
  const nonce = response.headers.get('content-security-policy')?.match(/'nonce-([^']+)'/)?.[1]
  const script = `<script${nonce ? ` nonce="${nonce}"` : ""}>import("/@vite/client")</script>`
  return injectStringToResponse(response, script)
}
return response

Related to

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

Successfully merging a pull request may close this issue.

1 participant