Skip to content

Commit 22be4e3

Browse files
mjbvzosortega
authored andcommitted
Merge pull request #256579 from mjbvz/surviving-cicada
Small additional cleanups to domSanitize
2 parents c55a054 + dd98df8 commit 22be4e3

File tree

9 files changed

+33
-26
lines changed

9 files changed

+33
-26
lines changed

src/vs/base/browser/domSanitize.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ export interface SanitizeOptions {
184184
readonly override?: readonly string[];
185185
};
186186

187-
readonly hooks?: {
187+
// TODO: move these into more controlled api
188+
readonly _do_not_use_hooks?: {
188189
readonly uponSanitizeElement?: UponSanitizeElementCb;
189190
readonly uponSanitizeAttribute?: UponSanitizeAttributeCb;
190191
};
@@ -218,7 +219,7 @@ export function sanitizeHtml(untrusted: string, config?: SanitizeOptions): Trust
218219
resolvedConfig.ALLOWED_TAGS = [...config.allowedTags.override];
219220
}
220221

221-
if (config?.allowedTags?.augment) {
222+
if (config.allowedTags.augment) {
222223
resolvedConfig.ALLOWED_TAGS = [...(resolvedConfig.ALLOWED_TAGS ?? []), ...config.allowedTags.augment];
223224
}
224225
}
@@ -228,7 +229,7 @@ export function sanitizeHtml(untrusted: string, config?: SanitizeOptions): Trust
228229
resolvedConfig.ALLOWED_ATTR = [...config.allowedAttributes.override];
229230
}
230231

231-
if (config?.allowedAttributes?.augment) {
232+
if (config.allowedAttributes.augment) {
232233
resolvedConfig.ALLOWED_ATTR = [...(resolvedConfig.ALLOWED_ATTR ?? []), ...config.allowedAttributes.augment];
233234
}
234235
}
@@ -237,12 +238,12 @@ export function sanitizeHtml(untrusted: string, config?: SanitizeOptions): Trust
237238
config?.allowedLinkProtocols?.override ?? [Schemas.http, Schemas.https],
238239
config?.allowedMediaProtocols?.override ?? [Schemas.http, Schemas.https]));
239240

240-
if (config?.hooks?.uponSanitizeElement) {
241-
store.add(addDompurifyHook('uponSanitizeElement', config?.hooks.uponSanitizeElement));
241+
if (config?._do_not_use_hooks?.uponSanitizeElement) {
242+
store.add(addDompurifyHook('uponSanitizeElement', config?._do_not_use_hooks.uponSanitizeElement));
242243
}
243244

244-
if (config?.hooks?.uponSanitizeAttribute) {
245-
store.add(addDompurifyHook('uponSanitizeAttribute', config.hooks.uponSanitizeAttribute));
245+
if (config?._do_not_use_hooks?.uponSanitizeAttribute) {
246+
store.add(addDompurifyHook('uponSanitizeAttribute', config._do_not_use_hooks.uponSanitizeAttribute));
246247
}
247248

248249
return dompurify.sanitize(untrusted, {
@@ -257,6 +258,6 @@ export function sanitizeHtml(untrusted: string, config?: SanitizeOptions): Trust
257258
/**
258259
* Sanitizes the given `value` and reset the given `node` with it.
259260
*/
260-
export function safeInnerHtml(node: HTMLElement, untrusted: string, config?: SanitizeOptions): void {
261+
export function safeSetInnerHtml(node: HTMLElement, untrusted: string, config?: SanitizeOptions): void {
261262
node.innerHTML = sanitizeHtml(untrusted, config) as any;
262263
}

src/vs/base/browser/markdownRenderer.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ export interface MarkdownRenderOptions extends FormattedTextRenderOptions {
4444

4545
export interface ISanitizerOptions {
4646
readonly replaceWithPlaintext?: boolean;
47-
readonly allowedTags?: readonly string[];
47+
readonly allowedTags?: {
48+
readonly override: readonly string[];
49+
};
4850
readonly customAttrSanitizer?: (attrName: string, attrValue: string) => boolean | string;
4951
readonly allowedProductProtocols?: readonly string[];
5052
}
@@ -472,7 +474,7 @@ function getSanitizerOptions(options: IInternalSanitizerOptions): domSanitize.Sa
472474
// HTML tags that can result from markdown are from reading https://spec.commonmark.org/0.29/
473475
// HTML table tags that can result from markdown are from https://github.github.com/gfm/#tables-extension-
474476
allowedTags: {
475-
override: options.allowedTags ?? domSanitize.basicMarkupHtmlTags
477+
override: options.allowedTags?.override ?? domSanitize.basicMarkupHtmlTags
476478
},
477479
allowedAttributes: {
478480
override: allowedMarkdownHtmlAttributes,
@@ -491,7 +493,7 @@ function getSanitizerOptions(options: IInternalSanitizerOptions): domSanitize.Sa
491493
Schemas.vscodeRemoteResource,
492494
]
493495
},
494-
hooks: {
496+
_do_not_use_hooks: {
495497
uponSanitizeAttribute: (element, e) => {
496498
if (options.customAttrSanitizer) {
497499
const result = options.customAttrSanitizer(e.attrName, e.attrValue);

src/vs/base/browser/ui/button/button.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { localize } from '../../../../nls.js';
2424
import type { IManagedHover } from '../hover/hover.js';
2525
import { getBaseLayerHoverDelegate } from '../hover/hoverDelegate2.js';
2626
import { IActionProvider } from '../dropdown/dropdown.js';
27-
import { safeInnerHtml, SanitizeOptions } from '../../domSanitize.js';
27+
import { safeSetInnerHtml, SanitizeOptions } from '../../domSanitize.js';
2828

2929
export interface IButtonOptions extends Partial<IButtonStyles> {
3030
readonly title?: boolean | string;
@@ -253,7 +253,7 @@ export class Button extends Disposable implements IButton {
253253
// Don't include outer `<p>`
254254
const root = rendered.element.querySelector('p')?.innerHTML;
255255
if (root) {
256-
safeInnerHtml(labelElement, root, buttonSanitizerOptions);
256+
safeSetInnerHtml(labelElement, root, buttonSanitizerOptions);
257257
} else {
258258
reset(labelElement);
259259
}
@@ -654,7 +654,7 @@ export class ButtonWithIcon extends Button {
654654

655655
const root = rendered.element.querySelector('p')?.innerHTML;
656656
if (root) {
657-
safeInnerHtml(this._mdlabelElement, root, buttonSanitizerOptions);
657+
safeSetInnerHtml(this._mdlabelElement, root, buttonSanitizerOptions);
658658
} else {
659659
reset(this._mdlabelElement);
660660
}

src/vs/workbench/contrib/chat/browser/chatMarkdownRenderer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ export class ChatMarkdownRenderer extends MarkdownRenderer {
7676
remoteImageIsAllowed: (_uri) => false,
7777
sanitizerOptions: {
7878
replaceWithPlaintext: true,
79-
allowedTags: allowedChatMarkdownHtmlTags,
79+
allowedTags: {
80+
override: allowedChatMarkdownHtmlTags,
81+
},
8082
...options?.sanitizerOptions,
8183
allowedProductProtocols: [product.urlProtocol]
8284
}

src/vs/workbench/contrib/issue/browser/issueFormService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (c) Microsoft Corporation. All rights reserved.
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
5-
import { safeInnerHtml } from '../../../../base/browser/domSanitize.js';
5+
import { safeSetInnerHtml } from '../../../../base/browser/domSanitize.js';
66
import { DisposableStore } from '../../../../base/common/lifecycle.js';
77
import { isLinux, isWindows } from '../../../../base/common/platform.js';
88
import Severity from '../../../../base/common/severity.js';
@@ -93,7 +93,7 @@ export class IssueFormService implements IIssueFormService {
9393
// removes preset monaco-workbench
9494
auxiliaryWindow.container.remove();
9595
auxiliaryWindow.window.document.body.appendChild(div);
96-
safeInnerHtml(div, BaseHtml(), {
96+
safeSetInnerHtml(div, BaseHtml(), {
9797
// Also allow input elements
9898
allowedTags: {
9999
augment: [

src/vs/workbench/contrib/markdown/browser/markedKatexSupport.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ export class MarkedKatexSupport {
1616
readonly allowedAttributes: readonly string[];
1717
}): ISanitizerOptions {
1818
return {
19-
allowedTags: [
20-
...baseConfig.allowedTags,
21-
...trustedMathMlTags,
22-
],
19+
allowedTags: {
20+
override: [
21+
...baseConfig.allowedTags,
22+
...trustedMathMlTags,
23+
]
24+
},
2325
customAttrSanitizer: (attrName, attrValue) => {
2426
if (attrName === 'class') {
2527
return true; // TODO: allows all classes for now since we don't have a list of possible katex classes

src/vs/workbench/contrib/notebook/browser/view/cellParts/codeCell.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ export class CodeCell extends Disposable {
362362
if (this.viewCell.isInputCollapsed && this._inputCollapseElement) {
363363
// flush the collapsed input with the latest tokens
364364
const content = this._getRichTextFromLineTokens(model);
365-
domSanitize.safeInnerHtml(this._inputCollapseElement, content);
365+
domSanitize.safeSetInnerHtml(this._inputCollapseElement, content);
366366
this._attachInputExpandButton(this._inputCollapseElement);
367367
}
368368
}));
@@ -442,7 +442,7 @@ export class CodeCell extends Disposable {
442442
// update preview
443443
const richEditorText = this.templateData.editor.hasModel() ? this._getRichTextFromLineTokens(this.templateData.editor.getModel()) : this._getRichText(this.viewCell.textBuffer, this.viewCell.language);
444444
const element = DOM.$('div.cell-collapse-preview');
445-
domSanitize.safeInnerHtml(element, richEditorText);
445+
domSanitize.safeSetInnerHtml(element, richEditorText);
446446
this._inputCollapseElement = element;
447447
this.templateData.cellInputCollapsedContainer.appendChild(element);
448448
this._attachInputExpandButton(element);

src/vs/workbench/contrib/notebook/browser/view/cellParts/markupCell.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ export class MarkupCell extends Disposable {
262262
const element = DOM.$('div');
263263
element.classList.add('cell-collapse-preview');
264264
const richEditorText = this.getRichText(this.viewCell.textBuffer, this.viewCell.language);
265-
domSanitize.safeInnerHtml(element, richEditorText);
265+
domSanitize.safeSetInnerHtml(element, richEditorText);
266266
this.templateData.cellInputCollapsedContainer.appendChild(element);
267267

268268
const expandIcon = DOM.append(element, DOM.$('span.expandInputIcon'));
@@ -404,7 +404,7 @@ export class MarkupCell extends Disposable {
404404
this.markdownAccessibilityContainer.innerText = '';
405405
if (this.viewCell.renderedHtml) {
406406
if (this.accessibilityService.isScreenReaderOptimized()) {
407-
domSanitize.safeInnerHtml(this.markdownAccessibilityContainer, this.viewCell.renderedHtml);
407+
domSanitize.safeSetInnerHtml(this.markdownAccessibilityContainer, this.viewCell.renderedHtml);
408408
} else {
409409
DOM.clearNode(this.markdownAccessibilityContainer);
410410
}

src/vs/workbench/contrib/welcomeWalkthrough/browser/walkThroughPart.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ export class WalkThroughPart extends EditorPane {
381381
}
382382

383383
private safeSetInnerHtml(node: HTMLElement, content: string) {
384-
domSanitize.safeInnerHtml(node, content, {
384+
domSanitize.safeSetInnerHtml(node, content, {
385385
allowedAttributes: {
386386
augment: [
387387
'id',

0 commit comments

Comments
 (0)