mod bitwarden;
mod csv;
mod keepass;
mod sefy;
mod xml;
pub use sefy::{EXPORT_VERSION, Export, ExportField, ExportItem, ExportStamp, ExportVersion};
use crate::db::{self, Stamp};
use crate::error::{Error, Result};
use crate::expiry;
use crate::history::Version;
use crate::model::{Field, ItemKind, ItemSummary, NewItem, Payload};
use crate::otp;
use crate::vault::Vault;
use zeroize::Zeroizing;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Format {
Sefy,
KeePass,
Bitwarden,
Csv,
}
impl Format {
pub fn describe(self) -> &'static str {
match self {
Self::Sefy => "a sefy export",
Self::KeePass => "a KeePass XML export",
Self::Bitwarden => "a Bitwarden JSON export",
Self::Csv => "a CSV of passwords",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Target {
Sefy {
history: bool,
},
KeePass {
history: bool,
},
Csv,
}
impl Target {
pub fn name(self) -> &'static str {
match self {
Self::Sefy { .. } => "a sefy export",
Self::KeePass { .. } => "KeePass XML",
Self::Csv => "CSV",
}
}
}
pub fn detect(text: &str) -> Result<Format> {
let body = body(text);
match body.as_bytes().first() {
Some(b'{') => {
let value: serde_json::Value =
serde_json::from_str(body).map_err(|error| Error::UnreadableImport {
format: "a JSON file",
reason: error.to_string(),
})?;
let object = value.as_object().ok_or(Error::UnrecognizedImport)?;
if object.contains_key("sefy_export") {
Ok(Format::Sefy)
} else if object.contains_key("encKeyValidation_DO_NOT_EDIT")
|| (object.contains_key("items")
&& ["encrypted", "folders", "collections"]
.iter()
.any(|key| object.contains_key(*key)))
{
Ok(Format::Bitwarden)
} else {
Err(Error::UnrecognizedImport)
}
}
Some(b'<') if body.contains("<KeePassFile") => Ok(Format::KeePass),
Some(b'<') | None => Err(Error::UnrecognizedImport),
Some(_) => Ok(Format::Csv),
}
}
pub fn import(vault: &mut Vault, text: &str) -> Result<ImportReport> {
let format = detect(text)?;
let body = body(text);
let reading = match format {
Format::Sefy => sefy::read(body)?,
Format::KeePass => keepass::read(body)?,
Format::Bitwarden => bitwarden::read(body)?,
Format::Csv => csv::read(body)?,
};
apply(vault, format, reading)
}
pub fn export(vault: &Vault, target: Target) -> Result<Exported> {
match target {
Target::Sefy { history } => sefy::write(vault, history),
Target::KeePass { history } => keepass::write(vault, history),
Target::Csv => csv::write(vault),
}
}
pub struct Exported {
pub text: Zeroizing<String>,
pub report: ExportReport,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ExportReport {
pub written: usize,
pub versions: usize,
pub left_out: Vec<(ItemKind, usize)>,
pub trimmed: usize,
pub unreadable: usize,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ImportReport {
pub format: Format,
pub added: Vec<(ItemKind, usize)>,
pub skipped: usize,
pub versions: usize,
pub notices: Vec<Notice>,
pub columns_left_out: Vec<String>,
pub kept_copy: Option<std::path::PathBuf>,
}
impl ImportReport {
pub fn added_total(&self) -> usize {
self.added.iter().map(|(_, count)| count).sum()
}
pub fn not_imported(&self) -> usize {
self.notices
.iter()
.filter(|notice| notice.outcome == Outcome::NotImported)
.count()
}
fn count(&mut self, kind: ItemKind) {
match self.added.iter_mut().find(|(known, _)| *known == kind) {
Some((_, count)) => *count += 1,
None => self.added.push((kind, 1)),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Notice {
pub title: String,
pub outcome: Outcome,
pub reason: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Outcome {
NotImported,
InPart,
Reshaped,
}
#[derive(Default)]
pub(crate) struct Reading {
pub entries: Vec<Entry>,
pub notices: Vec<Notice>,
pub columns_left_out: Vec<String>,
pending: Vec<Notice>,
}
impl Reading {
pub fn notice(&mut self, title: &str, outcome: Outcome, reason: impl Into<String>) {
let notice = Notice {
title: title.to_owned(),
outcome,
reason: reason.into(),
};
if outcome == Outcome::NotImported {
self.pending.clear();
self.notices.push(notice);
} else {
self.pending.push(notice);
}
}
pub fn push(&mut self, mut entry: Entry) {
entry.notices.append(&mut self.pending);
self.entries.push(entry);
}
}
pub(crate) struct Entry {
pub uuid: Option<String>,
pub item: NewItem,
pub created_at: Option<i64>,
pub updated_at: Option<i64>,
pub lineage: Lineage,
pub notices: Vec<Notice>,
}
impl Entry {
pub fn new(uuid: Option<String>, item: NewItem) -> Self {
Self {
uuid,
item,
created_at: None,
updated_at: None,
lineage: Lineage::Earlier(Vec::new()),
notices: Vec::new(),
}
}
}
pub(crate) enum Lineage {
Recorded {
stamp: Option<Stamp>,
past: Vec<Version>,
},
Earlier(Vec<(i64, Payload)>),
}
fn apply(vault: &mut Vault, format: Format, reading: Reading) -> Result<ImportReport> {
let mut report = ImportReport {
format,
added: Vec::new(),
skipped: 0,
versions: 0,
notices: reading.notices,
columns_left_out: reading.columns_left_out,
kept_copy: None,
};
let now = crate::vault::now();
for entry in reading.entries {
let uuid = match entry.uuid {
Some(uuid) => uuid,
None => db::new_uuid()?,
};
if vault.find_by_uuid(&uuid)?.is_some() {
report.skipped += 1;
continue;
}
report.notices.extend(entry.notices);
let created_at = entry.created_at.unwrap_or(now);
let updated_at = entry.updated_at.unwrap_or(created_at);
let kind = entry.item.payload.kind();
let (stamp, past) = settle(entry.lineage, &entry.item.payload, updated_at)?;
let (_, kept) =
vault.add_travelled(entry.item, &uuid, (created_at, updated_at), &stamp, &past)?;
report.count(kind);
report.versions += kept;
}
let known = ItemKind::known();
report.added.sort_by_key(|(kind, _)| {
known
.iter()
.position(|candidate| candidate == kind)
.unwrap_or(known.len())
});
if report.added_total() > 0 {
report.kept_copy = crate::copies::keep(vault.path())?;
vault.save()?;
}
Ok(report)
}
fn settle(lineage: Lineage, current: &Payload, updated_at: i64) -> Result<(Stamp, Vec<Version>)> {
match lineage {
Lineage::Recorded { stamp, past } => {
let stamp = match stamp {
Some(stamp) => stamp,
None => {
let after = past.iter().map(|version| version.seq).max().unwrap_or(0);
Stamp::new(after + 1, updated_at, None)?
}
};
Ok((stamp, past))
}
Lineage::Earlier(earlier) => {
let mut kept: Vec<(i64, Payload)> = Vec::new();
for (made_at, payload) in earlier {
if kept.last().is_none_or(|(_, last)| *last != payload) {
kept.push((made_at, payload));
}
}
if kept.last().is_some_and(|(_, last)| last == current) {
kept.pop();
}
let mut past = Vec::with_capacity(kept.len());
for (position, (made_at, payload)) in kept.into_iter().enumerate() {
past.push(Version {
uuid: db::new_uuid()?,
seq: position as i64 + 1,
made_at,
device: None,
conflict: false,
current: false,
payload,
});
}
let stamp = Stamp::new(past.len() as i64 + 1, updated_at, None)?;
Ok((stamp, past))
}
}
}
pub(crate) struct Outgoing {
pub summary: ItemSummary,
pub payload: Payload,
pub stamp: Stamp,
pub past: Vec<Version>,
}
pub(crate) fn gather(vault: &Vault, history: bool) -> Result<Vec<Outgoing>> {
let mut items = Vec::new();
for summary in vault.list()? {
let item = vault.get(summary.id)?;
let past = if history && !matches!(item.payload, Payload::Unknown { .. }) {
vault.past_versions(summary.id)?
} else {
Vec::new()
};
items.push(Outgoing {
stamp: vault.stamp(summary.id)?,
summary: item.summary,
payload: item.payload,
past,
});
}
Ok(items)
}
pub(crate) fn leave_out(report: &mut ExportReport, kind: &ItemKind) {
match report.left_out.iter_mut().find(|(known, _)| known == kind) {
Some((_, count)) => *count += 1,
None => report.left_out.push((kind.clone(), 1)),
}
}
pub(crate) struct Record {
kind: ItemKind,
fields: Vec<Field>,
}
impl Record {
pub fn new(kind: ItemKind) -> Self {
Self {
kind,
fields: Vec::new(),
}
}
pub fn put(&mut self, name: &str, value: &str, secret: Option<bool>) {
if value.trim().is_empty() {
return;
}
let name = match name.trim() {
"" => "field",
name => name,
};
let secret = self
.kind
.template()
.and_then(|template| template.field(name))
.map(|spec| spec.secret)
.or(secret)
.unwrap_or(true);
let value = match name {
expiry::FIELD => expiry::normalize(value).unwrap_or_else(|_| value.to_owned()),
_ => value.to_owned(),
};
let mut unique = name.to_owned();
let mut counter = 2;
while self.has(&unique) {
unique = format!("{name}-{counter}");
counter += 1;
}
self.fields.push(Field {
name: unique,
value,
secret,
});
}
pub fn put_totp(&mut self, value: &str) -> bool {
if value.trim().is_empty() {
return true;
}
match otp::normalize(value) {
Ok(key) => {
self.put(otp::FIELD, &key, Some(true));
true
}
Err(_) => {
self.put("otp", value.trim(), Some(true));
false
}
}
}
pub fn has(&self, name: &str) -> bool {
self.fields.iter().any(|field| field.name == name)
}
pub fn is_empty(&self) -> bool {
self.fields.is_empty()
}
pub fn into_payload(self) -> Option<Payload> {
if self.fields.is_empty() {
return None;
}
let template = self.kind.template();
let mut fields = self.fields;
expiry::upgrade_legacy(&self.kind, &mut fields);
fields.sort_by_key(|field| {
template
.and_then(|template| {
template
.fields
.iter()
.position(|spec| spec.name == field.name)
})
.unwrap_or(usize::MAX)
});
Some(Payload::Fields {
kind: self.kind,
fields,
})
}
}
pub(crate) const UNREADABLE_TOTP: &str =
"its one-time password key is not one sefy can read, kept as the field \"otp\"";
pub(crate) fn title_or(title: &str, url: &str, login: &str) -> String {
let title = title.trim();
if !title.is_empty() {
return title.to_owned();
}
let host = host_of(url);
if !host.is_empty() {
return host.to_owned();
}
match login.trim() {
"" => "untitled".to_owned(),
login => login.to_owned(),
}
}
fn host_of(url: &str) -> &str {
let url = url.trim();
let rest = url.split_once("://").map_or(url, |(_, rest)| rest);
let authority = rest.split(['/', '?', '#']).next().unwrap_or("");
let host = authority.rsplit('@').next().unwrap_or("");
let host = host.split(':').next().unwrap_or("");
host.strip_prefix("www.").unwrap_or(host)
}
fn body(text: &str) -> &str {
text.trim_start_matches('\u{feff}').trim_start()
}
pub(crate) fn uuid_from_bytes(bytes: &[u8]) -> Option<String> {
let bytes: &[u8; 16] = bytes.try_into().ok()?;
Some(db::hyphenated(bytes))
}
pub(crate) fn uuid_to_bytes(uuid: &str) -> Option<[u8; 16]> {
let hex: String = uuid.chars().filter(|c| *c != '-').collect();
if hex.len() != 32 {
return None;
}
let mut bytes = [0u8; 16];
for (index, byte) in bytes.iter_mut().enumerate() {
*byte = u8::from_str_radix(&hex[index * 2..index * 2 + 2], 16).ok()?;
}
Some(bytes)
}
pub(crate) fn normalize_uuid(text: &str) -> Option<String> {
let text = text.trim().trim_start_matches('{').trim_end_matches('}');
uuid_to_bytes(text).map(|bytes| db::hyphenated(&bytes))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn each_format_is_told_by_its_contents() {
assert_eq!(
detect(r#"{"sefy_export":1,"items":[]}"#).unwrap(),
Format::Sefy
);
assert_eq!(
detect(r#"{"encrypted":false,"folders":[],"items":[]}"#).unwrap(),
Format::Bitwarden
);
assert_eq!(
detect(r#"{"encrypted":false,"collections":[],"items":[]}"#).unwrap(),
Format::Bitwarden
);
assert_eq!(
detect("\u{feff}<?xml version=\"1.0\"?>\n<KeePassFile><Root/></KeePassFile>").unwrap(),
Format::KeePass
);
assert_eq!(detect("name,url,username,password\n").unwrap(), Format::Csv);
}
#[test]
fn json_and_xml_that_are_neither_are_refused_by_name() {
assert!(matches!(
detect(r#"{"items": []}"#),
Err(Error::UnrecognizedImport)
));
assert!(matches!(detect("[1, 2]"), Ok(Format::Csv)));
assert!(matches!(
detect("<html></html>"),
Err(Error::UnrecognizedImport)
));
assert!(matches!(detect(" "), Err(Error::UnrecognizedImport)));
assert!(matches!(
detect("{ not json"),
Err(Error::UnreadableImport { .. })
));
}
#[test]
fn a_field_name_already_taken_gets_a_number_instead_of_replacing() {
let mut record = Record::new(ItemKind::Login);
record.put("url", "https://one.example", None);
record.put("url", "https://two.example", None);
record.put("url", "https://three.example", None);
let payload = record.into_payload().unwrap();
assert_eq!(payload.field("url").unwrap().value, "https://one.example");
assert_eq!(payload.field("url-2").unwrap().value, "https://two.example");
assert_eq!(
payload.field("url-3").unwrap().value,
"https://three.example"
);
}
#[test]
fn a_card_from_another_tool_never_carries_its_date_under_the_old_name() {
let mut record = Record::new(ItemKind::Card);
record.put("number", "4111", None);
record.put(expiry::LEGACY_CARD_NAME, "03/2029", Some(false));
let payload = record.into_payload().unwrap();
assert_eq!(payload.field(expiry::FIELD).unwrap().value, "2029-03");
assert!(payload.field(expiry::LEGACY_CARD_NAME).is_none());
let mut record = Record::new(ItemKind::Card);
record.put(expiry::LEGACY_CARD_NAME, "2029", Some(false));
let payload = record.into_payload().unwrap();
let kept = payload.field(expiry::LEGACY_CARD_NAME).unwrap();
assert_eq!(kept.value, "2029");
assert!(!kept.secret);
}
#[test]
fn secrecy_comes_from_the_template_then_the_source_then_caution() {
let mut record = Record::new(ItemKind::Login);
record.put("password", "p", Some(false));
record.put("login", "ada", Some(true));
record.put("pin hint", "birthday", Some(false));
record.put("mystery", "?", None);
let payload = record.into_payload().unwrap();
assert!(payload.field("password").unwrap().secret);
assert!(!payload.field("login").unwrap().secret);
assert!(!payload.field("pin hint").unwrap().secret);
assert!(payload.field("mystery").unwrap().secret);
}
#[test]
fn fields_settle_in_the_templates_order_with_strangers_after() {
let mut record = Record::new(ItemKind::Login);
record.put("notes", "n", None);
record.put("extra", "e", None);
record.put("password", "p", None);
record.put("login", "l", None);
let Payload::Fields { fields, .. } = record.into_payload().unwrap() else {
unreachable!()
};
let names: Vec<&str> = fields.iter().map(|field| field.name.as_str()).collect();
assert_eq!(names, ["login", "password", "notes", "extra"]);
}
#[test]
fn an_empty_value_is_no_field_and_an_empty_record_is_no_payload() {
let mut record = Record::new(ItemKind::Login);
record.put("login", "", None);
record.put("password", " ", None);
assert!(record.into_payload().is_none());
}
#[test]
fn a_title_falls_back_to_the_site_then_the_account() {
assert_eq!(title_or(" mail ", "https://x.example", "ada"), "mail");
assert_eq!(
title_or("", "https://www.accounts.example.com:443/in?next=/", "ada"),
"accounts.example.com"
);
assert_eq!(
title_or("", "ftp://user@files.example/", ""),
"files.example"
);
assert_eq!(title_or("", "", "ada"), "ada");
assert_eq!(title_or("", "", ""), "untitled");
}
#[test]
fn identities_written_other_ways_come_out_the_same() {
let canonical = "3f2b8c1e-9a4d-4e6f-8b2a-1c0d9e8f7a6b";
assert_eq!(normalize_uuid(canonical).unwrap(), canonical);
assert_eq!(
normalize_uuid("{3F2B8C1E-9A4D-4E6F-8B2A-1C0D9E8F7A6B}").unwrap(),
canonical
);
assert_eq!(
uuid_from_bytes(&uuid_to_bytes(canonical).unwrap()).unwrap(),
canonical
);
assert!(normalize_uuid("not-a-uuid").is_none());
assert!(uuid_to_bytes("3f2b8c1e").is_none());
}
fn text(value: &str) -> Payload {
Payload::Note {
text: value.to_owned(),
}
}
#[test]
fn earlier_contents_settle_into_a_numbered_line_without_repeats() {
let earlier = vec![
(10, text("a")),
(20, text("a")),
(30, text("b")),
(40, text("a")),
(50, text("c")),
(60, text("c")),
];
let (stamp, past) = settle(Lineage::Earlier(earlier), &text("c"), 70).unwrap();
let line: Vec<(i64, i64, Payload)> = past
.iter()
.map(|version| (version.seq, version.made_at, version.payload.clone()))
.collect();
assert_eq!(
line,
[(1, 10, text("a")), (2, 30, text("b")), (3, 40, text("a"))]
);
assert_eq!((stamp.seq, stamp.made_at), (4, 70));
assert!(past.iter().all(|version| version.device.is_none()));
}
#[test]
fn a_recorded_line_without_a_stamp_continues_after_its_last_version() {
let past = vec![Version {
uuid: "kept".to_owned(),
seq: 7,
made_at: 1,
device: Some("laptop".to_owned()),
conflict: true,
current: false,
payload: text("old"),
}];
let (stamp, kept) = settle(
Lineage::Recorded {
stamp: None,
past: past.clone(),
},
&text("new"),
9,
)
.unwrap();
assert_eq!(kept, past, "another sefy's versions arrive as they were");
assert_eq!((stamp.seq, stamp.made_at), (8, 9));
}
}