Decode urls safely to avoid crashes (#512)
<!-- Please read https://github.com/SableClient/Sable/blob/dev/CONTRIBUTING.md before submitting your pull request --> ### Description <!-- Please include a summary of the change. Please also include relevant motivation and context. List any dependencies that are required for this change. --> Fixes #510 #### Type of change - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update ### Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings ### AI disclosure: - [ ] Partially AI assisted (clarify which code was AI assisted and briefly explain what it does). - [ ] Fully AI generated (explain what all the generated code does in moderate detail). <!-- Write any explanation required here, but do not generate the explanation using AI!! You must prove you understand what the code in this PR does. -->
This commit is contained in:
3 files changed
+19
-5
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
default: patch
|
||||
---
|
||||
|
||||
Fix crash when url contains malformed/dangling uri components.
|
||||
@@ -5,6 +5,7 @@ import { AsyncStatus, useAsyncCallback } from '$hooks/useAsyncCallback';
|
||||
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';
|
||||
@@ -100,7 +101,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 +199,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'],
|
||||
};
|
||||
|
||||
export 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