diff --git a/.changeset/fix-uri-crash.md b/.changeset/fix-uri-crash.md
new file mode 100644
index 00000000..3b0afb80
--- /dev/null
+++ b/.changeset/fix-uri-crash.md
@@ -0,0 +1,5 @@
+---
+default: patch
+---
+
+Fix crash when url contains malformed/dangling uri components.
diff --git a/src/app/components/url-preview/UrlPreviewCard.tsx b/src/app/components/url-preview/UrlPreviewCard.tsx
index 947cd67b..5b41a492 100644
--- a/src/app/components/url-preview/UrlPreviewCard.tsx
+++ b/src/app/components/url-preview/UrlPreviewCard.tsx
@@ -23,6 +23,14 @@ const openMediaInNewTab = async (url: string | undefined) => {
window.open(blobUrl, '_blank');
};
+const safeDecodeUrl = (url: string) => {
+ try {
+ return decodeURIComponent(url);
+ } catch (e) {
+ return url;
+ }
+};
+
export const UrlPreviewCard = as<'div', { url: string; ts: number; mediaType?: string | null }>(
({ url, ts, mediaType, ...props }, ref) => {
const mx = useMatrixClient();
@@ -100,7 +108,7 @@ export const UrlPreviewCard = as<'div', { url: string; ts: number; mediaType?: s
priority="300"
>
{typeof siteName === 'string' && `${siteName} | `}
- {decodeURIComponent(url)}
+ {safeDecodeUrl(url)}
{title && (
@@ -198,7 +206,7 @@ export const UrlPreviewCard = as<'div', { url: string; ts: number; mediaType?: s
size="T200"
priority="300"
>
- {decodeURIComponent(url)}
+ {safeDecodeUrl(url)}
);
diff --git a/src/app/plugins/react-custom-html-parser.tsx b/src/app/plugins/react-custom-html-parser.tsx
index 12a86926..3aa0dc8b 100644
--- a/src/app/plugins/react-custom-html-parser.tsx
+++ b/src/app/plugins/react-custom-html-parser.tsx
@@ -60,6 +60,14 @@ export const LINKIFY_OPTS: LinkifyOpts = {
ignoreTags: ['span'],
};
+const safeDecodeUrl = (url: string) => {
+ try {
+ return decodeURIComponent(url);
+ } catch {
+ return url;
+ }
+};
+
export const makeMentionCustomProps = (
handleMentionClick?: ReactEventHandler,
content?: string
@@ -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);
@@ -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;
}
@@ -492,7 +500,7 @@ export const getReactCustomHtmlParser = (
const mention = renderMatrixMention(
mx,
roomId,
- decodeURIComponent(props.href),
+ safeDecodeUrl(props.href),
makeMentionCustomProps(params.handleMentionClick, content),
params.nicknames
);