Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 7415aa4

Browse files
authored
Merge pull request #6591 from uhoreg/use_mimetype_from_info
Use the mimetype from the info property rather than the EncryptedFile
2 parents d7cb855 + 82c34e9 commit 7415aa4

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

src/customisations/models/IMediaEventContent.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,26 @@ export interface IEncryptedFile {
3131
v: string;
3232
}
3333

34-
export interface IMediaEventContent {
35-
body?: string;
36-
url?: string; // required on unencrypted media
37-
file?: IEncryptedFile; // required for *encrypted* media
38-
info?: {
39-
thumbnail_url?: string; // eslint-disable-line camelcase
40-
thumbnail_file?: IEncryptedFile; // eslint-disable-line camelcase
34+
export interface IMediaEventInfo {
35+
thumbnail_url?: string; // eslint-disable-line camelcase
36+
thumbnail_file?: IEncryptedFile; // eslint-disable-line camelcase
37+
thumbnail_info?: { // eslint-disable-line camelcase
4138
mimetype: string;
4239
w?: number;
4340
h?: number;
4441
size?: number;
4542
};
43+
mimetype: string;
44+
w?: number;
45+
h?: number;
46+
size?: number;
47+
}
48+
49+
export interface IMediaEventContent {
50+
body?: string;
51+
url?: string; // required on unencrypted media
52+
file?: IEncryptedFile; // required for *encrypted* media
53+
info?: IMediaEventInfo;
4654
}
4755

4856
export interface IPreparedMedia extends IMediaObject {

src/utils/DecryptFile.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,22 @@ limitations under the License.
1717
// Pull in the encryption lib so that we can decrypt attachments.
1818
import encrypt from 'browser-encrypt-attachment';
1919
import { mediaFromContent } from "../customisations/Media";
20-
import { IEncryptedFile } from "../customisations/models/IMediaEventContent";
20+
import { IEncryptedFile, IMediaEventInfo } from "../customisations/models/IMediaEventContent";
2121
import { getBlobSafeMimeType } from "./blobs";
2222

2323
/**
2424
* Decrypt a file attached to a matrix event.
25-
* @param {IEncryptedFile} file The json taken from the matrix event.
25+
* @param {IEncryptedFile} file The encrypted file information taken from the matrix event.
2626
* This passed to [link]{@link https://github.com/matrix-org/browser-encrypt-attachments}
2727
* as the encryption info object, so will also have the those keys in addition to
2828
* the keys below.
29+
* @param {IMediaEventInfo} info The info parameter taken from the matrix event.
2930
* @returns {Promise<Blob>} Resolves to a Blob of the file.
3031
*/
31-
export function decryptFile(file: IEncryptedFile): Promise<Blob> {
32+
export function decryptFile(
33+
file: IEncryptedFile,
34+
info?: IMediaEventInfo,
35+
): Promise<Blob> {
3236
const media = mediaFromContent({ file });
3337
// Download the encrypted file as an array buffer.
3438
return media.downloadSource().then((response) => {
@@ -44,7 +48,7 @@ export function decryptFile(file: IEncryptedFile): Promise<Blob> {
4448
// they introduce XSS attacks if the Blob URI is viewed directly in the
4549
// browser (e.g. by copying the URI into a new tab or window.)
4650
// See warning at top of file.
47-
let mimetype = file.mimetype ? file.mimetype.split(";")[0].trim() : '';
51+
let mimetype = info?.mimetype ? info.mimetype.split(";")[0].trim() : '';
4852
mimetype = getBlobSafeMimeType(mimetype);
4953

5054
return new Blob([dataArray], { type: mimetype });

src/utils/MediaEventHelper.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ export class MediaEventHelper implements IDestroyable {
7676

7777
private fetchSource = () => {
7878
if (this.media.isEncrypted) {
79-
return decryptFile(this.event.getContent<IMediaEventContent>().file);
79+
const content = this.event.getContent<IMediaEventContent>();
80+
return decryptFile(content.file, content.info);
8081
}
8182
return this.media.downloadSource().then(r => r.blob());
8283
};
@@ -87,7 +88,7 @@ export class MediaEventHelper implements IDestroyable {
8788
if (this.media.isEncrypted) {
8889
const content = this.event.getContent<IMediaEventContent>();
8990
if (content.info?.thumbnail_file) {
90-
return decryptFile(content.info.thumbnail_file);
91+
return decryptFile(content.info.thumbnail_file, content.info.thumbnail_info);
9192
} else {
9293
// "Should never happen"
9394
console.warn("Media claims to have thumbnail and is encrypted, but no thumbnail_file found");

0 commit comments

Comments
 (0)