Skip to content
This repository was archived by the owner on Aug 5, 2021. It is now read-only.

Commit b45ace4

Browse files
committed
Clear old sessions for a device when its identity changes
1 parent ee00f08 commit b45ace4

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

dist/libsignal-protocol.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35762,6 +35762,17 @@ Internal.SessionRecord = function() {
3576235762
delete sessions[util.toString(oldestBaseKey)];
3576335763
}
3576435764
},
35765+
removePreviousSessions: function() {
35766+
var sessions = this.sessions;
35767+
for (var key in sessions) {
35768+
if (sessions[key].indexInfo.closed === -1) {
35769+
var newSessions = {};
35770+
newSessions[key] = sessions[key];
35771+
this.sessions = newSessions;
35772+
return;
35773+
}
35774+
}
35775+
},
3576535776
};
3576635777

3576735778
return SessionRecord;
@@ -35857,7 +35868,11 @@ SessionBuilder.prototype = {
3585735868
record.updateSessionState(session);
3585835869
return Promise.all([
3585935870
this.storage.storeSession(address, record.serialize()),
35860-
this.storage.saveIdentity(this.remoteAddress.getName(), session.indexInfo.remoteIdentityKey)
35871+
this.storage.saveIdentity(this.remoteAddress.getName(), session.indexInfo.remoteIdentityKey).then(function(changed) {
35872+
if (changed) {
35873+
return record.removePreviousSessions();
35874+
}
35875+
}.bind(this))
3586135876
]);
3586235877
}.bind(this));
3586335878
}.bind(this));
@@ -35914,7 +35929,11 @@ SessionBuilder.prototype = {
3591435929
// end of decryptWhisperMessage ... to ensure that the sender
3591535930
// actually holds the private keys for all reported pubkeys
3591635931
record.updateSessionState(new_session);
35917-
return this.storage.saveIdentity(this.remoteAddress.getName(), message.identityKey.toArrayBuffer()).then(function() {
35932+
return this.storage.saveIdentity(this.remoteAddress.getName(), message.identityKey.toArrayBuffer()).then(function(changed) {
35933+
if (changed) {
35934+
return record.removePreviousSessions();
35935+
}
35936+
}).then(function() {
3591835937
return message.preKeyId;
3591935938
});
3592035939
}.bind(this));

src/SessionBuilder.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ SessionBuilder.prototype = {
5151
record.updateSessionState(session);
5252
return Promise.all([
5353
this.storage.storeSession(address, record.serialize()),
54-
this.storage.saveIdentity(this.remoteAddress.getName(), session.indexInfo.remoteIdentityKey)
54+
this.storage.saveIdentity(this.remoteAddress.getName(), session.indexInfo.remoteIdentityKey).then(function(changed) {
55+
if (changed) {
56+
return record.removePreviousSessions();
57+
}
58+
}.bind(this))
5559
]);
5660
}.bind(this));
5761
}.bind(this));
@@ -108,7 +112,11 @@ SessionBuilder.prototype = {
108112
// end of decryptWhisperMessage ... to ensure that the sender
109113
// actually holds the private keys for all reported pubkeys
110114
record.updateSessionState(new_session);
111-
return this.storage.saveIdentity(this.remoteAddress.getName(), message.identityKey.toArrayBuffer()).then(function() {
115+
return this.storage.saveIdentity(this.remoteAddress.getName(), message.identityKey.toArrayBuffer()).then(function(changed) {
116+
if (changed) {
117+
return record.removePreviousSessions();
118+
}
119+
}).then(function() {
112120
return message.preKeyId;
113121
});
114122
}.bind(this));

src/SessionRecord.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,17 @@ Internal.SessionRecord = function() {
263263
delete sessions[util.toString(oldestBaseKey)];
264264
}
265265
},
266+
removePreviousSessions: function() {
267+
var sessions = this.sessions;
268+
for (var key in sessions) {
269+
if (sessions[key].indexInfo.closed === -1) {
270+
var newSessions = {};
271+
newSessions[key] = sessions[key];
272+
this.sessions = newSessions;
273+
return;
274+
}
275+
}
276+
},
266277
};
267278

268279
return SessionRecord;

0 commit comments

Comments
 (0)