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) {
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}`;