Skip to content

Commit f7f1a72

Browse files
authored
Adopt using uuids for generating webview nonces (#8057)
1 parent 34c4d49 commit f7f1a72

File tree

5 files changed

+67
-15
lines changed

5 files changed

+67
-15
lines changed

src/common/uuid.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
/**
7+
* Copied from vscode/src/vs/base/common/uuid.ts
8+
*/
9+
export function generateUuid(): string {
10+
// use `randomUUID` if possible
11+
if (typeof crypto.randomUUID === 'function') {
12+
// see https://developer.mozilla.org/en-US/docs/Web/API/Window/crypto
13+
// > Although crypto is available on all windows, the returned Crypto object only has one
14+
// > usable feature in insecure contexts: the getRandomValues() method.
15+
// > In general, you should use this API only in secure contexts.
16+
17+
return crypto.randomUUID.bind(crypto)();
18+
}
19+
20+
// prep-work
21+
const _data = new Uint8Array(16);
22+
const _hex: string[] = [];
23+
for (let i = 0; i < 256; i++) {
24+
_hex.push(i.toString(16).padStart(2, '0'));
25+
}
26+
27+
// get data
28+
crypto.getRandomValues(_data);
29+
30+
// set version bits
31+
_data[6] = (_data[6] & 0x0f) | 0x40;
32+
_data[8] = (_data[8] & 0x3f) | 0x80;
33+
34+
// print as string
35+
let i = 0;
36+
let result = '';
37+
result += _hex[_data[i++]];
38+
result += _hex[_data[i++]];
39+
result += _hex[_data[i++]];
40+
result += _hex[_data[i++]];
41+
result += '-';
42+
result += _hex[_data[i++]];
43+
result += _hex[_data[i++]];
44+
result += '-';
45+
result += _hex[_data[i++]];
46+
result += _hex[_data[i++]];
47+
result += '-';
48+
result += _hex[_data[i++]];
49+
result += _hex[_data[i++]];
50+
result += '-';
51+
result += _hex[_data[i++]];
52+
result += _hex[_data[i++]];
53+
result += _hex[_data[i++]];
54+
result += _hex[_data[i++]];
55+
result += _hex[_data[i++]];
56+
result += _hex[_data[i++]];
57+
return result;
58+
}

src/common/webview.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,6 @@ export interface IReplyMessage {
2222
res?: any;
2323
}
2424

25-
export function getNonce() {
26-
let text = '';
27-
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
28-
for (let i = 0; i < 32; i++) {
29-
text += possible.charAt(Math.floor(Math.random() * possible.length));
30-
}
31-
return text;
32-
}
33-
3425
export class WebviewBase extends Disposable {
3526
protected _webview?: vscode.Webview;
3627

src/github/activityBarViewProvider.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import { emojify, ensureEmojis } from '../common/emoji';
1717
import { disposeAll } from '../common/lifecycle';
1818
import { ReviewEvent } from '../common/timelineEvent';
1919
import { formatError } from '../common/utils';
20-
import { getNonce, IRequestMessage, WebviewViewBase } from '../common/webview';
20+
import { generateUuid } from '../common/uuid';
21+
import { IRequestMessage, WebviewViewBase } from '../common/webview';
2122
import { ReviewManager } from '../view/reviewManager';
2223

2324
export class PullRequestViewProvider extends WebviewViewBase implements vscode.WebviewViewProvider {
@@ -489,7 +490,7 @@ export class PullRequestViewProvider extends WebviewViewBase implements vscode.W
489490
}
490491

491492
private _getHtmlForWebview() {
492-
const nonce = getNonce();
493+
const nonce = generateUuid();
493494

494495
const uri = vscode.Uri.joinPath(this._extensionUri, 'dist', 'webview-open-pr-view.js');
495496

src/github/createPRViewProvider.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ import {
3838
} from '../common/settingKeys';
3939
import { ITelemetry } from '../common/telemetry';
4040
import { asPromise, compareIgnoreCase, formatError, promiseWithTimeout } from '../common/utils';
41-
import { getNonce, IRequestMessage, WebviewViewBase } from '../common/webview';
41+
import { generateUuid } from '../common/uuid';
42+
import { IRequestMessage, WebviewViewBase } from '../common/webview';
4243
import { PREVIOUS_CREATE_METHOD } from '../extensionState';
4344
import { CreatePullRequestDataModel } from '../view/createPullRequestDataModel';
4445

@@ -549,7 +550,7 @@ export abstract class BaseCreatePullRequestViewProvider<T extends BasePullReques
549550
}
550551

551552
private _getHtmlForWebview() {
552-
const nonce = getNonce();
553+
const nonce = generateUuid();
553554

554555
const uri = vscode.Uri.joinPath(this._extensionUri, 'dist', 'webview-create-pr-view-new.js');
555556

src/github/issueOverview.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import { PR_SETTINGS_NAMESPACE, WEBVIEW_REFRESH_INTERVAL } from '../common/setti
2020
import { ITelemetry } from '../common/telemetry';
2121
import { CommentEvent, EventType, ReviewStateValue, TimelineEvent } from '../common/timelineEvent';
2222
import { asPromise, formatError } from '../common/utils';
23-
import { getNonce, IRequestMessage, WebviewBase } from '../common/webview';
23+
import { generateUuid } from '../common/uuid';
24+
import { IRequestMessage, WebviewBase } from '../common/webview';
2425

2526
export class IssueOverviewPanel<TItem extends IssueModel = IssueModel> extends WebviewBase {
2627
public static ID: string = 'IssueOverviewPanel';
@@ -705,7 +706,7 @@ export class IssueOverviewPanel<TItem extends IssueModel = IssueModel> extends W
705706
}
706707

707708
protected getHtmlForWebview() {
708-
const nonce = getNonce();
709+
const nonce = generateUuid();
709710

710711
const uri = vscode.Uri.joinPath(this._extensionUri, 'dist', 'webview-pr-description.js');
711712

0 commit comments

Comments
 (0)