-
-
Notifications
You must be signed in to change notification settings - Fork 7.2k
feat: csp nonce support #16052
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
Merged
Merged
feat: csp nonce support #16052
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
adaad77
feat: `html.cspNonce` option
sapphi-red 9fea365
feat: inject `meta[property=csp-nonce]`
sapphi-red 0d0a478
feat: add nonce when injecting style tag in dev
sapphi-red 8e50973
feat: use nonce value in meta tag for preload
sapphi-red e973af0
refactor: inject nonce attribute in a separate html hook
sapphi-red 44320b0
chore: reduce diff
sapphi-red 9e32652
chore: add comment about nonce attribute
sapphi-red d17074b
test: update preload sourcemap snapshot
sapphi-red 020c971
feat: don't add nonce attribute to style tags
sapphi-red 69f4f40
docs: csp
sapphi-red 0460ca9
fix: fallback to getAttribute for old firefox
sapphi-red af823e9
test: update preload sourcemap snapshot
sapphi-red c11ab0f
test: fix flaky
sapphi-red 2bee4b8
chore: merge main
sapphi-red 30e4d0e
docs: move docs to features page
sapphi-red File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { expect, test } from 'vitest' | ||
import { expectWithRetry, getColor, page } from '~utils' | ||
|
||
test('linked css', async () => { | ||
expect(await getColor('.linked')).toBe('blue') | ||
}) | ||
|
||
test('inline style tag', async () => { | ||
expect(await getColor('.inline')).toBe('green') | ||
}) | ||
|
||
test('imported css', async () => { | ||
expect(await getColor('.from-js')).toBe('blue') | ||
}) | ||
|
||
test('dynamic css', async () => { | ||
expect(await getColor('.dynamic')).toBe('red') | ||
}) | ||
|
||
test('script tag', async () => { | ||
await expectWithRetry(() => page.textContent('.js')).toBe('js: ok') | ||
}) | ||
|
||
test('dynamic js', async () => { | ||
await expectWithRetry(() => page.textContent('.dynamic-js')).toBe( | ||
'dynamic-js: ok', | ||
) | ||
}) | ||
|
||
test('meta[property=csp-nonce] is injected', async () => { | ||
const meta = await page.$('meta[property=csp-nonce]') | ||
expect(await (await meta.getProperty('nonce')).jsonValue()).not.toBe('') | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.dynamic { | ||
color: red; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import './dynamic.css' | ||
|
||
document.querySelector('.dynamic-js').textContent = 'dynamic-js: ok' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.from-js { | ||
color: blue; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<link rel="stylesheet" href="./linked.css" /> | ||
<style nonce="#$NONCE$#"> | ||
.inline { | ||
color: green; | ||
} | ||
</style> | ||
<script type="module" src="./index.js"></script> | ||
<p class="linked">direct</p> | ||
<p class="inline">inline</p> | ||
<p class="from-js">from-js</p> | ||
<p class="dynamic">dynamic</p> | ||
<p class="js">js: error</p> | ||
<p class="dynamic-js">dynamic-js: error</p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import './from-js.css' | ||
|
||
document.querySelector('.js').textContent = 'js: ok' | ||
|
||
import('./dynamic.js') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.linked { | ||
color: blue; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "@vitejs/test-csp", | ||
"private": true, | ||
"version": "0.0.0", | ||
"type": "module", | ||
"scripts": { | ||
"debug": "node --inspect-brk ../../packages/vite/bin/vite", | ||
"dev": "vite", | ||
"build": "vite build", | ||
"preview": "vite preview" | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.