fix: store timestamps in milliseconds to fix decrypt error

This commit is contained in:
Sorunome 2020-10-02 11:25:31 +02:00
parent 84cc925b08
commit ae79af6ea8
No known key found for this signature in database
GPG key ID: B19471D07FC9BE9C
12 changed files with 711 additions and 312 deletions

View file

@ -362,10 +362,10 @@ class Encryption {
} }
void dispose() { void dispose() {
_backgroundTasksRunning = false;
keyManager.dispose(); keyManager.dispose();
olmManager.dispose(); olmManager.dispose();
keyVerificationManager.dispose(); keyVerificationManager.dispose();
_backgroundTasksRunning = false;
} }
} }

View file

@ -286,7 +286,7 @@ class KeyManager {
roomId, roomId,
sess.outboundGroupSession.pickle(client.userID), sess.outboundGroupSession.pickle(client.userID),
json.encode(sess.devices), json.encode(sess.devices),
sess.creationTime, sess.creationTime.millisecondsSinceEpoch,
sess.sentMessages); sess.sentMessages);
} }

View file

@ -238,8 +238,12 @@ class OlmManager {
// update an existing session // update an existing session
_olmSessions[session.identityKey][ix] = session; _olmSessions[session.identityKey][ix] = session;
} }
client.database.storeOlmSession(client.id, session.identityKey, client.database.storeOlmSession(
session.sessionId, session.pickledSession, session.lastReceived); client.id,
session.identityKey,
session.sessionId,
session.pickledSession,
session.lastReceived.millisecondsSinceEpoch);
} }
ToDeviceEvent _decryptToDeviceEvent(ToDeviceEvent event) { ToDeviceEvent _decryptToDeviceEvent(ToDeviceEvent event) {

View file

@ -46,7 +46,7 @@ class OlmSession {
identityKey = dbEntry.identityKey; identityKey = dbEntry.identityKey;
sessionId = dbEntry.sessionId; sessionId = dbEntry.sessionId;
lastReceived = lastReceived =
dbEntry.lastReceived ?? DateTime.fromMillisecondsSinceEpoch(0); DateTime.fromMillisecondsSinceEpoch(dbEntry.lastReceived ?? 0);
assert(sessionId == session.session_id()); assert(sessionId == session.session_id());
} catch (e, s) { } catch (e, s) {
Logs.error('[LibOlm] Could not unpickle olm session: ' + e.toString(), s); Logs.error('[LibOlm] Could not unpickle olm session: ' + e.toString(), s);

View file

@ -44,7 +44,7 @@ class OutboundGroupSession {
try { try {
outboundGroupSession.unpickle(key, dbEntry.pickle); outboundGroupSession.unpickle(key, dbEntry.pickle);
devices = List<String>.from(json.decode(dbEntry.deviceIds)); devices = List<String>.from(json.decode(dbEntry.deviceIds));
creationTime = dbEntry.creationTime; creationTime = DateTime.fromMillisecondsSinceEpoch(dbEntry.creationTime);
sentMessages = dbEntry.sentMessages; sentMessages = dbEntry.sentMessages;
} catch (e, s) { } catch (e, s) {
dispose(); dispose();

View file

@ -1579,15 +1579,19 @@ sort order of ${prevState.sortOrder}. This should never happen...''');
/// you can safely make this Client instance null. /// you can safely make this Client instance null.
Future<void> dispose({bool closeDatabase = false}) async { Future<void> dispose({bool closeDatabase = false}) async {
_disposed = true; _disposed = true;
encryption?.dispose();
encryption = null;
try { try {
await _currentTransaction; await _currentTransaction;
} catch (_) { } catch (_) {
// No-OP // No-OP
} }
try {
if (closeDatabase) await database?.close(); if (closeDatabase) await database?.close();
} catch (error, stacktrace) {
Logs.warning('Failed to close database: ' + error.toString(), stacktrace);
}
database = null; database = null;
encryption?.dispose();
encryption = null;
return; return;
} }
} }

View file

@ -55,7 +55,7 @@ class Database extends _$Database {
Database.connect(DatabaseConnection connection) : super.connect(connection); Database.connect(DatabaseConnection connection) : super.connect(connection);
@override @override
int get schemaVersion => 6; int get schemaVersion => 7;
int get maxFileSize => 1 * 1024 * 1024; int get maxFileSize => 1 * 1024 * 1024;
@ -117,6 +117,27 @@ class Database extends _$Database {
inboundGroupSessions, inboundGroupSessions.senderClaimedKeys); inboundGroupSessions, inboundGroupSessions.senderClaimedKeys);
from++; from++;
} }
if (from == 6) {
// DATETIME was internally an int, so we should be able to re-use the
// olm_sessions table.
await m.deleteTable('outbound_group_sessions');
await m.createTable(outboundGroupSessions);
await m.deleteTable('events');
await m.createTable(events);
await m.deleteTable('room_states');
await m.createTable(roomStates);
await m.deleteTable('files');
await m.createTable(files);
// and now clear cache
await delete(presences).go();
await delete(roomAccountData).go();
await delete(accountData).go();
await delete(roomStates).go();
await delete(events).go();
await delete(rooms).go();
await delete(outboundGroupSessions).go();
await customStatement('UPDATE clients SET prev_batch = null');
}
} catch (e, s) { } catch (e, s) {
Logs.error(e, s); Logs.error(e, s);
onError.add(SdkError(exception: e, stackTrace: s)); onError.add(SdkError(exception: e, stackTrace: s));
@ -484,10 +505,8 @@ class Database extends _$Database {
eventContent['event_id'], eventContent['event_id'],
chatId, chatId,
oldEvent?.sortOrder ?? eventUpdate.sortOrder, oldEvent?.sortOrder ?? eventUpdate.sortOrder,
eventContent['origin_server_ts'] != null eventContent['origin_server_ts'] ??
? DateTime.fromMillisecondsSinceEpoch( DateTime.now().millisecondsSinceEpoch,
eventContent['origin_server_ts'])
: DateTime.now(),
eventContent['sender'], eventContent['sender'],
eventContent['type'], eventContent['type'],
json.encode(eventContent['unsigned'] ?? ''), json.encode(eventContent['unsigned'] ?? ''),
@ -520,10 +539,7 @@ class Database extends _$Database {
eventContent['event_id'] ?? now.millisecondsSinceEpoch.toString(), eventContent['event_id'] ?? now.millisecondsSinceEpoch.toString(),
chatId, chatId,
eventUpdate.sortOrder ?? 0.0, eventUpdate.sortOrder ?? 0.0,
eventContent['origin_server_ts'] != null eventContent['origin_server_ts'] ?? now.millisecondsSinceEpoch,
? DateTime.fromMillisecondsSinceEpoch(
eventContent['origin_server_ts'])
: now,
eventContent['sender'], eventContent['sender'],
eventContent['type'], eventContent['type'],
json.encode(eventContent['unsigned'] ?? ''), json.encode(eventContent['unsigned'] ?? ''),

File diff suppressed because it is too large Load diff

View file

@ -48,7 +48,7 @@ CREATE TABLE olm_sessions (
identity_key TEXT NOT NULL, identity_key TEXT NOT NULL,
session_id TEXT NOT NULL, session_id TEXT NOT NULL,
pickle TEXT NOT NULL, pickle TEXT NOT NULL,
last_received DATETIME, last_received BIGINT,
UNIQUE(client_id, identity_key, session_id) UNIQUE(client_id, identity_key, session_id)
) AS DbOlmSessions; ) AS DbOlmSessions;
CREATE INDEX olm_sessions_index ON olm_sessions(client_id); CREATE INDEX olm_sessions_index ON olm_sessions(client_id);
@ -58,7 +58,7 @@ CREATE TABLE outbound_group_sessions (
room_id TEXT NOT NULL, room_id TEXT NOT NULL,
pickle TEXT NOT NULL, pickle TEXT NOT NULL,
device_ids TEXT NOT NULL, device_ids TEXT NOT NULL,
creation_time DATETIME NOT NULL, creation_time BIGINT NOT NULL,
sent_messages INTEGER NOT NULL DEFAULT '0', sent_messages INTEGER NOT NULL DEFAULT '0',
UNIQUE(client_id, room_id) UNIQUE(client_id, room_id)
) AS DbOutboundGroupSession; ) AS DbOutboundGroupSession;
@ -108,7 +108,7 @@ CREATE TABLE events (
event_id TEXT NOT NULL, event_id TEXT NOT NULL,
room_id TEXT NOT NULL, room_id TEXT NOT NULL,
sort_order DOUBLE NOT NULL, sort_order DOUBLE NOT NULL,
origin_server_ts DATETIME NOT NULL, origin_server_ts BIGINT NOT NULL,
sender TEXT NOT NULL, sender TEXT NOT NULL,
type TEXT NOT NULL, type TEXT NOT NULL,
unsigned TEXT, unsigned TEXT,
@ -125,7 +125,7 @@ CREATE TABLE room_states (
event_id TEXT NOT NULL, event_id TEXT NOT NULL,
room_id TEXT NOT NULL, room_id TEXT NOT NULL,
sort_order DOUBLE NOT NULL, sort_order DOUBLE NOT NULL,
origin_server_ts DATETIME NOT NULL, origin_server_ts BIGINT NOT NULL,
sender TEXT NOT NULL, sender TEXT NOT NULL,
type TEXT NOT NULL, type TEXT NOT NULL,
unsigned TEXT, unsigned TEXT,
@ -166,7 +166,7 @@ CREATE INDEX presences_index ON presences(client_id);
CREATE TABLE files ( CREATE TABLE files (
mxc_uri TEXT NOT NULL PRIMARY KEY, mxc_uri TEXT NOT NULL PRIMARY KEY,
bytes BLOB, bytes BLOB,
saved_at DATETIME, saved_at BIGINT,
UNIQUE(mxc_uri) UNIQUE(mxc_uri)
) AS DbFile; ) AS DbFile;

View file

@ -192,7 +192,9 @@ class Event extends MatrixEvent {
eventId: dbEntry.eventId, eventId: dbEntry.eventId,
roomId: dbEntry.roomId, roomId: dbEntry.roomId,
senderId: dbEntry.sender, senderId: dbEntry.sender,
originServerTs: dbEntry.originServerTs ?? DateTime.now(), originServerTs: dbEntry.originServerTs != null
? DateTime.fromMillisecondsSinceEpoch(dbEntry.originServerTs)
: DateTime.now(),
unsigned: unsigned, unsigned: unsigned,
room: room, room: room,
sortOrder: dbEntry.sortOrder ?? 0.0, sortOrder: dbEntry.sortOrder ?? 0.0,
@ -425,8 +427,8 @@ class Event extends MatrixEvent {
storeable = storeable && storeable = storeable &&
uint8list.lengthInBytes < room.client.database.maxFileSize; uint8list.lengthInBytes < room.client.database.maxFileSize;
if (storeable) { if (storeable) {
await room.client.database await room.client.database.storeFile(mxContent.toString(), uint8list,
.storeFile(mxContent.toString(), uint8list, DateTime.now()); DateTime.now().millisecondsSinceEpoch);
} }
} }

View file

@ -31,3 +31,7 @@ dev_dependencies:
build_runner: ^1.5.2 build_runner: ^1.5.2
pedantic: ^1.9.0 pedantic: ^1.9.0
moor_ffi: ^0.5.0 moor_ffi: ^0.5.0
dependency_overrides:
# See https://github.com/flutter/flutter/issues/62240
analyzer: '0.39.14'

View file

@ -73,7 +73,7 @@ void main() {
eventId: '143273582443PhrSn:example.org', eventId: '143273582443PhrSn:example.org',
roomId: id, roomId: id,
sortOrder: 0.0, sortOrder: 0.0,
originServerTs: DateTime.fromMillisecondsSinceEpoch(1432735824653), originServerTs: 1432735824653,
sender: '@example:example.org', sender: '@example:example.org',
type: 'm.room.join_rules', type: 'm.room.join_rules',
unsigned: '{"age": 1234}', unsigned: '{"age": 1234}',