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

Fix sending of oh_hais on bad sessions #213

Merged
merged 2 commits into from Sep 17, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 41 additions & 26 deletions lib/crypto/index.js
Expand Up @@ -86,6 +86,7 @@ function Crypto(baseApis, eventEmitter, sessionStore, userId, deviceId) {

_registerEventHandlers(this, eventEmitter);

// map from userId -> deviceId -> roomId -> timestamp
this._lastNewDeviceMessageTsByUserDeviceRoom = {};
}

Expand Down Expand Up @@ -872,13 +873,21 @@ Crypto.prototype.decryptEvent = function(event) {
payload.keysProved = r.keysProved;
return payload;
} else {
// We've got a message for a session we don't have.
// Maybe the sender forgot to tell us about the session.
// Remind the sender that we exist so that they might
// tell us about the sender.
if (event.getRoomId !== undefined && event.getSender !== undefined) {
// We've got a message for a session we don't have. Maybe the sender
// forgot to tell us about the session. Remind the sender that we
// exist so that they might tell us about the session on their next
// send.
//
// (Alternatively, it might be that we are just looking at
// scrollback... at least we rate-limit the m.new_device events :/)
//
// XXX: this is a band-aid which masks symptoms of other bugs. It would
Copy link
Member

Choose a reason for hiding this comment

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

Is the bandaid not useful in papering over scenarios where we upgrade the protocol (e.g. the session id change from 1.2 to 1.3)?

Copy link
Member Author

Choose a reason for hiding this comment

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

uh, mayyybee? I'm afraid I'm not entirely clear on the difference between 1.2 and 1.3; and in any case I don't really think this is a good way to solve that problem.

// be nice to get rid of it.
if (event.room_id !== undefined && event.sender !== undefined &&
event.content.device_id !== undefined
) {
this._sendPingToDevice(
event.getSender(), event.content.device, event.getRoomId
event.sender, event.content.device_id, event.room_id
);
}

Expand All @@ -897,34 +906,40 @@ Crypto.prototype.decryptEvent = function(event) {
* @param {string} roomId The ID of the room we want to remind them about.
*/
Crypto.prototype._sendPingToDevice = function(userId, deviceId, roomId) {
if (deviceId === undefined) {
deviceId = "*";
var lastMessageTsMap = this._lastNewDeviceMessageTsByUserDeviceRoom;

var lastTsByDevice = lastMessageTsMap[userId];
if (!lastTsByDevice) {
lastTsByDevice = lastMessageTsMap[userId] = {};
}

var lastTsByRoom = lastTsByDevice[deviceId];
if (!lastTsByRoom) {
lastTsByRoom = lastTsByDevice[deviceId] = {};
}

var lastMessageTsMap = this._lastNewDeviceMessageTsByUserDeviceRoom;
var lastTsByDevice = lastMessageTsMap[userId] || {};
var lastTsByRoom = lastTsByDevice[deviceId] || {};
var lastTs = lastTsByRoom[roomId];
var timeNowMs = Date.now();
var oneHourMs = 1000 * 60 * 60;

if (lastTs === undefined || lastTs + oneHourMs < timeNowMs) {
var content = {
userId: {
deviceId: {
device_id: this._deviceId,
rooms: [roomId],
}
}
};
if (lastTs !== undefined && lastTs + oneHourMs > timeNowMs) {
// rate-limiting
return;
}

lastTsByRoom[roomId] = timeNowMs;
var content = {};
content[userId] = {};
content[userId][deviceId] = {
device_id: this._deviceId,
rooms: [roomId],
};

this._baseApis.sendToDevice(
"m.new_device", // OH HAI!
content
).done(function() {});
}
this._baseApis.sendToDevice(
"m.new_device", // OH HAI!
content
).done();

lastTsByRoom[roomId] = timeNowMs;
};

/**
Expand Down