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

fix(browser): allow iframe to load even if there is a custom CSP header #5841

Merged
merged 1 commit into from
Jun 4, 2024
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
13 changes: 13 additions & 0 deletions packages/browser/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@ export default (project: WorkspaceProject, base = '/'): Plugin[] => {
__VITEST_TYPE__: url.pathname === base ? '"orchestrator"' : '"tester"',
})

// remove custom iframe related headers to allow the iframe to load
res.removeHeader('X-Frame-Options')

if (url.pathname === base) {
// disable CSP for the orchestrator as we are the ones controlling it
res.removeHeader('Content-Security-Policy')

if (!indexScripts)
indexScripts = await formatScripts(project.config.browser.indexScripts, server)

Expand Down Expand Up @@ -105,6 +111,13 @@ export default (project: WorkspaceProject, base = '/'): Plugin[] => {
return
}

const csp = res.getHeader('Content-Security-Policy')
if (typeof csp === 'string') {
// add frame-ancestors to allow the iframe to be loaded by Vitest,
// but keep the rest of the CSP
res.setHeader('Content-Security-Policy', csp.replace(/frame-ancestors [^;]+/, 'frame-ancestors *'))
}

const decodedTestFile = decodeURIComponent(url.pathname.slice(testerPrefix.length))
const testFiles = await project.globTestFiles()
// if decoded test file is "__vitest_all__" or not in the list of known files, run all tests
Expand Down
3 changes: 3 additions & 0 deletions test/browser/vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export default defineConfig({
server: {
headers: {
'x-custom': 'hello',
// Vitest iframe should still be loaded
'X-Frame-Options': 'DENY',
'content-security-policy': 'frame-src https://example.com; frame-ancestors https://example.com',
},
},
optimizeDeps: {
Expand Down
Loading