Browse Source

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

pull/16882/head
Igor Kulikov 2 days ago
parent
commit
e1f3acfa96
  1. 2
      ui-ngx/src/app/modules/home/components/ai/ai-action-detail.component.html
  2. 2
      ui-ngx/src/app/modules/home/components/ai/ai-chat-messages.component.html
  3. 5
      ui-ngx/src/app/modules/home/components/ai/ai-chat.component.ts
  4. 2
      ui-ngx/src/app/modules/home/components/ai/ai-file-body.component.ts
  5. 5
      ui-ngx/src/app/modules/home/components/ai/ai-widget-check.service.ts
  6. 2
      ui-ngx/src/app/modules/home/components/ai/solution/pane/solution-dashboards.component.html
  7. 2
      ui-ngx/src/app/modules/home/components/ai/solution/solution-info/solution-info-dialog.component.html
  8. 33
      ui-ngx/src/app/shared/components/markdown-html-sanitize.ts
  9. 8
      ui-ngx/src/app/shared/components/markdown.component.ts

2
ui-ngx/src/app/modules/home/components/ai/ai-action-detail.component.html

@ -216,7 +216,7 @@
}
@case ('markdown') {
<div class="k-detail-md">
<tb-markdown usePlainMarkdown [highlight]="false" [data]="block.text"></tb-markdown>
<tb-markdown usePlainMarkdown sanitizeHtml [highlight]="false" [data]="block.text"></tb-markdown>
</div>
}
@case ('images') {

2
ui-ngx/src/app/modules/home/components/ai/ai-chat-messages.component.html

@ -177,7 +177,7 @@
<p class="lead">{{ msg.lead }}</p>
}
@for (key of [decorationKey(msg)]; track key) {
<tb-markdown usePlainMarkdown [highlight]="false" [additionalStyles]="additionalStyles" [data]="parseResult(msg.done !== undefined ? withoutDoneMark(msg.content) : msg.content)" (ready)="decorateCode(answer, msg)"></tb-markdown>
<tb-markdown usePlainMarkdown sanitizeHtml [highlight]="false" [additionalStyles]="additionalStyles" [data]="parseResult(msg.done !== undefined ? withoutDoneMark(msg.content) : msg.content)" (ready)="decorateCode(answer, msg)"></tb-markdown>
}
@let madeHere = made[$index] ?? [];
@for (shown of records[$index] ?? []; track $index) {

5
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<boolean> {

2
ui-ngx/src/app/modules/home/components/ai/ai-file-body.component.ts

@ -42,7 +42,7 @@ const SAVED_MS = 1600;
</div>
} @else if (file().markdown) {
<div class="file-md art-page agent">
<tb-markdown usePlainMarkdown [data]="file().content" [additionalStyles]="markdownStyles"></tb-markdown>
<tb-markdown usePlainMarkdown sanitizeHtml [data]="file().content" [additionalStyles]="markdownStyles"></tb-markdown>
</div>
} @else if (file().image) {
<div class="file-svg">

5
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 {

2
ui-ngx/src/app/modules/home/components/ai/solution/pane/solution-dashboards.component.html

@ -25,7 +25,7 @@
@if (dashboard.overview) {
<p class="sol-dash-lead">{{ dashboard.overview }}</p>
}
<tb-markdown class="sol-dash-doc" usePlainMarkdown [additionalStyles]="markdownStyles" [data]="details(dashboard)"></tb-markdown>
<tb-markdown class="sol-dash-doc" usePlainMarkdown sanitizeHtml [additionalStyles]="markdownStyles" [data]="details(dashboard)"></tb-markdown>
</section>
} @empty {
<p class="sol-empty">{{ 'solution-creator.pane.no-dashboards' | translate }}</p>

2
ui-ngx/src/app/modules/home/components/ai/solution/solution-info/solution-info-dialog.component.html

@ -13,7 +13,7 @@
</mat-toolbar>
<div mat-dialog-content class="relative flex flex-col gap-2">
<div class="text-justify">
<tb-markdown usePlainMarkdown [data]=solution.data.solutionDescription></tb-markdown>
<tb-markdown usePlainMarkdown sanitizeHtml [data]=solution.data.solutionDescription></tb-markdown>
</div>
@let dashboardEntries = aggregatedFor(EntityType.DASHBOARD);
@if (dashboardEntries.length > 0) {

33
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(`<!DOCTYPE html><html><body>${html}</body></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;
}

8
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}`;

Loading…
Cancel
Save