Reject symlinked privacy paths

This commit is contained in:
2026-09-12 12:59:58 +02:00
parent 16d9a5d2e5
commit ec232197b4
3 changed files with 45 additions and 2 deletions

View File

@@ -103,6 +103,9 @@ Each bounded iteration must:
- [x] X11-feature tests pass: 24 tests, including rejection of `None` and
`PointerRoot` as paste targets (plus Docker GTK/X11/package build,
2026-09-12).
- [x] Reject symlinked config and database paths before reads, permission
changes, or SQLite opens (27 X11-feature tests and Docker GTK/X11/package
build, 2026-09-12).
- [x] Rust formatting check passes on 2026-09-10.
- [x] X11-only cargo check passes on 2026-09-10.
- [x] Full GTK/X11 build passes in the declared Docker environment (Docker build, 2026-09-10).
@@ -121,5 +124,7 @@ Each bounded iteration must:
real application-target acceptance remains manual evidence.
- Diagnostics previously printed a fixed log filename even though the rolling
logger creates date-suffixed files; this is now reported as the log directory.
- Path hardening rejects final-file symlinks; parent-directory symlink,
ownership, and no-follow atomic-open guarantees remain future hardening.
- The initial implementation is now committed; future work must keep the tree
clean at each checkpoint.

View File

@@ -36,8 +36,21 @@ impl Config {
}
pub fn load_or_default() -> Result<Self> {
let path = Self::path();
if !path.exists() {
return Ok(Self::default());
match fs::symlink_metadata(&path) {
Ok(metadata) if metadata.file_type().is_symlink() => {
anyhow::bail!(
"refusing to read symlinked configuration {}",
path.display()
)
}
Ok(_) => {}
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
return Ok(Self::default())
}
Err(error) => {
return Err(error)
.with_context(|| format!("failed to inspect configuration {}", path.display()))
}
}
if let Some(parent) = path.parent() {
fs::set_permissions(parent, fs::Permissions::from_mode(0o700)).with_context(|| {

View File

@@ -25,6 +25,13 @@ impl Database {
pub fn open(path: impl AsRef<Path>) -> Result<Self> {
let path = path.as_ref().to_path_buf();
if let Ok(metadata) = std::fs::symlink_metadata(&path) {
anyhow::ensure!(
!metadata.file_type().is_symlink(),
"refusing to open symlinked database {}",
path.display()
);
}
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).with_context(|| {
format!("failed to create database directory {}", parent.display())
@@ -234,6 +241,24 @@ mod tests {
let _ = std::fs::remove_file(path);
}
#[test]
fn refuses_symlinked_database_path() {
let directory =
std::env::temp_dir().join(format!("clipbhoard-symlink-{}", std::process::id()));
let _ = std::fs::remove_file(&directory);
let target = directory.with_extension("target");
let _ = std::fs::remove_file(&target);
std::fs::write(&target, "not a database").unwrap();
std::os::unix::fs::symlink(&target, &directory).unwrap();
let error = match Database::open(&directory) {
Ok(_) => panic!("symlinked database was opened"),
Err(error) => error.to_string(),
};
assert!(error.contains("symlinked database"));
let _ = std::fs::remove_file(&directory);
let _ = std::fs::remove_file(target);
}
#[test]
fn trimming_keeps_pinned_entries_and_newest_unpinned_entries() {
let db = Database::open("file:clipbhoard-trim?mode=memory&cache=shared").unwrap();