Add Content Security Policy to webviews#1648
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR addresses Issue #171 by adding a Content Security Policy (CSP) to the extension’s webview HTML, ensuring the existing script nonce mechanism is actually enforced and tightening the default security posture of all webviews.
Changes:
- Injected a
Content-Security-Policy<meta http-equiv="...">tag into each webview HTML template. - Read and incorporated
webview.cspSourceinto CSP directives to allow extension-hosted resources. - Standardized CSP directives across 8 webviews (default deny, nonce-restricted scripts, style allowances compatible with current bundling).
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/overview/index.ts | Adds CSP meta tag using webviewPanel.webview.cspSource and existing nonce. |
| src/welcome/index.ts | Adds CSP meta tag using webviewPanel.webview.cspSource and existing nonce. |
| src/ext-guide/index.ts | Adds CSP meta tag using webviewPanel.webview.cspSource and existing nonce. |
| src/beginner-tips/index.ts | Adds CSP meta tag using this._panel!.webview.cspSource and existing nonce. |
| src/java-runtime/index.ts | Adds CSP meta tag using webviewPanel.webview.cspSource and existing nonce. |
| src/install-jdk/index.ts | Adds CSP meta tag using this._panel!.webview.cspSource and existing nonce. |
| src/formatter-settings/index.ts | Adds CSP meta tag using this.webviewPanel!.webview.cspSource and existing nonce. |
| src/project-settings/projectSettingsView.ts | Adds CSP meta tag using webview.cspSource and existing nonce. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
wenytang-ms
approved these changes
Jun 1, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #171
Problem
Issue #171 (filed by the VS Code webview API owner) reported that the extension's webviews do not set a Content Security Policy. After the webview refactor, all webviews wired up a
nonceon their<script>tag, but none declared a CSP<meta>tag — so the nonce had no effect and no CSP was enforced.Change
Added a
Content-Security-Policymeta tag to all 8 webviews. EachgetHtmlForWebviewnow readswebview.cspSourceand injects:Policy rationale
default-src 'none'— deny by default.script-src 'nonce-${nonce}'— only the bundled, nonce-tagged script runs. This activates the existing (previously inert) nonce mechanism.style-src ${cspSource} 'unsafe-inline'— these React/bundled webviews inject inline<style>tags via style-loader, so'unsafe-inline'is required (matches the VS Code docs example).img-src/font-srcallow${cspSource},https:, anddata:— needed because pages such as the Overview and Extensions Guide load extension icons and remote images.Webviews updated
src/overview/index.tssrc/welcome/index.tssrc/ext-guide/index.tssrc/beginner-tips/index.tssrc/java-runtime/index.tssrc/install-jdk/index.tssrc/formatter-settings/index.tssrc/project-settings/projectSettingsView.tsVerification
npx tsc --noEmitpasses with no errors.Notes
'unsafe-inline'for styles is the pragmatic choice given how CSS is currently injected; tightening to nonce/hash-based styles would require build-side changes.