Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Store CLI vault keys as Base64 in system secret storage

Manav Rathi3c90adbb249c
diff --git a/docs/docs/cli/troubleshooting.md b/docs/docs/cli/troubleshooting.md
index 9e251e6081..49acf172bc 100644
--- a/docs/docs/cli/troubleshooting.md
+++ b/docs/docs/cli/troubleshooting.md
@@ -31,6 +31,12 @@ If `ente` is not found, the installation directory is not on your PATH; see [Ins
| `cannot unlock CLI vault: wrong key or damaged file` | `ENTE_CLI_VAULT_KEY` differs from the key used when the vault was created. |
| `ENTE_CLI_VAULT_KEY must be standard padded base64 of exactly 32 bytes` | Use the value printed by `ente vault key generate` without edits. |
+### "system vault key is invalid" with KeePassXC
+
+CLI 0.4.0 could save an unreadable key in KeePassXC. Upgrade to 0.4.1, delete the `io.ente.cli` entry for your CLI home from KeePassXC, and delete the [CLI home folder](./accounts.md#cli-home) (`~/.local/share/ente-cli` by default; use your configured path if you set `XDG_DATA_HOME` or `ENTE_CLI_HOME`). Then log in again.
+
+Your data in Ente and exported files are untouched. If you have an existing Photos export, run your usual export command with `--adopt` once to reconnect it.
+
## Login or creation did not finish
- `login requires otp in --input`: the server wants an email code. Request one with `ente account request-otp --email [email protected] --purpose login`, then add it to the input JSON.
diff --git a/rust/apps/cli/changes/linux-keyring.md b/rust/apps/cli/changes/linux-keyring.md
new file mode 100644
index 0000000000..98c2ffbe8f
--- /dev/null
+++ b/rust/apps/cli/changes/linux-keyring.md
@@ -0,0 +1 @@
+- Fixed saved logins becoming unreadable when using KeePassXC for system secret storage on Linux.
diff --git a/rust/apps/cli/src/vault.rs b/rust/apps/cli/src/vault.rs
index faeb22b3d2..82620c9cd6 100644
--- a/rust/apps/cli/src/vault.rs
+++ b/rust/apps/cli/src/vault.rs
@@ -260,7 +260,9 @@ fn lock(home: &Path) -> Result<File> {
fn environment_key() -> Result<Option<Key>> {
match env::var("ENTE_CLI_VAULT_KEY") {
- Ok(value) => decode_key(&Zeroizing::new(value)).map(Some),
+ Ok(value) => decode_key(&Zeroizing::new(value))
+ .context("ENTE_CLI_VAULT_KEY must be standard padded base64 of exactly 32 bytes")
+ .map(Some),
Err(env::VarError::NotPresent) => Ok(None),
Err(env::VarError::NotUnicode(_)) => {
bail!("ENTE_CLI_VAULT_KEY must be standard padded base64 of exactly 32 bytes")
@@ -269,10 +271,8 @@ fn environment_key() -> Result<Option<Key>> {
}
fn decode_key(value: &str) -> Result<Key> {
- let bytes = Zeroizing::new(b64::decode(value).map_err(|_| {
- anyhow::anyhow!("ENTE_CLI_VAULT_KEY must be standard padded base64 of exactly 32 bytes")
- })?);
- Key::try_from_slice(&bytes).context("ENTE_CLI_VAULT_KEY must decode to exactly 32 bytes")
+ let bytes = Zeroizing::new(b64::decode(value)?);
+ Ok(Key::try_from_slice(&bytes)?)
}
fn load_key(
@@ -285,12 +285,23 @@ fn load_key(
}
let entry = entry().map_err(|error| keyring_error(error, exists))?;
match entry.get_secret() {
- Ok(bytes) => Key::try_from_slice(&Zeroizing::new(bytes))
- .context("system vault key is invalid; restore the original key"),
+ Ok(bytes) => {
+ let bytes = Zeroizing::new(bytes);
+ if bytes.len() == Key::BYTES {
+ Key::try_from_slice(&bytes).map_err(Into::into)
+ } else {
+ std::str::from_utf8(&bytes)
+ .map_err(anyhow::Error::from)
+ .and_then(decode_key)
+ }
+ .context(
+ "system vault key is invalid; reset the saved key and CLI home, then log in again",
+ )
+ }
Err(keyring::Error::NoEntry) if !exists => {
let key = Key::generate();
entry
- .set_secret(key.as_bytes())
+ .set_secret(Zeroizing::new(b64::encode(key.as_bytes())).as_bytes())
.map_err(|error| keyring_error(error, false))?;
Ok(key)
}
@@ -362,11 +373,16 @@ mod tests {
expected
);
- let entry = keyring::Entry::new_with_credential(Box::new(
- keyring::mock::MockCredential::default(),
- ));
- entry.set_secret(expected.as_bytes()).unwrap();
- assert_eq!(load_key(None, exists, || Ok(entry)).unwrap(), expected);
+ for stored in [
+ expected.as_bytes().to_vec(),
+ b64::encode(expected.as_bytes()).into_bytes(),
+ ] {
+ let entry = keyring::Entry::new_with_credential(Box::new(
+ keyring::mock::MockCredential::default(),
+ ));
+ entry.set_secret(&stored).unwrap();
+ assert_eq!(load_key(None, exists, || Ok(entry)).unwrap(), expected);
+ }
}
let entry = || {
Ok(keyring::Entry::new_with_credential(Box::new(
diff --git a/rust/apps/cli/tests/cli/main.rs b/rust/apps/cli/tests/cli/main.rs
index 51bfbeb673..018df1aad3 100644
--- a/rust/apps/cli/tests/cli/main.rs
+++ b/rust/apps/cli/tests/cli/main.rs
@@ -1036,6 +1036,15 @@ fn native_keyring_survives_separate_processes() {
assert_eq!(account["id"], "42");
assert_eq!(account["email"], "[email protected]");
assert_eq!(account["apps"], json!(["photos"]));
+ #[cfg(target_os = "linux")]
+ {
+ let stored = keyring::Entry::new("io.ente.cli", &path.to_string_lossy())
+ .unwrap()
+ .get_password()
+ .unwrap();
+ let key = Key::try_from_slice(&b64::decode(&stored).unwrap()).unwrap();
+ blob::decrypt_combined(&home.read_encrypted_vault(), &key).unwrap();
+ }
verify.assert_async().await;
attrs.assert_async().await;
});