fix(appd): create the runtime directory private wherever it is made

appd created $XDG_RUNTIME_DIR/weft with the umask mode when it opened
its own socket, wrote its port or system token, or opened a portal
socket, so the 0700 mode requested for session sockets never applied.
All of them now use one helper that creates the directory 0700 and
tightens an existing one.
This commit is contained in:
Marco Allegretti 2026-10-09 09:27:28 +00:00
parent ec00531c75
commit 010573eb16
No known key found for this signature in database
2 changed files with 19 additions and 8 deletions

View file

@ -190,7 +190,7 @@ async fn run() -> anyhow::Result<()> {
let socket_path = appd_socket_path()?; let socket_path = appd_socket_path()?;
if let Some(parent) = socket_path.parent() { if let Some(parent) = socket_path.parent() {
std::fs::create_dir_all(parent).with_context(|| format!("create {}", parent.display()))?; private_dir(parent).with_context(|| format!("create {}", parent.display()))?;
} }
let _ = std::fs::remove_file(&socket_path); let _ = std::fs::remove_file(&socket_path);
@ -366,7 +366,7 @@ fn system_token_path() -> std::io::Result<PathBuf> {
.map_err(|_| std::io::Error::other("XDG_RUNTIME_DIR not set"))?, .map_err(|_| std::io::Error::other("XDG_RUNTIME_DIR not set"))?,
) )
.join("weft"); .join("weft");
std::fs::create_dir_all(&dir)?; private_dir(&dir)?;
Ok(dir.join("appd.systoken")) Ok(dir.join("appd.systoken"))
} }
@ -375,7 +375,7 @@ fn write_ws_port(port: u16) -> anyhow::Result<()> {
let runtime_dir = std::env::var("XDG_RUNTIME_DIR").context("XDG_RUNTIME_DIR not set")?; let runtime_dir = std::env::var("XDG_RUNTIME_DIR").context("XDG_RUNTIME_DIR not set")?;
let path = PathBuf::from(runtime_dir).join("weft/appd.wsport"); let path = PathBuf::from(runtime_dir).join("weft/appd.wsport");
if let Some(parent) = path.parent() { if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).with_context(|| format!("create {}", parent.display()))?; private_dir(parent).with_context(|| format!("create {}", parent.display()))?;
} }
std::fs::write(&path, port.to_string()).with_context(|| format!("write {}", path.display()))?; std::fs::write(&path, port.to_string()).with_context(|| format!("write {}", path.display()))?;
Ok(()) Ok(())
@ -415,14 +415,25 @@ async fn handle_connection(
pub(crate) fn session_ipc_socket_path(session_id: u64) -> Option<PathBuf> { pub(crate) fn session_ipc_socket_path(session_id: u64) -> Option<PathBuf> {
let runtime_dir = std::env::var("XDG_RUNTIME_DIR").ok()?; let runtime_dir = std::env::var("XDG_RUNTIME_DIR").ok()?;
let dir = PathBuf::from(runtime_dir).join("weft"); let dir = PathBuf::from(runtime_dir).join("weft");
// Created accessible to the user only, so session sockets are private private_dir(&dir).ok()?;
// whatever the umask. Some(dir.join(format!("ipc-{session_id}.sock")))
}
/// Creates appd's runtime directory (`$XDG_RUNTIME_DIR/weft`) or another of
/// its directories, accessible to the user only whatever the umask, also
/// when it already existed.
pub(crate) fn private_dir(dir: &std::path::Path) -> std::io::Result<()> {
let mut builder = std::fs::DirBuilder::new(); let mut builder = std::fs::DirBuilder::new();
builder.recursive(true); builder.recursive(true);
#[cfg(unix)] #[cfg(unix)]
std::os::unix::fs::DirBuilderExt::mode(&mut builder, 0o700); std::os::unix::fs::DirBuilderExt::mode(&mut builder, 0o700);
builder.create(&dir).ok()?; builder.create(dir)?;
Some(dir.join(format!("ipc-{session_id}.sock"))) #[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(dir, std::fs::Permissions::from_mode(0o700))?;
}
Ok(())
} }
pub(crate) async fn dispatch(req: Request, registry: &Registry) -> Response { pub(crate) async fn dispatch(req: Request, registry: &Registry) -> Response {

View file

@ -212,7 +212,7 @@ fn spawn_app_shell(
fn portal_socket_path(session_id: u64) -> Option<PathBuf> { fn portal_socket_path(session_id: u64) -> Option<PathBuf> {
let runtime_dir = std::env::var("XDG_RUNTIME_DIR").ok()?; let runtime_dir = std::env::var("XDG_RUNTIME_DIR").ok()?;
let dir = PathBuf::from(runtime_dir).join("weft"); let dir = PathBuf::from(runtime_dir).join("weft");
std::fs::create_dir_all(&dir).ok()?; crate::private_dir(&dir).ok()?;
Some(dir.join(format!("portal-{session_id}.sock"))) Some(dir.join(format!("portal-{session_id}.sock")))
} }