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
4 changes: 1 addition & 3 deletions src/components/FilePreview/FilePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ const FilePreview = ({
if (isHtmlFile(file)) {
let htmlContent = content;
if (htmlContent.includes('<') || htmlContent.includes('"')) {
const div = document.createElement('div');
div.innerHTML = htmlContent;
htmlContent = div.textContent || div.innerText || htmlContent;
htmlContent = stripHTML(htmlContent) || htmlContent;
}
htmlContent = stripDocumentAttachmentTags(htmlContent);
return htmlContent;
Expand Down
5 changes: 2 additions & 3 deletions src/components/MediaWidget/MediaItemWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import React, {
import { getResourceUrl } from '../../helpers/media';
import {
withLinksOpenInNewTab,
stripHTML,
stripDocumentAttachmentTags,
isAssetOnlyDocumentAttachment,
getDocumentAttachmentAssetUrl,
Expand Down Expand Up @@ -433,9 +434,7 @@ export const RenderMediaItem = memo(function RenderMediaItem({
htmlContent.includes('<document_attachment')
) {
if (htmlContent.includes('&lt;') || htmlContent.includes('&quot;')) {
const div = document.createElement('div');
div.innerHTML = htmlContent;
htmlContent = div.textContent || div.innerText || htmlContent;
htmlContent = stripHTML(htmlContent) || htmlContent;
}
htmlContent = stripDocumentAttachmentTags(htmlContent);
}
Expand Down
8 changes: 2 additions & 6 deletions src/components/MediaWidget/MediaPreviewModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,7 @@ export function MediaPreviewModal({
// HTML document attachment: display HTML content in a code-like format
let htmlContent = medium.content || '';
if (htmlContent.includes('&lt;') || htmlContent.includes('&quot;')) {
const div = document.createElement('div');
div.innerHTML = htmlContent;
htmlContent = div.textContent || div.innerText || htmlContent;
htmlContent = stripHTML(htmlContent) || htmlContent;
} else {
htmlContent = stripDocumentAttachmentTags(htmlContent);
}
Expand All @@ -296,9 +294,7 @@ export function MediaPreviewModal({
// Other document attachments: render as plain text
let displayContent = medium.content;
if (displayContent.includes('&lt;') || displayContent.includes('&quot;')) {
const div = document.createElement('div');
div.innerHTML = displayContent;
displayContent = div.textContent || div.innerText || displayContent;
displayContent = stripHTML(displayContent) || displayContent;
} else {
displayContent = stripDocumentAttachmentTags(displayContent);
}
Expand Down
29 changes: 28 additions & 1 deletion src/helpers/message.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
import { renderMsg, stripAttachmentTags, stripAllInternalTags } from './message';
import {
renderMsg,
sanitizeMsg,
stripAttachmentTags,
stripAllInternalTags,
} from './message';

describe('sanitizeMsg', () => {
it('strips onerror from partner XSS img payload', () => {
const payload =
'<img src="immagine-inesistente.jpg" onerror="alert(\'XSS Eseguito!\')">';
const result = sanitizeMsg(payload);
expect(result).toBe('<img src="immagine-inesistente.jpg">');
expect(result).not.toMatch(/onerror/i);
});

it('strips svg onload handlers', () => {
const result = sanitizeMsg('<svg onload="alert(1)"></svg>');
expect(result).not.toMatch(/onload/i);
});
});

describe('renderMsg', () => {
it('should render message with reasoning and output tag adjacent correctly', () => {
Expand All @@ -11,6 +31,13 @@ describe('renderMsg', () => {
'<details class="memori-think"><summary>Reasoning...</summary>L\'utente vuole che io elenchi i parametri dell\'ALBERO ESISTENTE.</details>\n\n<output class="rails-query" style="display:none">\nproject = Project.find("68d99b05783713e16737d32b")\n</output>\n\n<p>Recupero tutti i 7 parametri dell\'ALBERO ESISTENTE...</p>'
);
});

it('strips onerror from XSS img payload in rendered output', () => {
const payload =
'<img src="immagine-inesistente.jpg" onerror="alert(\'XSS Eseguito!\')">';
const result = renderMsg(payload, false, 'Reasoning...', false);
expect(result.text).not.toMatch(/onerror/i);
});
});

describe('stripAttachmentTags', () => {
Expand Down
8 changes: 4 additions & 4 deletions src/helpers/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ export const truncateMessage = (message: string) => {
return truncatedMessage;
};

export const sanitizeMsg = (msg: string) =>
DOMPurify.sanitize(msg, { ADD_ATTR: ['target'] });

export const renderMsg = (
text: string,
useMathFormatting = false,
Expand Down Expand Up @@ -196,9 +199,6 @@ export const renderMsg = (
return { text: finalText };
} catch (e) {
console.error('Error rendering message:', e);
return { text };
return { text: sanitizeMsg(text) };
}
};

export const sanitizeMsg = (msg: string) =>
DOMPurify.sanitize(msg, { ADD_ATTR: ['target'] });
49 changes: 49 additions & 0 deletions src/helpers/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
difference,
stripEmojis,
stripHTML,
stripMarkdown,
stripOutputTags,
escapeHTML,
Expand Down Expand Up @@ -254,6 +255,54 @@ describe('utils/attachment helpers', () => {
});
});

describe('utils/stripHTML', () => {
it('strips tags and returns text content', () => {
expect(stripHTML('<p>Hello <strong>world</strong></p>')).toBe(
'Hello world'
);
});

it('does not retain onerror from partner XSS payload', () => {
const payload =
'<img src="immagine-inesistente.jpg" onerror="alert(\'XSS Eseguito!\')">';
const result = stripHTML(payload);
expect(result).toBe('');
expect(result).not.toMatch(/onerror/i);
// Re-parse result: must not introduce an executable handler attribute
const reparsed = new DOMParser().parseFromString(result, 'text/html');
expect(reparsed.querySelector('[onerror]')).toBeNull();
expect(reparsed.querySelector('img')).toBeNull();
});

it('strips svg onload payloads without leaving handlers', () => {
const payload = '<svg onload="alert(1)"><text>x</text></svg>';
const result = stripHTML(payload);
expect(result).toBe('x');
expect(result).not.toMatch(/onload/i);
});

it('decodes HTML entities without executing nested markup', () => {
const encoded =
'&lt;img src="x" onerror="alert(1)"&gt;hello&lt;/img&gt;';
const result = stripHTML(encoded);
expect(result).toContain('hello');
// Entity-decoded text may contain the string "onerror" as text; ensure no live element
const reparsed = new DOMParser().parseFromString(
`<div>${result.replace(/</g, '&lt;')}</div>`,
'text/html'
);
expect(reparsed.querySelector('[onerror]')).toBeNull();
});

it('handles nested tags', () => {
expect(stripHTML('<div><span>a</span><b>b</b></div>')).toBe('ab');
});

it('returns empty string for empty input', () => {
expect(stripHTML('')).toBe('');
});
});

describe('utils/parsing combined', () => {
it('should remove output tag from real message', () => {
const result = escapeHTML(
Expand Down
19 changes: 16 additions & 3 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,23 @@ export const stripReasoningTags = (text: string) => {
return strippedText;
};

/**
* Strip HTML tags / decode entities without executing scripts or event handlers.
* Uses an inert DOMParser document (no resource loads / onerror), unlike live innerHTML.
*/
export const stripHTML = (text: string) => {
const el = document.createElement('div');
el.innerHTML = text;
return el.textContent || '';
if (typeof DOMParser !== 'undefined') {
try {
return (
new DOMParser().parseFromString(text, 'text/html').body.textContent ||
''
);
} catch {
// fall through
}
}
// Non-DOM / parse failure: strip tags only (does not decode entities)
return text.replace(/<[^>]*>/g, '');
};

/** Ensures all <a> tags in HTML open in a new tab (target="_blank" rel="noopener noreferrer"). */
Expand Down
Loading