1use std::collections::{BTreeSet, HashMap};
12use std::hash::{BuildHasher, Hasher};
13use std::sync::{Arc, Mutex};
14use std::time::{Duration, Instant, SystemTime};
15
16#[derive(Debug, Clone)]
18pub struct NonceScope {
19 command: String,
21 paths: BTreeSet<String>,
23}
24
25impl NonceScope {
26 pub fn command(&self) -> &str {
28 &self.command
29 }
30
31 pub fn paths(&self) -> &BTreeSet<String> {
33 &self.paths
34 }
35}
36
37#[derive(Clone, Debug)]
44pub struct NonceStore {
45 inner: Arc<Mutex<NonceStoreInner>>,
46 ttl: Duration,
47}
48
49#[derive(Debug)]
50struct NonceStoreInner {
51 nonces: HashMap<String, (Instant, NonceScope)>,
53}
54
55impl NonceStore {
56 pub fn new() -> Self {
58 Self::with_ttl(Duration::from_secs(60))
59 }
60
61 pub fn with_ttl(ttl: Duration) -> Self {
63 Self {
64 inner: Arc::new(Mutex::new(NonceStoreInner {
65 nonces: HashMap::new(),
66 })),
67 ttl,
68 }
69 }
70
71 pub fn lookup(&self, nonce: &str) -> Result<NonceScope, String> {
76 let now = Instant::now();
77 let ttl = self.ttl;
78
79 #[allow(clippy::expect_used)]
80 let inner = self.inner.lock().expect("nonce store poisoned");
81
82 match inner.nonces.get(nonce) {
83 Some((created, scope)) => {
84 if now.duration_since(*created) >= ttl {
85 Err("nonce expired".to_string())
86 } else {
87 Ok(scope.clone())
88 }
89 }
90 None => Err("invalid nonce".to_string()),
91 }
92 }
93
94 pub fn issue(&self, command: &str, paths: &[&str]) -> String {
98 let nonce = generate_nonce();
99 let now = Instant::now();
100 let ttl = self.ttl;
101
102 let scope = NonceScope {
103 command: command.to_string(),
104 paths: paths.iter().map(|p| p.to_string()).collect(),
105 };
106
107 #[allow(clippy::expect_used)]
108 let mut inner = self.inner.lock().expect("nonce store poisoned");
109
110 inner.nonces.retain(|_, (created, _)| now.duration_since(*created) < ttl);
112
113 inner.nonces.insert(nonce.clone(), (now, scope));
114 nonce
115 }
116
117 pub fn validate(&self, nonce: &str, command: &str, paths: &[&str]) -> Result<(), String> {
124 let now = Instant::now();
125 let ttl = self.ttl;
126
127 #[allow(clippy::expect_used)]
128 let inner = self.inner.lock().expect("nonce store poisoned");
129
130 match inner.nonces.get(nonce) {
131 Some((created, scope)) => {
132 if now.duration_since(*created) >= ttl {
133 return Err("nonce expired".to_string());
134 }
135
136 if scope.command != command {
137 return Err(format!(
138 "nonce scope mismatch: issued for command '{}', got '{}'",
139 scope.command, command
140 ));
141 }
142
143 if let Some(unauthorized) = paths.iter().find(|p| !scope.paths.contains(**p)) {
146 return Err(format!(
147 "nonce scope mismatch: unauthorized path '{}' (authorized: {:?})",
148 unauthorized,
149 scope.paths.iter().collect::<Vec<_>>()
150 ));
151 }
152
153 Ok(())
154 }
155 None => Err("invalid nonce".to_string()),
156 }
157 }
158
159 pub fn ttl(&self) -> Duration {
161 self.ttl
162 }
163}
164
165impl Default for NonceStore {
166 fn default() -> Self {
167 Self::new()
168 }
169}
170
171fn generate_nonce() -> String {
173 let hasher_state = std::collections::hash_map::RandomState::new();
174 let mut hasher = hasher_state.build_hasher();
175
176 let now = SystemTime::now()
178 .duration_since(SystemTime::UNIX_EPOCH)
179 .unwrap_or_default();
180 hasher.write_u128(now.as_nanos());
181
182 let hasher_state2 = std::collections::hash_map::RandomState::new();
184 let mut hasher2 = hasher_state2.build_hasher();
185 hasher2.write_u64(0xdeadbeef);
186 hasher.write_u64(hasher2.finish());
187
188 format!("{:08x}", hasher.finish() as u32)
189}
190
191#[cfg(test)]
192mod tests {
193 use super::*;
194
195 #[test]
196 fn issue_and_validate() {
197 let store = NonceStore::new();
198 let nonce = store.issue("rm", &["/tmp/important"]);
199 assert_eq!(nonce.len(), 8);
200 assert!(nonce.chars().all(|c| c.is_ascii_hexdigit()));
201
202 let result = store.validate(&nonce, "rm", &["/tmp/important"]);
203 assert!(result.is_ok());
204 }
205
206 #[test]
207 fn idempotent_reuse() {
208 let store = NonceStore::new();
209 let nonce = store.issue("rm", &["bigdir/"]);
210
211 let first = store.validate(&nonce, "rm", &["bigdir/"]);
212 let second = store.validate(&nonce, "rm", &["bigdir/"]);
213 assert!(first.is_ok());
214 assert!(second.is_ok());
215 }
216
217 #[test]
218 fn expired_nonce_fails() {
219 let store = NonceStore::with_ttl(Duration::from_millis(0));
220 let nonce = store.issue("rm", &["ephemeral"]);
221
222 std::thread::sleep(Duration::from_millis(1));
224 let result = store.validate(&nonce, "rm", &["ephemeral"]);
225 assert_eq!(result, Err("nonce expired".to_string()));
226 }
227
228 #[test]
229 fn invalid_nonce_fails() {
230 let store = NonceStore::new();
231 let result = store.validate("bogus123", "rm", &["anything"]);
232 assert_eq!(result, Err("invalid nonce".to_string()));
233 }
234
235 #[test]
236 fn nonces_are_unique() {
237 let store = NonceStore::new();
238 let a = store.issue("rm", &["first"]);
239 let b = store.issue("rm", &["second"]);
240 assert_ne!(a, b);
241 }
242
243 #[test]
244 fn clone_shares_state() {
245 let store = NonceStore::new();
246 let cloned = store.clone();
247 let nonce = store.issue("rm", &["/shared"]);
248
249 let result = cloned.validate(&nonce, "rm", &["/shared"]);
250 assert!(result.is_ok());
251 }
252
253 #[test]
254 fn gc_cleans_expired() {
255 let store = NonceStore::with_ttl(Duration::from_millis(10));
256 let old_nonce = store.issue("rm", &["old"]);
257
258 std::thread::sleep(Duration::from_millis(20));
259
260 let _new = store.issue("rm", &["new"]);
262
263 let result = store.validate(&old_nonce, "rm", &["old"]);
265 assert!(result.is_err());
266 }
267
268 #[test]
271 fn path_mismatch_rejected() {
272 let store = NonceStore::new();
273 let nonce = store.issue("rm", &["fileA.txt"]);
274
275 let result = store.validate(&nonce, "rm", &["fileB.txt"]);
276 assert!(result.is_err());
277 assert!(result.unwrap_err().contains("nonce scope mismatch"));
278 }
279
280 #[test]
281 fn subset_accepted() {
282 let store = NonceStore::new();
283 let nonce = store.issue("rm", &["a.txt", "b.txt", "c.txt"]);
284
285 let result = store.validate(&nonce, "rm", &["a.txt", "b.txt"]);
287 assert!(result.is_ok());
288 }
289
290 #[test]
291 fn superset_rejected() {
292 let store = NonceStore::new();
293 let nonce = store.issue("rm", &["a.txt", "b.txt"]);
294
295 let result = store.validate(&nonce, "rm", &["a.txt", "b.txt", "c.txt"]);
297 assert!(result.is_err());
298 assert!(result.unwrap_err().contains("unauthorized"));
299 }
300
301 #[test]
302 fn command_mismatch_rejected() {
303 let store = NonceStore::new();
304 let nonce = store.issue("rm", &["file.txt"]);
305
306 let result = store.validate(&nonce, "kaish-trash empty", &[]);
307 assert!(result.is_err());
308 assert!(result.unwrap_err().contains("command"));
309 }
310
311 #[test]
312 fn empty_paths_command_only() {
313 let store = NonceStore::new();
314 let nonce = store.issue("kaish-trash empty", &[]);
315
316 let result = store.validate(&nonce, "kaish-trash empty", &[]);
317 assert!(result.is_ok());
318 }
319
320 #[test]
321 fn empty_paths_rejects_nonempty() {
322 let store = NonceStore::new();
323 let nonce = store.issue("kaish-trash empty", &[]);
324
325 let result = store.validate(&nonce, "kaish-trash empty", &["sneaky.txt"]);
327 assert!(result.is_err());
328 }
329
330}