Skip to content
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
10 changes: 8 additions & 2 deletions packages/browser/src/node/serverOrchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export async function resolveOrchestrator(
for (const attr in script.attrs || {}) {
html += `${attr}="${script.attrs![attr]}" `
}
html += `>${script.children}</script>`
html += `>${escapeInlineScript(typeof script.children === 'string' ? script.children : '')}</script>`
return html
}).join('\n')
}
Expand Down Expand Up @@ -96,8 +96,14 @@ export async function resolveOrchestrator(
__VITEST_FAVICON__: globalServer.faviconUrl,
__VITEST_TITLE__: 'Vitest Browser Runner',
__VITEST_SCRIPTS__: globalServer.orchestratorScripts,
__VITEST_INJECTOR__: `<script type="module">${injector}</script>`,
__VITEST_INJECTOR__: `<script type="module">${escapeInlineScript(injector)}</script>`,
__VITEST_ERROR_CATCHER__: `<script type="module" src="${globalServer.errorCatcherUrl}"></script>`,
__VITEST_SESSION_ID__: JSON.stringify(sessionId),
})
}

function escapeInlineScript(content: string): string {
return content
.replace(/<!--/g, '<\\!--')
.replace(/<\/(script)/gi, '</\\$1')
}
5 changes: 5 additions & 0 deletions test/browser/fixtures/inline-script/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { expect, inject, test } from 'vitest'

test('provide', () => {
expect(inject('someKey' as never)).toBe('</script><h1>inject1</h1><!--')
})
26 changes: 26 additions & 0 deletions test/browser/fixtures/inline-script/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { defineConfig } from 'vitest/config'
import { instances, provider } from '../../settings'

export default defineConfig({
cacheDir: path.join(
path.dirname(fileURLToPath(import.meta.url)),
'node_modules/.vite'
),
test: {
browser: {
enabled: true,
provider,
instances: [instances[0]],
orchestratorScripts: [
{
content: 'globalThis.__injected = "</script><h1>inject2</h1><!--"',
},
],
},
provide: {
someKey: "</script><h1>inject1</h1><!--",
},
},
})
16 changes: 16 additions & 0 deletions test/browser/specs/inline-script.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expect, test } from 'vitest'
import { runBrowserTests } from './utils'

test('escape inline script', async () => {
const result = await runBrowserTests({
root: './fixtures/inline-script',
})
expect(result.stderr).toMatchInlineSnapshot(`""`)
expect(result.errorTree()).toMatchInlineSnapshot(`
{
"basic.test.ts": {
"provide": "passed",
},
}
`)
})
Loading