Skip to content

Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.

Write the desktop .env readable only by its owner (#432)

David McKay60d8dace2d1a
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 88de99ba..6e31a484 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,14 @@ Newest first. `Unreleased` is what is on `main` and not yet tagged.
## Unreleased
+### The desktop app writes its `.env` readable only by its owner
+
+The desktop `.env` holds `KEY_ENCRYPTION_KEY` and every minted token, and those are now long-lived:
+the first start writes them and every later start reads them back. It was created at the default
+umask (`0644`), so on a shared macOS or Linux machine another local user could read the vault key off
+disk. The file is now narrowed to `0600` after it is written. Windows has no equivalent mode and its
+single-user desktop profile is already the boundary, so the change is Unix-only.
+
### The desktop app stops adding a banner to `.env` on every start
`env::write` keeps the lines it did not write, and its own header comment is one of them, so each
diff --git a/desktop/src-tauri/src/env.rs b/desktop/src-tauri/src/env.rs
index 9032afce..d1d9b154 100644
--- a/desktop/src-tauri/src/env.rs
+++ b/desktop/src-tauri/src/env.rs
@@ -306,7 +306,20 @@ pub fn write(path: &Path, owned: &BTreeMap<String, String>) -> std::io::Result<(
out.push_str(&format!("{key}={value}\n"));
}
- std::fs::write(path, out)
+ std::fs::write(path, &out)?;
+
+ // The file holds `KEY_ENCRYPTION_KEY` and every minted token, and those are now long-lived: the
+ // first start writes them and every later start reads them back. `fs::write` creates the file at
+ // the process umask, which is `0644` by default, so on a shared macOS or Linux box another local
+ // user could read the vault key. Narrow it to the owner. Windows has no equivalent mode, and its
+ // single-user desktop profile is already the boundary, so this is Unix-only.
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::PermissionsExt;
+ std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600))?;
+ }
+
+ Ok(())
}
#[cfg(test)]
@@ -397,6 +410,26 @@ HTTPS_PROXY=http://proxy:8080
assert_eq!(text.matches(BANNER).count(), 1);
}
+ #[cfg(unix)]
+ #[test]
+ fn the_written_file_is_readable_only_by_its_owner() {
+ // It holds KEY_ENCRYPTION_KEY and every minted token, so another local user must not be able
+ // to read it off a shared machine.
+ use std::os::unix::fs::PermissionsExt;
+ let dir = std::env::temp_dir().join(format!("openbot-env-perms-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&dir);
+ std::fs::create_dir_all(&dir).unwrap();
+ let path = dir.join(".env");
+
+ let mut owned = BTreeMap::new();
+ owned.insert("KEY_ENCRYPTION_KEY".to_string(), "abc=".to_string());
+ write(&path, &owned).unwrap();
+
+ let mode = std::fs::metadata(&path).unwrap().permissions().mode() & 0o777;
+ let _ = std::fs::remove_dir_all(&dir);
+ assert_eq!(mode, 0o600);
+ }
+
#[test]
fn a_pasted_value_is_trimmed_the_way_the_model_key_beside_it_is() {
// The setup screen enables its button on `value.trim() !== ""` and sends the untrimmed