Skip to main content

agentmux_srv_core/backend/vault/import/
keeper.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Keeper's CSV export (spec ยง8.4). It has no header row: each row is `folder, title, login,
5//! password, website, notes, shared folder`, then custom fields as pairs of a name and a value,
6//! and a one-time code is the field `TFC:Keeper`. Written from the format as Bitwarden's importer
7//! reads it, not from an export Keeper made.
8
9use super::shared::{cell, label_of, parse_csv, present, sealed, Sink};
10use super::{Imported, ReadError, Source};
11
12/// Whether a CSV that no other reader knew is Keeper's: no header, every row at least the seven
13/// columns, a title in each, a website column that is empty or an address in every row, and an
14/// address in at least one. A table with a header row fails this, since its header names are no
15/// addresses.
16pub(super) fn read(text: &str) -> Option<Result<Imported, ReadError>> {
17    let t = parse_csv(text, false).ok()?;
18    if t.rows.is_empty() || t.rows.iter().any(|r| r.len() < 7 || cell(r, Some(1)).is_empty()) {
19        return None;
20    }
21    let is_address = |w: &str| w.contains('.') || w.contains("://");
22    if t.rows.iter().any(|r| !cell(r, Some(4)).is_empty() && !is_address(cell(r, Some(4)))) || !t.rows.iter().any(|r| is_address(cell(r, Some(4)))) {
23        return None;
24    }
25    let mut sink = Sink::default();
26    for row in &t.rows {
27        let c = |i: usize| cell(row, Some(i));
28        let mut totp = String::new();
29        let mut kept: Vec<String> = Vec::new();
30        let mut i = 7;
31        while i < row.len() {
32            let (name, value) = (c(i), c(i + 1));
33            if name == "TFC:Keeper" {
34                totp = value.to_string();
35            } else if let Some(v) = present(value) {
36                kept.push(if name.is_empty() { v.to_string() } else { format!("{name}: {v}") });
37            }
38            i += 2;
39        }
40        let label = label_of(c(1), c(4), c(2));
41        let secret = sealed(&[("", c(5)), ("", &kept.join("\n"))]);
42        let password = row.get(3).unwrap_or("");
43        let tag = c(0).replace('\\', "/");
44        sink.login(&label, c(2), password, c(4), &totp, &secret, tag.trim_matches('/'));
45    }
46    Some(sink.done(Source::Keeper))
47}