From cdfae27afc6ec3a8a05d62ccd8c4771e43f63b45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gianguido=20Sor=C3=A0?= Date: Fri, 12 Aug 2022 18:47:38 +0200 Subject: [PATCH] feat(vault): implement basics of dupe detection Still needs a way to deterministically generate a key for each entry. Idea: a `hash() -> String` method on StorageContent so that new_record can call it, store said object with the newly-generated key. Since this is a deterministic operation, restoring a backup with dupes would not overwrite what's present in PDDB already. --- apps/vault/src/actions.rs | 4 +- apps/vault/src/storage.rs | 63 +++++++++++++++++++++++++++---- apps/vault/src/vendor_commands.rs | 19 ++++++---- 3 files changed, 69 insertions(+), 17 deletions(-) diff --git a/apps/vault/src/actions.rs b/apps/vault/src/actions.rs index fd28ef5b7..ef291a47a 100644 --- a/apps/vault/src/actions.rs +++ b/apps/vault/src/actions.rs @@ -321,7 +321,7 @@ impl ActionManager { count: 0, }; - match self.storage.borrow_mut().new_record(&mut record, None) { + match self.storage.borrow_mut().new_record(&mut record, None, true) { Ok(_) => (), Err(error) => { log::error!("internal error"); @@ -383,7 +383,7 @@ impl ActionManager { notes: t!("vault.notes", xous::LANG).to_string(), }; - match self.storage.borrow_mut().new_record(&mut totp, None) { + match self.storage.borrow_mut().new_record(&mut totp, None, true) { Ok(_) => (), Err(error) => { log::error!("internal error"); diff --git a/apps/vault/src/storage.rs b/apps/vault/src/storage.rs index 9f217e651..835608b05 100644 --- a/apps/vault/src/storage.rs +++ b/apps/vault/src/storage.rs @@ -17,6 +17,8 @@ pub enum Error { IoError(std::io::Error), TotpSerError(TOTPSerializationError), PasswordSerError(PasswordSerializationError), + KeyExists, + DupesExist(Vec), } impl From for Error { @@ -85,6 +87,25 @@ impl Manager { hex::encode(guid) } + fn pddb_exists(&self, dict: &str, key_name: &str, basis: Option) -> bool { + match self.pddb.get( + dict, + &key_name, + basis.as_deref(), + false, + false, + None, + Some(crate::basis_change), + ) { + Ok(_) => return true, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => false, + Err(e) => { + log::error!("error while trying to lookup if a key exists, {}", e); + false + } + } + } + fn pddb_store( &self, payload: &[u8], @@ -93,12 +114,18 @@ impl Manager { alloc_hint: Option, basis: Option, sync: bool, + overwrite: bool, ) -> Result<(), Error> { + if !overwrite && self.pddb_exists(dict, key_name, basis.clone()) { + return Err(Error::KeyExists); + } + match self.pddb.get( dict, &key_name, basis.as_deref(), - true, + true, // if overwrite, we wanna create both the dict and the key if they don't + // exist true, alloc_hint, Some(crate::basis_change), @@ -155,6 +182,7 @@ impl Manager { &mut self, record: &mut dyn StorageContent, basis: Option, + overwrite: bool, ) -> Result<(), Error> { let record = record; let settings = record.settings(); @@ -169,6 +197,7 @@ impl Manager { settings.alloc_hint, basis.clone(), true, + overwrite, ) } @@ -176,9 +205,11 @@ impl Manager { &mut self, records: Vec>, basis: Option, + overwrite: bool, ) -> Result<(), Error> { let mut precords = records.into_iter().peekable(); let mut current_idx = 0; // idk how to use peekable + enumerate + let mut dupes = vec![]; while let Some(record) = precords.next() { let mut record = record; let settings = record.settings(); @@ -187,9 +218,12 @@ impl Manager { let guid = self.gen_guid(); let should_sync = precords.peek().is_none() || current_idx % 10 == 0; - log::debug!("current_idx: {}, should_sync: {}, is_none: {}", current_idx, should_sync, precords.peek().is_none()); - - current_idx += 1; + log::debug!( + "current_idx: {}, should_sync: {}, is_none: {}", + current_idx, + should_sync, + precords.peek().is_none() + ); match self.pddb_store( &serialized_record, @@ -198,10 +232,23 @@ impl Manager { settings.alloc_hint, basis.clone(), should_sync, + overwrite, ) { - Ok(()) => continue, - Err(error) => return Err(error), - }; + Ok(()) => (), + Err(error) => match error { + Error::KeyExists => { + dupes.push(current_idx); + () + } + _ => return Err(error), + }, + } + + current_idx += 1; + } + + if !dupes.is_empty() { + return Err(Error::DupesExist(dupes)); } Ok(()) @@ -250,7 +297,7 @@ impl Manager { self.pddb .delete_key(&settings.dict, key_name, Some(&basis))?; - self.new_record(&mut *record, Some(basis)) + self.new_record(&mut *record, Some(basis), true) } pub fn delete(&mut self, kind: ContentKind, key_name: &str) -> Result<(), Error> { diff --git a/apps/vault/src/vendor_commands.rs b/apps/vault/src/vendor_commands.rs index 294ba12be..1ca6a0b1c 100644 --- a/apps/vault/src/vendor_commands.rs +++ b/apps/vault/src/vendor_commands.rs @@ -44,11 +44,7 @@ impl VendorSession { *self = VendorSession::default() } - fn read_from_wire( - &mut self, - w: backup::Wire, - command: u8, - ) -> Result<(), SessionError> { + fn read_from_wire(&mut self, w: backup::Wire, command: u8) -> Result<(), SessionError> { self.data.append(&mut w.data.clone()); self.finished = !w.more_data; @@ -298,8 +294,17 @@ fn handle_restore(data: Vec, xns: &xous_names::XousNames) -> Result, } }; - storage.new_records(entries, None)?; - Ok(vec![0xca, 0xfe, 0xba, 0xbe]) + match storage.new_records(entries, None, false) { + Ok(()) => Ok(vec![0xca, 0xfe, 0xba, 0xbe]), + Err(error) => match error { + crate::storage::Error::DupesExist(dupes) => { + // this is a non-fatal error, let's just print the dupes and continue + log::info!("dupes detected while restoring! {:?}", dupes); + Ok(vec![0xca, 0xfe, 0xba, 0xbe]) + } + _ => Err(error)?, + }, + } } fn handle_backup(