Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reinstate device blocking for simple Olm #181

Merged
merged 1 commit into from
Aug 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 11 additions & 12 deletions lib/crypto-algorithms/olm.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ var q = require('q');

var utils = require("../utils");
var olmlib = require("../olmlib");
var DeviceInfo = require("../crypto-deviceinfo");
var DeviceVerification = DeviceInfo.DeviceVerification;


var base = require("./base");

Expand Down Expand Up @@ -77,21 +80,17 @@ OlmEncryption.prototype.encryptMessage = function(room, eventType, content) {
var userId = users[i];
var devices = this._crypto.getStoredDevicesForUser(userId);
for (var j = 0; j < devices.length; ++j) {
var dev = devices[j];
if (dev.blocked) {
var deviceInfo = devices[j];
var key = deviceInfo.getIdentityKey();
if (key == this._olmDevice.deviceCurve25519Key) {
// don't bother setting up session to ourself
continue;
}

for (var keyId in dev.keys) {
if (keyId.indexOf("curve25519:") === 0) {
var key = dev.keys[keyId];

// don't send to ourselves.
if (key != this._olmDevice.deviceCurve25519Key) {
participantKeys.push(key);
}
}
if (deviceInfo.verified == DeviceVerification.BLOCKED) {
// don't bother setting up sessions with blocked users
continue;
}
participantKeys.push(key);
}
}

Expand Down
54 changes: 36 additions & 18 deletions spec/integ/matrix-client-crypto.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
var sdk = require("../..");
var q = require("q");
var HttpBackend = require("../mock-request");
var utils = require("../test-utils");
var utils = require("../../lib/utils");
var test_utils = require("../test-utils");

function MockStorageApi() {
this.data = {};
}
Expand Down Expand Up @@ -119,7 +121,7 @@ function expectBobKeyUpload() {
}

function bobUploadsKeys() {
bobClient.uploadKeys(5).catch(utils.failTest);
bobClient.uploadKeys(5).catch(test_utils.failTest);
return expectBobKeyUpload();
}

Expand Down Expand Up @@ -194,7 +196,7 @@ function aliEnablesEncryption() {
// can't query keys before bob has uploaded them
expect(bobOneTimeKeys).toBeDefined();

aliQueryKeys().catch(utils.failTest);
aliQueryKeys().catch(test_utils.failTest);
aliHttpBackend.when("POST", "/keys/claim").respond(200, function(path, content) {
expect(content.one_time_keys[bobUserId][bobDeviceId]).toEqual("curve25519");
for (var keyId in bobOneTimeKeys) {
Expand Down Expand Up @@ -223,7 +225,7 @@ function aliEnablesEncryption() {
}

function bobEnablesEncryption() {
bobQueryKeys().catch(utils.failTest);
bobQueryKeys().catch(test_utils.failTest);
return bobClient.setRoomEncryption(roomId, {
algorithm: "m.olm.v1.curve25519-aes-sha2",
}).then(function(res) {
Expand All @@ -237,6 +239,7 @@ function bobEnablesEncryption() {
function aliSendsMessage() {
return sendMessage(aliHttpBackend, aliClient).then(function(content) {
aliMessages.push(content);
expect(utils.keys(content.ciphertext)).toEqual([bobDeviceCurve25519Key]);
var ciphertext = content.ciphertext[bobDeviceCurve25519Key];
expect(ciphertext).toBeDefined();
});
Expand All @@ -247,6 +250,7 @@ function bobSendsMessage() {
bobMessages.push(content);
var aliKeyId = "curve25519:" + aliDeviceId;
var aliDeviceCurve25519Key = aliDeviceKeys.keys[aliKeyId];
expect(utils.keys(content.ciphertext)).toEqual([aliDeviceCurve25519Key]);
var ciphertext = content.ciphertext[aliDeviceCurve25519Key];
expect(ciphertext).toBeDefined();
return ciphertext;
Expand Down Expand Up @@ -293,7 +297,7 @@ function recvMessage(httpBackend, client, message) {
syncData.rooms.join[roomId] = {
timeline: {
events: [
utils.mkEvent({
test_utils.mkEvent({
type: "m.room.encrypted",
room: roomId,
content: message
Expand Down Expand Up @@ -331,15 +335,15 @@ function recvMessage(httpBackend, client, message) {


function aliStartClient() {
expectAliKeyUpload().catch(utils.failTest);
expectAliKeyUpload().catch(test_utils.failTest);
startClient(aliHttpBackend, aliClient);
return aliHttpBackend.flush().then(function() {
console.log("Ali client started");
});
}

function bobStartClient() {
expectBobKeyUpload().catch(utils.failTest);
expectBobKeyUpload().catch(test_utils.failTest);
startClient(bobHttpBackend, bobClient);
return bobHttpBackend.flush().then(function() {
console.log("Bob client started");
Expand Down Expand Up @@ -368,11 +372,11 @@ function startClient(httpBackend, client) {
syncData.rooms.join[roomId] = {
state: {
events: [
utils.mkMembership({
test_utils.mkMembership({
mship: "join",
user: aliUserId,
}),
utils.mkMembership({
test_utils.mkMembership({
mship: "join",
user: bobUserId,
}),
Expand All @@ -397,7 +401,7 @@ describe("MatrixClient crypto", function() {
aliLocalStore = new MockStorageApi();
aliStorage = new sdk.WebStorageSessionStore(aliLocalStore);
bobStorage = new sdk.WebStorageSessionStore(new MockStorageApi());
utils.beforeEach(this);
test_utils.beforeEach(this);

aliHttpBackend = new HttpBackend();
aliClient = sdk.createClient({
Expand Down Expand Up @@ -436,14 +440,14 @@ describe("MatrixClient crypto", function() {
it("Bob uploads without one-time keys and with one-time keys", function(done) {
q()
.then(bobUploadsKeys)
.catch(utils.failTest).done(done);
.catch(test_utils.failTest).done(done);
});

it("Ali downloads Bobs keys", function(done) {
q()
.then(bobUploadsKeys)
.then(aliDownloadsKeys)
.catch(utils.failTest).done(done);
.catch(test_utils.failTest).done(done);
});

it("Ali gets keys with an invalid signature", function(done) {
Expand All @@ -461,15 +465,15 @@ describe("MatrixClient crypto", function() {
// should get an empty list
expect(aliClient.listDeviceKeys(bobUserId)).toEqual([]);
})
.catch(utils.failTest).done(done);
.catch(test_utils.failTest).done(done);
});

it("Ali enables encryption", function(done) {
q()
.then(bobUploadsKeys)
.then(aliStartClient)
.then(aliEnablesEncryption)
.catch(utils.failTest).done(done);
.catch(test_utils.failTest).done(done);
});

it("Ali sends a message", function(done) {
Expand All @@ -478,7 +482,7 @@ describe("MatrixClient crypto", function() {
.then(aliStartClient)
.then(aliEnablesEncryption)
.then(aliSendsMessage)
.catch(utils.failTest).done(done);
.catch(test_utils.failTest).done(done);
});

it("Bob receives a message", function(done) {
Expand All @@ -489,7 +493,21 @@ describe("MatrixClient crypto", function() {
.then(aliSendsMessage)
.then(bobStartClient)
.then(bobRecvMessage)
.catch(utils.failTest).done(done);
.catch(test_utils.failTest).done(done);
});

it("Ali blocks Bob's device", function(done) {
q()
.then(bobUploadsKeys)
.then(aliStartClient)
.then(aliEnablesEncryption)
.then(function() {
aliClient.setDeviceBlocked(bobUserId, bobDeviceId, true);
return sendMessage(aliHttpBackend, aliClient);
}).then(function(sentContent) {
// no unblocked devices, so the ciphertext should be empty
expect(sentContent.ciphertext).toEqual({});
}).catch(test_utils.failTest).done(done);
});

it("Bob receives two pre-key messages", function(done) {
Expand All @@ -502,7 +520,7 @@ describe("MatrixClient crypto", function() {
.then(bobRecvMessage)
.then(aliSendsMessage)
.then(bobRecvMessage)
.catch(utils.failTest).done(done);
.catch(test_utils.failTest).done(done);
});

it("Bob replies to the message", function(done) {
Expand All @@ -517,6 +535,6 @@ describe("MatrixClient crypto", function() {
.then(bobSendsMessage).then(function(ciphertext) {
expect(ciphertext.type).toEqual(1);
}).then(aliRecvMessage)
.catch(utils.failTest).done(done);
.catch(test_utils.failTest).done(done);
});
});