Skip to content

Commit 0ad56ae

Browse files
committed
fix(indexeddb): skip encoding event id when constructing bounds
In the implementation of EventCacheStore, there are a number of places where the upper and lower bounds of an EventId are constructed. It is important to bypass hashing and encryption when constructing these bounds, otherwise the values will be modified and will no longer represent the bounds. Signed-off-by: Michael Goldenberg <[email protected]>
1 parent 7c88e49 commit 0ad56ae

File tree

2 files changed

+13
-30
lines changed

2 files changed

+13
-30
lines changed

crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/constants.rs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@
1515
use std::sync::LazyLock;
1616

1717
use matrix_sdk_base::linked_chunk::ChunkIdentifier;
18-
use ruma::OwnedEventId;
1918

20-
use crate::{
21-
event_cache_store::types::Position,
22-
serializer::{INDEXED_KEY_LOWER_CHARACTER, INDEXED_KEY_UPPER_CHARACTER},
23-
};
19+
use crate::event_cache_store::types::Position;
2420

2521
/// A [`ChunkIdentifier`] constructed with `0`.
2622
///
@@ -38,24 +34,6 @@ pub static INDEXED_KEY_LOWER_CHUNK_IDENTIFIER: LazyLock<ChunkIdentifier> =
3834
pub static INDEXED_KEY_UPPER_CHUNK_IDENTIFIER: LazyLock<ChunkIdentifier> =
3935
LazyLock::new(|| ChunkIdentifier::new(js_sys::Number::MAX_SAFE_INTEGER as u64));
4036

41-
/// An [`OwnedEventId`] constructed with [`INDEXED_KEY_LOWER_CHARACTER`].
42-
///
43-
/// This value is useful for constructing a key range over all keys which
44-
/// contain [`EventId`]s when used in conjunction with
45-
/// [`INDEXED_KEY_UPPER_EVENT_ID`].
46-
pub static INDEXED_KEY_LOWER_EVENT_ID: LazyLock<OwnedEventId> = LazyLock::new(|| {
47-
OwnedEventId::try_from(format!("${INDEXED_KEY_LOWER_CHARACTER}")).expect("valid event id")
48-
});
49-
50-
/// An [`OwnedEventId`] constructed with [`INDEXED_KEY_UPPER_CHARACTER`].
51-
///
52-
/// This value is useful for constructing a key range over all keys which
53-
/// contain [`EventId`]s when used in conjunction with
54-
/// [`INDEXED_KEY_LOWER_EVENT_ID`].
55-
pub static INDEXED_KEY_UPPER_EVENT_ID: LazyLock<OwnedEventId> = LazyLock::new(|| {
56-
OwnedEventId::try_from(format!("${INDEXED_KEY_UPPER_CHARACTER}")).expect("valid event id")
57-
});
58-
5937
/// The lowest possible index that can be used to reference an [`Event`] inside
6038
/// a [`Chunk`] - i.e., `0`.
6139
///

crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/indexed_types.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ use crate::{
3737
event_cache_store::{
3838
migrations::current::keys,
3939
serializer::constants::{
40-
INDEXED_KEY_LOWER_CHUNK_IDENTIFIER, INDEXED_KEY_LOWER_EVENT_ID,
41-
INDEXED_KEY_LOWER_EVENT_INDEX, INDEXED_KEY_LOWER_EVENT_POSITION,
42-
INDEXED_KEY_UPPER_CHUNK_IDENTIFIER, INDEXED_KEY_UPPER_EVENT_ID,
40+
INDEXED_KEY_LOWER_CHUNK_IDENTIFIER, INDEXED_KEY_LOWER_EVENT_INDEX,
41+
INDEXED_KEY_LOWER_EVENT_POSITION, INDEXED_KEY_UPPER_CHUNK_IDENTIFIER,
4342
INDEXED_KEY_UPPER_EVENT_INDEX, INDEXED_KEY_UPPER_EVENT_POSITION,
4443
},
4544
types::{Chunk, Event, Gap, Lease, Position},
@@ -399,14 +398,18 @@ impl IndexedPrefixKeyBounds<Event, LinkedChunkId<'_>> for IndexedEventIdKey {
399398
linked_chunk_id: LinkedChunkId<'_>,
400399
serializer: &SafeEncodeSerializer,
401400
) -> Self {
402-
Self::encode((linked_chunk_id, &*INDEXED_KEY_LOWER_EVENT_ID), serializer)
401+
let linked_chunk_id =
402+
serializer.hash_key(keys::LINKED_CHUNK_IDS, linked_chunk_id.storage_key());
403+
Self(linked_chunk_id, (*INDEXED_KEY_LOWER_STRING).clone())
403404
}
404405

405406
fn upper_key_with_prefix(
406407
linked_chunk_id: LinkedChunkId<'_>,
407408
serializer: &SafeEncodeSerializer,
408409
) -> Self {
409-
Self::encode((linked_chunk_id, &*INDEXED_KEY_UPPER_EVENT_ID), serializer)
410+
let linked_chunk_id =
411+
serializer.hash_key(keys::LINKED_CHUNK_IDS, linked_chunk_id.storage_key());
412+
Self(linked_chunk_id, (*INDEXED_KEY_UPPER_STRING).clone())
410413
}
411414
}
412415

@@ -437,11 +440,13 @@ impl IndexedKey<Event> for IndexedEventRoomKey {
437440

438441
impl IndexedPrefixKeyBounds<Event, &RoomId> for IndexedEventRoomKey {
439442
fn lower_key_with_prefix(room_id: &RoomId, serializer: &SafeEncodeSerializer) -> Self {
440-
Self::encode((room_id, &*INDEXED_KEY_LOWER_EVENT_ID), serializer)
443+
let room_id = serializer.encode_key_as_string(keys::ROOMS, room_id.as_str());
444+
Self(room_id, (*INDEXED_KEY_LOWER_STRING).clone())
441445
}
442446

443447
fn upper_key_with_prefix(room_id: &RoomId, serializer: &SafeEncodeSerializer) -> Self {
444-
Self::encode((room_id, &*INDEXED_KEY_UPPER_EVENT_ID), serializer)
448+
let room_id = serializer.encode_key_as_string(keys::ROOMS, room_id.as_str());
449+
Self(room_id, (*INDEXED_KEY_UPPER_STRING).clone())
445450
}
446451
}
447452

0 commit comments

Comments
 (0)