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() {
_backgroundTasksRunning = false;
keyManager.dispose();
olmManager.dispose();
keyVerificationManager.dispose();
_backgroundTasksRunning = false;
}
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -55,7 +55,7 @@ class Database extends _$Database {
Database.connect(DatabaseConnection connection) : super.connect(connection);
@override
int get schemaVersion => 6;
int get schemaVersion => 7;
int get maxFileSize => 1 * 1024 * 1024;
@ -117,6 +117,27 @@ class Database extends _$Database {
inboundGroupSessions, inboundGroupSessions.senderClaimedKeys);
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) {
Logs.error(e, s);
onError.add(SdkError(exception: e, stackTrace: s));
@ -484,10 +505,8 @@ class Database extends _$Database {
eventContent['event_id'],
chatId,
oldEvent?.sortOrder ?? eventUpdate.sortOrder,
eventContent['origin_server_ts'] != null
? DateTime.fromMillisecondsSinceEpoch(
eventContent['origin_server_ts'])
: DateTime.now(),
eventContent['origin_server_ts'] ??
DateTime.now().millisecondsSinceEpoch,
eventContent['sender'],
eventContent['type'],
json.encode(eventContent['unsigned'] ?? ''),
@ -520,10 +539,7 @@ class Database extends _$Database {
eventContent['event_id'] ?? now.millisecondsSinceEpoch.toString(),
chatId,
eventUpdate.sortOrder ?? 0.0,
eventContent['origin_server_ts'] != null
? DateTime.fromMillisecondsSinceEpoch(
eventContent['origin_server_ts'])
: now,
eventContent['origin_server_ts'] ?? now.millisecondsSinceEpoch,
eventContent['sender'],
eventContent['type'],
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,
session_id TEXT NOT NULL,
pickle TEXT NOT NULL,
last_received DATETIME,
last_received BIGINT,
UNIQUE(client_id, identity_key, session_id)
) AS DbOlmSessions;
CREATE INDEX olm_sessions_index ON olm_sessions(client_id);
@ -58,7 +58,7 @@ CREATE TABLE outbound_group_sessions (
room_id TEXT NOT NULL,
pickle 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',
UNIQUE(client_id, room_id)
) AS DbOutboundGroupSession;
@ -108,7 +108,7 @@ CREATE TABLE events (
event_id TEXT NOT NULL,
room_id TEXT NOT NULL,
sort_order DOUBLE NOT NULL,
origin_server_ts DATETIME NOT NULL,
origin_server_ts BIGINT NOT NULL,
sender TEXT NOT NULL,
type TEXT NOT NULL,
unsigned TEXT,
@ -125,7 +125,7 @@ CREATE TABLE room_states (
event_id TEXT NOT NULL,
room_id TEXT NOT NULL,
sort_order DOUBLE NOT NULL,
origin_server_ts DATETIME NOT NULL,
origin_server_ts BIGINT NOT NULL,
sender TEXT NOT NULL,
type TEXT NOT NULL,
unsigned TEXT,
@ -166,7 +166,7 @@ CREATE INDEX presences_index ON presences(client_id);
CREATE TABLE files (
mxc_uri TEXT NOT NULL PRIMARY KEY,
bytes BLOB,
saved_at DATETIME,
saved_at BIGINT,
UNIQUE(mxc_uri)
) AS DbFile;

View file

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

View file

@ -31,3 +31,7 @@ dev_dependencies:
build_runner: ^1.5.2
pedantic: ^1.9.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',
roomId: id,
sortOrder: 0.0,
originServerTs: DateTime.fromMillisecondsSinceEpoch(1432735824653),
originServerTs: 1432735824653,
sender: '@example:example.org',
type: 'm.room.join_rules',
unsigned: '{"age": 1234}',