From e1f3acfa9604ce6cbdde44022c26e9e8887c1afe Mon Sep 17 00:00:00 2001 From: Igor Kulikov Date: Thu, 3 Dec 2026 15:42:57 +0200 Subject: [PATCH] Sanitized the HTML in AI Assistant content (answers, action details, written files, solution panes): event handlers, javascript: links, scripts and embedded frames are dropped before rendering, tables and code blocks are kept; file names in AI toasts are escaped --- .../ai/ai-action-detail.component.html | 2 +- .../ai/ai-chat-messages.component.html | 2 +- .../home/components/ai/ai-chat.component.ts | 5 +-- .../components/ai/ai-file-body.component.ts | 2 +- .../components/ai/ai-widget-check.service.ts | 5 ++- .../pane/solution-dashboards.component.html | 2 +- .../solution-info-dialog.component.html | 2 +- .../components/markdown-html-sanitize.ts | 33 +++++++++++++++++++ .../shared/components/markdown.component.ts | 8 ++++- 9 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 ui-ngx/src/app/shared/components/markdown-html-sanitize.ts diff --git a/ui-ngx/src/app/modules/home/components/ai/ai-action-detail.component.html b/ui-ngx/src/app/modules/home/components/ai/ai-action-detail.component.html index d4eb2310ea..fd61708266 100644 --- a/ui-ngx/src/app/modules/home/components/ai/ai-action-detail.component.html +++ b/ui-ngx/src/app/modules/home/components/ai/ai-action-detail.component.html @@ -216,7 +216,7 @@ } @case ('markdown') {
- +
} @case ('images') { diff --git a/ui-ngx/src/app/modules/home/components/ai/ai-chat-messages.component.html b/ui-ngx/src/app/modules/home/components/ai/ai-chat-messages.component.html index 210cdbde74..59a674dcf7 100644 --- a/ui-ngx/src/app/modules/home/components/ai/ai-chat-messages.component.html +++ b/ui-ngx/src/app/modules/home/components/ai/ai-chat-messages.component.html @@ -177,7 +177,7 @@

{{ msg.lead }}

} @for (key of [decorationKey(msg)]; track key) { - + } @let madeHere = made[$index] ?? []; @for (shown of records[$index] ?? []; track $index) { diff --git a/ui-ngx/src/app/modules/home/components/ai/ai-chat.component.ts b/ui-ngx/src/app/modules/home/components/ai/ai-chat.component.ts index a842b04685..b7491ae3be 100644 --- a/ui-ngx/src/app/modules/home/components/ai/ai-chat.component.ts +++ b/ui-ngx/src/app/modules/home/components/ai/ai-chat.component.ts @@ -1,5 +1,6 @@ // SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. // SPDX-License-Identifier: BUSL-1.1 +import { escapeHtml } from '@home/components/iot-hub/iot-hub-markdown.utils'; import { AI_TOOLTIP_PROVIDERS } from '@home/components/ai/ai-tooltip'; import { aiCreditsLimitOf, AiCreditsLimitType } from '@home/components/ai/ai-credits.models'; import { aiClientOs, aiPageView, isAiTitledView } from '@shared/models/ai-chat.models'; @@ -689,11 +690,11 @@ export class AiChatComponent implements OnInit, OnDestroy { if (chatId) { AiChatComponent.store(AiChatComponent.UNLISTED_KEY + chatId, JSON.stringify([...next])); } - this.showToast(this.translate.instant('ai-assistant.file.unlisted', {name: file.name}), 'success'); + this.showToast(this.translate.instant('ai-assistant.file.unlisted', {name: escapeHtml(file.name)}), 'success'); } onFileDiscarded(file: AiFile): void { - this.showToast(this.translate.instant('ai-assistant.file.discarded', {name: file.name}), 'success'); + this.showToast(this.translate.instant('ai-assistant.file.discarded', {name: escapeHtml(file.name)}), 'success'); } private saveFile(content: string): Promise { diff --git a/ui-ngx/src/app/modules/home/components/ai/ai-file-body.component.ts b/ui-ngx/src/app/modules/home/components/ai/ai-file-body.component.ts index 42a7b1ca85..d184adf89b 100644 --- a/ui-ngx/src/app/modules/home/components/ai/ai-file-body.component.ts +++ b/ui-ngx/src/app/modules/home/components/ai/ai-file-body.component.ts @@ -42,7 +42,7 @@ const SAVED_MS = 1600; } @else if (file().markdown) {
- +
} @else if (file().image) {
diff --git a/ui-ngx/src/app/modules/home/components/ai/ai-widget-check.service.ts b/ui-ngx/src/app/modules/home/components/ai/ai-widget-check.service.ts index ddee5bfc4e..852110daf2 100644 --- a/ui-ngx/src/app/modules/home/components/ai/ai-widget-check.service.ts +++ b/ui-ngx/src/app/modules/home/components/ai/ai-widget-check.service.ts @@ -352,9 +352,8 @@ export class AiWidgetCheckService { } private plain(html: string): string { - const div = document.createElement('div'); - div.innerHTML = html; - return (div.textContent || '').replace(/\s+/g, ' ').trim(); + const body = new DOMParser().parseFromString(html, 'text/html').body; + return (body.textContent || '').replace(/\s+/g, ' ').trim(); } private clip(text: string): string { diff --git a/ui-ngx/src/app/modules/home/components/ai/solution/pane/solution-dashboards.component.html b/ui-ngx/src/app/modules/home/components/ai/solution/pane/solution-dashboards.component.html index db1327463d..4a24e86361 100644 --- a/ui-ngx/src/app/modules/home/components/ai/solution/pane/solution-dashboards.component.html +++ b/ui-ngx/src/app/modules/home/components/ai/solution/pane/solution-dashboards.component.html @@ -25,7 +25,7 @@ @if (dashboard.overview) {

{{ dashboard.overview }}

} - + } @empty {

{{ 'solution-creator.pane.no-dashboards' | translate }}

diff --git a/ui-ngx/src/app/modules/home/components/ai/solution/solution-info/solution-info-dialog.component.html b/ui-ngx/src/app/modules/home/components/ai/solution/solution-info/solution-info-dialog.component.html index b2ce060ca6..fb24ae192e 100644 --- a/ui-ngx/src/app/modules/home/components/ai/solution/solution-info/solution-info-dialog.component.html +++ b/ui-ngx/src/app/modules/home/components/ai/solution/solution-info/solution-info-dialog.component.html @@ -13,7 +13,7 @@
- +
@let dashboardEntries = aggregatedFor(EntityType.DASHBOARD); @if (dashboardEntries.length > 0) { diff --git a/ui-ngx/src/app/shared/components/markdown-html-sanitize.ts b/ui-ngx/src/app/shared/components/markdown-html-sanitize.ts new file mode 100644 index 0000000000..5a0907b629 --- /dev/null +++ b/ui-ngx/src/app/shared/components/markdown-html-sanitize.ts @@ -0,0 +1,33 @@ +// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. +// SPDX-License-Identifier: BUSL-1.1 + +const REMOVED_ELEMENTS = 'script,iframe,frame,frameset,object,embed,applet,link,meta,base,form,style,template,noscript,svg,math'; + +const URL_ATTRIBUTES = new Set(['href', 'src', 'srcset', 'xlink:href', 'action', 'formaction', 'background', 'poster', 'data', 'ping']); + +const UNSAFE_URL = /^(javascript|vbscript|data:(?!image\/(png|jpe?g|gif|webp|bmp);))/i; + +const COPY_CODE_CLICK = /^markdownCopyCode\(\d+\)$/; + +export function sanitizeMarkdownHtml(html: string): string { + const doc = new DOMParser().parseFromString(`${html}`, 'text/html'); + doc.body.querySelectorAll(REMOVED_ELEMENTS).forEach(element => element.remove()); + doc.body.querySelectorAll('*').forEach(element => { + for (const attribute of Array.from(element.attributes)) { + const name = attribute.name.toLowerCase(); + const value = attribute.value; + if (name.startsWith('on')) { + if (!(name === 'onclick' && COPY_CODE_CLICK.test(value.trim()))) { + element.removeAttribute(attribute.name); + } + } else if (URL_ATTRIBUTES.has(name) && UNSAFE_URL.test(value.replace(/[\u0000- \u007f-\u009f]/g, ''))) { + element.removeAttribute(attribute.name); + } else if (name === 'style' && /url\s*\(|expression\s*\(|@import/i.test(value)) { + element.removeAttribute(attribute.name); + } else if (name === 'srcdoc' || name === 'formaction') { + element.removeAttribute(attribute.name); + } + } + }); + return doc.body.innerHTML; +} diff --git a/ui-ngx/src/app/shared/components/markdown.component.ts b/ui-ngx/src/app/shared/components/markdown.component.ts index ddeacb71a2..39b4381e34 100644 --- a/ui-ngx/src/app/shared/components/markdown.component.ts +++ b/ui-ngx/src/app/shared/components/markdown.component.ts @@ -25,6 +25,7 @@ import { SHARED_MODULE_TOKEN } from '@shared/components/tokens'; import { guid, isDefinedAndNotNull } from '@core/utils'; import { Observable, of, ReplaySubject } from 'rxjs'; import { coerceBoolean } from '@shared/decorators/coercion'; +import { sanitizeMarkdownHtml } from '@shared/components/markdown-html-sanitize'; import { sanitizeTemplate } from './markdown-sanitize.helper'; let defaultMarkdownStyle: string; @@ -68,6 +69,10 @@ export class TbMarkdownComponent implements OnChanges { @coerceBoolean() usePlainMarkdown = false; + @Input() + @coerceBoolean() + sanitizeHtml = false; + @Input() @coerceBoolean() highlight = true; @@ -106,7 +111,8 @@ export class TbMarkdownComponent implements OnChanges { } private render(markdown: string) { - const compiled = this.markdownService.parse(markdown, { decodeHtml: false, disableSanitizer: true }); + const parsed = this.markdownService.parse(markdown, { decodeHtml: false, disableSanitizer: true }); + const compiled = this.sanitizeHtml ? sanitizeMarkdownHtml(parsed as string) : parsed; let markdownClass = 'tb-markdown-view'; if (this.markdownClass) { markdownClass += ` ${this.markdownClass}`;