Skip to content

Commit 60d74ed

Browse files
committed
fix: bad nonce handling in realtime view
1 parent 01ce805 commit 60d74ed

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
"compile:table": "lerna run --scope vscode-js-profile-table compile",
1010
"compile:flame": "lerna run --scope vscode-js-profile-flame compile",
1111
"watch": "concurrently \"npm:watch:core:*\" \"npm:watch:flame:*\" \"npm:watch:table:*\"",
12-
"watch:core": "lerna run --scope vscode-js-profile-core watch",
13-
"watch:table": "lerna run --scope vscode-js-profile-table watch",
14-
"watch:flame": "lerna run --scope vscode-js-profile-flame watch",
12+
"watch:core": "lerna run --stream --scope vscode-js-profile-core watch",
13+
"watch:table": "lerna run --stream --scope vscode-js-profile-table watch",
14+
"watch:flame": "lerna run --stream --scope vscode-js-profile-flame watch",
1515
"pack": "npm run compile && lerna run pack",
1616
"fmt": "prettier --write \"packages/**/*.{ts,tsx,css}\" \"!**/out/**\" && npm run test:lint -- --fix",
1717
"test": "concurrently \"npm:test:*\"",

packages/vscode-js-profile-core/src/bundlePage.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
*--------------------------------------------------------*/
44

55
import * as vscode from 'vscode';
6+
import { makeNonce, nonceHeader } from './nonce';
67

78
export const bundlePage = async (bundleUri: vscode.Uri, constants: { [key: string]: unknown }) => {
8-
const nonce = Math.random().toString();
9+
const nonce = makeNonce();
910
const html = `<!DOCTYPE html>
1011
<html lang="en">
1112
<head>
1213
<meta charset="UTF-8">
1314
<meta name="viewport" content="width=device-width, initial-scale=1.0">
15+
${nonceHeader(nonce)}
1416
<title>Profile Custom Editor</title>
1517
</head>
1618
<body>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*---------------------------------------------------------
2+
* Copyright (C) Microsoft Corporation. All rights reserved.
3+
*--------------------------------------------------------*/
4+
5+
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
6+
7+
export function makeNonce(length = 32) {
8+
let str = '';
9+
for (let i = 0; i < length; i++) {
10+
str += chars[Math.floor(Math.random() * chars.length)];
11+
}
12+
13+
return str;
14+
}
15+
16+
export const nonceHeader = (nonce: string) =>
17+
`<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src 'unsafe-inline'; script-src 'nonce-${nonce}';">`;

packages/vscode-js-profile-flame/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"watch": "concurrently \"npm:watch:*\"",
3737
"watch:client": "webpack --mode development --config webpack.client.js --watch",
3838
"watch:realtime": "webpack --mode development --config webpack.realtime.js --watch",
39-
"watch:ext": "webpack --mode development --config webpack.extension.js --watch"
39+
"watch:ext": "webpack --mode development --config webpack.extension.js --watch --target node"
4040
},
4141
"icon": "resources/logo.png",
4242
"activationEvents": [

packages/vscode-js-profile-flame/src/realtimeWebviewProvider.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*--------------------------------------------------------*/
44

55
import * as vscode from 'vscode';
6+
import { makeNonce, nonceHeader } from 'vscode-js-profile-core/out/nonce';
67
import { FromWebViewMessage, MessageType } from './realtime/protocol';
78
import { RealtimeSessionTracker } from './realtimeSessionTracker';
89

@@ -40,13 +41,13 @@ export class RealtimeWebviewProvider implements vscode.WebviewViewProvider {
4041
const scriptUri = webview.asWebviewUri(
4142
vscode.Uri.joinPath(this.extensionUri, 'out', 'realtime.bundle.js'),
4243
);
43-
const nonce = Math.random().toString();
44+
const nonce = makeNonce();
4445

4546
return `<!DOCTYPE html>
4647
<html lang="en">
4748
<head>
4849
<meta charset="UTF-8">
49-
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src 'unsafe-inline'; script-src 'nonce-${nonce}';">
50+
${nonceHeader(nonce)}
5051
<meta name="viewport" content="width=device-width, initial-scale=1.0">
5152
<title>Realtime Performance</title>
5253
</head>

0 commit comments

Comments
 (0)