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

Encrypt attachments in encrypted rooms, #533

Merged
merged 19 commits into from
Nov 9, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
},
"dependencies": {
"babel-runtime": "^6.11.6",
"browser-encrypt-attachment": "0.0.0",
"browser-request": "^0.3.3",
"classnames": "^2.1.2",
"draft-js": "^0.8.1",
Expand Down
49 changes: 47 additions & 2 deletions src/ContentMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ var MatrixClientPeg = require('./MatrixClientPeg');
var sdk = require('./index');
var Modal = require('./Modal');

var encrypt = require("browser-encrypt-attachment");

function infoForImageFile(imageFile) {
var deferred = q.defer();

Expand Down Expand Up @@ -81,6 +83,24 @@ function infoForVideoFile(videoFile) {
return deferred.promise;
}

/**
* Read the file as an ArrayBuffer.
* @return {Promise} A promise that resolves with an ArrayBuffer when the file
* is read.
*/
function readFileAsArrayBuffer(file) {
var deferred = q.defer();
var reader = new FileReader();
reader.onload = function(e) {
deferred.resolve(e.target.result);
};
reader.onerror = function(e) {
deferred.reject(e);
};
reader.readAsArrayBuffer(file);
return deferred.promise;
}


class ContentMessages {
constructor() {
Expand Down Expand Up @@ -137,10 +157,26 @@ class ContentMessages {
this.inprogress.push(upload);
dis.dispatch({action: 'upload_started'});

var encryptInfo = null;
var error;
var self = this;
return def.promise.then(function() {
upload.promise = matrixClient.uploadContent(file);
if (matrixClient.isRoomEncrypted(room_id)) {
// If the room is encrypted then encrypt the file before uploading it.
// First read the file into memory.
upload.promise = readFileAsArrayBuffer(file).then(function(data) {
// Then encrypt the file.
return encrypt.encryptAttachment(data);
}).then(function(encryptResult) {
// Record the information needed to decrypt the attachment.
encryptInfo = encryptResult.info;
// Pass the encrypted data as a Blob to the uploader.
var blob = new Blob([encryptResult.data]);
return matrixClient.uploadContent(blob);
});
} else {
upload.promise = matrixClient.uploadContent(file);
}
return upload.promise;
}).progress(function(ev) {
if (ev) {
Expand All @@ -149,7 +185,16 @@ class ContentMessages {
dis.dispatch({action: 'upload_progress', upload: upload});
}
}).then(function(url) {
content.url = url;
if (encryptInfo === null) {
// If the attachment isn't encrypted then include the URL directly.
content.url = url;
} else {
// If the attachment is encrypted then bundle the URL along
// with the information needed to decrypt the attachment and
// add it under a file key.
encryptInfo.url = url;
content.file = encryptInfo;
}
return matrixClient.sendMessage(roomId, content);
}, function(err) {
error = err;
Expand Down
44 changes: 43 additions & 1 deletion src/components/views/messages/MImageBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ limitations under the License.
var React = require('react');
var filesize = require('filesize');

// Pull in the encryption lib so that we can decrypt attachments.
var encrypt = require("browser-encrypt-attachment");
// Pull in a fetch polyfill so we can download encrypted attachments.
require("isomorphic-fetch");

var MatrixClientPeg = require('../../../MatrixClientPeg');
var ImageUtils = require('../../../ImageUtils');
var Modal = require('../../../Modal');
var sdk = require('../../../index');
var dis = require("../../../dispatcher");


module.exports = React.createClass({
displayName: 'MImageBody',

Expand Down Expand Up @@ -85,6 +91,33 @@ module.exports = React.createClass({
componentDidMount: function() {
this.dispatcherRef = dis.register(this.onAction);
this.fixupHeight();
var content = this.props.mxEvent.getContent();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const

if (content.file !== undefined) {
// TODO: hook up an error handler to the promise.
this.decryptFile(content.file);
}
},

decryptFile: function(file) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love the fact that a function called decryptFile downloads and decrypts a file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...and also then displays it.

var url = MatrixClientPeg.get().mxcUrlToHttp(file.url);
var self = this;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We tend to use arrow functions rather than the self = this hack nowadays

// Download the encrypted file as an array buffer.
return fetch(url).then(function (response) {
return response.arrayBuffer();
}).then(function (responseData) {
// 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 use createObjectURL to make
// a url that we can use as an img src.
var blob = new Blob([dataArray]);
var blobUrl = window.URL.createObjectURL(blob);
self.refs.image.src = blobUrl;
self.refs.image.onload = function() {
window.URL.revokeObjectURL(blobUrl);
};
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this post-render DOM mangling isn't great, but we do appear to do it for animated gifs anyway. Relatedly, this will mean the current play-on-hover behaviour of animated gifs won't work with encrypted attachments, so this is a thing we'll need to fix too.

},

componentWillUnmount: function() {
Expand Down Expand Up @@ -148,7 +181,16 @@ module.exports = React.createClass({
}

var thumbUrl = this._getThumbUrl();
if (thumbUrl) {
if (content.file !== undefined) {
// Need to decrypt the attachment
// The attachment is decrypted in componentDidMount.
return (
<span className="mx_MImageBody" ref="body">
<img className="mx_MImageBody_thumbnail" src="img/encrypted-placeholder.svg" ref="image"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does the placeholder come from?

alt={content.body} />
</span>
);
} else if (thumbUrl) {
return (
<span className="mx_MImageBody" ref="body">
<a href={cli.mxcUrlToHttp(content.url)} onClick={ this.onClick }>
Expand Down