Skip to content

IndexedDB: Make IndexeddbSerializer shareable between modules in matrix-sdk-indexeddb #4996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ use indexed_db_futures::{prelude::*, web_sys::DomException};
use tracing::info;
use wasm_bindgen::JsValue;

use crate::{
crypto_store::{indexeddb_serializer::IndexeddbSerializer, Result},
IndexeddbCryptoStoreError,
};
use crate::{crypto_store::Result, serializer::IndexeddbSerializer, IndexeddbCryptoStoreError};

mod old_keys;
mod v0_to_v5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ use indexed_db_futures::IdbQuerySource;
use wasm_bindgen::JsValue;
use web_sys::{DomException, IdbTransactionMode};

use crate::crypto_store::{
indexeddb_serializer::IndexeddbSerializer,
keys,
migrations::{do_schema_upgrade, old_keys, MigrationDb},
use crate::{
crypto_store::{
keys,
migrations::{do_schema_upgrade, old_keys, MigrationDb},
},
serializer::IndexeddbSerializer,
};

/// Migrate data from `backup_keys.backup_key_v1` to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ use web_sys::{DomException, IdbTransactionMode};

use crate::{
crypto_store::{
indexeddb_serializer::IndexeddbSerializer,
keys,
migrations::{add_nonunique_index, do_schema_upgrade, old_keys, v7, MigrationDb},
Result,
},
serializer::IndexeddbSerializer,
IndexeddbCryptoStoreError,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ use web_sys::{DomException, IdbTransactionMode};

use crate::{
crypto_store::{
indexeddb_serializer::IndexeddbSerializer,
migrations::{do_schema_upgrade, old_keys, v7, MigrationDb},
Result,
},
serializer::IndexeddbSerializer,
IndexeddbCryptoStoreError,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ use web_sys::{DomException, IdbTransactionMode};

use crate::{
crypto_store::{
indexeddb_serializer::IndexeddbSerializer,
keys,
migrations::{
add_nonunique_index, do_schema_upgrade, old_keys,
v7::InboundGroupSessionIndexedDbObject2, MigrationDb,
},
InboundGroupSessionIndexedDbObject, Result,
},
serializer::IndexeddbSerializer,
IndexeddbCryptoStoreError,
};

Expand Down
31 changes: 22 additions & 9 deletions crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@ use tracing::{debug, warn};
use wasm_bindgen::JsValue;
use web_sys::IdbKeyRange;

use self::indexeddb_serializer::MaybeEncrypted;
use crate::crypto_store::{
indexeddb_serializer::IndexeddbSerializer, migrations::open_and_upgrade_db,
use crate::{
crypto_store::migrations::open_and_upgrade_db,
serializer::{IndexeddbSerializer, IndexeddbSerializerError, MaybeEncrypted},
};

mod indexeddb_serializer;
mod migrations;

mod keys {
Expand Down Expand Up @@ -148,6 +147,20 @@ pub enum IndexeddbCryptoStoreError {
SchemaTooNewError { max_supported_version: u32, current_version: u32 },
}

impl From<IndexeddbSerializerError> for IndexeddbCryptoStoreError {
fn from(value: IndexeddbSerializerError) -> Self {
match value {
IndexeddbSerializerError::Serialization(error) => Self::Serialization(error),
IndexeddbSerializerError::DomException { code, name, message } => {
Self::DomException { code, name, message }
}
IndexeddbSerializerError::CryptoStoreError(crypto_store_error) => {
Self::CryptoStoreError(crypto_store_error)
}
}
}
}

impl From<web_sys::DomException> for IndexeddbCryptoStoreError {
fn from(frm: web_sys::DomException) -> IndexeddbCryptoStoreError {
IndexeddbCryptoStoreError::DomException {
Expand Down Expand Up @@ -1141,7 +1154,7 @@ impl_crypto_store! {
.object_store(keys::DEVICES)?
.get(&key)?
.await?
.map(|i| self.serializer.deserialize_value(i))
.map(|i| self.serializer.deserialize_value(i).map_err(Into::into))
.transpose()
}

Expand Down Expand Up @@ -1178,7 +1191,7 @@ impl_crypto_store! {
.object_store(keys::IDENTITIES)?
.get(&self.serializer.encode_key(keys::IDENTITIES, user_id))?
.await?
.map(|i| self.serializer.deserialize_value(i))
.map(|i| self.serializer.deserialize_value(i).map_err(Into::into))
.transpose()
}

Expand Down Expand Up @@ -1355,7 +1368,7 @@ impl_crypto_store! {
.object_store(keys::ROOM_SETTINGS)?
.get(&key)?
.await?
.map(|v| self.serializer.deserialize_value(v))
.map(|v| self.serializer.deserialize_value(v).map_err(Into::into))
.transpose()
}

Expand All @@ -1372,7 +1385,7 @@ impl_crypto_store! {
.object_store(keys::CORE)?
.get(&JsValue::from_str(key))?
.await?
.map(|v| self.serializer.deserialize_value(v))
.map(|v| self.serializer.deserialize_value(v).map_err(Into::into))
.transpose()
}

Expand Down Expand Up @@ -1769,7 +1782,7 @@ mod unit_tests {
use ruma::{device_id, room_id, user_id};

use super::InboundGroupSessionIndexedDbObject;
use crate::crypto_store::indexeddb_serializer::{IndexeddbSerializer, MaybeEncrypted};
use crate::serializer::{IndexeddbSerializer, MaybeEncrypted};

#[test]
fn needs_backup_is_serialized_as_a_u8_in_json() {
Expand Down
2 changes: 2 additions & 0 deletions crates/matrix-sdk-indexeddb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ mod crypto_store;
mod safe_encode;
#[cfg(feature = "e2e-encryption")]
mod serialize_bool_for_indexeddb;
#[cfg(feature = "e2e-encryption")]
mod serializer;
#[cfg(feature = "state-store")]
mod state_store;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ use wasm_bindgen::JsValue;
use web_sys::IdbKeyRange;
use zeroize::Zeroizing;

use crate::{safe_encode::SafeEncode, IndexeddbCryptoStoreError};
use crate::safe_encode::SafeEncode;

type Result<A, E = IndexeddbCryptoStoreError> = std::result::Result<A, E>;
type Result<A, E = IndexeddbSerializerError> = std::result::Result<A, E>;

const BASE64: GeneralPurpose = GeneralPurpose::new(&alphabet::STANDARD, general_purpose::NO_PAD);

Expand All @@ -39,6 +39,35 @@ pub struct IndexeddbSerializer {
store_cipher: Option<Arc<StoreCipher>>,
}

#[derive(Debug, thiserror::Error)]
pub enum IndexeddbSerializerError {
#[error(transparent)]
Serialization(#[from] serde_json::Error),
#[error("DomException {name} ({code}): {message}")]
DomException {
/// DomException code
code: u16,
/// Specific name of the DomException
name: String,
/// Message given to the DomException
message: String,
},
#[error(transparent)]
CryptoStoreError(#[from] CryptoStoreError),
}

impl From<web_sys::DomException> for IndexeddbSerializerError {
fn from(frm: web_sys::DomException) -> Self {
Self::DomException { name: frm.name(), message: frm.message(), code: frm.code() }
}
}

impl From<serde_wasm_bindgen::Error> for IndexeddbSerializerError {
fn from(e: serde_wasm_bindgen::Error) -> Self {
Self::Serialization(serde::de::Error::custom(e.to_string()))
}
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(untagged)]
pub enum MaybeEncrypted {
Expand Down Expand Up @@ -90,15 +119,15 @@ impl IndexeddbSerializer {
&self,
table_name: &str,
key: T,
) -> Result<IdbKeyRange, IndexeddbCryptoStoreError>
) -> Result<IdbKeyRange, IndexeddbSerializerError>
where
T: SafeEncode,
{
match &self.store_cipher {
Some(cipher) => key.encode_to_range_secure(table_name, cipher),
None => key.encode_to_range(),
}
.map_err(|e| IndexeddbCryptoStoreError::DomException {
.map_err(|e| IndexeddbSerializerError::DomException {
code: 0,
name: "IdbKeyRangeMakeError".to_owned(),
message: e,
Expand All @@ -113,7 +142,7 @@ impl IndexeddbSerializer {
pub fn serialize_value(
&self,
value: &impl Serialize,
) -> Result<JsValue, IndexeddbCryptoStoreError> {
) -> Result<JsValue, IndexeddbSerializerError> {
let serialized = self.maybe_encrypt_value(value)?;
Ok(serde_wasm_bindgen::to_value(&serialized)?)
}
Expand Down Expand Up @@ -167,7 +196,7 @@ impl IndexeddbSerializer {
pub fn deserialize_value<T: DeserializeOwned>(
&self,
value: JsValue,
) -> Result<T, IndexeddbCryptoStoreError> {
) -> Result<T, IndexeddbSerializerError> {
// Objects which are serialized nowadays should be represented as a
// `MaybeEncrypted`. However, `serialize_value` previously used a
// different format, so we need to handle that in case we have old data.
Expand Down Expand Up @@ -221,11 +250,11 @@ impl IndexeddbSerializer {
pub fn deserialize_legacy_value<T: DeserializeOwned>(
&self,
value: JsValue,
) -> Result<T, IndexeddbCryptoStoreError> {
) -> Result<T, IndexeddbSerializerError> {
match &self.store_cipher {
Some(cipher) => {
if !value.is_array() {
return Err(IndexeddbCryptoStoreError::CryptoStoreError(
return Err(IndexeddbSerializerError::CryptoStoreError(
CryptoStoreError::UnpicklingError,
));
}
Expand Down
Loading