Skip to content

Commit c0f4a57

Browse files
committed
use trusted html in issue reporter, fyi @RMacfarlane, #106395
1 parent f8c0e3b commit c0f4a57

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

src/vs/base/browser/dom.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import * as platform from 'vs/base/common/platform';
1515
import { URI } from 'vs/base/common/uri';
1616
import { FileAccess, RemoteAuthorities } from 'vs/base/common/network';
1717
import { BrowserFeatures } from 'vs/base/browser/canIUse';
18+
import { insane, InsaneOptions } from 'vs/base/common/insane/insane';
1819

1920
export function clearNode(node: HTMLElement): void {
2021
while (node.firstChild) {
@@ -1311,3 +1312,48 @@ export function detectFullscreen(): IDetectedFullscreen | null {
13111312
// Not in fullscreen
13121313
return null;
13131314
}
1315+
1316+
// -- sanitize and trusted html
1317+
1318+
function newInsaneOptions(allowedTags: string[], allowedAttributesForAll: string[], allowedAttributes: Record<string, string[]>): InsaneOptions {
1319+
for (let tag of allowedTags) {
1320+
let array = allowedAttributes[tag];
1321+
if (!array) {
1322+
array = allowedAttributesForAll;
1323+
} else {
1324+
array = array.concat(allowedAttributesForAll);
1325+
}
1326+
allowedAttributes[tag] = array;
1327+
}
1328+
const value: InsaneOptions = {
1329+
allowedTags,
1330+
allowedAttributes,
1331+
};
1332+
return value;
1333+
}
1334+
1335+
1336+
const _ttpStaticHtml = window.trustedTypes?.createPolicy('staticHtml', {
1337+
createHTML(value, options: InsaneOptions) {
1338+
return insane(value, options);
1339+
}
1340+
});
1341+
1342+
export function sanitizeStaticHtml(value: string): TrustedHTML | string {
1343+
1344+
const options = newInsaneOptions(
1345+
['a', 'button', 'code', 'div', 'h1', 'h2', 'h3', 'input', 'label', 'li', 'p', 'pre', 'select', 'small', 'span', 'textarea', 'ul'],
1346+
['class', 'id', 'role', 'tabindex'],
1347+
{
1348+
'a': ['href'],
1349+
'button': ['data-href'],
1350+
'input': ['type', 'placeholder', 'checked', 'required'],
1351+
'label': ['for'],
1352+
'select': ['required'],
1353+
'span': ['data-command', 'role'],
1354+
'textarea': ['name', 'placeholder', 'required'],
1355+
}
1356+
);
1357+
1358+
return _ttpStaticHtml?.createHTML(value, options) ?? insane(value, options);
1359+
}

src/vs/code/electron-sandbox/issue/issueReporterMain.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { INativeHostService } from 'vs/platform/native/electron-sandbox/native';
99
import { NativeHostService } from 'vs/platform/native/electron-sandbox/nativeHostService';
1010
import { ipcRenderer, process } from 'vs/base/parts/sandbox/electron-sandbox/globals';
1111
import { applyZoom, zoomIn, zoomOut } from 'vs/platform/windows/electron-sandbox/window';
12-
import { $, reset, windowOpenNoOpener } from 'vs/base/browser/dom';
12+
import { $, reset, sanitizeStaticHtml, windowOpenNoOpener } from 'vs/base/browser/dom';
1313
import { Button } from 'vs/base/browser/ui/button/button';
1414
import { CodiconLabel } from 'vs/base/browser/ui/codicons/codiconLabel';
1515
import * as collections from 'vs/base/common/collections';
@@ -58,7 +58,9 @@ export function startup(configuration: IssueReporterConfiguration) {
5858
const platformClass = platform.isWindows ? 'windows' : platform.isLinux ? 'linux' : 'mac';
5959
document.body.classList.add(platformClass); // used by our fonts
6060

61-
document.body.innerHTML = BaseHtml();
61+
const baseHtml = sanitizeStaticHtml(BaseHtml());
62+
document.body.innerHTML = baseHtml as unknown as string;
63+
6264
const issueReporter = new IssueReporter(configuration);
6365
issueReporter.render();
6466
document.body.style.display = 'block';

0 commit comments

Comments
 (0)