2016-11-04 15:46:45 +03:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
2018-04-29 05:07:31 +03:00
|
|
|
Copyright 2018 New Vector Ltd
|
2016-11-04 15:46:45 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Pull in the encryption lib so that we can decrypt attachments.
|
2016-11-08 14:42:20 +03:00
|
|
|
import encrypt from 'browser-encrypt-attachment';
|
2016-11-04 15:46:45 +03:00
|
|
|
// Grab the client so that we can turn mxc:// URLs into https:// URLS.
|
2019-12-21 00:13:46 +03:00
|
|
|
import {MatrixClientPeg} from '../MatrixClientPeg';
|
2021-03-04 04:16:27 +03:00
|
|
|
import {mediaFromContent} from "../customisations/Media";
|
2016-11-04 18:39:39 +03:00
|
|
|
|
2018-04-29 05:07:31 +03:00
|
|
|
// WARNING: We have to be very careful about what mime-types we allow into blobs,
|
|
|
|
// as for performance reasons these are now rendered via URL.createObjectURL()
|
|
|
|
// rather than by converting into data: URIs.
|
|
|
|
//
|
|
|
|
// This means that the content is rendered using the origin of the script which
|
|
|
|
// called createObjectURL(), and so if the content contains any scripting then it
|
|
|
|
// will pose a XSS vulnerability when the browser renders it. This is particularly
|
|
|
|
// bad if the user right-clicks the URI and pastes it into a new window or tab,
|
2020-08-03 18:02:26 +03:00
|
|
|
// as the blob will then execute with access to Element's full JS environment(!)
|
2018-04-29 05:07:31 +03:00
|
|
|
//
|
|
|
|
// See https://github.com/matrix-org/matrix-react-sdk/pull/1820#issuecomment-385210647
|
|
|
|
// for details.
|
|
|
|
//
|
|
|
|
// We mitigate this by only allowing mime-types into blobs which we know don't
|
|
|
|
// contain any scripting, and instantiate all others as application/octet-stream
|
|
|
|
// regardless of what mime-type the event claimed. Even if the payload itself
|
|
|
|
// is some malicious HTML, the fact we instantiate it with a media mimetype or
|
|
|
|
// application/octet-stream means the browser doesn't try to render it as such.
|
|
|
|
//
|
|
|
|
// One interesting edge case is image/svg+xml, which empirically *is* rendered
|
|
|
|
// correctly if the blob is set to the src attribute of an img tag (for thumbnails)
|
|
|
|
// *even if the mimetype is application/octet-stream*. However, empirically JS
|
|
|
|
// in the SVG isn't executed in this scenario, so we seem to be okay.
|
|
|
|
//
|
|
|
|
// Tested on Chrome 65 and Firefox 60
|
|
|
|
//
|
|
|
|
// The list below is taken mainly from
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/HTML/Supported_media_formats
|
|
|
|
// N.B. Matrix doesn't currently specify which mimetypes are valid in given
|
|
|
|
// events, so we pick the ones which HTML5 browsers should be able to display
|
|
|
|
//
|
|
|
|
// For the record, mime-types which must NEVER enter this list below include:
|
|
|
|
// text/html, text/xhtml, image/svg, image/svg+xml, image/pdf, and similar.
|
2016-11-04 18:39:39 +03:00
|
|
|
|
2018-04-29 05:07:31 +03:00
|
|
|
const ALLOWED_BLOB_MIMETYPES = {
|
|
|
|
'image/jpeg': true,
|
|
|
|
'image/gif': true,
|
|
|
|
'image/png': true,
|
2016-11-04 15:46:45 +03:00
|
|
|
|
2018-04-29 05:07:31 +03:00
|
|
|
'video/mp4': true,
|
|
|
|
'video/webm': true,
|
|
|
|
'video/ogg': true,
|
|
|
|
|
|
|
|
'audio/mp4': true,
|
|
|
|
'audio/webm': true,
|
|
|
|
'audio/aac': true,
|
|
|
|
'audio/mpeg': true,
|
|
|
|
'audio/ogg': true,
|
|
|
|
'audio/wave': true,
|
|
|
|
'audio/wav': true,
|
|
|
|
'audio/x-wav': true,
|
|
|
|
'audio/x-pn-wav': true,
|
|
|
|
'audio/flac': true,
|
|
|
|
'audio/x-flac': true,
|
2018-10-27 06:50:35 +03:00
|
|
|
};
|
2016-11-04 15:46:45 +03:00
|
|
|
|
2016-11-08 14:42:20 +03:00
|
|
|
/**
|
|
|
|
* Decrypt a file attached to a matrix event.
|
2020-10-30 21:18:17 +03:00
|
|
|
* @param {Object} file The json taken from the matrix event.
|
2016-11-08 14:42:20 +03:00
|
|
|
* 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.
|
2020-10-30 21:18:17 +03:00
|
|
|
* @param {string} file.url An mxc:// URL for the encrypted file.
|
|
|
|
* @param {string} file.mimetype The MIME-type of the plaintext file.
|
|
|
|
* @returns {Promise}
|
2016-11-08 14:42:20 +03:00
|
|
|
*/
|
2016-11-04 15:46:45 +03:00
|
|
|
export function decryptFile(file) {
|
2021-03-04 04:16:27 +03:00
|
|
|
const media = mediaFromContent({file});
|
2016-11-04 15:46:45 +03:00
|
|
|
// Download the encrypted file as an array buffer.
|
2021-03-04 04:16:27 +03:00
|
|
|
return media.downloadSource().then(function(response) {
|
2016-11-04 15:46:45 +03:00
|
|
|
return response.arrayBuffer();
|
2016-11-08 14:42:20 +03:00
|
|
|
}).then(function(responseData) {
|
2016-11-04 15:46:45 +03:00
|
|
|
// Decrypt the array buffer using the information taken from
|
|
|
|
// the event content.
|
|
|
|
return encrypt.decryptAttachment(responseData, file);
|
|
|
|
}).then(function(dataArray) {
|
|
|
|
// Turn the array into a Blob and give it the correct MIME-type.
|
2018-04-29 05:07:31 +03:00
|
|
|
|
|
|
|
// IMPORTANT: we must not allow scriptable mime-types into Blobs otherwise
|
|
|
|
// 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.
|
2018-04-29 05:09:17 +03:00
|
|
|
let mimetype = file.mimetype ? file.mimetype.split(";")[0].trim() : '';
|
2018-04-29 05:07:31 +03:00
|
|
|
if (!ALLOWED_BLOB_MIMETYPES[mimetype]) {
|
|
|
|
mimetype = 'application/octet-stream';
|
|
|
|
}
|
|
|
|
|
|
|
|
const blob = new Blob([dataArray], {type: mimetype});
|
2016-12-02 17:21:07 +03:00
|
|
|
return blob;
|
2016-11-04 15:46:45 +03:00
|
|
|
});
|
|
|
|
}
|