From 3b9810719f65bf1726d9156df9f3c9127c4c6a51 Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Tue, 10 Aug 2021 19:25:56 -0400 Subject: [PATCH 1/2] use the mimetype from the info property rather than the EncryptedFile the mimetype in EncryptedFile is undocumented and redundant. see https://github.com/matrix-org/matrix-doc/pull/2582 --- .../models/IMediaEventContent.ts | 22 +++++++++++++------ src/utils/DecryptFile.ts | 12 ++++++---- src/utils/MediaEventHelper.ts | 5 +++-- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/customisations/models/IMediaEventContent.ts b/src/customisations/models/IMediaEventContent.ts index 62dfe4ee19..a3e0231ebc 100644 --- a/src/customisations/models/IMediaEventContent.ts +++ b/src/customisations/models/IMediaEventContent.ts @@ -31,18 +31,26 @@ export interface IEncryptedFile { v: string; } -export interface IMediaEventContent { - body?: string; - url?: string; // required on unencrypted media - file?: IEncryptedFile; // required for *encrypted* media - info?: { - thumbnail_url?: string; // eslint-disable-line camelcase - thumbnail_file?: IEncryptedFile; // eslint-disable-line camelcase +export interface IMediaEventInfo { + thumbnail_url?: string; // eslint-disable-line camelcase + thumbnail_file?: IEncryptedFile; // eslint-disable-line camelcase + thumbnail_info?: { // eslint-disable-line camelcase mimetype: string; w?: number; h?: number; size?: number; }; + mimetype: string; + w?: number; + h?: number; + size?: number; +} + +export interface IMediaEventContent { + body?: string; + url?: string; // required on unencrypted media + file?: IEncryptedFile; // required for *encrypted* media + info?: IMediaEventInfo; } export interface IPreparedMedia extends IMediaObject { diff --git a/src/utils/DecryptFile.ts b/src/utils/DecryptFile.ts index e66db4ffb2..34c42abb65 100644 --- a/src/utils/DecryptFile.ts +++ b/src/utils/DecryptFile.ts @@ -17,18 +17,22 @@ limitations under the License. // Pull in the encryption lib so that we can decrypt attachments. import encrypt from 'browser-encrypt-attachment'; import { mediaFromContent } from "../customisations/Media"; -import { IEncryptedFile } from "../customisations/models/IMediaEventContent"; +import { IEncryptedFile, IMediaEventInfo } from "../customisations/models/IMediaEventContent"; import { getBlobSafeMimeType } from "./blobs"; /** * Decrypt a file attached to a matrix event. - * @param {IEncryptedFile} file The json taken from the matrix event. + * @param {IEncryptedFile} file The encrypted file information taken from the matrix event. * This passed to [link]{@link https://github.com/matrix-org/browser-encrypt-attachments} * as the encryption info object, so will also have the those keys in addition to * the keys below. + * @param {IMediaEventInfo} info The info parameter taken from the matrix event. * @returns {Promise} Resolves to a Blob of the file. */ -export function decryptFile(file: IEncryptedFile): Promise { +export function decryptFile( + file: IEncryptedFile, + info: IMediaEventInfo | undefined, +): Promise { const media = mediaFromContent({ file }); // Download the encrypted file as an array buffer. return media.downloadSource().then((response) => { @@ -44,7 +48,7 @@ export function decryptFile(file: IEncryptedFile): Promise { // they introduce XSS attacks if the Blob URI is viewed directly in the // browser (e.g. by copying the URI into a new tab or window.) // See warning at top of file. - let mimetype = file.mimetype ? file.mimetype.split(";")[0].trim() : ''; + let mimetype = info?.mimetype ? info.mimetype.split(";")[0].trim() : ''; mimetype = getBlobSafeMimeType(mimetype); return new Blob([dataArray], { type: mimetype }); diff --git a/src/utils/MediaEventHelper.ts b/src/utils/MediaEventHelper.ts index 8b8edcc62a..f68da309bc 100644 --- a/src/utils/MediaEventHelper.ts +++ b/src/utils/MediaEventHelper.ts @@ -76,7 +76,8 @@ export class MediaEventHelper implements IDestroyable { private fetchSource = () => { if (this.media.isEncrypted) { - return decryptFile(this.event.getContent().file); + const content = this.event.getContent(); + return decryptFile(content.file, content.info); } return this.media.downloadSource().then(r => r.blob()); }; @@ -87,7 +88,7 @@ export class MediaEventHelper implements IDestroyable { if (this.media.isEncrypted) { const content = this.event.getContent(); if (content.info?.thumbnail_file) { - return decryptFile(content.info.thumbnail_file); + return decryptFile(content.info.thumbnail_file, content.info.thumbnail_info); } else { // "Should never happen" console.warn("Media claims to have thumbnail and is encrypted, but no thumbnail_file found"); From 82c34e9ed105a33571eba3a986bcb34df49d067b Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Tue, 10 Aug 2021 19:37:18 -0400 Subject: [PATCH 2/2] Update src/utils/DecryptFile.ts Co-authored-by: Michael Telatynski <7t3chguy@gmail.com> --- src/utils/DecryptFile.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/DecryptFile.ts b/src/utils/DecryptFile.ts index 34c42abb65..891439ffe1 100644 --- a/src/utils/DecryptFile.ts +++ b/src/utils/DecryptFile.ts @@ -31,7 +31,7 @@ import { getBlobSafeMimeType } from "./blobs"; */ export function decryptFile( file: IEncryptedFile, - info: IMediaEventInfo | undefined, + info?: IMediaEventInfo, ): Promise { const media = mediaFromContent({ file }); // Download the encrypted file as an array buffer.