Skip to content

Commit 061cd2a

Browse files
tsmaederfbricon
authored andcommitted
Added Content-Security-Policy meta tags to webviews
Signed-off-by: Thomas Mäder <[email protected]>
1 parent d4caa5b commit 061cd2a

File tree

7 files changed

+121
-38
lines changed

7 files changed

+121
-38
lines changed

package-lock.json

Lines changed: 96 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2024,7 +2024,7 @@
20242024
"mocha": "^11.1.0",
20252025
"request": "^2.88.2",
20262026
"sinon": "^14.0.0",
2027-
"style-loader": "^3.3.1",
2027+
"mini-css-extract-plugin": "^2.9.4",
20282028
"ts-loader": "^9.4.2",
20292029
"typescript": "^4.6.4",
20302030
"webpack": "^5.94.0",

src/dashboard/dashboard.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,27 +110,23 @@ class DashboardPanel {
110110
}
111111

112112
private getWebviewContent(): string {
113-
const scriptUri = getUri(this.webView, this.context.extensionUri, [
114-
"dist",
115-
"dashboard.js",
116-
]);
117-
118-
const nonce = getNonce();
119-
const codiconsUri = this.webView.asWebviewUri(vscode.Uri.joinPath(this.context.extensionUri, 'node_modules', '@vscode/codicons', 'dist', 'codicon.css'));
113+
const scriptUri = getUri(this.webView, this.context.extensionUri, "dist", "dashboard.js");
114+
const styleUri = getUri(this.webView, this.context.extensionUri, "dist", "dashboard.css");
120115

121116
return /* html*/ `
122117
<!DOCTYPE html>
123118
<html lang="en">
124119
<head>
125120
<meta charset="utf-8">
121+
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; script-src ${this.webView.cspSource}; font-src ${this.webView.cspSource} data:; style-src ${this.webView.cspSource};">
126122
<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
127123
<meta name="theme-color" content="#000000">
128-
<link href="${codiconsUri}" rel="stylesheet" />
129124
<title>Dashboard</title>
125+
<link href="${styleUri}" rel="stylesheet">
130126
</head>
131127
<body>
132128
<div id="root"></div>
133-
<script nonce="${nonce}" src="${scriptUri}"></script>
129+
<script src="${scriptUri}"></script>
134130
</body>
135131
</html>
136132
`;

src/refactoring/changeSignaturePanel.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,26 +108,24 @@ export class ChangeSignaturePanel {
108108
}
109109

110110
private getWebviewContent(webview: Webview, extensionUri: Uri) {
111-
112-
const scriptUri = getUri(webview, extensionUri, [
113-
"dist",
114-
"changeSignature.js",
115-
]);
116-
117-
const nonce = getNonce();
111+
const scriptUri = getUri(webview, extensionUri, "dist", "changeSignature.js");
112+
const styleUri = getUri(webview, extensionUri, "dist", "changeSignature.css");
118113

119114
return /* html*/ `
120115
<!DOCTYPE html>
121116
<html lang="en">
122117
<head>
123118
<meta charset="utf-8">
119+
<!-- We need the 'unsafe-inline' because of webview UI toolkit setting style attributes -->
120+
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; script-src ${webview.cspSource}; font-src ${webview.cspSource} data:; style-src ${webview.cspSource} 'unsafe-inline';">
124121
<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
125122
<meta name="theme-color" content="#000000">
126123
<title>Change Signature</title>
124+
<link rel = "stylesheet" href="${styleUri}">
127125
</head>
128126
<body>
129127
<div id="root"></div>
130-
<script nonce="${nonce}" src="${scriptUri}"></script>
128+
<script src="${scriptUri}"></script>
131129
</body>
132130
</html>
133131
`;

src/webview/dashboard/dashboard.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@import "../../../node_modules/@vscode/codicons/dist/codicon.css";
2+
13
.toolbar {
24
margin: 8px;
35
width: 100%;

src/webviewUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Uri, Webview } from "vscode";
22

3-
export function getUri(webview: Webview, extensionUri: Uri, pathList: string[]) {
3+
export function getUri(webview: Webview, extensionUri: Uri, ...pathList: string[]) {
44
return webview.asWebviewUri(Uri.joinPath(extensionUri, ...pathList));
55
}
66

webpack.config.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ const config = {
5353
},
5454
}
5555

56+
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
57+
5658
const configChangeSignature = {
5759
name: 'changeSignature',
5860
mode: 'none',
@@ -70,7 +72,7 @@ const configChangeSignature = {
7072
}, {
7173
test: /\.(css)$/,
7274
use: [{
73-
loader: 'style-loader'
75+
loader: MiniCssExtractPlugin.loader,
7476
}, {
7577
loader: 'css-loader'
7678
}]
@@ -86,6 +88,9 @@ const configChangeSignature = {
8688
devtoolModuleFilenameTemplate: "../[resource-path]"
8789
},
8890
plugins: [
91+
new MiniCssExtractPlugin({
92+
filename: 'changeSignature.css'
93+
}),
8994
new webpack.ProvidePlugin({
9095
process: 'process/browser',
9196
}),
@@ -113,7 +118,7 @@ const configDashboard = {
113118
}, {
114119
test: /\.(css)$/,
115120
use: [{
116-
loader: 'style-loader'
121+
loader: MiniCssExtractPlugin.loader,
117122
}, {
118123
loader: 'css-loader'
119124
}]
@@ -129,6 +134,9 @@ const configDashboard = {
129134
devtoolModuleFilenameTemplate: "../[resource-path]"
130135
},
131136
plugins: [
137+
new MiniCssExtractPlugin({
138+
filename: 'dashboard.css'
139+
}),
132140
new webpack.ProvidePlugin({
133141
process: 'process/browser',
134142
}),

0 commit comments

Comments
 (0)