fix: unwrap encrypted media download URLs (#1789)

<!-- 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

- [ ] 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:
Erwan Leboucher authored and GitHub committed 2026-08-14 13:32:55 +02:00
commit 5bb9a3a6fa
3 files changed
+43 -5

No files matched your search

@@ -13,6 +13,7 @@ import {
mxcUrlToHttp,
rewriteAuthenticatedMediaUrl,
} from '$utils/matrix';
import { getTauriMediaSourceUrl } from '$utils/mediaUrl';
import { FALLBACK_MIMETYPE } from '$utils/mimeTypes';
import { setMediaEncryption } from '$utils/tauriMediaEncryption';
import { isTauri } from '@tauri-apps/api/core';
@@ -129,8 +130,10 @@ function ResolvedRoomMedia({
getDownloadBlob={
item.encInfo && rawMediaUrl
? () =>
downloadEncryptedMedia(rawMediaUrl, (buffer) =>
decryptFile(buffer, item.mimeType ?? FALLBACK_MIMETYPE, item.encInfo!)
downloadEncryptedMedia(
getTauriMediaSourceUrl(rawMediaUrl) ?? rawMediaUrl,
(buffer) =>
decryptFile(buffer, item.mimeType ?? FALLBACK_MIMETYPE, item.encInfo!)
)
: undefined
}
@@ -2,6 +2,7 @@ import type { ReactNode } from 'react';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';
import { ImageContent } from './ImageContent';
import { downloadEncryptedMedia } from '$utils/matrix';
const screenMocks = vi.hoisted(() => ({ isMobile: true, tauri: false }));
vi.mock('$hooks/useScreenSize', () => ({
@@ -200,6 +201,35 @@ describe('ImageContent', () => {
}
});
it('unwraps the Tauri media URL before downloading an encrypted image', async () => {
screenMocks.tauri = true;
const renderViewer = vi.fn<(props: { getDownloadBlob?: () => Promise<Blob> }) => ReactNode>(
() => <div>viewer</div>
);
vi.mocked(downloadEncryptedMedia).mockResolvedValue(new Blob(['encrypted']));
try {
render(
<ImageContent
url="mxc://example.org/abc123"
encInfo={{ key: {}, iv: 'iv', hashes: {} } as never}
renderImage={() => <img alt="preview" />}
renderViewer={renderViewer}
/>
);
touchTap(screen.getByRole('button', { name: 'View' }));
await waitFor(() => expect(renderViewer).toHaveBeenCalledOnce());
await renderViewer.mock.calls[0]?.[0].getDownloadBlob?.();
expect(downloadEncryptedMedia).toHaveBeenCalledWith(
'https://hs.example/_matrix/client/v1/media/download/example.org/abc123?__sable_media_cache=3',
expect.any(Function)
);
} finally {
screenMocks.tauri = false;
}
});
it('still allows ordinary message touches to start long press', () => {
vi.useFakeTimers();
const messageLongPress = vi.fn<() => void>();
@@ -40,7 +40,11 @@ import {
mxcUrlToHttp,
rewriteAuthenticatedMediaUrl,
} from '$utils/matrix';
import { addTauriMediaRetryRevision, getTauriMediaRetryTarget } from '$utils/mediaUrl';
import {
addTauriMediaRetryRevision,
getTauriMediaRetryTarget,
getTauriMediaSourceUrl,
} from '$utils/mediaUrl';
import { setMediaEncryption } from '$utils/tauriMediaEncryption';
import { isTauri } from '@tauri-apps/api/core';
import { useMediaAuthentication } from '$hooks/useMediaAuthentication';
@@ -288,8 +292,9 @@ export const ImageContent = as<'div', ImageContentProps>(
getDownloadBlob:
encInfo && rawMediaUrl
? () =>
downloadEncryptedMedia(rawMediaUrl, (buffer) =>
decryptFile(buffer, mimeType ?? FALLBACK_MIMETYPE, encInfo)
downloadEncryptedMedia(
getTauriMediaSourceUrl(rawMediaUrl) ?? rawMediaUrl,
(buffer) => decryptFile(buffer, mimeType ?? FALLBACK_MIMETYPE, encInfo)
)
: undefined,
})