Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 15 additions & 8 deletions src/app/components/message/Reply.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { useMatrixClient } from '$hooks/useMatrixClient';
import { useMemberEventParser } from '$hooks/useMemberEventParser';
import { StateEvent, MessageEvent } from '$types/matrix/room';
import { useMentionClickHandler } from '$hooks/useMentionClickHandler';
import { sanitizeCustomHtml } from '$utils/sanitize';
import { useTranslation } from 'react-i18next';
import * as customHtmlCss from '$styles/CustomHtml.css';
import {
Expand Down Expand Up @@ -91,6 +92,18 @@ type ReplyProps = {
replyIcon?: JSX.Element;
};

export const sanitizeReplyFormattedPreview = (formattedBody: string): string => {
const strippedHtml = trimReplyFromFormattedBody(formattedBody)
.replaceAll(/<br\s*\/?>/gi, ' ')
.replaceAll(/<\/p>\s*<p[^>]*>/gi, ' ')
.replaceAll(/<\/?p[^>]*>/gi, '')
.replaceAll(/<\/li>\s*<li[^>]*>/gi, ' ')
.replaceAll(/<\/?(ul|ol|li|blockquote|h[1-6]|pre|div)[^>]*>/gi, '')
.replaceAll(/(?:\r\n|\r|\n)/g, ' ');

return sanitizeCustomHtml(strippedHtml);
};

export const Reply = as<'div', ReplyProps>(
(
{ room, timelineSet, replyEventId, threadRootId, mentions, onClick, replyIcon, ...props },
Expand Down Expand Up @@ -158,20 +171,14 @@ export const Reply = as<'div', ReplyProps>(
);

if (format === 'org.matrix.custom.html' && formattedBody) {
const strippedHtml = trimReplyFromFormattedBody(formattedBody)
.replaceAll(/<br\s*\/?>/gi, ' ')
.replaceAll(/<\/p>\s*<p[^>]*>/gi, ' ')
.replaceAll(/<\/?p[^>]*>/gi, '')
.replaceAll(/<\/li>\s*<li[^>]*>/gi, ' ')
.replaceAll(/<\/?(ul|ol|li|blockquote|h[1-6]|pre|div)[^>]*>/gi, '')
.replaceAll(/(?:\r\n|\r|\n)/g, ' ');
const sanitizedHtml = sanitizeReplyFormattedPreview(formattedBody);
const parserOpts = getReactCustomHtmlParser(mx, room.roomId, {
linkifyOpts: replyLinkifyOpts,
useAuthentication,
nicknames,
handleMentionClick: mentionClickHandler,
});
bodyJSX = parse(strippedHtml, parserOpts) as JSX.Element;
bodyJSX = parse(sanitizedHtml, parserOpts) as JSX.Element;
} else if (body) {
const strippedBody = trimReplyFromBody(body).replaceAll(/(?:\r\n|\r|\n)/g, ' ');
bodyJSX = scaleSystemEmoji(strippedBody);
Expand Down
26 changes: 25 additions & 1 deletion src/app/components/message/ThreadIndicator.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import { describe, it, expect } from 'vitest';
import { render, screen } from '@testing-library/react';
import { RelationType } from '$types/matrix-sdk';
import { ThreadIndicator } from './Reply';
import { sanitizeReplyFormattedPreview, ThreadIndicator } from './Reply';

function Subject({ relType, threadRootId }: { relType?: string; threadRootId?: string }) {
return <>{relType === RelationType.Thread && !threadRootId && <ThreadIndicator />}</>;
Expand All @@ -41,3 +41,27 @@
expect(screen.queryByText('Thread')).not.toBeInTheDocument();
});
});

describe('sanitizeReplyFormattedPreview', () => {
it('removes mx-reply quote block content and keeps message text', () => {
const html =

Check failure on line 47 in src/app/components/message/ThreadIndicator.test.tsx

View workflow job for this annotation

GitHub Actions / Lint

Delete `⏎·····`
'<mx-reply><blockquote>quoted</blockquote></mx-reply><p>Visible message</p>';

const result = sanitizeReplyFormattedPreview(html);

expect(result).not.toContain('quoted');
expect(result).toContain('Visible message');
});

it('sanitizes dangerous HTML before parse', () => {
const html =
'<mx-reply><blockquote>old</blockquote></mx-reply><p>safe<script>alert(1)</script></p><a href="javascript:alert(1)">x</a>';

const result = sanitizeReplyFormattedPreview(html);

expect(result).not.toContain('<script');
expect(result).not.toContain('alert(1)');
expect(result).not.toContain('javascript:');

Check failure on line 64 in src/app/components/message/ThreadIndicator.test.tsx

View workflow job for this annotation

GitHub Actions / Lint

Script URL is a form of eval
expect(result).toContain('safe');
});
});
Loading