[vault] Items: optional folder and attachments (SecretBytes), for KeePass groups and files
This commit is contained in:
@@ -16,10 +16,10 @@ serde_json.workspace = true
|
||||
thiserror.workspace = true
|
||||
hkdf.workspace = true
|
||||
sha2.workspace = true
|
||||
data-encoding.workspace = true
|
||||
# The Domi identity (docs/IDENTITY.md): the mnemonic and the root key.
|
||||
bip39 = { version = "2", default-features = false, features = ["std", "zeroize"] }
|
||||
ed25519-dalek = { version = "2", default-features = false, features = ["std", "zeroize"] }
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3"
|
||||
data-encoding.workspace = true
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! Typed items and their templates (docs/FORMAT.md, "Item types and their fields").
|
||||
|
||||
use crate::secret::SecretString;
|
||||
use crate::secret::{SecretBytes, SecretString};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
||||
@@ -116,6 +116,19 @@ impl fmt::Debug for HistoryEntry {
|
||||
}
|
||||
}
|
||||
|
||||
/// A file kept with an item (imported from KeePass, docs/KDBX.md). The content counts as secret.
|
||||
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Attachment {
|
||||
pub name: String,
|
||||
pub data: SecretBytes,
|
||||
}
|
||||
|
||||
impl fmt::Debug for Attachment {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("Attachment").field("name", &self.name).field("len", &self.data.len()).finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Item {
|
||||
pub id: String,
|
||||
@@ -132,6 +145,11 @@ pub struct Item {
|
||||
pub modified: i64,
|
||||
#[serde(default)]
|
||||
pub history: Vec<HistoryEntry>,
|
||||
/// Folder names, outermost first (a KeePass group path). Empty: no folder.
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub folder: Vec<String>,
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub attachments: Vec<Attachment>,
|
||||
}
|
||||
|
||||
impl Item {
|
||||
@@ -151,6 +169,8 @@ impl Item {
|
||||
created: now,
|
||||
modified: now,
|
||||
history: Vec::new(),
|
||||
folder: Vec::new(),
|
||||
attachments: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,9 +24,9 @@ mod item;
|
||||
mod save;
|
||||
mod secret;
|
||||
|
||||
pub use item::{Field, HISTORY_LIMIT, HistoryEntry, Item, ItemType, new_id, now_ms};
|
||||
pub use item::{Attachment, Field, HISTORY_LIMIT, HistoryEntry, Item, ItemType, new_id, now_ms};
|
||||
pub use save::{CrashPoint, save_atomic, save_atomic_crashing};
|
||||
pub use secret::SecretString;
|
||||
pub use secret::{SecretBytes, SecretString};
|
||||
|
||||
use crypto::KEY_LEN;
|
||||
use format::{
|
||||
|
||||
@@ -57,3 +57,55 @@ impl<'de> Deserialize<'de> for SecretString {
|
||||
String::deserialize(d).map(Self::new)
|
||||
}
|
||||
}
|
||||
|
||||
/// Bytes that are wiped from memory when dropped and never printed (an attachment's content).
|
||||
/// `Debug` gives `[redacted]`; serialized as base64.
|
||||
#[derive(Clone, Default, PartialEq, Eq)]
|
||||
pub struct SecretBytes(Zeroizing<Vec<u8>>);
|
||||
|
||||
impl SecretBytes {
|
||||
pub fn new(b: Vec<u8>) -> Self {
|
||||
Self(Zeroizing::new(b))
|
||||
}
|
||||
|
||||
/// The content. Keep the borrow short.
|
||||
pub fn expose(&self) -> &[u8] {
|
||||
&self.0
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.0.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.0.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&[u8]> for SecretBytes {
|
||||
fn from(b: &[u8]) -> Self {
|
||||
Self::new(b.to_vec())
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for SecretBytes {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str("[redacted]")
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for SecretBytes {
|
||||
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
||||
s.serialize_str(&Zeroizing::new(data_encoding::BASE64.encode(&self.0)))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for SecretBytes {
|
||||
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
||||
let s = Zeroizing::new(String::deserialize(d)?);
|
||||
data_encoding::BASE64
|
||||
.decode(s.as_bytes())
|
||||
.map(Self::new)
|
||||
.map_err(|_| serde::de::Error::custom("attachment data is not base64"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
//! password change, no secret in Debug/Display.
|
||||
|
||||
use custos_vault::{
|
||||
CrashPoint, Error, Field, Item, ItemType, KdfParams, SecretString, Vault, save_atomic, save_atomic_crashing,
|
||||
Attachment, CrashPoint, Error, Field, Item, ItemType, KdfParams, SecretBytes, SecretString, Vault, save_atomic,
|
||||
save_atomic_crashing,
|
||||
};
|
||||
use std::path::Path;
|
||||
|
||||
@@ -229,3 +230,44 @@ fn device_wrap_opens_the_same_vault_and_nothing_else() {
|
||||
assert!(Vault::open_with_device(&bytes, &other, material).is_err());
|
||||
assert!(!format!("{:?}", Error::DeviceKeyRejected).contains("signature"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn folders_and_attachments_survive_a_save_and_old_bodies_still_open() {
|
||||
// core 0.8.0 (docs/FORMAT.md): `folder` and `attachments` are optional item members.
|
||||
let mut item = Item::new(ItemType::SecureNote, "Keys", 1);
|
||||
item.folder = vec!["Internet".into(), "Mail".into()];
|
||||
item.attachments =
|
||||
vec![Attachment { name: "id_ed25519".into(), data: SecretBytes::from(&b"\x00\xffAttached-Secret"[..]) }];
|
||||
let mut v = Vault::create(&pw("pw"), FAST).unwrap();
|
||||
v.add(item.clone());
|
||||
v.add(Item::new(ItemType::WebLogin, "Plain", 1));
|
||||
let back = Vault::open(&v.to_bytes().unwrap(), &pw("pw")).unwrap();
|
||||
assert_eq!(back.items(), v.items());
|
||||
assert_eq!(back.items()[0].folder, ["Internet", "Mail"]);
|
||||
assert_eq!(back.items()[0].attachments[0].data.expose(), b"\x00\xffAttached-Secret");
|
||||
assert!(back.items()[1].folder.is_empty() && back.items()[1].attachments.is_empty());
|
||||
|
||||
// An item written by an older core has neither member.
|
||||
let old: Item = serde_json::from_str(
|
||||
r#"{"id":"00112233445566778899aabbccddeeff","type":"web-login","title":"Old","created":1,"modified":1}"#,
|
||||
)
|
||||
.unwrap();
|
||||
assert!(old.folder.is_empty() && old.attachments.is_empty());
|
||||
// Without them, the JSON has no trace of them either (an older core reads it unchanged).
|
||||
let json = serde_json::to_string(&old).unwrap();
|
||||
assert!(!json.contains("folder") && !json.contains("attachments"), "{json}");
|
||||
// Attachment data that is not base64 is refused, not guessed.
|
||||
let bad = r#"{"id":"0","type":"secure-note","title":"x","created":1,"modified":1,"attachments":[{"name":"a","data":"!!"}]}"#;
|
||||
assert!(serde_json::from_str::<Item>(bad).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn attachment_bytes_never_print() {
|
||||
let a = Attachment { name: "secret.txt".into(), data: SecretBytes::from(&b"Attached-Secret-Bytes"[..]) };
|
||||
let mut item = Item::new(ItemType::SecureNote, "With file", 1);
|
||||
item.attachments.push(a.clone());
|
||||
for out in [format!("{a:?}"), format!("{item:?}"), format!("{item:#?}"), format!("{:?}", a.data)] {
|
||||
assert!(!out.contains("Attached-Secret-Bytes") && !out.contains("QXR0YWNoZWQt"), "{out}");
|
||||
}
|
||||
assert!(format!("{a:?}").contains("secret.txt"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user