Skip to content

Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.

fix(desktop): settings and the local API token are private to the user

Bee300c787df335
diff --git a/desktop/app/src/local_api.rs b/desktop/app/src/local_api.rs
index ea4b286..90c987c 100644
--- a/desktop/app/src/local_api.rs
+++ b/desktop/app/src/local_api.rs
@@ -124,7 +124,7 @@ pub fn write_token_file(data_dir: &Path, port: u16, token: &str) -> std::io::Res
let tmp = path.with_extension("json.tmp");
let data = ApiTokenFile { port, token: token.to_owned() };
{
- let mut file = fs::File::create(&tmp)?;
+ let mut file = crate::settings::create_private_file(&tmp)?;
file.write_all(serde_json::to_string_pretty(&data)?.as_bytes())?;
file.sync_all()?;
}
diff --git a/desktop/app/src/settings.rs b/desktop/app/src/settings.rs
index 9498d19..7982e46 100644
--- a/desktop/app/src/settings.rs
+++ b/desktop/app/src/settings.rs
@@ -648,7 +648,7 @@ impl Settings {
let path = Self::path(dir);
let tmp = path.with_extension("json.tmp");
{
- let mut file = fs::File::create(&tmp)?;
+ let mut file = create_private_file(&tmp)?;
file.write_all(serde_json::to_string_pretty(self)?.as_bytes())?;
file.sync_all()?;
}
@@ -656,10 +656,47 @@ impl Settings {
}
}
+/// Creates (or truncates) a file only this user can read: settings hold the
+/// OBS password and the local API token file grants control of the app. On
+/// Windows the per-user AppData folder already keeps others out.
+pub(crate) fn create_private_file(path: &Path) -> std::io::Result<fs::File> {
+ let mut options = fs::OpenOptions::new();
+ options.write(true).create(true).truncate(true);
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::OpenOptionsExt;
+ options.mode(0o600);
+ }
+ let file = options.open(path)?;
+ // The mode above only applies to a new file; an older one keeps its own.
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::PermissionsExt;
+ file.set_permissions(fs::Permissions::from_mode(0o600))?;
+ }
+ Ok(file)
+}
+
#[cfg(test)]
mod tests {
use super::*;
+ #[cfg(unix)]
+ #[test]
+ fn settings_and_token_files_are_private() {
+ use std::os::unix::fs::PermissionsExt;
+ let dir = tempfile::tempdir().unwrap();
+ // A leftover file from before keeps its mode unless it's reset.
+ let leftover = dir.path().join("settings.json.tmp");
+ fs::write(&leftover, "{}").unwrap();
+ fs::set_permissions(&leftover, fs::Permissions::from_mode(0o644)).unwrap();
+ Settings::default().save(dir.path()).unwrap();
+ let mode = fs::metadata(Settings::path(dir.path())).unwrap().permissions().mode() & 0o777;
+ assert_eq!(mode, 0o600);
+ let token = crate::local_api::write_token_file(dir.path(), 1234, &"a".repeat(64)).unwrap();
+ assert_eq!(fs::metadata(token).unwrap().permissions().mode() & 0o777, 0o600);
+ }
+
#[test]
fn round_trips_and_defaults() {
let dir = tempfile::tempdir().unwrap();