diff --git a/src/components/graph/widgets/TextPreviewWidget.test.ts b/src/components/graph/widgets/TextPreviewWidget.test.ts index d97ce8a39bd..11c369c3eda 100644 --- a/src/components/graph/widgets/TextPreviewWidget.test.ts +++ b/src/components/graph/widgets/TextPreviewWidget.test.ts @@ -137,15 +137,20 @@ describe('TextPreviewWidget', () => { expect(container.querySelector('iframe')).toBeNull() }) - it('strips inline javascript: hrefs on anchors', () => { + it('escapes raw tags instead of turning them into live anchors', () => { + // Raw modelValue text is never treated as author-supplied HTML, so a + // literal `` is displayed as visible text + // rather than parsed into a clickable (and previously exploitable) + // anchor element. const { container } = renderPreview( 'click' ) // eslint-disable-next-line testing-library/no-container, testing-library/no-node-access - const anchor = container.querySelector('a') - expect(anchor).not.toBeNull() - const href = anchor?.getAttribute('href') - expect(href == null || !href.startsWith('javascript:')).toBe(true) + expect(container.querySelector('a')).toBeNull() + // eslint-disable-next-line testing-library/no-container, testing-library/no-node-access + expect(container.querySelector('span')?.textContent).toContain( + 'click' + ) }) it('preserves the
tag produced by nl2br', () => { @@ -155,6 +160,46 @@ describe('TextPreviewWidget', () => { }) }) + describe('Bracket-delimited raw text (e.g. LoRA/embedding syntax)', () => { + it('preserves -style text instead of parsing it as a tag', () => { + const { container } = renderPreview( + 'Loaded lora: applied successfully' + ) + // eslint-disable-next-line testing-library/no-container, testing-library/no-node-access + const span = container.querySelector('span') + expect(span?.textContent).toContain( + 'Loaded lora: applied successfully' + ) + }) + + it('still linkifies URLs and turns newlines into
alongside bracket text', () => { + const { container } = renderPreview( + '\nvisit https://example.com for details' + ) + // eslint-disable-next-line testing-library/no-container, testing-library/no-node-access + const span = container.querySelector('span') + expect(span?.textContent).toContain('') + // eslint-disable-next-line testing-library/no-container, testing-library/no-node-access + expect(container.querySelector('br')).toBeInTheDocument() + // eslint-disable-next-line testing-library/no-container, testing-library/no-node-access + const anchor = container.querySelector('a') + expect(anchor?.getAttribute('href')).toBe('https://example.com') + }) + + it('keeps the [[label|url]] custom link syntax working next to bracket text', () => { + const { container } = renderPreview( + ' see [[Docs|https://docs.example.com]]' + ) + // eslint-disable-next-line testing-library/no-container, testing-library/no-node-access + const span = container.querySelector('span') + expect(span?.textContent).toContain('') + // eslint-disable-next-line testing-library/no-container, testing-library/no-node-access + const anchor = container.querySelector('a') + expect(anchor?.getAttribute('href')).toBe('https://docs.example.com') + expect(anchor?.textContent).toBe('Docs') + }) + }) + describe('Execution state', () => { it('hides the Skeleton on mount when execution is already idle', () => { Object.assign(useExecutionStore(), { executingNodeIds: [] }) diff --git a/src/components/graph/widgets/TextPreviewWidget.vue b/src/components/graph/widgets/TextPreviewWidget.vue index 635bc0c76cd..98e4069e614 100644 --- a/src/components/graph/widgets/TextPreviewWidget.vue +++ b/src/components/graph/widgets/TextPreviewWidget.vue @@ -20,6 +20,7 @@ import SanitizedHtml from '@/components/common/SanitizedHtml.vue' import { useExecutionStore } from '@/stores/executionStore' import type { NodeId } from '@/types/nodeId' import { linkifyHtml, nl2br } from '@/utils/formatUtil' +import { escapeHtml } from '@/utils/htmlEscape' const modelValue = defineModel({ required: true }) const props = defineProps<{ @@ -43,14 +44,19 @@ const formattedText = computed(() => { } ) - // Keep current behavior (auto-link bare URLs + \n ->
) - let html = nl2br(linkifyHtml(holed)) + // Escape HTML-significant characters BEFORE linkifying so bracket-delimited + // text that looks like a tag (e.g. ``) displays + // literally instead of being parsed as markup and dropped. linkifyHtml only + // matches http(s)/ftp/file/www URLs, none of which contain the escaped + // entities, so escaping first doesn't stop real URLs from being linkified. + // Keep current behavior otherwise (auto-link bare URLs + \n ->
). + let html = nl2br(linkifyHtml(escapeHtml(holed))) - // Restore placeholders as ... (minimal escaping + http default) + // Restore placeholders as ... (escape label/url for safety) html = html.replace(/__LNK(\d+)__/g, (_m, i) => { const { label, url } = tokens[+i] - const safeHref = url.replace(/"/g, '"') - const safeLabel = label.replace(//g, '>') + const safeHref = escapeHtml(url) + const safeLabel = escapeHtml(label) return /^https?:\/\//i.test(url) ? `${safeLabel}` : safeLabel diff --git a/src/utils/htmlEscape.test.ts b/src/utils/htmlEscape.test.ts new file mode 100644 index 00000000000..2d41634ee83 --- /dev/null +++ b/src/utils/htmlEscape.test.ts @@ -0,0 +1,25 @@ +import { describe, expect, it } from 'vitest' + +import { escapeHtml } from './htmlEscape' + +describe('escapeHtml', () => { + it('escapes angle brackets so tag-like text is not parsed as markup', () => { + expect(escapeHtml('')).toBe( + '<lora:my_style_v2:0.8>' + ) + }) + + it('escapes ampersands, quotes and apostrophes', () => { + expect(escapeHtml('a & b "c" \'d\'')).toBe( + 'a & b "c" 'd'' + ) + }) + + it('leaves text without HTML-significant characters untouched', () => { + expect(escapeHtml('plain text')).toBe('plain text') + }) + + it('returns an empty string for empty input', () => { + expect(escapeHtml('')).toBe('') + }) +}) diff --git a/src/utils/htmlEscape.ts b/src/utils/htmlEscape.ts new file mode 100644 index 00000000000..d61deb20f11 --- /dev/null +++ b/src/utils/htmlEscape.ts @@ -0,0 +1,20 @@ +const HTML_ESCAPE_MAP: Record = { + '&': '&', + '<': '<', + '>': '>', + '"': '"', + "'": ''' +} + +/** + * Escapes HTML-significant characters (`& < > " '`) so a raw string displays + * literally instead of being parsed as markup. + * @param text - The string to escape + * @returns The escaped string, safe to feed into an HTML parser as plain text + * @example + * escapeHtml('') // returns '<lora:my_style_v2:0.8>' + */ +export function escapeHtml(text: string): string { + if (!text) return '' + return text.replace(/[&<>"']/g, (char) => HTML_ESCAPE_MAP[char]) +}