Skip to content

Commit aaf48e5

Browse files
committed
Add Content Security Policy and script nonces to webview HTML utility
└── apps └── editor └── src └── views ├── chats │ └── backend │ └── chats-view-provider.ts ├── prompt │ └── backend │ ├── message-handlers │ │ └── handle-open-website.ts │ └── prompt-view-provider.ts ├── settings │ └── backend │ └── settings-view-provider.ts └── shared └── utils └── webview-html.ts
1 parent b1b0af4 commit aaf48e5

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

‎apps/editor/src/views/shared/utils/webview-html.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
import * as vscode from 'vscode'
22

3+
function getNonce() {
4+
let text = ''
5+
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
6+
for (let i = 0; i < 32; i++) {
7+
text += possible.charAt(Math.floor(Math.random() * possible.length))
8+
}
9+
return text
10+
}
11+
312
export const webview_html = (params: {
413
webview: vscode.Webview
514
extension_uri: vscode.Uri
@@ -23,19 +32,23 @@ export const webview_html = (params: {
2332
)
2433
)
2534

35+
const nonce = getNonce()
36+
2637
const resources_uri = params.webview.asWebviewUri(
2738
vscode.Uri.joinPath(params.extension_uri, 'resources')
2839
)
29-
const resources_script = `\n <script>\n window.resources_uri = "${resources_uri}";\n </script>`
40+
const resources_script = `\n <script nonce="${nonce}">\n window.resources_uri = "${resources_uri}";\n </script>`
3041

3142
const overflow_style = params.overflow_hidden ? '\n body { }' : ''
3243

3344
const title_tag = params.title ? `\n <title>${params.title}</title>` : ''
45+
const csp_source = params.webview.cspSource
3446

3547
return `<!DOCTYPE html>
3648
<html lang="${vscode.env.language}">
3749
<head>
3850
<meta charset="UTF-8">
51+
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src ${csp_source} https: data: blob:; script-src 'nonce-${nonce}' ${csp_source} 'unsafe-eval'; style-src ${csp_source} 'unsafe-inline'; font-src ${csp_source} data:; connect-src ${csp_source} https: http: ws: wss:; worker-src ${csp_source} blob: data:; media-src ${csp_source} data:;">
3952
<meta name="viewport" content="width=device-width, initial-scale=1.0">
4053
<link rel="stylesheet" href="${style_uri}">${resources_script}
4154
<style>
@@ -47,7 +60,7 @@ export const webview_html = (params: {
4760
</head>
4861
<body>
4962
<div id="root"></div>
50-
<script src="${script_uri}"></script>
63+
<script nonce="${nonce}" src="${script_uri}"></script>
5164
</body>
5265
</html>`
5366
}

0 commit comments

Comments
 (0)