Fail closed on privacy permission errors
This commit is contained in:
@@ -16,6 +16,10 @@ Each bounded iteration must:
|
||||
|
||||
## Privacy and data protection
|
||||
|
||||
- [x] Fail closed when configuration permissions cannot be restricted, and
|
||||
refuse file logging when the state directory or existing log permissions
|
||||
cannot be restricted (24 X11-feature tests and Docker GTK/X11/package build,
|
||||
2026-09-12).
|
||||
- [x] Create database, log, config, lock, and runtime directories with private permissions (mode probe and Docker build, 2026-09-10).
|
||||
- [x] Handle pre-existing overly-permissive database/log permissions safely (existing database and date-suffixed log regression tests, 17 tests and Docker build, 2026-09-10).
|
||||
- [x] Add tests/probes for file modes under permissive umasks (database regression test plus runtime probe, 2026-09-10).
|
||||
|
||||
@@ -37,9 +37,22 @@ impl Config {
|
||||
if !path.exists() {
|
||||
return Ok(Self::default());
|
||||
}
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::set_permissions(parent, fs::Permissions::from_mode(0o700)).with_context(|| {
|
||||
format!(
|
||||
"failed to restrict configuration directory {}",
|
||||
parent.display()
|
||||
)
|
||||
})?;
|
||||
}
|
||||
let text = fs::read_to_string(&path)
|
||||
.with_context(|| format!("failed to read configuration {}", path.display()))?;
|
||||
let _ = fs::set_permissions(&path, fs::Permissions::from_mode(0o600));
|
||||
fs::set_permissions(&path, fs::Permissions::from_mode(0o600)).with_context(|| {
|
||||
format!(
|
||||
"failed to restrict configuration permissions {}",
|
||||
path.display()
|
||||
)
|
||||
})?;
|
||||
let config: Self = toml::from_str(&text).with_context(|| {
|
||||
format!(
|
||||
"invalid configuration {}; fix it or remove it",
|
||||
|
||||
@@ -12,8 +12,10 @@ pub fn init(configured_level: &str) -> LoggingGuard {
|
||||
let filter = std::env::var("RUST_LOG").unwrap_or_else(|_| configured_level.to_owned());
|
||||
let state_dir = dirs::state_dir().map(|path| path.join("clipbhoard"));
|
||||
if let Some(state_dir) = state_dir {
|
||||
if std::fs::create_dir_all(&state_dir).is_ok() {
|
||||
let _ = std::fs::set_permissions(&state_dir, std::fs::Permissions::from_mode(0o700));
|
||||
if std::fs::create_dir_all(&state_dir).is_ok()
|
||||
&& restrict_directory_permissions(&state_dir).is_ok()
|
||||
&& restrict_log_permissions(&state_dir).is_ok()
|
||||
{
|
||||
prune_old_logs(&state_dir, RETAINED_LOG_FILES);
|
||||
let appender = tracing_appender::rolling::daily(&state_dir, "clipbhoard.log");
|
||||
let (writer, guard) = tracing_appender::non_blocking(appender);
|
||||
@@ -23,7 +25,6 @@ pub fn init(configured_level: &str) -> LoggingGuard {
|
||||
.try_init()
|
||||
.is_ok()
|
||||
{
|
||||
restrict_log_permissions(&state_dir);
|
||||
return LoggingGuard::File(guard);
|
||||
}
|
||||
}
|
||||
@@ -32,17 +33,22 @@ pub fn init(configured_level: &str) -> LoggingGuard {
|
||||
LoggingGuard::Stderr
|
||||
}
|
||||
|
||||
fn restrict_log_permissions(directory: &std::path::Path) {
|
||||
if let Ok(entries) = std::fs::read_dir(directory) {
|
||||
for entry in entries.flatten().filter(|entry| {
|
||||
entry
|
||||
.file_name()
|
||||
.to_string_lossy()
|
||||
.starts_with("clipbhoard.log")
|
||||
}) {
|
||||
let _ = std::fs::set_permissions(entry.path(), std::fs::Permissions::from_mode(0o600));
|
||||
fn restrict_directory_permissions(directory: &std::path::Path) -> std::io::Result<()> {
|
||||
std::fs::set_permissions(directory, std::fs::Permissions::from_mode(0o700))
|
||||
}
|
||||
|
||||
fn restrict_log_permissions(directory: &std::path::Path) -> std::io::Result<()> {
|
||||
for entry in std::fs::read_dir(directory)? {
|
||||
let entry = entry?;
|
||||
if entry
|
||||
.file_name()
|
||||
.to_string_lossy()
|
||||
.starts_with("clipbhoard.log")
|
||||
{
|
||||
std::fs::set_permissions(entry.path(), std::fs::Permissions::from_mode(0o600))?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn prune_old_logs(directory: &std::path::Path, keep: usize) {
|
||||
@@ -107,7 +113,7 @@ mod tests {
|
||||
std::fs::write(&active, "diagnostic").unwrap();
|
||||
std::fs::set_permissions(&active, std::fs::Permissions::from_mode(0o644)).unwrap();
|
||||
|
||||
restrict_log_permissions(&directory);
|
||||
restrict_log_permissions(&directory).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
std::fs::metadata(active).unwrap().permissions().mode() & 0o777,
|
||||
|
||||
Reference in New Issue
Block a user