From 0b6c587e2bce9739f38e9d0e7a8ce95fcd24505c Mon Sep 17 00:00:00 2001 From: ansuz Date: Mon, 22 May 2017 14:37:38 +0200 Subject: [PATCH 1/2] fetch and decrypt metadata with range requests --- www/file/file-crypto.js | 46 +++++++++++++++++++++++++++++++++++++++++ www/file/main.js | 12 ++++++----- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/www/file/file-crypto.js b/www/file/file-crypto.js index a36a3fc19..61e11ecdc 100644 --- a/www/file/file-crypto.js +++ b/www/file/file-crypto.js @@ -61,6 +61,43 @@ define([ return new Blob(chunks); }; + var concatBuffer = function (a, b) { // TODO make this not so ugly + return new Uint8Array(slice(a).concat(slice(b))); + }; + + var fetchMetadata = function (src, cb) { + var done = false; + var CB = function (err, res) { + if (done) { return; } + done = true; + cb(err, res); + }; + + var xhr = new XMLHttpRequest(); + xhr.open("GET", src, true); + xhr.setRequestHeader('Range', 'bytes=0-1'); + xhr.responseType = 'arraybuffer'; + + xhr.onload = function () { + if (/^4/.test('' + this.status)) { return CB('XHR_ERROR'); } + var res = new Uint8Array(xhr.response); + var size = decodePrefix(res); + var xhr2 = new XMLHttpRequest(); + + xhr2.open("GET", src, true); + xhr2.setRequestHeader('Range', 'bytes=2-' + (size + 2)); + xhr2.responseType = 'arraybuffer'; + xhr2.onload = function () { + if (/^4/.test('' + this.status)) { return CB('XHR_ERROR'); } + var res2 = new Uint8Array(xhr2.response); + var all = concatBuffer(res, res2); + CB(void 0, all); + }; + xhr2.send(null); + }; + xhr.send(null); + }; + var decryptMetadata = function (u8, key) { var prefix = u8.subarray(0, 2); var metadataLength = decodePrefix(prefix); @@ -74,6 +111,13 @@ define([ catch (e) { return null; } }; + var fetchDecryptedMetadata = function (src, key, cb) { + fetchMetadata(src, function (e, buffer) { + if (e) { return cb(e); } + cb(void 0, decryptMetadata(buffer, key)); + }); + }; + var decrypt = function (u8, key, done, progress) { var MAX = u8.length; var _progress = function (offset) { @@ -212,5 +256,7 @@ define([ joinChunks: joinChunks, computeEncryptedSize: computeEncryptedSize, decryptMetadata: decryptMetadata, + fetchMetadata: fetchMetadata, + fetchDecryptedMetadata: fetchDecryptedMetadata, }; }); diff --git a/www/file/main.js b/www/file/main.js index 66c749b89..04bf80259 100644 --- a/www/file/main.js +++ b/www/file/main.js @@ -255,19 +255,21 @@ define([ if (myFile) { return void exportFile(); } var src = Cryptpad.getBlobPathFromHex(hexFileName); + var cryptKey = secret.keys && secret.keys.fileKeyStr; + var key = Nacl.util.decodeBase64(cryptKey); + +/* return FileCrypto.fetchDecryptedMetadata(src, key, function (e, metadata) { + if (e) { return console.error(e); } + console.log(metadata); + });*/ return Cryptpad.fetch(src, function (e, u8) { if (e) { return void Cryptpad.alert(e); } - // now decrypt the u8 - var cryptKey = secret.keys && secret.keys.fileKeyStr; - var key = Nacl.util.decodeBase64(cryptKey); - if (!u8 || !u8.length) { return void Cryptpad.errorLoadingScreen(e); } - return console.error(FileCrypto.decryptMetadata(u8, key)); FileCrypto.decrypt(u8, key, function (e, data) { if (e) { return console.error(e); From 71dc0af6c76a4ab04040070a11e44fc41cb5611d Mon Sep 17 00:00:00 2001 From: ansuz Date: Mon, 22 May 2017 14:37:57 +0200 Subject: [PATCH 2/2] better test for success error codes --- www/common/common-util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/common/common-util.js b/www/common/common-util.js index f04b85199..6fb2ad7bb 100644 --- a/www/common/common-util.js +++ b/www/common/common-util.js @@ -114,7 +114,7 @@ define([], function () { xhr.open("GET", src, true); xhr.responseType = "arraybuffer"; xhr.onload = function () { - if ([200, 304].indexOf(this.status) === -1) { + if (/^4/.test(''+this.status)) { return CB('XHR_ERROR'); } return void CB(void 0, new Uint8Array(xhr.response));