fix(media): revoke decrypted media blob URLs that outlive their component

This commit is contained in:
Erwan Leboucher committed 2026-07-26 16:55:26 +02:00
1 parent 22b19bac7b
commit 2a60778e60
8 files changed
+115 -43

No files matched your search

@@ -28,7 +28,7 @@ import {
} from '$utils/matrix';
import { setMediaEncryption } from '$utils/tauriMediaEncryption';
import { useMediaAuthentication } from '$hooks/useMediaAuthentication';
import { useRevokeObjectURL } from '$hooks/useObjectURL';
import { useCreateObjectURL } from '$hooks/useObjectURL';
import { MEDIA_VOLUME_KEY } from '$components/media';
import { hasControllingServiceWorker } from '$utils/platform';
@@ -60,6 +60,8 @@ export function AudioContent({
const mx = useMatrixClient();
const useAuthentication = useMediaAuthentication();
const createObjectURL = useCreateObjectURL();
const [srcState, loadSrc] = useAsyncCallback(
useCallback(async () => {
const mediaUrl = mxcUrlToHttp(mx, url, useAuthentication);
@@ -74,15 +76,13 @@ export function AudioContent({
const fileContent = await downloadEncryptedMedia(mediaUrl, (encBuf) =>
decryptFile(encBuf, mimeType, encInfo)
);
return URL.createObjectURL(fileContent);
return createObjectURL(fileContent);
}
const fileContent = await downloadMedia(mediaUrl);
return URL.createObjectURL(fileContent);
}, [mx, url, useAuthentication, mimeType, encInfo])
return createObjectURL(fileContent);
}, [mx, url, useAuthentication, mimeType, encInfo, createObjectURL])
);
useRevokeObjectURL(srcState.status === AsyncStatus.Success ? srcState.data : undefined);
const audioRef = useRef<HTMLAudioElement | null>(null);
useEffect(() => {
@@ -15,7 +15,7 @@ import {
} from '$utils/mimeTypes';
import { decryptFile, downloadEncryptedMedia, downloadMedia, mxcUrlToHttp } from '$utils/matrix';
import { useMediaAuthentication } from '$hooks/useMediaAuthentication';
import { useRevokeObjectURL } from '$hooks/useObjectURL';
import { useCreateObjectURL } from '$hooks/useObjectURL';
import { useDismissOnBack } from '$utils/androidBack';
import { ModalWide } from '$styles/Modal.css';
import { getDownloadFilename, saveFileToDevice } from '$utils/download';
@@ -154,6 +154,8 @@ export function ReadPdfFile({ body, mimeType, url, encInfo, renderViewer }: Read
// Android back closes the PDF viewer instead of navigating away.
useDismissOnBack(() => setPdfViewer(false), pdfViewer);
const createObjectURL = useCreateObjectURL();
const [pdfState, loadPdf] = useAsyncCallback(
useCallback(async () => {
const mediaUrl = mxcUrlToHttp(mx, url, useAuthentication);
@@ -162,12 +164,10 @@ export function ReadPdfFile({ body, mimeType, url, encInfo, renderViewer }: Read
? await downloadEncryptedMedia(mediaUrl, (encBuf) => decryptFile(encBuf, mimeType, encInfo))
: await downloadMedia(mediaUrl);
setPdfViewer(true);
return URL.createObjectURL(fileContent);
}, [mx, url, useAuthentication, mimeType, encInfo])
return createObjectURL(fileContent);
}, [mx, url, useAuthentication, mimeType, encInfo, createObjectURL])
);
useRevokeObjectURL(pdfState.status === AsyncStatus.Success ? pdfState.data : undefined);
return (
<>
{pdfState.status === AsyncStatus.Success && (
@@ -223,6 +223,8 @@ export function DownloadFile({ body, mimeType, url, info, encInfo }: DownloadFil
const mx = useMatrixClient();
const useAuthentication = useMediaAuthentication();
const createObjectURL = useCreateObjectURL();
const [downloadState, download] = useAsyncCallback(
useCallback(async () => {
const mediaUrl = mxcUrlToHttp(mx, url, useAuthentication);
@@ -231,14 +233,12 @@ export function DownloadFile({ body, mimeType, url, info, encInfo }: DownloadFil
? await downloadEncryptedMedia(mediaUrl, (encBuf) => decryptFile(encBuf, mimeType, encInfo))
: await downloadMedia(mediaUrl);
const fileURL = URL.createObjectURL(fileContent);
const fileURL = createObjectURL(fileContent);
await saveFileToDevice(fileContent, getDownloadFilename(body), mimeType);
return fileURL;
}, [mx, url, useAuthentication, mimeType, encInfo, body])
}, [mx, url, useAuthentication, mimeType, encInfo, body, createObjectURL])
);
useRevokeObjectURL(downloadState.status === AsyncStatus.Success ? downloadState.data : undefined);
return downloadState.status === AsyncStatus.Error ? (
renderErrorButton(download, `Retry Download (${bytesToSize(info.size ?? 0)})`)
) : (
@@ -52,7 +52,7 @@ import {
} from '../../../../unstable/prefixes';
import { useFavoriteGifs } from '$hooks/useFavoriteGifs';
import { useRenderableMediaUrl } from '$hooks/useRenderableMediaUrl';
import { useRevokeObjectURL } from '$hooks/useObjectURL';
import { useCreateObjectURL } from '$hooks/useObjectURL';
import { ModalOverlay } from '$components/modal-overlay/ModalOverlay';
export function checkIfGif(url: string, mimetype?: string, body?: string) {
@@ -155,6 +155,8 @@ export const ImageContent = as<'div', ImageContentProps>(
const resolvedMediaUrl = useRenderableMediaUrl(encInfo ? undefined : rawMediaUrl);
const createObjectURL = useCreateObjectURL();
const [srcState, loadSrc] = useAsyncCallback(
useCallback(async () => {
if (encInfo) {
@@ -166,10 +168,10 @@ export const ImageContent = as<'div', ImageContentProps>(
const fileContent = await downloadEncryptedMedia(rawMediaUrl, (encBuf) =>
decryptFile(encBuf, mimeType ?? FALLBACK_MIMETYPE, encInfo)
);
return URL.createObjectURL(fileContent);
return createObjectURL(fileContent);
}
return resolvedMediaUrl ?? rawMediaUrl ?? url;
}, [rawMediaUrl, resolvedMediaUrl, url, mimeType, encInfo])
}, [rawMediaUrl, resolvedMediaUrl, url, mimeType, encInfo, createObjectURL])
);
useEffect(() => {
@@ -213,10 +215,6 @@ export const ImageContent = as<'div', ImageContentProps>(
if (autoPlay) loadSrc().catch(() => undefined);
}, [autoPlay, loadSrc]);
useRevokeObjectURL(
encInfo && srcState.status === AsyncStatus.Success ? srcState.data : undefined
);
const imageW = info?.w;
const imageH = info?.h;
const hasDimensions = typeof imageW === 'number' && typeof imageH === 'number';
@@ -14,7 +14,7 @@ import { setMediaEncryption } from '$utils/tauriMediaEncryption';
import { isTauri } from '@tauri-apps/api/core';
import { useMediaAuthentication } from '$hooks/useMediaAuthentication';
import { useRenderableMediaUrl } from '$hooks/useRenderableMediaUrl';
import { useRevokeObjectURL } from '$hooks/useObjectURL';
import { useCreateObjectURL } from '$hooks/useObjectURL';
import { FALLBACK_MIMETYPE } from '$utils/mimeTypes';
export type ThumbnailContentProps = {
@@ -35,6 +35,8 @@ export function ThumbnailContent({ info, renderImage }: ThumbnailContentProps) {
const resolvedMediaUrl = useRenderableMediaUrl(encInfo ? undefined : rawMediaUrl);
const createObjectURL = useCreateObjectURL();
const [thumbSrcState, loadThumbSrc] = useAsyncCallback(
useCallback(async () => {
const thumbInfo = info.thumbnail_info;
@@ -52,10 +54,10 @@ export function ThumbnailContent({ info, renderImage }: ThumbnailContentProps) {
const fileContent = await downloadEncryptedMedia(rawMediaUrl, (encBuf) =>
decryptFile(encBuf, thumbInfo?.mimetype ?? FALLBACK_MIMETYPE, encInfo)
);
return URL.createObjectURL(fileContent);
return createObjectURL(fileContent);
}
return resolvedMediaUrl ?? rawMediaUrl ?? thumbMxcUrl;
}, [info, thumbMxcUrl, rawMediaUrl, resolvedMediaUrl, encInfo])
}, [info, thumbMxcUrl, rawMediaUrl, resolvedMediaUrl, encInfo, createObjectURL])
);
useEffect(() => {
@@ -64,10 +66,6 @@ export function ThumbnailContent({ info, renderImage }: ThumbnailContentProps) {
loadThumbSrc().catch(() => undefined);
}, [loadThumbSrc]);
useRevokeObjectURL(
encInfo && thumbSrcState.status === AsyncStatus.Success ? thumbSrcState.data : undefined
);
if (thumbSrcState.status === AsyncStatus.Success) return renderImage(thumbSrcState.data);
if (thumbSrcState.status === AsyncStatus.Loading) {
@@ -32,7 +32,7 @@ import {
} from '$utils/matrix';
import { setMediaEncryption } from '$utils/tauriMediaEncryption';
import { useMediaAuthentication } from '$hooks/useMediaAuthentication';
import { useRevokeObjectURL } from '$hooks/useObjectURL';
import { useCreateObjectURL } from '$hooks/useObjectURL';
import { validBlurHash } from '$utils/blurHash';
import * as css from './style.css';
import { MATRIX_UNSTABLE_BLUR_HASH_PROPERTY_NAME } from '../../../../unstable/prefixes';
@@ -86,6 +86,8 @@ export const VideoContent = as<'div', VideoContentProps>(
const [blurred, setBlurred] = useState(markedAsSpoiler ?? false);
const [isHovered, setIsHovered] = useState(false);
const createObjectURL = useCreateObjectURL();
const [srcState, loadSrc] = useAsyncCallback(
useCallback(async () => {
if (url.startsWith('http')) return url;
@@ -102,11 +104,11 @@ export const VideoContent = as<'div', VideoContentProps>(
const fileContent = await downloadEncryptedMedia(mediaUrl, (encBuf) =>
decryptFile(encBuf, mimeType, encInfo)
);
return URL.createObjectURL(fileContent);
return createObjectURL(fileContent);
}
const fileContent = await downloadMedia(mediaUrl);
return URL.createObjectURL(fileContent);
}, [mx, url, useAuthentication, mimeType, encInfo])
return createObjectURL(fileContent);
}, [mx, url, useAuthentication, mimeType, encInfo, createObjectURL])
);
// When the source download succeeds, reset video-element error state so the
@@ -145,8 +147,6 @@ export const VideoContent = as<'div', VideoContentProps>(
if (autoPlay) loadSrc().catch(() => undefined);
}, [autoPlay, loadSrc]);
useRevokeObjectURL(srcState.status === AsyncStatus.Success ? srcState.data : undefined);
return (
<Box
className={classNames(css.RelativeBase, className)}