mirror of
https://github.com/SableClient/Sable.git
synced 2026-12-01 22:32:32 -08:00
ensure gifs require klipy proxy, harden url parsing, and add tests
This commit is contained in:
1 parent
988346959e
commit
8099c4c494
6 files changed
+193
-19
No files matched your search
@@ -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>(() => <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,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_-]+$/
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -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