From 8099c4c4947bf39ace7d74ec85f5055421bf4d66 Mon Sep 17 00:00:00 2001 From: 7w1 Date: Fri, 17 Jul 2026 14:40:07 -0500 Subject: [PATCH 1/3] ensure gifs require klipy proxy, harden url parsing, and add tests --- .../message/MsgTypeRenderers.test.tsx | 136 ++++++++++++++++++ .../components/message/MsgTypeRenderers.tsx | 32 ++--- src/app/features/room/RoomInput.tsx | 6 +- src/app/features/room/msgContent.ts | 4 +- src/app/utils/klipy.test.ts | 27 ++++ src/app/utils/klipy.ts | 7 + 6 files changed, 193 insertions(+), 19 deletions(-) create mode 100644 src/app/components/message/MsgTypeRenderers.test.tsx create mode 100644 src/app/utils/klipy.test.ts diff --git a/src/app/components/message/MsgTypeRenderers.test.tsx b/src/app/components/message/MsgTypeRenderers.test.tsx new file mode 100644 index 000000000..376968726 --- /dev/null +++ b/src/app/components/message/MsgTypeRenderers.test.tsx @@ -0,0 +1,136 @@ +import type { ReactNode } from 'react'; +import { render } from '@testing-library/react'; +import { describe, expect, it, vi } from 'vitest'; +import type { + IAudioContent, + IFileContent, + IImageContent, + IVideoContent, +} from '$types/matrix/common'; +import { MAudio, MFile, MImage, MSticker, MVideo } from './MsgTypeRenderers'; + +describe('incoming image renderer', () => { + it('rejects arbitrary HTTP(S) image URLs', () => { + const renderImageContent = vi.fn<(props: unknown) => ReactNode>(() => rendered); + + render( + + ); + + expect(renderImageContent).not.toHaveBeenCalled(); + expect(document.body).toHaveTextContent('Broken message: remote image'); + }); + + it('renders only mxc image URLs', () => { + const renderImageContent = vi.fn<(props: unknown) => ReactNode>(() => rendered); + + render( + + ); + + expect(renderImageContent).toHaveBeenCalledWith( + expect.objectContaining({ url: 'mxc://example.org/image' }) + ); + }); + + it('rejects an arbitrary encrypted-file URL even when the legacy URL is MXC', () => { + const renderImageContent = vi.fn<(props: unknown) => ReactNode>(() => rendered); + + render( + + ); + + expect(renderImageContent).not.toHaveBeenCalled(); + }); + + it('rejects arbitrary video URLs without invoking a renderer or fallback', () => { + const renderAsFile = vi.fn<() => ReactNode>(() => file fallback); + const renderVideoContent = vi.fn<(props: unknown) => ReactNode>(() => video); + + render( + + ); + + expect(renderVideoContent).not.toHaveBeenCalled(); + expect(renderAsFile).not.toHaveBeenCalled(); + }); + + it('rejects arbitrary audio URLs without invoking a renderer or fallback', () => { + const renderAsFile = vi.fn<() => ReactNode>(() => file fallback); + const renderAudioContent = vi.fn<(props: unknown) => ReactNode>(() => audio); + + render( + + ); + + expect(renderAudioContent).not.toHaveBeenCalled(); + expect(renderAsFile).not.toHaveBeenCalled(); + }); + + it('rejects arbitrary file URLs', () => { + const renderFileContent = vi.fn<(props: unknown) => ReactNode>(() => ( + rendered file + )); + + render( + + ); + + expect(renderFileContent).not.toHaveBeenCalled(); + }); + + it('rejects arbitrary sticker URLs', () => { + const renderImageContent = vi.fn<(props: unknown) => ReactNode>(() => ( + rendered sticker + )); + + render( + + ); + + expect(renderImageContent).not.toHaveBeenCalled(); + }); +}); diff --git a/src/app/components/message/MsgTypeRenderers.tsx b/src/app/components/message/MsgTypeRenderers.tsx index 982548d6f..8ee905bc3 100644 --- a/src/app/components/message/MsgTypeRenderers.tsx +++ b/src/app/components/message/MsgTypeRenderers.tsx @@ -116,6 +116,10 @@ export function BrokenContent({ body }: BrokenContentProps) { ); } +function getIncomingMediaMxcUrl(url: unknown): string | undefined { + return typeof url === 'string' && url.startsWith('mxc://') ? url : undefined; +} + type RenderBodyProps = { body: string; customBody?: string; @@ -437,8 +441,8 @@ type MImageProps = { }; export function MImage({ content, renderImageContent, outlined, fitParent }: MImageProps) { const imgInfo = content?.info; - const mxcUrl = content.file?.url ?? content.url; - if (typeof mxcUrl !== 'string') { + const mxcUrl = getIncomingMediaMxcUrl(content.file?.url ?? content.url); + if (!mxcUrl) { return ; } const MAX_SIZE = 400; @@ -501,15 +505,13 @@ type MVideoProps = { }; export function MVideo({ content, renderAsFile, renderVideoContent, outlined }: MVideoProps) { const videoInfo = content?.info; - const mxcUrl = content.file?.url ?? content.url; + const mxcUrl = getIncomingMediaMxcUrl(content.file?.url ?? content.url); const safeMimeType = getBlobSafeMimeType(videoInfo?.mimetype ?? ''); - if (!videoInfo || !safeMimeType.startsWith('video') || typeof mxcUrl !== 'string') { - if (mxcUrl) { - return renderAsFile(); - } + if (!mxcUrl) { return ; } + if (!videoInfo || !safeMimeType.startsWith('video')) return renderAsFile(); const displayWidth = Math.min(videoInfo.w || 400, 400); const height = Math.min(scaleYDimension(videoInfo.w || 400, 400, videoInfo.h || 400), 400); @@ -604,15 +606,13 @@ export function MAudio({ fitParent, }: MAudioProps) { const audioInfo = content?.info; - const mxcUrl = content.file?.url ?? content.url; + const mxcUrl = getIncomingMediaMxcUrl(content.file?.url ?? content.url); const safeMimeType = getBlobSafeMimeType(audioInfo?.mimetype ?? ''); - if (!audioInfo || !safeMimeType.startsWith('audio') || typeof mxcUrl !== 'string') { - if (mxcUrl) { - return renderAsFile(); - } + if (!mxcUrl) { return ; } + if (!audioInfo || !safeMimeType.startsWith('audio')) return renderAsFile(); const filename = getAttachmentFilename(content.filename, content.body, 'Audio'); const durationMs = getAudioDurationMs(content, audioInfo); @@ -666,9 +666,9 @@ type MFileProps = { }; export function MFile({ content, renderFileContent, outlined }: MFileProps) { const fileInfo = content?.info; - const mxcUrl = content.file?.url ?? content.url; + const mxcUrl = getIncomingMediaMxcUrl(content.file?.url ?? content.url); - if (typeof mxcUrl !== 'string') { + if (!mxcUrl) { return ; } @@ -778,8 +778,8 @@ type MStickerProps = { }; export function MSticker({ content, renderImageContent }: MStickerProps) { const imgInfo = content?.info; - const mxcUrl = content.file?.url ?? content.url; - if (typeof mxcUrl !== 'string') { + const mxcUrl = getIncomingMediaMxcUrl(content.file?.url ?? content.url); + if (!mxcUrl) { return ; } const height = scaleYDimension(imgInfo?.w || 152, 152, imgInfo?.h || 152); diff --git a/src/app/features/room/RoomInput.tsx b/src/app/features/room/RoomInput.tsx index 08616583c..6fe77af88 100644 --- a/src/app/features/room/RoomInput.tsx +++ b/src/app/features/room/RoomInput.tsx @@ -175,7 +175,7 @@ import { getGalleryItemContent, } from './msgContent'; import { outgoingMessageTransforms } from './outgoingMessageTransforms'; -import { getKlipyMxcUrl } from '$utils/klipy'; +import { getSendableKlipyMxcUrl } from '$utils/klipy'; import { CommandAutocomplete } from './CommandAutocomplete'; import type { AudioMessageRecorderHandle, @@ -1384,9 +1384,11 @@ export const RoomInput = forwardRef( }; const handleGifSelect = async (gif: GifData, spoiler?: boolean) => { - const url = getKlipyMxcUrl(gif.url, clientConfig.gifs?.proxyUrl); + const url = getSendableKlipyMxcUrl(gif.url, clientConfig.gifs?.proxyUrl); + if (!url) return; const content = await getGifMsgContent(mx, gif, url, spoiler); + if (!content) return; await handleSendContents([content]); }; diff --git a/src/app/features/room/msgContent.ts b/src/app/features/room/msgContent.ts index 7b63518b9..7bacc2ba1 100644 --- a/src/app/features/room/msgContent.ts +++ b/src/app/features/room/msgContent.ts @@ -252,7 +252,9 @@ export const getGifMsgContent = async ( gif: GifData, mxcUrl: string, spoiler?: boolean -): Promise => { +): Promise => { + if (!mxcUrl.startsWith('mxc://')) return undefined; + const proxyUrl = mxcUrlToHttp(mx, mxcUrl, true); const [imgError, imgEl] = await to(loadImageElement(proxyUrl ?? gif.url, 'anonymous')); if (imgError) { diff --git a/src/app/utils/klipy.test.ts b/src/app/utils/klipy.test.ts new file mode 100644 index 000000000..f7bf6b72f --- /dev/null +++ b/src/app/utils/klipy.test.ts @@ -0,0 +1,27 @@ +import { describe, expect, it } from 'vitest'; +import { getKlipyMxcUrl, getSendableKlipyMxcUrl } from './klipy'; + +describe('getKlipyMxcUrl', () => { + const gifUrl = 'https://static.klipy.com/ii/example.gif'; + + it('does not create an MXC URL without a proxy', () => { + expect(getKlipyMxcUrl(gifUrl)).toBe(gifUrl); + expect(getSendableKlipyMxcUrl(gifUrl)).toBeUndefined(); + expect(getSendableKlipyMxcUrl(gifUrl, ' ')).toBeUndefined(); + }); + + it('allows an existing MXC favorite without a proxy', () => { + expect(getSendableKlipyMxcUrl('mxc://example.org/existing')).toBe( + 'mxc://example.org/existing' + ); + }); + + it('creates an MXC URL when a proxy is configured', () => { + expect(getKlipyMxcUrl(gifUrl, 'media.example.org')).toMatch( + /^mxc:\/\/media\.example\.org\/klipy_[A-Za-z0-9_-]+$/ + ); + expect(getSendableKlipyMxcUrl(gifUrl, 'media.example.org')).toMatch( + /^mxc:\/\/media\.example\.org\/klipy_[A-Za-z0-9_-]+$/ + ); + }); +}); diff --git a/src/app/utils/klipy.ts b/src/app/utils/klipy.ts index aa04c07f6..e4c8ad613 100644 --- a/src/app/utils/klipy.ts +++ b/src/app/utils/klipy.ts @@ -23,3 +23,10 @@ export function getKlipyMxcUrl(url: string, proxyUrl?: string): string { } return url; } + +export function getSendableKlipyMxcUrl(url: string, proxyUrl?: string): string | undefined { + if (url.startsWith('mxc://')) return url; + if (!proxyUrl?.trim()) return undefined; + const mxcUrl = getKlipyMxcUrl(url, proxyUrl); + return mxcUrl.startsWith('mxc://') ? mxcUrl : undefined; +} From 0cb4fbcb3d9391fa1d73b65ff08f92b976ed1453 Mon Sep 17 00:00:00 2001 From: 7w1 Date: Fri, 17 Jul 2026 15:09:56 -0500 Subject: [PATCH 2/3] changeset and update knope formatting --- .changeset/harden-media-url-validation.md | 5 +++++ knope.toml | 12 ++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 .changeset/harden-media-url-validation.md diff --git a/.changeset/harden-media-url-validation.md b/.changeset/harden-media-url-validation.md new file mode 100644 index 000000000..daa9c564c --- /dev/null +++ b/.changeset/harden-media-url-validation.md @@ -0,0 +1,5 @@ +--- +default: security +--- + +This release includes an important security fix. Please update as soon as possible. Sensitive Matrix account data is not exposed by this issue. diff --git a/knope.toml b/knope.toml index fc533824c..cb417155b 100644 --- a/knope.toml +++ b/knope.toml @@ -2,6 +2,18 @@ versioned_files = ["package.json"] changelog = "CHANGELOG.md" extra_changelog_sections = [ + { name = "Security", types = [ + "security", + ] }, + { name = "Breaking Changes", types = [ + "major", + ] }, + { name = "Features", types = [ + "minor", + ] }, + { name = "Fixes", types = [ + "patch", + ] }, { name = "Documentation", types = [ "docs", ] }, From 8c61ff1498722c116fa4f12a2ec1d91c12a4a36f Mon Sep 17 00:00:00 2001 From: 7w1 Date: Fri, 17 Jul 2026 15:11:47 -0500 Subject: [PATCH 3/3] formatting --- src/app/components/message/MsgTypeRenderers.test.tsx | 4 +++- src/app/utils/klipy.test.ts | 4 +--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/app/components/message/MsgTypeRenderers.test.tsx b/src/app/components/message/MsgTypeRenderers.test.tsx index 376968726..68745f568 100644 --- a/src/app/components/message/MsgTypeRenderers.test.tsx +++ b/src/app/components/message/MsgTypeRenderers.test.tsx @@ -15,7 +15,9 @@ describe('incoming image renderer', () => { render( ); diff --git a/src/app/utils/klipy.test.ts b/src/app/utils/klipy.test.ts index f7bf6b72f..e2d84cd27 100644 --- a/src/app/utils/klipy.test.ts +++ b/src/app/utils/klipy.test.ts @@ -11,9 +11,7 @@ describe('getKlipyMxcUrl', () => { }); it('allows an existing MXC favorite without a proxy', () => { - expect(getSendableKlipyMxcUrl('mxc://example.org/existing')).toBe( - 'mxc://example.org/existing' - ); + expect(getSendableKlipyMxcUrl('mxc://example.org/existing')).toBe('mxc://example.org/existing'); }); it('creates an MXC URL when a proxy is configured', () => {