From b081654c5164ac0e772cdadce1bf2d5f3fee55cf Mon Sep 17 00:00:00 2001 From: ragebreaker <125530737+mlm-games@users.noreply.github.com> Date: Wed, 7 Jan 2026 15:37:36 +0530 Subject: [PATCH] fix(search): Create key dirs if they don't exist. (#5992) The issue is related to the encrypt_store_dir fn where, when creating the key file, it doesn't ensure that the parent directory exists first. It might be not optimal for the user of the crate to ensure in an non hacky manner, as the sdk iterates through most of its directories internally. Have a test to verify it, which can be removed later (if being merged) Needs a review, might not be the optimal solution as this is my first pr with the crate and am not that familiar with it (although do use it in one of my apps). --- .../src/encrypted/encrypted_dir.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/crates/matrix-sdk-search/src/encrypted/encrypted_dir.rs b/crates/matrix-sdk-search/src/encrypted/encrypted_dir.rs index a00ea91d1..ce3afa541 100644 --- a/crates/matrix-sdk-search/src/encrypted/encrypted_dir.rs +++ b/crates/matrix-sdk-search/src/encrypted/encrypted_dir.rs @@ -13,7 +13,7 @@ // limitations under the License. use std::{ - fs::File, + fs::{File, create_dir_all}, io::{BufWriter, Cursor, Error as IoError, ErrorKind, Read, Write}, path::Path, sync::Arc, @@ -413,10 +413,13 @@ impl EncryptedMmapDirectory { passphrase: &str, pbkdf_count: u32, ) -> Result { + let dir_path = key_path.parent().unwrap_or(key_path); + + create_dir_all(dir_path).map_err(|err| err.into_tv_err(dir_path))?; // Derive a AES key from our passphrase using a randomly generated salt // to prevent bruteforce attempts using rainbow tables. let (key, hmac_key, salt) = EncryptedMmapDirectory::derive_key(passphrase, pbkdf_count) - .map_err(|err| err.into_tv_err(key_path))?; + .map_err(|err| err.into_tv_err(dir_path))?; // Generate a new random store key. This key will encrypt our Tantivy // indexing files. The key itself is stored encrypted using the derived // key. @@ -696,4 +699,13 @@ mod tests { let _ = EncryptedMmapDirectory::open(tmpdir.path(), "password") .expect("Can't open the store with the new passphrase"); } + + #[test] + fn create_store_in_nonexistent_directory() { + let tmpdir = tempdir().unwrap(); + let nested_path = tmpdir.path().join("nested").join("directory"); + let dir = EncryptedMmapDirectory::open_or_create(&nested_path, "password", PBKDF_COUNT) + .expect("Should create store in non-existent nested directory"); + drop(dir); + } }