Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 50 additions & 5 deletions src/components/graph/widgets/TextPreviewWidget.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,20 @@ describe('TextPreviewWidget', () => {
expect(container.querySelector('iframe')).toBeNull()
})

it('strips inline javascript: hrefs on anchors', () => {
it('escapes raw <a> tags instead of turning them into live anchors', () => {
// Raw modelValue text is never treated as author-supplied HTML, so a
// literal `<a href="javascript:...">` is displayed as visible text
// rather than parsed into a clickable (and previously exploitable)
// anchor element.
const { container } = renderPreview(
'<a href="javascript:alert(1)">click</a>'
)
// 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(
'<a href="javascript:alert(1)">click</a>'
)
})

it('preserves the <br> tag produced by nl2br', () => {
Expand All @@ -155,6 +160,46 @@ describe('TextPreviewWidget', () => {
})
})

describe('Bracket-delimited raw text (e.g. LoRA/embedding syntax)', () => {
it('preserves <lora:name:weight>-style text instead of parsing it as a tag', () => {
const { container } = renderPreview(
'Loaded lora: <lora:my_style_v2:0.8> 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: <lora:my_style_v2:0.8> applied successfully'
)
})

it('still linkifies URLs and turns newlines into <br> alongside bracket text', () => {
const { container } = renderPreview(
'<lora:foo:1.0>\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('<lora:foo:1.0>')
// 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(
'<lora:foo:1.0> 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('<lora:foo:1.0>')
// 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: [] })
Expand Down
16 changes: 11 additions & 5 deletions src/components/graph/widgets/TextPreviewWidget.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>({ required: true })
const props = defineProps<{
Expand All @@ -43,14 +44,19 @@ const formattedText = computed(() => {
}
)

// Keep current behavior (auto-link bare URLs + \n -> <br>)
let html = nl2br(linkifyHtml(holed))
// Escape HTML-significant characters BEFORE linkifying so bracket-delimited
// text that looks like a tag (e.g. `<lora:my_style_v2:0.8>`) 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 -> <br>).
let html = nl2br(linkifyHtml(escapeHtml(holed)))

// Restore placeholders as <a>...</a> (minimal escaping + http default)
// Restore placeholders as <a>...</a> (escape label/url for safety)
html = html.replace(/__LNK(\d+)__/g, (_m, i) => {
const { label, url } = tokens[+i]
const safeHref = url.replace(/"/g, '&quot;')
const safeLabel = label.replace(/</g, '&lt;').replace(/>/g, '&gt;')
const safeHref = escapeHtml(url)
const safeLabel = escapeHtml(label)
return /^https?:\/\//i.test(url)
? `<a href="${safeHref}" target="_blank" rel="noopener noreferrer">${safeLabel}</a>`
: safeLabel
Expand Down
25 changes: 25 additions & 0 deletions src/utils/htmlEscape.test.ts
Original file line number Diff line number Diff line change
@@ -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('<lora:my_style_v2:0.8>')).toBe(
'&lt;lora:my_style_v2:0.8&gt;'
)
})

it('escapes ampersands, quotes and apostrophes', () => {
expect(escapeHtml('a & b "c" \'d\'')).toBe(
'a &amp; b &quot;c&quot; &#39;d&#39;'
)
})

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('')
})
})
20 changes: 20 additions & 0 deletions src/utils/htmlEscape.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const HTML_ESCAPE_MAP: Record<string, string> = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;'
}

/**
* 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('<lora:my_style_v2:0.8>') // returns '&lt;lora:my_style_v2:0.8&gt;'
*/
export function escapeHtml(text: string): string {
if (!text) return ''
return text.replace(/[&<>"']/g, (char) => HTML_ESCAPE_MAP[char])
}
Loading