Skip to content

Commit

Permalink
Automatic session reset
Browse files Browse the repository at this point in the history
  • Loading branch information
scottnonnenberg-signal authored and josh-signal committed Mar 19, 2021
1 parent fe18722 commit 98e7e65
Show file tree
Hide file tree
Showing 26 changed files with 798 additions and 220 deletions.
16 changes: 16 additions & 0 deletions _locales/en/messages.json
Expand Up @@ -1077,6 +1077,22 @@
"message": "Secure session reset",
"description": "This is a past tense, informational message. In other words, your secure session has been reset."
},
"ChatRefresh--notification": {
"message": "Chat session refreshed",
"description": "Shown in timeline when a error happened, and the session was automatically reset."
},
"ChatRefresh--learnMore": {
"message": "Learn More",
"description": "Shown in timeline when session is automatically reset, to provide access to a popup info dialog"
},
"ChatRefresh--summary": {
"message": "Signal uses end-to-end encryption and it may need to refresh your chat session sometimes. This doesn’t affect your chat’s security but you may have missed a message from this contact and you can ask them to resend it.",
"description": "Shown on explainer dialog available from chat session refreshed timeline events"
},
"ChatRefresh--contactSupport": {
"message": "Contact Support",
"description": "Shown on explainer dialog available from chat session refreshed timeline events"
},
"quoteThumbnailAlt": {
"message": "Thumbnail of image from quoted message",
"description": "Used in alt tag of thumbnail images inside of an embedded message quote"
Expand Down
12 changes: 12 additions & 0 deletions images/chat-session-refresh.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions images/icons/v2/refresh-16.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion libtextsecure/test/fake_web_api.js
Expand Up @@ -14,7 +14,7 @@ const fakeAPI = {
getAvatar: fakeCall,
getDevices: fakeCall,
// getKeysForIdentifier : fakeCall,
getMessageSocket: fakeCall,
getMessageSocket: () => new window.MockSocket('ws://localhost:8081/'),
getMyKeys: fakeCall,
getProfile: fakeCall,
getProvisioningSocket: fakeCall,
Expand Down
11 changes: 11 additions & 0 deletions libtextsecure/test/in_memory_signal_protocol_store.js
Expand Up @@ -166,4 +166,15 @@ SignalProtocolStore.prototype = {
resolve(deviceIds);
});
},

getUnprocessedCount: () => Promise.resolve(0),
getAllUnprocessed: () => Promise.resolve([]),
getUnprocessedById: () => Promise.resolve(null),
addUnprocessed: () => Promise.resolve(),
addMultipleUnprocessed: () => Promise.resolve(),
updateUnprocessedAttempts: () => Promise.resolve(),
updateUnprocessedWithData: () => Promise.resolve(),
updateUnprocessedsWithData: () => Promise.resolve(),
removeUnprocessed: () => Promise.resolve(),
removeAllUnprocessed: () => Promise.resolve(),
};
5 changes: 2 additions & 3 deletions libtextsecure/test/index.html
Expand Up @@ -24,17 +24,15 @@
<script type="text/javascript" src="../libsignal-protocol.js"></script>
<script type="text/javascript" src="../protobufs.js" data-cover></script>
<script type="text/javascript" src="../storage/user.js" data-cover></script>
<script type="text/javascript" src="../storage/unprocessed.js" data-cover></script>
<script type="text/javascript" src="../protocol_wrapper.js" data-cover></script>

<script type="text/javascript" src="../../js/libphonenumber-util.js"></script>
<script type="text/javascript" src="../../js/components.js" data-cover></script>
<script type="text/javascript" src="../../js/signal_protocol_store.js" data-cover></script>
<script type="text/javascript" src="../../js/storage.js" data-cover></script>
<script type="text/javascript" src="../../js/models/blockedNumbers.js" data-cover></script>
<script type="text/javascript" src="../../js/models/messages.js" data-cover></script>
<script type="text/javascript" src="../../js/models/conversations.js" data-cover></script>

<script type="text/javascript" src="errors_test.js"></script>
<script type="text/javascript" src="helpers_test.js"></script>
<script type="text/javascript" src="storage_test.js"></script>
<script type="text/javascript" src="crypto_test.js"></script>
Expand All @@ -44,6 +42,7 @@
<script type="text/javascript" src="websocket-resources_test.js"></script>
<script type="text/javascript" src="task_with_timeout_test.js"></script>
<script type="text/javascript" src="account_manager_test.js"></script>
<script type="text/javascript" src="message_receiver_test.js"></script>
<script type="text/javascript" src="sendmessage_test.js"></script>

<!-- Comment out to turn off code coverage. Useful for getting real callstacks. -->
Expand Down
178 changes: 105 additions & 73 deletions libtextsecure/test/message_receiver_test.js
@@ -1,112 +1,144 @@
// Copyright 2015-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only

/* global libsignal, textsecure, SignalProtocolStore */
/* global libsignal, textsecure */

describe('MessageReceiver', () => {
textsecure.storage.impl = new SignalProtocolStore();
const { WebSocket } = window;
const number = '+19999999999';
const uuid = 'AAAAAAAA-BBBB-4CCC-9DDD-EEEEEEEEEEEE';
const deviceId = 1;
const signalingKey = libsignal.crypto.getRandomBytes(32 + 20);

before(() => {
localStorage.clear();
window.WebSocket = MockSocket;
textsecure.storage.user.setNumberAndDeviceId(number, deviceId, 'name');
textsecure.storage.user.setUuidAndDeviceId(uuid, deviceId);
textsecure.storage.put('password', 'password');
textsecure.storage.put('signaling_key', signalingKey);
});
after(() => {
localStorage.clear();
window.WebSocket = WebSocket;
});

describe('connecting', () => {
const attrs = {
type: textsecure.protobuf.Envelope.Type.CIPHERTEXT,
source: number,
sourceUuid: uuid,
sourceDevice: deviceId,
timestamp: Date.now(),
};
const websocketmessage = new textsecure.protobuf.WebSocketMessage({
type: textsecure.protobuf.WebSocketMessage.Type.REQUEST,
request: { verb: 'PUT', path: '/messages' },
});
let attrs;
let websocketmessage;

before(() => {
attrs = {
type: textsecure.protobuf.Envelope.Type.CIPHERTEXT,
source: number,
sourceUuid: uuid,
sourceDevice: deviceId,
timestamp: Date.now(),
content: libsignal.crypto.getRandomBytes(200),
};
const body = new textsecure.protobuf.Envelope(attrs).toArrayBuffer();

before(done => {
const signal = new textsecure.protobuf.Envelope(attrs).toArrayBuffer();

const aesKey = signalingKey.slice(0, 32);
const macKey = signalingKey.slice(32, 32 + 20);

window.crypto.subtle
.importKey('raw', aesKey, { name: 'AES-CBC' }, false, ['encrypt'])
.then(key => {
const iv = libsignal.crypto.getRandomBytes(16);
window.crypto.subtle
.encrypt({ name: 'AES-CBC', iv: new Uint8Array(iv) }, key, signal)
.then(ciphertext => {
window.crypto.subtle
.importKey(
'raw',
macKey,
{ name: 'HMAC', hash: { name: 'SHA-256' } },
false,
['sign']
)
.then(innerKey => {
window.crypto.subtle
.sign({ name: 'HMAC', hash: 'SHA-256' }, innerKey, signal)
.then(mac => {
const version = new Uint8Array([1]);
const message = dcodeIO.ByteBuffer.concat([
version,
iv,
ciphertext,
mac,
]);
websocketmessage.request.body = message.toArrayBuffer();
done();
});
});
});
});
websocketmessage = new textsecure.protobuf.WebSocketMessage({
type: textsecure.protobuf.WebSocketMessage.Type.REQUEST,
request: { verb: 'PUT', path: '/api/v1/message', body },
});
});

it('connects', done => {
const mockServer = new MockServer(
`ws://localhost:8080/v1/websocket/?login=${encodeURIComponent(
uuid
)}.1&password=password`
);
it('generates light-session-reset event when it cannot decrypt', done => {
const mockServer = new MockServer('ws://localhost:8081/');

mockServer.on('connection', server => {
server.send(new Blob([websocketmessage.toArrayBuffer()]));
setTimeout(() => {
server.send(new Blob([websocketmessage.toArrayBuffer()]));
}, 1);
});

window.addEventListener('textsecure:message', ev => {
const signal = ev.proto;
const keys = Object.keys(attrs);

for (let i = 0, max = keys.length; i < max; i += 1) {
const key = keys[i];
assert.strictEqual(attrs[key], signal[key]);
const messageReceiver = new textsecure.MessageReceiver(
'oldUsername',
'username',
'password',
'signalingKey',
{
serverTrustRoot: 'AAAAAAAA',
}
assert.strictEqual(signal.message.body, 'hello');
mockServer.close();
);

done();
});
messageReceiver.addEventListener('light-session-reset', done());
});
});

window.messageReceiver = new textsecure.MessageReceiver(
describe('methods', () => {
let messageReceiver;
let mockServer;

beforeEach(() => {
// Necessary to populate the server property inside of MockSocket. Without it, we
// crash when doing any number of things to a MockSocket instance.
mockServer = new MockServer('ws://localhost:8081');

messageReceiver = new textsecure.MessageReceiver(
'oldUsername',
'username',
'password',
'signalingKey'
// 'ws://localhost:8080',
// window,
'signalingKey',
{
serverTrustRoot: 'AAAAAAAA',
}
);
});
afterEach(() => {
mockServer.close();
});

describe('#isOverHourIntoPast', () => {
it('returns false for now', () => {
assert.isFalse(messageReceiver.isOverHourIntoPast(Date.now()));
});
it('returns false for 5 minutes ago', () => {
const fiveMinutesAgo = Date.now() - 5 * 60 * 1000;
assert.isFalse(messageReceiver.isOverHourIntoPast(fiveMinutesAgo));
});
it('returns true for 65 minutes ago', () => {
const sixtyFiveMinutesAgo = Date.now() - 65 * 60 * 1000;
assert.isTrue(messageReceiver.isOverHourIntoPast(sixtyFiveMinutesAgo));
});
});

describe('#cleanupSessionResets', () => {
it('leaves empty object alone', () => {
window.storage.put('sessionResets', {});
messageReceiver.cleanupSessionResets();
const actual = window.storage.get('sessionResets');

const expected = {};
assert.deepEqual(actual, expected);
});
it('filters out any timestamp older than one hour', () => {
const startValue = {
one: Date.now() - 1,
two: Date.now(),
three: Date.now() - 65 * 60 * 1000,
};
window.storage.put('sessionResets', startValue);
messageReceiver.cleanupSessionResets();
const actual = window.storage.get('sessionResets');

const expected = window._.pick(startValue, ['one', 'two']);
assert.deepEqual(actual, expected);
});
it('filters out falsey items', () => {
const startValue = {
one: 0,
two: false,
three: Date.now(),
};
window.storage.put('sessionResets', startValue);
messageReceiver.cleanupSessionResets();
const actual = window.storage.get('sessionResets');

const expected = window._.pick(startValue, ['three']);
assert.deepEqual(actual, expected);
});
});
});
});
1 change: 1 addition & 0 deletions preload.js
Expand Up @@ -45,6 +45,7 @@ try {

window.platform = process.platform;
window.getTitle = () => title;
window.getLocale = () => config.locale;
window.getEnvironment = getEnvironment;
window.getAppInstance = () => config.appInstance;
window.getVersion = () => config.version;
Expand Down

0 comments on commit 98e7e65

Please sign in to comment.