mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 00:51:17 -08:00
Fix klipy sending arbitrary urls and fix rendering arbitrary urls (#1107)
<!-- 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 # #### 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:
8 files changed
+210
-19
No files matched your search
@@ -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.
|
||||
+12
@@ -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",
|
||||
] },
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
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>(() => <img alt="rendered" />);
|
||||
|
||||
render(
|
||||
<MImage
|
||||
content={
|
||||
{ body: 'remote image', url: 'https://attacker.example/image.png' } as IImageContent
|
||||
}
|
||||
renderImageContent={renderImageContent}
|
||||
/>
|
||||
);
|
||||
|
||||
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>(() => <img alt="rendered" />);
|
||||
|
||||
render(
|
||||
<MImage
|
||||
content={{ body: 'matrix image', url: 'mxc://example.org/image' } as IImageContent}
|
||||
renderImageContent={renderImageContent}
|
||||
/>
|
||||
);
|
||||
|
||||
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>(() => <img alt="rendered" />);
|
||||
|
||||
render(
|
||||
<MImage
|
||||
content={
|
||||
{
|
||||
body: 'remote image',
|
||||
url: 'mxc://example.org/image',
|
||||
file: { url: 'https://attacker.example/image.png' },
|
||||
} as unknown as IImageContent
|
||||
}
|
||||
renderImageContent={renderImageContent}
|
||||
/>
|
||||
);
|
||||
|
||||
expect(renderImageContent).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rejects arbitrary video URLs without invoking a renderer or fallback', () => {
|
||||
const renderAsFile = vi.fn<() => ReactNode>(() => <span>file fallback</span>);
|
||||
const renderVideoContent = vi.fn<(props: unknown) => ReactNode>(() => <span>video</span>);
|
||||
|
||||
render(
|
||||
<MVideo
|
||||
content={
|
||||
{
|
||||
body: 'remote video',
|
||||
url: 'https://attacker.example/video.mp4',
|
||||
info: { mimetype: 'video/mp4' },
|
||||
} as IVideoContent
|
||||
}
|
||||
renderAsFile={renderAsFile}
|
||||
renderVideoContent={renderVideoContent}
|
||||
/>
|
||||
);
|
||||
|
||||
expect(renderVideoContent).not.toHaveBeenCalled();
|
||||
expect(renderAsFile).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rejects arbitrary audio URLs without invoking a renderer or fallback', () => {
|
||||
const renderAsFile = vi.fn<() => ReactNode>(() => <span>file fallback</span>);
|
||||
const renderAudioContent = vi.fn<(props: unknown) => ReactNode>(() => <span>audio</span>);
|
||||
|
||||
render(
|
||||
<MAudio
|
||||
content={
|
||||
{
|
||||
body: 'remote audio',
|
||||
url: 'https://attacker.example/audio.mp3',
|
||||
info: { mimetype: 'audio/mpeg' },
|
||||
} as IAudioContent
|
||||
}
|
||||
renderAsFile={renderAsFile}
|
||||
renderAudioContent={renderAudioContent}
|
||||
/>
|
||||
);
|
||||
|
||||
expect(renderAudioContent).not.toHaveBeenCalled();
|
||||
expect(renderAsFile).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rejects arbitrary file URLs', () => {
|
||||
const renderFileContent = vi.fn<(props: unknown) => ReactNode>(() => (
|
||||
<span>rendered file</span>
|
||||
));
|
||||
|
||||
render(
|
||||
<MFile
|
||||
content={{ body: 'remote file', url: 'https://attacker.example/file.txt' } as IFileContent}
|
||||
renderFileContent={renderFileContent}
|
||||
/>
|
||||
);
|
||||
|
||||
expect(renderFileContent).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rejects arbitrary sticker URLs', () => {
|
||||
const renderImageContent = vi.fn<(props: unknown) => ReactNode>(() => (
|
||||
<img alt="rendered sticker" />
|
||||
));
|
||||
|
||||
render(
|
||||
<MSticker
|
||||
content={
|
||||
{ body: 'remote sticker', url: 'https://attacker.example/sticker.png' } as IImageContent
|
||||
}
|
||||
renderImageContent={renderImageContent}
|
||||
/>
|
||||
);
|
||||
|
||||
expect(renderImageContent).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -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 <BrokenContent body={content.body ?? content.filename} />;
|
||||
}
|
||||
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 <BrokenContent body={content.body ?? content.filename} />;
|
||||
}
|
||||
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 <BrokenContent body={content.body ?? content.filename} />;
|
||||
}
|
||||
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 <BrokenContent body={content.body ?? content.filename} />;
|
||||
}
|
||||
|
||||
@@ -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 <MessageBrokenContent body={content.body} />;
|
||||
}
|
||||
const height = scaleYDimension(imgInfo?.w || 152, 152, imgInfo?.h || 152);
|
||||
|
||||
@@ -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<HTMLDivElement, RoomInputProps>(
|
||||
};
|
||||
|
||||
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]);
|
||||
};
|
||||
|
||||
@@ -252,7 +252,9 @@ export const getGifMsgContent = async (
|
||||
gif: GifData,
|
||||
mxcUrl: string,
|
||||
spoiler?: boolean
|
||||
): Promise<IContent> => {
|
||||
): Promise<IContent | undefined> => {
|
||||
if (!mxcUrl.startsWith('mxc://')) return undefined;
|
||||
|
||||
const proxyUrl = mxcUrlToHttp(mx, mxcUrl, true);
|
||||
const [imgError, imgEl] = await to(loadImageElement(proxyUrl ?? gif.url, 'anonymous'));
|
||||
if (imgError) {
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
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_-]+$/
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -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;
|
||||
}
|
||||
Reference in new issue
Block a user