Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix crash when decoding malformed urls #1865

Merged
merged 1 commit into from
Aug 4, 2024
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
3 changes: 2 additions & 1 deletion src/app/components/editor/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
parseMatrixToUser,
testMatrixTo,
} from '../../plugins/matrix-to';
import { tryDecodeURIComponent } from '../../utils/dom';

const markNodeToType: Record<string, MarkType> = {
b: MarkType.Bold,
Expand Down Expand Up @@ -73,7 +74,7 @@ const elementToInlineNode = (node: Element): MentionElement | EmoticonElement |
return createEmoticonElement(src, alt || 'Unknown Emoji');
}
if (node.name === 'a') {
const href = decodeURIComponent(node.attribs.href);
const href = tryDecodeURIComponent(node.attribs.href);
if (typeof href !== 'string') return undefined;
if (testMatrixTo(href)) {
const userMention = parseMatrixToUser(href);
Expand Down
3 changes: 2 additions & 1 deletion src/app/components/url-preview/UrlPreviewCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
useIntersectionObserver,
} from '../../hooks/useIntersectionObserver';
import * as css from './UrlPreviewCard.css';
import { tryDecodeURIComponent } from '../../utils/dom';

const linkStyles = { color: color.Success.Main };

Expand Down Expand Up @@ -43,7 +44,7 @@ export const UrlPreviewCard = as<'div', { url: string; ts: number }>(
priority="300"
>
{typeof prev['og:site_name'] === 'string' && `${prev['og:site_name']} | `}
{decodeURIComponent(url)}
{tryDecodeURIComponent(url)}
</Text>
<Text truncate priority="400">
<b>{prev['og:title']}</b>
Expand Down
5 changes: 3 additions & 2 deletions src/app/pages/auth/AuthLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { AutoDiscoveryInfoProvider } from '../../hooks/useAutoDiscoveryInfo';
import { AuthFlowsLoader } from '../../components/AuthFlowsLoader';
import { AuthFlowsProvider } from '../../hooks/useAuthFlows';
import { AuthServerProvider } from '../../hooks/useAuthServer';
import { tryDecodeURIComponent } from '../../utils/dom';

const currentAuthPath = (pathname: string): string => {
if (matchPath(LOGIN_PATH, pathname)) {
Expand Down Expand Up @@ -72,7 +73,7 @@ export function AuthLayout() {
const clientConfig = useClientConfig();

const defaultServer = clientDefaultServer(clientConfig);
let server: string = urlEncodedServer ? decodeURIComponent(urlEncodedServer) : defaultServer;
let server: string = urlEncodedServer ? tryDecodeURIComponent(urlEncodedServer) : defaultServer;

if (!clientAllowedServer(clientConfig, server)) {
server = defaultServer;
Expand All @@ -94,7 +95,7 @@ export function AuthLayout() {

// if server is mismatches with path server, update path
useEffect(() => {
if (!urlEncodedServer || decodeURIComponent(urlEncodedServer) !== server) {
if (!urlEncodedServer || tryDecodeURIComponent(urlEncodedServer) !== server) {
navigate(
generatePath(currentAuthPath(location.pathname), {
server: encodeURIComponent(server),
Expand Down
9 changes: 5 additions & 4 deletions src/app/plugins/react-custom-html-parser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
testMatrixTo,
} from './matrix-to';
import { onEnterOrSpace } from '../utils/keyboard';
import { tryDecodeURIComponent } from '../utils/dom';

const ReactPrism = lazy(() => import('./react-prism/ReactPrism'));

Expand Down Expand Up @@ -134,8 +135,8 @@ export const factoryRenderLinkifyWithMention = (
attributes,
content,
}) => {
if (tagName === 'a' && testMatrixTo(decodeURIComponent(attributes.href))) {
const mention = mentionRender(decodeURIComponent(attributes.href));
if (tagName === 'a' && testMatrixTo(tryDecodeURIComponent(attributes.href))) {
const mention = mentionRender(tryDecodeURIComponent(attributes.href));
if (mention) return mention;
}

Expand Down Expand Up @@ -325,11 +326,11 @@ export const getReactCustomHtmlParser = (
}
}

if (name === 'a' && testMatrixTo(decodeURIComponent(props.href))) {
if (name === 'a' && testMatrixTo(tryDecodeURIComponent(props.href))) {
const mention = renderMatrixMention(
mx,
roomId,
decodeURIComponent(props.href),
tryDecodeURIComponent(props.href),
makeMentionCustomProps(params.handleMentionClick)
);
if (mention) return mention;
Expand Down
8 changes: 8 additions & 0 deletions src/app/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,11 @@ export const setFavicon = (url: string): void => {
if (!favicon) return;
favicon.setAttribute('href', url);
};

export const tryDecodeURIComponent = (encodedURIComponent: string): string => {
try {
return decodeURIComponent(encodedURIComponent);
} catch {
return encodedURIComponent;
}
};
Loading