decode urls safely!
This commit is contained in:
3 files changed
+26
-5
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
default: patch
|
||||
---
|
||||
|
||||
Fix crash when url contains malformed/dangling uri components.
|
||||
@@ -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)}
|
||||
</Text>
|
||||
{title && (
|
||||
<Text truncate priority="400">
|
||||
@@ -198,7 +206,7 @@ export const UrlPreviewCard = as<'div', { url: string; ts: number; mediaType?: s
|
||||
size="T200"
|
||||
priority="300"
|
||||
>
|
||||
{decodeURIComponent(url)}
|
||||
{safeDecodeUrl(url)}
|
||||
</Text>
|
||||
</UrlPreviewContent>
|
||||
);
|
||||
|
||||
@@ -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<HTMLElement>,
|
||||
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
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user