mermaid_cli/utils/private_tmp.rs
1//! Per-user private scratch directory for short-lived sensitive files (Cause 7).
2//!
3//! Screenshots and clipboard images used to be written to fixed, predictable
4//! names directly in the shared system temp dir (`/tmp/mermaid-screenshot-N.png`,
5//! `/tmp/mermaid-clipboard-paste.png`). On a multi-user host another local user
6//! could read those frames, or pre-create / symlink the path to redirect the
7//! write. Routing them through a `0700` directory under the app data dir closes
8//! that window — only the owning user can traverse it (#11, #33).
9
10use std::path::PathBuf;
11
12/// Return (creating if needed) a `0700` per-user scratch directory under the app
13/// data dir. Callers write transient sensitive files here instead of the shared
14/// system temp dir.
15pub fn private_temp_dir() -> std::io::Result<PathBuf> {
16 let base = crate::runtime::data_dir()
17 .map_err(|e| std::io::Error::other(e.to_string()))?
18 .join("tmp");
19 std::fs::create_dir_all(&base)?;
20 #[cfg(unix)]
21 {
22 use std::os::unix::fs::PermissionsExt;
23 // Best-effort tighten to owner-only every time — cheap, and self-heals a
24 // dir that was somehow created with looser bits.
25 let _ = std::fs::set_permissions(&base, std::fs::Permissions::from_mode(0o700));
26 }
27 Ok(base)
28}