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
5 changes: 5 additions & 0 deletions .changeset/fix-uri-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
default: patch
---

Fix crash when url contains malformed/dangling uri components.
5 changes: 3 additions & 2 deletions src/app/components/url-preview/UrlPreviewCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { useMatrixClient } from '$hooks/useMatrixClient';
import { mxcUrlToHttp, downloadMedia } from '$utils/matrix';
import { useMediaAuthentication } from '$hooks/useMediaAuthentication';
import { safeDecodeUrl } from '$plugins/react-custom-html-parser';
import * as css from './UrlPreviewCard.css';
import { UrlPreview, UrlPreviewContent, UrlPreviewDescription } from './UrlPreview';
import { AudioContent, ImageContent, VideoContent } from '../message';
Expand All @@ -15,7 +16,7 @@

const openMediaInNewTab = async (url: string | undefined) => {
if (!url) {
console.warn('Attempted to open an empty url');

Check warning on line 19 in src/app/components/url-preview/UrlPreviewCard.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement
return;
}
const blob = await downloadMedia(url);
Expand Down Expand Up @@ -58,14 +59,14 @@
);
const handleAuxClick = (ev: React.MouseEvent) => {
if (!prev['og:image']) {
console.warn('No image');

Check warning on line 62 in src/app/components/url-preview/UrlPreviewCard.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement
return;
}
if (ev.button === 1) {
ev.preventDefault();
const mxcUrl = mxcUrlToHttp(mx, prev['og:image'], /* useAuthentication */ true);
if (!mxcUrl) {
console.error('Error converting mxc:// url.');

Check warning on line 69 in src/app/components/url-preview/UrlPreviewCard.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement
return;
}
openMediaInNewTab(mxcUrl);
Expand Down Expand Up @@ -100,7 +101,7 @@
priority="300"
>
{typeof siteName === 'string' && `${siteName} | `}
{decodeURIComponent(url)}
{safeDecodeUrl(url)}
</Text>
{title && (
<Text truncate priority="400">
Expand Down Expand Up @@ -198,7 +199,7 @@
size="T200"
priority="300"
>
{decodeURIComponent(url)}
{safeDecodeUrl(url)}
</Text>
</UrlPreviewContent>
);
Expand Down
14 changes: 11 additions & 3 deletions src/app/plugins/react-custom-html-parser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ export const LINKIFY_OPTS: LinkifyOpts = {
ignoreTags: ['span'],
};

export const safeDecodeUrl = (url: string) => {
try {
return decodeURIComponent(url);
} catch {
return url;
}
};

export const makeMentionCustomProps = (
handleMentionClick?: ReactEventHandler<HTMLElement>,
content?: string
Expand Down Expand Up @@ -161,7 +169,7 @@ export const factoryRenderLinkifyWithMention = (
content,
}) => {
const encodedHref = attributes.href;
const decodedHref = encodedHref && decodeURIComponent(encodedHref);
const decodedHref = encodedHref && safeDecodeUrl(encodedHref);

if (tagName === 'a' && decodedHref && testMatrixTo(decodedHref)) {
const mention = mentionRender(decodedHref);
Expand Down Expand Up @@ -480,7 +488,7 @@ export const getReactCustomHtmlParser = (

if (name === 'a' && typeof props.href === 'string') {
const encodedHref = props.href;
const decodedHref = encodedHref && decodeURIComponent(encodedHref);
const decodedHref = encodedHref && safeDecodeUrl(encodedHref);
if (!decodedHref || !testMatrixTo(decodedHref)) {
return undefined;
}
Expand All @@ -492,7 +500,7 @@ export const getReactCustomHtmlParser = (
const mention = renderMatrixMention(
mx,
roomId,
decodeURIComponent(props.href),
safeDecodeUrl(props.href),
makeMentionCustomProps(params.handleMentionClick, content),
params.nicknames
);
Expand Down
Loading