Skip to main content

agentmux_srv_core/backend/vault/import/
zoho.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Zoho Vault's CSV export (spec §8.4): `Password Name, Description, Password URL, SecretData,
5//! Notes, CustomData, Tags, Classification, Favorite, TOTP, Folder Name`. The username and
6//! password are not columns: they sit in `SecretData` as `Key:Value` lines. Written from the
7//! format as Bitwarden's importer reads it, not from an export Zoho made.
8
9use super::shared::{cell, label_of, parse_csv, sealed, Sink};
10use super::{Imported, ReadError, Source};
11
12pub(super) fn looks_like(names: &[String]) -> bool {
13    let has = |n: &str| names.iter().any(|x| x == n);
14    has("secretdata") && (has("passwordname") || has("secretname"))
15}
16
17/// The `Key:Value` lines of a cell. A line with no key continues the value before it.
18fn pairs(data: &str) -> Vec<(String, String)> {
19    let mut out: Vec<(String, String)> = Vec::new();
20    for line in data.lines() {
21        match line.split_once(':') {
22            // A key is short and isn't the scheme of an address (`https://…`); a password that
23            // starts with `//` is still the value of `Password`.
24            Some((k, v)) if !k.trim().is_empty() && k.len() <= 40 && !(v.starts_with("//") && URL_SCHEMES.contains(&k.trim().to_ascii_lowercase().as_str())) => {
25                out.push((k.trim().to_string(), v.to_string()));
26            }
27            _ => match out.last_mut() {
28                Some((_, v)) => {
29                    v.push('\n');
30                    v.push_str(line);
31                }
32                None if !line.trim().is_empty() => out.push((String::new(), line.to_string())),
33                None => {}
34            },
35        }
36    }
37    out
38}
39
40const URL_SCHEMES: &[&str] = &["http", "https", "ftp", "ftps", "sftp", "ssh", "ws", "wss", "file"];
41const USER_KEYS: &[&str] = &["user name", "username", "login", "login name", "user", "user id", "userid", "email"];
42const PASSWORD_KEYS: &[&str] = &["password", "passphrase", "pin", "key"];
43
44pub(super) fn read(text: &str) -> Result<Imported, ReadError> {
45    let t = parse_csv(text, true)?;
46    let mut sink = Sink::default();
47    for row in &t.rows {
48        let c = |names: &[&str]| cell(row, t.col(names));
49        let (name, url) = (c(&["passwordname", "secretname"]), c(&["passwordurl", "secreturl"]));
50        if name.is_empty() && url.is_empty() {
51            continue;
52        }
53        let (mut username, mut password) = (String::new(), String::new());
54        let mut kept: Vec<String> = Vec::new();
55        // The raw cells: trimming them would take the spaces off the end of a password.
56        let raw = |names: &[&str]| t.col(names).and_then(|i| row.get(i)).unwrap_or("");
57        for (k, v) in pairs(raw(&["secretdata"])).into_iter().chain(pairs(raw(&["customdata"]))) {
58            let lower = k.to_ascii_lowercase();
59            if lower == "secrettype" {
60                continue;
61            }
62            if USER_KEYS.contains(&lower.as_str()) && username.is_empty() {
63                username = v.trim().to_string();
64            } else if PASSWORD_KEYS.contains(&lower.as_str()) && password.is_empty() {
65                password = v;
66            } else if !v.trim().is_empty() {
67                kept.push(if k.is_empty() { v.trim().to_string() } else { format!("{k}: {}", v.trim()) });
68            }
69        }
70        let label = label_of(name, url, &username);
71        let secret = sealed(&[("", c(&["notes"])), ("", &kept.join("\n"))]);
72        sink.login(&label, &username, &password, url, c(&["totp", "logintotp"]), &secret, c(&["foldername"]));
73    }
74    sink.done(Source::ZohoVault)
75}