Repository navigation
fix(repo): warn once when .sc/local-key permissions cannot be restricted (#214) #225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1c59449
f3a1e43
5f79ed3
c3c0dbc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,9 @@ | |
| //! nothing — it does not reintroduce the equality oracle this phase closes. | ||
| //! A lost/stale/corrupt cache degrades to spurious re-seals, never | ||
| //! incorrectness and never a hard error. | ||
| //! Where the key file cannot be made owner-only (non-unix; #214) it is | ||
| //! still written with default permissions and a one-time warning is printed | ||
| //! once the cache holds a protected entry: see [`can_restrict_permissions`]. | ||
| //! | ||
| //! **The keyed tag is the sole authority for a hit.** `mtime_ns`/`size` are | ||
| //! recorded for documentation (when the entry was observed) and as a | ||
|
|
@@ -31,6 +34,53 @@ use scl_core::ObjectId; | |
| use crate::error::{Error, Result}; | ||
| use crate::layout::Layout; | ||
|
|
||
| /// Whether `.sc/local-key` can be created readable by its owner only. Only | ||
| /// unix can (`mode(0o600)` from the first byte); there is no Windows ACL path | ||
| /// yet (#214), so elsewhere the key keeps default permissions and a warning is | ||
| /// printed. Tests force `false` via `force_unrestricted_for_test`. | ||
| fn can_restrict_permissions() -> bool { | ||
| #[cfg(test)] | ||
| if FORCE_UNRESTRICTED.with(|f| f.get()) { | ||
| return false; | ||
| } | ||
| cfg!(unix) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: grep -n 'cfg\|fn local_key_is_minted_once_and_stable\|fn save_load_roundtrip\|force_cacheless\|fn can_restrict_permissions\|#\[test\]' crates/repo/src/cache.rs | head -80
sed -n '30,100p' crates/repo/src/cache.rs
ls .github/workflows 2>/dev/null && grep -rn 'windows\|macos\|runs-on\|matrix' .github/workflows | head -30Repository: git-agentic/src-control Length of output: 4822 🏁 Script executed: set -e
printf '%s\n' '--- cache tests and cache implementation ---'
sed -n '330,545p' crates/repo/src/cache.rs
printf '%s\n' '--- remaining cache tests ---'
sed -n '545,640p' crates/repo/src/cache.rs
printf '%s\n' '--- repo test references to cache persistence ---'
rg -n -F --glob '*.rs' -e 'save_load_roundtrip_and_corrupt_file_is_empty' -e 'local_key_is_minted_once_and_stable' -e 'ProtectedCache::open' -e 'local_key(' -e 'protected-cache' crates/repo/src/repo.rs crates/repo/src/cache.rs || test "$?" -eq 1
printf '%s\n' '--- CI workflow ---'
sed -n '1,90p' .github/workflows/ci.yml
printf '%s\n' '--- target/platform documentation references ---'
rg -n -i --glob '*.md' --glob '*.yml' --glob '*.yaml' --glob 'Cargo.toml' -e 'windows' -e 'non-unix' -e 'supported target' -e 'target.*platform' -e 'macos' -e 'linux' README.md docs crates 2>/dev/null || test "$?" -eq 1Repository: git-agentic/src-control Length of output: 22121 🏁 Script executed: sed -n '340,545p' crates/repo/src/cache.rs
sed -n '545,640p' crates/repo/src/cache.rs
rg -n -F --glob '*.rs' -e 'ProtectedCache::open' -e 'local_key(' -e 'protected-cache' crates/repo/src/repo.rs crates/repo/src/cache.rs || test "$?" -eq 1
sed -n '1,90p' .github/workflows/ci.yml
rg -n -i --glob '*.md' --glob '*.yml' --glob '*.yaml' --glob 'Cargo.toml' -e 'windows' -e 'non-unix' -e 'supported target' -e 'target.*platform' -e 'macos' -e 'linux' README.md docs crates 2>/dev/null || test "$?" -eq 1Repository: git-agentic/src-control Length of output: 21188 🏁 Script executed: sed -n '170,225p' crates/repo/src/cache.rs
sed -n '4545,4605p' crates/repo/src/repo.rs
sed -n '78,98p' docs/THREAT-MODEL.mdRepository: git-agentic/src-control Length of output: 6319 Update all persistence-dependent tests for cacheless targets. When
Gate the persistent assertions on Unix. Add non-Unix assertions for fresh keys, disabled persistence, and the intended re-seal behavior. The existing This is a minor test-workflow issue, not a major release issue. CI runs only Ubuntu and macOS, and the threat model documents the non-Unix behavior as compiled but unverified on Windows. 🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| #[cfg(test)] | ||
| thread_local! { | ||
| static FORCE_UNRESTRICTED: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; | ||
| } | ||
|
|
||
| /// Test seam: make this thread behave like a non-unix target. | ||
| #[cfg(test)] | ||
| pub(crate) fn force_unrestricted_for_test(on: bool) { | ||
| FORCE_UNRESTRICTED.with(|f| f.set(on)); | ||
| } | ||
|
|
||
| /// Print the unrestricted-key warning the first time `flag` is unset; `true` | ||
| /// iff it printed. The flag is a parameter so a test can use a private one. | ||
| fn warn_unrestricted_once(flag: &std::sync::atomic::AtomicBool) -> bool { | ||
| if flag.swap(true, std::sync::atomic::Ordering::Relaxed) { | ||
| return false; | ||
| } | ||
| eprintln!( | ||
| "warning: this platform cannot restrict .sc/local-key to its owner; other \ | ||
| local users may be able to read it and test guesses against the \ | ||
| protected-file cache tags (the protected plaintext checked out in the \ | ||
| working tree is equally readable by default)" | ||
| ); | ||
| true | ||
| } | ||
|
|
||
| /// Warn (once per process) when a cache that holds protected entries is | ||
| /// backed by a key whose permissions could not be restricted. | ||
| fn warn_if_unrestricted(has_entries: bool) { | ||
| static WARNED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); | ||
| if has_entries && !can_restrict_permissions() { | ||
| warn_unrestricted_once(&WARNED); | ||
| } | ||
| } | ||
|
|
||
| /// Load (minting if absent) the per-repo random cache key. The key file is | ||
| /// the only hard-error surface here: if it can't be created/read, the cache | ||
| /// cannot be safely keyed, so surface it rather than running unkeyed. | ||
|
|
@@ -77,6 +127,7 @@ fn write_key_0600(path: &Path, bytes: &[u8]) -> std::io::Result<()> { | |
| f.write_all(bytes) | ||
| } | ||
|
|
||
| /// Non-unix: default (inherited) permissions; see [`can_restrict_permissions`]. | ||
| #[cfg(not(unix))] | ||
| fn write_key_0600(path: &Path, bytes: &[u8]) -> std::io::Result<()> { | ||
| use std::io::Write; | ||
|
|
@@ -186,6 +237,9 @@ impl ProtectedCache { | |
| } | ||
| } | ||
| } | ||
| if path.is_some() { | ||
| warn_if_unrestricted(!entries.is_empty()); | ||
| } | ||
| ProtectedCache { | ||
| key, | ||
| root, | ||
|
|
@@ -253,6 +307,7 @@ impl ProtectedCache { | |
| let Some(path) = &self.path else { | ||
| return Ok(()); | ||
| }; | ||
| warn_if_unrestricted(!self.entries.is_empty()); | ||
| let mut out = String::new(); | ||
| for (p, e) in &self.entries { | ||
| if !is_plain_file(&self.root, p) { | ||
|
|
@@ -322,6 +377,42 @@ mod tests { | |
| cleanup(&layout); | ||
| } | ||
|
|
||
| #[test] | ||
| fn unrestricted_key_still_persists_and_hits() { | ||
| force_unrestricted_for_test(true); | ||
| let layout = tmp("unrestricted"); | ||
| let key = local_key(&layout).unwrap(); | ||
| assert_eq!(key, local_key(&layout).unwrap(), "key persisted"); | ||
| assert!(layout.local_key_path().exists()); | ||
|
|
||
| let abs = layout.root.join("secret.txt"); | ||
| std::fs::write(&abs, b"v1").unwrap(); | ||
| let id = ObjectId::of(b"cipher-of-v1"); | ||
| let mut c = ProtectedCache::open( | ||
| key, | ||
| layout.root.clone(), | ||
| Some(layout.protected_cache_path()), | ||
| ); | ||
| c.record("secret.txt", b"v1", id); | ||
| c.save().unwrap(); | ||
| assert!(layout.protected_cache_path().exists(), "cache persisted"); | ||
| let c = ProtectedCache::open( | ||
| key, | ||
| layout.root.clone(), | ||
| Some(layout.protected_cache_path()), | ||
| ); | ||
| assert_eq!(c.unchanged("secret.txt", b"v1"), Some(id), "hit survives"); | ||
| force_unrestricted_for_test(false); | ||
| cleanup(&layout); | ||
| } | ||
|
|
||
| #[test] | ||
| fn unrestricted_warning_prints_once() { | ||
| let flag = std::sync::atomic::AtomicBool::new(false); | ||
| assert!(warn_unrestricted_once(&flag)); | ||
| assert!(!warn_unrestricted_once(&flag)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn tag_is_authoritative_for_hits_and_misses() { | ||
| let layout = tmp("hits"); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On Unix hosts backed by filesystems that do not enforce POSIX mode bits—such as vfat/exFAT or some CIFS mounts—
cfg!(unix)enables the persistent cache even thoughmode(0o600)may produce a group/world-readable file. Because neither newly created nor existing.sc/local-keyfiles are checked after opening, a local reader can obtain the key and test plaintext guesses against the cache, preserving the vulnerability this change is intended to close. Inspect the effective permissions and fall back to cacheless operation when group/other access remains enabled.Useful? React with 👍 / 👎.