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

feat(dev-server): add CSP support for vite's injectClientScript option #202

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rich-apes-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/vite-dev-server': minor
---

feat: add CSP support to vite's `injectClientScript` option
16 changes: 16 additions & 0 deletions packages/dev-server/e2e-bun/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ test('Should contain an injected script tag', async ({ page }) => {
const lastScriptTag = await page.$('script:last-of-type')
expect(lastScriptTag).not.toBeNull()

const nonce = await lastScriptTag?.getAttribute('nonce')
expect(nonce).toBeNull()

const content = await lastScriptTag?.textContent()
expect(content).toBe('import("/@vite/client")')
})

test('Should contain an injected script tag with a nonce', async ({ page }) => {
await page.goto('/with-nonce')

const lastScriptTag = await page.$('script:last-of-type')
expect(lastScriptTag).not.toBeNull()

const nonce = await lastScriptTag?.getAttribute('nonce')
expect(nonce).not.toBeNull()

const content = await lastScriptTag?.textContent()
expect(content).toBe('import("/@vite/client")')
})
Expand Down
5 changes: 5 additions & 0 deletions packages/dev-server/e2e-bun/mock/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ app.get('/', (c) => {
return c.html('<h1>Hello Vite!</h1>')
})

app.get('/with-nonce', (c) => {
c.header('content-security-policy', 'script-src-elem \'self\' \'nonce-ZMuLoN/taD7JZTUXfl5yvQ==\';')
return c.html('<h1>Hello Vite!</h1>')
})

app.get('/file.ts', (c) => {
return c.text('console.log("exclude me!")')
})
Expand Down
16 changes: 16 additions & 0 deletions packages/dev-server/e2e/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ test('Should contain an injected script tag', async ({ page }) => {
const lastScriptTag = await page.$('script:last-of-type')
expect(lastScriptTag).not.toBeNull()

const nonce = await lastScriptTag?.getAttribute('nonce')
expect(nonce).toBeNull()

const content = await lastScriptTag?.textContent()
expect(content).toBe('import("/@vite/client")')
})

test('Should contain an injected script tag with a nonce', async ({ page }) => {
await page.goto('/with-nonce')

const lastScriptTag = await page.$('script:last-of-type')
expect(lastScriptTag).not.toBeNull()

const nonce = await lastScriptTag?.getAttribute('nonce')
expect(nonce).not.toBeNull()

const content = await lastScriptTag?.textContent()
expect(content).toBe('import("/@vite/client")')
})
Expand Down
5 changes: 5 additions & 0 deletions packages/dev-server/e2e/mock/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ app.get('/', (c) => {
return c.html('<h1>Hello Vite!</h1>')
})

app.get('/with-nonce', (c) => {
c.header('content-security-policy', 'script-src-elem \'self\' \'nonce-ZMuLoN/taD7JZTUXfl5yvQ==\';')
return c.html('<h1>Hello Vite!</h1>')
})

app.get('/name', (c) => c.html(`<h1>My name is ${c.env.NAME}</h1>`))

app.get('/wait-until', (c) => {
Expand Down
5 changes: 4 additions & 1 deletion packages/dev-server/src/dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ export function devServer(options?: DevServerOptions): VitePlugin {
options?.injectClientScript !== false &&
response.headers.get('content-type')?.match(/^text\/html/)
) {
const script = '<script>import("/@vite/client")</script>'
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
Expand Down
Loading