From e232be64d9c22258cb6bfb15050071816daf7665 Mon Sep 17 00:00:00 2001 From: Evie Gauthier Date: Thu, 5 Mar 2026 15:10:57 -0500 Subject: [PATCH] fix(media): remove allowRedirects, add error fallbacks, apply useAuthentication in html parser --- src/app/components/message/Reaction.tsx | 77 ++++++++++++-------- src/app/plugins/react-custom-html-parser.tsx | 73 ++++++++++++++++++- src/app/utils/matrix.ts | 5 +- 3 files changed, 119 insertions(+), 36 deletions(-) diff --git a/src/app/components/message/Reaction.tsx b/src/app/components/message/Reaction.tsx index edc8640b..155de11f 100644 --- a/src/app/components/message/Reaction.tsx +++ b/src/app/components/message/Reaction.tsx @@ -1,4 +1,5 @@ -import { Box, Text, as } from 'folds'; +import { useState } from 'react'; +import { Box, Icon, Icons, Text, as } from 'folds'; import classNames from 'classnames'; import { MatrixClient, MatrixEvent, Room } from '$types/matrix-sdk'; import { getHexcodeForEmoji, getShortcodeFor } from '$plugins/emoji'; @@ -16,34 +17,52 @@ export const Reaction = as< reaction: string; useAuthentication?: boolean; } ->(({ className, mx, count, reaction, useAuthentication, ...props }, ref) => ( - - - {reaction.startsWith('mxc://') ? ( - {reaction} - ) : ( - - {reaction} - - )} - - - {count} - - -)); +>(({ className, mx, count, reaction, useAuthentication, ...props }, ref) => { + const [imgError, setImgError] = useState(false); + + return ( + + + {reaction.startsWith('mxc://') ? ( + (() => { + if (imgError) + return ( + // Image loaded but fetch failed — show a small warning icon so the + // reaction button still renders correctly and the user can see + // something went wrong rather than a browser broken-image icon. + + + + ); + return ( + {reaction} setImgError(true)} + /> + ); + })() + ) : ( + + {reaction} + + )} + + + {count} + + + ); +}); type ReactionTooltipMsgProps = { room: Room; diff --git a/src/app/plugins/react-custom-html-parser.tsx b/src/app/plugins/react-custom-html-parser.tsx index 84abacf8..dc3bf484 100644 --- a/src/app/plugins/react-custom-html-parser.tsx +++ b/src/app/plugins/react-custom-html-parser.tsx @@ -3,6 +3,7 @@ import { ComponentPropsWithoutRef, lazy, ReactEventHandler, + ReactNode, Suspense, useMemo, useState, @@ -316,6 +317,20 @@ export function CodeBlock({ ); } +/** + * Thin wrapper around that gracefully swaps in a fallback node when the + * image fails to load (e.g. federation down, mxc URL unavailable). Avoids the + * silent browser broken-image icon showing up in message bodies. + */ +function FallbackImg({ + fallback, + ...props +}: ComponentPropsWithoutRef<'img'> & { fallback: ReactNode }) { + const [failed, setFailed] = useState(false); + if (failed) return <>{fallback}; + return setFailed(true)} />; +} + export const getReactCustomHtmlParser = ( mx: MatrixClient, roomId: string | undefined, @@ -501,8 +516,15 @@ export const getReactCustomHtmlParser = ( } if (name === 'img') { + // Guard: img without a src survives sanitisation (fix for crash #1731) + // but we can't convert it — skip rendering rather than passing + // undefined into mxcUrlToHttp where it would throw. + if (!props.src) return null; + const htmlSrc = mxcUrlToHttp(mx, props.src, params.useAuthentication) ?? undefined; + // Non-mxc images were already converted to links by the sanitiser, + // but handle the edge case defensively here too. if (htmlSrc && !props.src.startsWith('mxc://')) { return ( @@ -511,7 +533,18 @@ export const getReactCustomHtmlParser = ( ); } - if (htmlSrc && 'data-mx-emoticon' in props) { + if ('data-mx-emoticon' in props) { + // When the mxc URL can't be resolved (e.g. federation unavailable), + // fall back to rendering the shortcode text so the message stays readable. + if (!htmlSrc) { + const label = props.alt || props.title || ''; + return ( + + {label ? `:${label}:` : ''} + + ); + } + const siblingCount = domNode.parent?.children.length ?? 0; // seperate style for bundled emojis @@ -519,11 +552,19 @@ export const getReactCustomHtmlParser = ( return ( - + {props.alt || props.title ? `:${props.alt || props.title}:` : '?'} + + } /> @@ -534,13 +575,37 @@ export const getReactCustomHtmlParser = ( return ( - + + {props.alt || props.title ? `:${props.alt || props.title}:` : '?'} + + } + /> ); } - if (htmlSrc) return ; + if (htmlSrc) + return ( + + {props.alt || '[media]'} + + } + /> + ); } } diff --git a/src/app/utils/matrix.ts b/src/app/utils/matrix.ts index 46d5e6ec..ea1a8514 100644 --- a/src/app/utils/matrix.ts +++ b/src/app/utils/matrix.ts @@ -293,8 +293,7 @@ export const mxcUrlToHttp = ( width?: number, height?: number, resizeMethod?: string, - allowDirectLinks?: boolean, - allowRedirects?: boolean + allowDirectLinks?: boolean ): string | null => mx.mxcUrlToHttp( mxcUrl.replace(/^["']|["']$/g, ''), @@ -302,7 +301,7 @@ export const mxcUrlToHttp = ( height, resizeMethod, allowDirectLinks, - allowRedirects, + undefined, useAuthentication );