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.
This commit is contained in:
Gianguido Sorà 2022-08-12 18:47:38 +02:00
parent 473e1cf77b
commit cdfae27afc
3 changed files with 69 additions and 17 deletions

View file

@ -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");

View file

@ -17,6 +17,8 @@ pub enum Error {
IoError(std::io::Error),
TotpSerError(TOTPSerializationError),
PasswordSerError(PasswordSerializationError),
KeyExists,
DupesExist(Vec<usize>),
}
impl From<std::io::Error> for Error {
@ -85,6 +87,25 @@ impl Manager {
hex::encode(guid)
}
fn pddb_exists(&self, dict: &str, key_name: &str, basis: Option<String>) -> 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<usize>,
basis: Option<String>,
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<String>,
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<Box<dyn StorageContent>>,
basis: Option<String>,
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> {

View file

@ -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<u8>, xns: &xous_names::XousNames) -> Result<Vec<u8>,
}
};
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(