Adopt uuids for generating webview nonces

This commit is contained in:
Matt Bierner
2025-10-29 13:53:47 -07:00
parent f8e2f71c2f
commit c47a56c7bc
17 changed files with 291 additions and 45 deletions

View File

@@ -12,6 +12,7 @@
"@vscode/extension-telemetry": "^0.9.8"
},
"devDependencies": {
"@types/node": "22.x",
"@types/vscode-webview": "^1.57.0",
"@vscode/codicons": "^0.0.36"
},
@@ -143,6 +144,16 @@
"integrity": "sha512-OUUJTh3fnaUSzg9DEHgv3d7jC+DnPL65mIO7RaR+jWve7+MmcgIvF79gY97DPQ4frH+IpNR78YAYd/dW4gK3kg==",
"license": "MIT"
},
"node_modules/@types/node": {
"version": "22.18.13",
"resolved": "https://registry.npmjs.org/@types/node/-/node-22.18.13.tgz",
"integrity": "sha512-Bo45YKIjnmFtv6I1TuC8AaHBbqXtIo+Om5fE4QiU1Tj8QR/qt+8O3BAtOimG5IFmwaWiPmB3Mv3jtYzBA4Us2A==",
"dev": true,
"license": "MIT",
"dependencies": {
"undici-types": "~6.21.0"
}
},
"node_modules/@types/vscode-webview": {
"version": "1.57.0",
"resolved": "https://registry.npmjs.org/@types/vscode-webview/-/vscode-webview-1.57.0.tgz",
@@ -169,6 +180,13 @@
"engines": {
"vscode": "^1.75.0"
}
},
"node_modules/undici-types": {
"version": "6.21.0",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.21.0.tgz",
"integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==",
"dev": true,
"license": "MIT"
}
}
}

View File

@@ -69,6 +69,7 @@
"@vscode/extension-telemetry": "^0.9.8"
},
"devDependencies": {
"@types/node": "22.x",
"@types/vscode-webview": "^1.57.0",
"@vscode/codicons": "^0.0.36"
},

View File

@@ -5,6 +5,7 @@
import * as vscode from 'vscode';
import { Disposable } from './dispose';
import { generateUuid } from './uuid';
export interface ShowOptions {
@@ -112,7 +113,7 @@ export class SimpleBrowserView extends Disposable {
private getHtml(url: string) {
const configuration = vscode.workspace.getConfiguration('simpleBrowser');
const nonce = getNonce();
const nonce = generateUuid();
const mainJs = this.extensionResourceUrl('media', 'index.js');
const mainCss = this.extensionResourceUrl('media', 'main.css');
@@ -181,12 +182,3 @@ export class SimpleBrowserView extends Disposable {
function escapeAttribute(value: string | vscode.Uri): string {
return value.toString().replace(/"/g, '"');
}
function getNonce() {
let text = '';
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < 64; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}

View File

@@ -0,0 +1,58 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/**
* Copied from src/vs/base/common/uuid.ts
*/
export function generateUuid(): string {
// use `randomUUID` if possible
if (typeof crypto.randomUUID === 'function') {
// see https://developer.mozilla.org/en-US/docs/Web/API/Window/crypto
// > Although crypto is available on all windows, the returned Crypto object only has one
// > usable feature in insecure contexts: the getRandomValues() method.
// > In general, you should use this API only in secure contexts.
return crypto.randomUUID.bind(crypto)();
}
// prep-work
const _data = new Uint8Array(16);
const _hex: string[] = [];
for (let i = 0; i < 256; i++) {
_hex.push(i.toString(16).padStart(2, '0'));
}
// get data
crypto.getRandomValues(_data);
// set version bits
_data[6] = (_data[6] & 0x0f) | 0x40;
_data[8] = (_data[8] & 0x3f) | 0x80;
// print as string
let i = 0;
let result = '';
result += _hex[_data[i++]];
result += _hex[_data[i++]];
result += _hex[_data[i++]];
result += _hex[_data[i++]];
result += '-';
result += _hex[_data[i++]];
result += _hex[_data[i++]];
result += '-';
result += _hex[_data[i++]];
result += _hex[_data[i++]];
result += '-';
result += _hex[_data[i++]];
result += _hex[_data[i++]];
result += '-';
result += _hex[_data[i++]];
result += _hex[_data[i++]];
result += _hex[_data[i++]];
result += _hex[_data[i++]];
result += _hex[_data[i++]];
result += _hex[_data[i++]];
return result;
}

View File

@@ -2,7 +2,6 @@
"extends": "../tsconfig.base.json",
"compilerOptions": {
"outDir": "./out",
"types": [],
"typeRoots": [
"./node_modules/@types"
]