Coverage / crates/oxy-core/src/repo/hosted.rs 98.9%
619 of 626 lines run, 28 of 40 functions · run #283 on main · run missed
| 1 | //! What a host (a hub) does to a repository without a workspace (hub spec | |
| 2 | //! §6.9, §7.6): build a change from an edit set on any state, sign a | |
| 3 | //! reference update with a key it holds, and apply both through the same | |
| 4 | //! validation a received push gets. Nothing here bypasses policy. | |
| 5 | | |
| 6 | use super::*; | |
| 7 | use crate::identity::SecretKey; | |
| 8 | ||
| 9 | /// One file of an edit set: new content, or `None` to remove the path. | |
| 10 | #[derive(Clone, Debug, PartialEq, Eq)] | |
| 11 | pub struct FileEdit { | |
| 12 | pub path: String, | |
| 13 | pub content: Option<Vec<u8>>, | |
| 14 | } | |
| 15 | ||
| 16 | impl Repo { | |
| 17 | /// Build (but do not store or sign) a change that turns the state with | |
| 18 | /// `heads` into that state with `edits` applied, authored by `author`. | |
| 19 | /// Files the edits do not name keep their content, conflict markers | |
| 20 | /// included, so an edit that removes a file's markers resolves that | |
| 21 | /// file's conflict (a resolution change, spec §9.6). | |
| 22 | 16× | pub fn build_change( |
| 23 | 16× | &self, |
| 24 | 16× | heads: &[ChangeHash], |
| 25 | 16× | edits: &[FileEdit], |
| 26 | 16× | message: &str, |
| 27 | 16× | author: KeyId, |
| 28 | 16× | ) -> Result<Change> { |
| 29 | 16× | if message.trim().is_empty() { |
| 30 | 0× | return fail("a change message is required"); |
| 31 | 16× | } |
| 32 | 16× | if edits.is_empty() { |
| 33 | 0× | return fail("nothing to change"); |
| 34 | 16× | } |
| 35 | 16× | let base = self.load_state(heads)?; |
| 36 | 16× | let base_snapshot = base.materialize(); |
| 37 | 16× | let mut entries: BTreeMap<String, FsEntry> = BTreeMap::new(); |
| 38 | 24× | for (path, f) in &base_snapshot.files { |
| 39 | 24× | let entry = match (&f.kind, &f.content) { |
| 40 | 4× | (NodeKind::Dir, _) | (_, FileContent::Dir) => continue, |
| 41 | 18× | (kind, FileContent::Text { bytes, .. }) => { |
| 42 | 18× | FsEntry::new(*kind, f.mode.exec, bytes.clone()) |
| 43 | } | |
| 44 | 2× | (kind, FileContent::Opaque { blobs }) => FsEntry { |
| 45 | 2× | blob: blobs.first().copied(), |
| 46 | 2× | ..FsEntry::new(*kind, f.mode.exec, vec![]) |
| 47 | 2× | }, |
| 48 | }; | |
| 49 | 20× | entries.insert(path.clone(), entry); |
| 50 | } | |
| 51 | 17× | for e in edits { |
| 52 | 17× | let path = e.path.trim_matches('/'); |
| 53 | 17× | if path.is_empty() |
| 54 | 17× | || path |
| 55 | 17× | .split('/') |
| 56 | 19× | .any(|c| c.is_empty() || c == "." || c == ".." || c == OXY_DIR) |
| 57 | { | |
| 58 | 1× | return fail(format!("{:?} is not a valid path", e.path)); |
| 59 | 16× | } |
| 60 | 16× | match &e.content { |
| 61 | 15× | Some(bytes) => { |
| 62 | 15× | let exec = entries.get(path).map(|x| x.exec).unwrap_or(false); |
| 63 | // A file cannot sit where a directory is, or inside a file. | |
| 64 | 15× | if base_snapshot |
| 65 | 15× | .files |
| 66 | 15× | .get(path) |
| 67 | 15× | .is_some_and(|f| f.kind == NodeKind::Dir) |
| 68 | { | |
| 69 | 0× | return fail(format!("{path} is a directory")); |
| 70 | 15× | } |
| 71 | 15× | let mut parent = path; |
| 72 | 16× | while let Some((p, _)) = parent.rsplit_once('/') { |
| 73 | 2× | if base_snapshot |
| 74 | 2× | .files |
| 75 | 2× | .get(p) |
| 76 | 2× | .is_some_and(|f| f.kind != NodeKind::Dir) |
| 77 | { | |
| 78 | 1× | return fail(format!("{p} is a file, not a directory")); |
| 79 | 1× | } |
| 80 | 1× | parent = p; |
| 81 | } | |
| 82 | 14× | entries.insert( |
| 83 | 14× | path.to_string(), |
| 84 | 14× | FsEntry::new(NodeKind::File, exec, bytes.clone()), |
| 85 | ); | |
| 86 | } | |
| 87 | None => { | |
| 88 | 1× | if entries.remove(path).is_none() { |
| 89 | // Removing a directory removes everything below it. | |
| 90 | 1× | let prefix = format!("{path}/"); |
| 91 | 1× | let before = entries.len(); |
| 92 | 2× | entries.retain(|p, _| !p.starts_with(&prefix)); |
| 93 | 1× | if entries.len() == before { |
| 94 | 0× | return fail(format!("{path} does not exist")); |
| 95 | 1× | } |
| 96 | 0× | } |
| 97 | } | |
| 98 | } | |
| 99 | } | |
| 100 | // A path the user writes settles its replace conflict, if any: they | |
| 101 | // chose that content (spec §9.3). | |
| 102 | 14× | let written: std::collections::HashSet<String> = edits |
| 103 | 14× | .iter() |
| 104 | 15× | .filter(|e| e.content.is_some()) |
| 105 | 14× | .map(|e| e.path.trim_matches('/').to_string()) |
| 106 | 14× | .collect(); |
| 107 | 14× | let built = self.build_from_entries_resolving( |
| 108 | 14× | &base, |
| 109 | 14× | &base_snapshot, |
| 110 | 14× | HashMap::new(), |
| 111 | 14× | &entries, |
| 112 | 14× | &written, |
| 113 | 0× | )?; |
| 114 | 14× | if let Some(s) = built |
| 115 | 14× | .statuses |
| 116 | 14× | .iter() |
| 117 | 15× | .find(|s| s.status == ChangeStatus::ConflictEdited) |
| 118 | { | |
| 119 | 1× | return fail(format!("{} still contains conflict markers", s.path)); |
| 120 | 13× | } |
| 121 | 13× | if built.tree_ops.is_empty() && built.edits.is_empty() { |
| 122 | 1× | return fail("the edits change nothing"); |
| 123 | 12× | } |
| 124 | 12× | let mut change = Change { |
| 125 | version: crate::objects::CHANGE_VERSION, | |
| 126 | 12× | change_id: ChangeId::random(), |
| 127 | 12× | deps: vec![], |
| 128 | 12× | author, |
| 129 | 12× | hlc: Hlc::now(), |
| 130 | 12× | message: message.trim_end().to_string(), |
| 131 | 12× | tree_ops: built.tree_ops, |
| 132 | 12× | edits: built.edits, |
| 133 | 12× | kind: if built.resolves { |
| 134 | 3× | ChangeKind::Resolution |
| 135 | } else { | |
| 136 | 9× | ChangeKind::Normal |
| 137 | }, | |
| 138 | }; | |
| 139 | 12× | let mut deps: BTreeSet<ChangeHash> = referenced_changes(&change, &base); |
| 140 | 12× | deps.extend(built.contributors.iter().copied()); |
| 141 | 12× | change.deps = deps.into_iter().collect(); |
| 142 | 12× | change.normalize(); |
| 143 | 12× | Ok(change) |
| 144 | 16× | } |
| 145 | | |
| 146 | /// A reference update of `bookmark` to `heads`, attested by `signer` | |
| 147 | /// when given (the attestation a push would carry, spec §14.4). | |
| 148 | 10× | pub fn host_ref_update( |
| 149 | 10× | &self, |
| 150 | 10× | bookmark: &str, |
| 151 | 10× | heads: &[ChangeHash], |
| 152 | 10× | signer: Option<(&KeyId, &SecretKey)>, |
| 153 | 10× | ) -> Result<RefUpdate> { |
| 154 | 10× | let view = self.view()?; |
| 155 | 10× | let empty = self.store.put_state(&StateObject::new(vec![]))?; |
| 156 | 10× | let old_state = view.bookmarks.get(bookmark).copied().unwrap_or(empty); |
| 157 | 10× | let new_state = self.store.put_state(&StateObject::new(heads.to_vec()))?; |
| 158 | 10× | let attestation = match signer { |
| 159 | 9× | Some((key, sk)) => { |
| 160 | 9× | let serial = self |
| 161 | 9× | .latest_ref_attest(bookmark)? |
| 162 | 9× | .and_then(|(_, a)| match a.attest.body { |
| 163 | 9× | AttestBody::Ref { serial, .. } => Some(serial), |
| 164 | 0× | _ => None, |
| 165 | 9× | }) |
| 166 | 9× | .map(|s| s + 1) |
| 167 | 9× | .unwrap_or(1); |
| 168 | 9× | let attest = Attest { |
| 169 | 9× | repo: self.id()?, |
| 170 | 9× | hlc: Hlc::now(), |
| 171 | 9× | body: AttestBody::Ref { |
| 172 | 9× | name: bookmark.to_string(), |
| 173 | 9× | old_state, |
| 174 | 9× | new_state, |
| 175 | 9× | serial, |
| 176 | 9× | }, |
| 177 | }; | |
| 178 | 9× | let r = attest.hash(); |
| 179 | 9× | let sig = sk.sign(Domain::Attest, key, &r); |
| 180 | 9× | Some(SignedAttest { |
| 181 | 9× | attest, |
| 182 | 9× | sigs: vec![sig], |
| 183 | 9× | }) |
| 184 | } | |
| 185 | 1× | None => None, |
| 186 | }; | |
| 187 | 10× | Ok(RefUpdate { |
| 188 | 10× | name: bookmark.to_string(), |
| 189 | 10× | old_state, |
| 190 | 10× | new_state, |
| 191 | 10× | force: false, |
| 192 | 10× | attestation, |
| 193 | 10× | }) |
| 194 | 10× | } |
| 195 | | |
| 196 | /// Apply a host-made update as if it had been pushed: the new change | |
| 197 | /// (with its signatures) if any, the identities it needs, and the | |
| 198 | /// reference update, all through `receive_push_report`, so the same | |
| 199 | /// authentication, compare-and-swap and policy checks apply. | |
| 200 | 10× | pub fn apply_hosted( |
| 201 | 10× | &self, |
| 202 | 10× | change: Option<(Change, Vec<Signature>)>, |
| 203 | 10× | identities: &[Identity], |
| 204 | 10× | update: RefUpdate, |
| 205 | 10× | ) -> Result<PushReport> { |
| 206 | 10× | let mut bundle = Bundle::default(); |
| 207 | 17× | for id in identities { |
| 208 | 17× | bundle.objects.push((ObjectType::Identity, id.encode())); |
| 209 | 17× | } |
| 210 | 10× | if let Ok(st) = self.store.get_state(&update.new_state) { |
| 211 | 10× | bundle.objects.push((ObjectType::State, st.encode())); |
| 212 | 10× | } |
| 213 | 10× | if let Some((c, sigs)) = change { |
| 214 | 9× | bundle.changes.push((c, sigs)); |
| 215 | 9× | } |
| 216 | 10× | self.receive_push_report(bundle, vec![update], None, vec![]) |
| 217 | 10× | } |
| 218 | } | |
| 219 | ||
| 220 | #[cfg(test)] | |
| 221 | mod tests { | |
| 222 | use super::*; | |
| 223 | use crate::identity::SecretKey; | |
| 224 | | |
| 225 | 7× | fn hermetic() { |
| 226 | static ONCE: std::sync::Once = std::sync::Once::new(); | |
| 227 | static DIR: std::sync::OnceLock<tempfile::TempDir> = std::sync::OnceLock::new(); | |
| 228 | 7× | ONCE.call_once(|| { |
| 229 | 1× | let d = tempfile::tempdir().unwrap(); |
| 230 | 1× | std::env::set_var("OXY_HOME", d.path()); |
| 231 | 1× | DIR.set(d).ok(); |
| 232 | 1× | }); |
| 233 | 7× | } |
| 234 | | |
| 235 | 9× | fn write(root: &Path, rel: &str, content: &[u8]) { |
| 236 | 9× | let p = root.join(rel); |
| 237 | 9× | fs::create_dir_all(p.parent().unwrap()).unwrap(); |
| 238 | 9× | fs::write(p, content).unwrap(); |
| 239 | 9× | } |
| 240 | | |
| 241 | /// A user repository pushed to a bare host, and a hub key the user | |
| 242 | /// has delegated to. Returns (host, user, hub key, hub secret). | |
| 243 | 7× | fn setup( |
| 244 | 7× | root: &Path, |
| 245 | 7× | with_policy: bool, |
| 246 | 7× | delegate: bool, |
| 247 | 7× | ) -> (Repo, Repo, KeyId, SecretKey, Identity) { |
| 248 | 7× | hermetic(); |
| 249 | 7× | let host_dir = root.join("host"); |
| 250 | 7× | Repo::init_bare_unbound(&host_dir).unwrap(); |
| 251 | 7× | let work = root.join("work"); |
| 252 | 7× | let mut user = Repo::init(&work).unwrap(); |
| 253 | 7× | user.create_identity("Andy", "[email protected]", IdentityScope::Repo) |
| 254 | 7× | .unwrap(); |
| 255 | 7× | user.config.set("core.sign", "change").unwrap(); |
| 256 | 7× | user.config |
| 257 | 7× | .set("remote.origin.url", host_dir.to_str().unwrap()) |
| 258 | 7× | .unwrap(); |
| 259 | 7× | user.save_config().unwrap(); |
| 260 | 7× | let hub_sk = SecretKey::generate(); |
| 261 | 7× | let hub_id = hub_sk.identity("Hub", "hub@example", 1); |
| 262 | 7× | let hub_key = hub_id.hash(); |
| 263 | 7× | if with_policy { |
| 264 | 3× | user.policy_init().unwrap(); |
| 265 | 4× | } |
| 266 | 7× | if delegate { |
| 267 | 3× | user.delegate_add(hub_id.clone(), Scope::CHANGES | Scope::REFS, None) |
| 268 | 3× | .unwrap(); |
| 269 | 4× | } |
| 270 | 7× | write(&work, "a.txt", b"one\ntwo\n"); |
| 271 | 7× | user.snapshot("t").unwrap(); |
| 272 | 7× | user.commit("first", &[]).unwrap(); |
| 273 | 7× | let r = user.push("origin", false).unwrap(); |
| 274 | 7× | assert!(r.iter().all(|x| x.ok), "{r:?}"); |
| 275 | 7× | ( |
| 276 | 7× | Repo::open(&host_dir).unwrap(), |
| 277 | 7× | user, |
| 278 | 7× | hub_key, |
| 279 | 7× | hub_sk, |
| 280 | 7× | hub_id, |
| 281 | 7× | ) |
| 282 | 7× | } |
| 283 | | |
| 284 | #[test] | |
| 285 | 1× | fn a_host_edit_signed_by_a_delegate_lands() { |
| 286 | 1× | let dir = tempfile::tempdir().unwrap(); |
| 287 | 1× | let (host, user, hub_key, hub_sk, hub_id) = setup(dir.path(), false, true); |
| 288 | 1× | let author = user.config.author_key.unwrap(); |
| 289 | 1× | let heads = host.bookmark_heads().unwrap(); |
| 290 | 1× | let change = host |
| 291 | 1× | .build_change( |
| 292 | 1× | &heads, |
| 293 | 1× | &[ |
| 294 | 1× | FileEdit { |
| 295 | 1× | path: "a.txt".into(), |
| 296 | 1× | content: Some(b"one\ntwo\nthree\n".to_vec()), |
| 297 | 1× | }, |
| 298 | 1× | FileEdit { |
| 299 | 1× | path: "docs/new.md".into(), |
| 300 | 1× | content: Some(b"# New\n".to_vec()), |
| 301 | 1× | }, |
| 302 | 1× | ], |
| 303 | 1× | "Edit on the web", |
| 304 | 1× | author, |
| 305 | ) | |
| 306 | 1× | .unwrap(); |
| 307 | 1× | assert_eq!(change.author, author); |
| 308 | 1× | assert_eq!(change.kind, ChangeKind::Normal); |
| 309 | 1× | let hash = change.hash(); |
| 310 | 1× | let sig = hub_sk.sign(Domain::Change, &hub_key, &hash); |
| 311 | 1× | let mut new_heads = heads.clone(); |
| 312 | 1× | new_heads.push(hash); |
| 313 | 1× | let update = host |
| 314 | 1× | .host_ref_update("main", &[hash], Some((&hub_key, &hub_sk))) |
| 315 | 1× | .unwrap(); |
| 316 | 1× | let ids = vec![hub_id, user.identity(&author).unwrap()]; |
| 317 | 1× | let report = host |
| 318 | 1× | .apply_hosted(Some((change, vec![sig])), &ids, update) |
| 319 | 1× | .unwrap(); |
| 320 | 1× | assert!(report.results.iter().all(|r| r.ok), "{:?}", report.results); |
| 321 | 1× | assert_eq!(host.bookmark_heads().unwrap(), vec![hash]); |
| 322 | 1× | assert_eq!( |
| 323 | 1× | host.file_at("a.txt", Some(&[hash])).unwrap(), |
| 324 | b"one\ntwo\nthree\n" | |
| 325 | ); | |
| 326 | 1× | assert_eq!( |
| 327 | 1× | host.file_at("docs/new.md", Some(&[hash])).unwrap(), |
| 328 | b"# New\n" | |
| 329 | ); | |
| 330 | // Authored by the user, authenticated by the hub key as delegate. | |
| 331 | 1× | assert!( |
| 332 | 1× | matches!(host.authenticate_change(&hash).unwrap(), SignatureStatus::Valid { key, name, .. } if key == hub_key && name == "Hub") |
| 333 | ); | |
| 334 | 1× | let _ = new_heads; |
| 335 | // Bad paths and empty edits are refused. | |
| 336 | 1× | assert!(host |
| 337 | 1× | .build_change( |
| 338 | 1× | &[hash], |
| 339 | 1× | &[FileEdit { |
| 340 | 1× | path: "../x".into(), |
| 341 | 1× | content: Some(vec![]) |
| 342 | 1× | }], |
| 343 | 1× | "m", |
| 344 | 1× | author |
| 345 | 1× | ) |
| 346 | 1× | .is_err()); |
| 347 | 1× | assert!( |
| 348 | 1× | host.build_change( |
| 349 | 1× | &[hash], |
| 350 | 1× | &[FileEdit { |
| 351 | 1× | path: "a.txt".into(), |
| 352 | 1× | content: Some(b"one\ntwo\nthree\n".to_vec()) |
| 353 | 1× | }], |
| 354 | 1× | "m", |
| 355 | 1× | author |
| 356 | 1× | ) |
| 357 | 1× | .is_err(), |
| 358 | "no-op edits are refused" | |
| 359 | ); | |
| 360 | 1× | assert!( |
| 361 | 1× | host.build_change( |
| 362 | 1× | &[hash], |
| 363 | 1× | &[FileEdit { |
| 364 | 1× | path: "a.txt/b".into(), |
| 365 | 1× | content: Some(vec![]) |
| 366 | 1× | }], |
| 367 | 1× | "m", |
| 368 | 1× | author |
| 369 | 1× | ) |
| 370 | 1× | .is_err(), |
| 371 | "a file inside a file" | |
| 372 | ); | |
| 373 | 1× | let rm = host |
| 374 | 1× | .build_change( |
| 375 | 1× | &[hash], |
| 376 | 1× | &[FileEdit { |
| 377 | 1× | path: "docs".into(), |
| 378 | 1× | content: None, |
| 379 | 1× | }], |
| 380 | 1× | "Remove docs", |
| 381 | 1× | author, |
| 382 | ) | |
| 383 | 1× | .unwrap(); |
| 384 | 1× | assert!(!rm.tree_ops.is_empty()); |
| 385 | 1× | } |
| 386 | | |
| 387 | #[test] | |
| 388 | 1× | fn a_host_edit_by_a_key_that_is_not_a_delegate_is_refused() { |
| 389 | 1× | let dir = tempfile::tempdir().unwrap(); |
| 390 | 1× | let (host, user, hub_key, hub_sk, hub_id) = setup(dir.path(), false, false); |
| 391 | 1× | let author = user.config.author_key.unwrap(); |
| 392 | 1× | let heads = host.bookmark_heads().unwrap(); |
| 393 | 1× | let change = host |
| 394 | 1× | .build_change( |
| 395 | 1× | &heads, |
| 396 | 1× | &[FileEdit { |
| 397 | 1× | path: "a.txt".into(), |
| 398 | 1× | content: Some(b"x\n".to_vec()), |
| 399 | 1× | }], |
| 400 | 1× | "Edit", |
| 401 | 1× | author, |
| 402 | ) | |
| 403 | 1× | .unwrap(); |
| 404 | 1× | let hash = change.hash(); |
| 405 | 1× | let sig = hub_sk.sign(Domain::Change, &hub_key, &hash); |
| 406 | 1× | let update = host |
| 407 | 1× | .host_ref_update("main", &[hash], Some((&hub_key, &hub_sk))) |
| 408 | 1× | .unwrap(); |
| 409 | 1× | let report = host |
| 410 | 1× | .apply_hosted( |
| 411 | 1× | Some((change, vec![sig])), |
| 412 | 1× | &[hub_id, user.identity(&author).unwrap()], |
| 413 | 1× | update, |
| 414 | ) | |
| 415 | 1× | .unwrap(); |
| 416 | // Without a policy the host accepts unauthenticated changes, but the | |
| 417 | // signature does not verify as the author's: it stays unauthenticated. | |
| 418 | 1× | assert!(report.results.iter().all(|r| r.ok)); |
| 419 | 1× | assert!( |
| 420 | 1× | matches!(host.authenticate_change(&hash).unwrap(), SignatureStatus::Invalid(why) if why.contains("neither the author")) |
| 421 | ); | |
| 422 | 1× | } |
| 423 | | |
| 424 | #[test] | |
| 425 | 1× | fn under_a_policy_a_delegate_may_update_the_reference_and_a_stranger_may_not() { |
| 426 | 1× | let dir = tempfile::tempdir().unwrap(); |
| 427 | 1× | let (host, user, hub_key, hub_sk, hub_id) = setup(dir.path(), true, true); |
| 428 | 1× | assert!(host.current_policy().unwrap().is_some()); |
| 429 | 1× | let author = user.config.author_key.unwrap(); |
| 430 | 1× | let heads = host.bookmark_heads().unwrap(); |
| 431 | 1× | let change = host |
| 432 | 1× | .build_change( |
| 433 | 1× | &heads, |
| 434 | 1× | &[FileEdit { |
| 435 | 1× | path: "a.txt".into(), |
| 436 | 1× | content: Some(b"one\n".to_vec()), |
| 437 | 1× | }], |
| 438 | 1× | "Trim", |
| 439 | 1× | author, |
| 440 | ) | |
| 441 | 1× | .unwrap(); |
| 442 | 1× | let hash = change.hash(); |
| 443 | 1× | let sig = hub_sk.sign(Domain::Change, &hub_key, &hash); |
| 444 | 1× | let ids = vec![hub_id.clone(), user.identity(&author).unwrap()]; |
| 445 | 1× | let update = host |
| 446 | 1× | .host_ref_update("main", &[hash], Some((&hub_key, &hub_sk))) |
| 447 | 1× | .unwrap(); |
| 448 | 1× | let report = host |
| 449 | 1× | .apply_hosted(Some((change, vec![sig])), &ids, update) |
| 450 | 1× | .unwrap(); |
| 451 | 1× | assert!(report.results.iter().all(|r| r.ok), "{:?}", report.results); |
| 452 | 1× | assert_eq!(host.bookmark_heads().unwrap(), vec![hash]); |
| 453 | | |
| 454 | // A stranger's key cannot move the reference, even with the user as author. | |
| 455 | 1× | let stranger = SecretKey::generate(); |
| 456 | 1× | let sid = stranger.identity("Mallory", "m@example", 1); |
| 457 | 1× | let skey = sid.hash(); |
| 458 | 1× | let change2 = host |
| 459 | 1× | .build_change( |
| 460 | 1× | &[hash], |
| 461 | 1× | &[FileEdit { |
| 462 | 1× | path: "a.txt".into(), |
| 463 | 1× | content: Some(b"evil\n".to_vec()), |
| 464 | 1× | }], |
| 465 | 1× | "Evil", |
| 466 | 1× | author, |
| 467 | ) | |
| 468 | 1× | .unwrap(); |
| 469 | 1× | let h2 = change2.hash(); |
| 470 | 1× | let sig2 = stranger.sign(Domain::Change, &skey, &h2); |
| 471 | 1× | let update = host |
| 472 | 1× | .host_ref_update("main", &[h2], Some((&skey, &stranger))) |
| 473 | 1× | .unwrap(); |
| 474 | 1× | let report = host |
| 475 | 1× | .apply_hosted(Some((change2, vec![sig2])), &[sid], update) |
| 476 | 1× | .unwrap(); |
| 477 | 1× | assert!(report.results.iter().all(|r| !r.ok), "{:?}", report.results); |
| 478 | 1× | assert_eq!( |
| 479 | 1× | host.bookmark_heads().unwrap(), |
| 480 | 1× | vec![hash], |
| 481 | "the reference did not move" | |
| 482 | ); | |
| 483 | // An unsigned update is refused under a policy too. | |
| 484 | 1× | let update = host.host_ref_update("main", &[hash], None).unwrap(); |
| 485 | 1× | let report = host.apply_hosted(None, &[], update).unwrap(); |
| 486 | 1× | assert!(report.results.iter().all(|r| !r.ok)); |
| 487 | 1× | } |
| 488 | | |
| 489 | /// Agent doc §2: an agent's key signs changes for its owner and nothing | |
| 490 | /// more. Its reference attestation is refused even though its owner's | |
| 491 | /// would pass, and a seal it makes authenticates nothing. | |
| 492 | #[test] | |
| 493 | 1× | fn a_changes_only_delegate_signs_changes_but_cannot_seal_or_move_a_reference() { |
| 494 | 1× | let dir = tempfile::tempdir().unwrap(); |
| 495 | 1× | let (host, mut user, _, _, _) = setup(dir.path(), true, false); |
| 496 | 1× | let agent_sk = SecretKey::generate(); |
| 497 | 1× | let agent_id = agent_sk.identity("aider on a laptop", "agent@example", 1); |
| 498 | 1× | let agent = agent_id.hash(); |
| 499 | 1× | user.delegate_add(agent_id.clone(), Scope::CHANGES, None) |
| 500 | 1× | .unwrap(); |
| 501 | // The successor identity reaches the host with the rotated policy. | |
| 502 | 1× | assert!(user.push("origin", false).unwrap().iter().all(|x| x.ok)); |
| 503 | 1× | let author = user.config.author_key.unwrap(); |
| 504 | 1× | let ids = vec![agent_id.clone(), user.identity(&author).unwrap()]; |
| 505 | 1× | let heads = host.bookmark_heads().unwrap(); |
| 506 | 1× | let change = host |
| 507 | 1× | .build_change( |
| 508 | 1× | &heads, |
| 509 | 1× | &[FileEdit { |
| 510 | 1× | path: "a.txt".into(), |
| 511 | 1× | content: Some(b"by the agent\n".to_vec()), |
| 512 | 1× | }], |
| 513 | 1× | "Agent edit", |
| 514 | 1× | author, |
| 515 | ) | |
| 516 | 1× | .unwrap(); |
| 517 | 1× | let hash = change.hash(); |
| 518 | 1× | let sig = agent_sk.sign(Domain::Change, &agent, &hash); |
| 519 | // Attested by the agent: refused, the reference stays. | |
| 520 | 1× | let update = host |
| 521 | 1× | .host_ref_update("main", &[hash], Some((&agent, &agent_sk))) |
| 522 | 1× | .unwrap(); |
| 523 | 1× | let report = host |
| 524 | 1× | .apply_hosted(Some((change.clone(), vec![sig.clone()])), &ids, update) |
| 525 | 1× | .unwrap(); |
| 526 | 1× | assert!(report.results.iter().all(|r| !r.ok), "{:?}", report.results); |
| 527 | 1× | assert_eq!(host.bookmark_heads().unwrap(), heads); |
| 528 | // Attested by the owner: the agent-signed change lands. | |
| 529 | 1× | let (owner, owner_sk) = user.secret_key().unwrap().unwrap(); |
| 530 | 1× | let update = host |
| 531 | 1× | .host_ref_update("main", &[hash], Some((&owner, &owner_sk))) |
| 532 | 1× | .unwrap(); |
| 533 | 1× | let report = host |
| 534 | 1× | .apply_hosted(Some((change, vec![sig])), &ids, update) |
| 535 | 1× | .unwrap(); |
| 536 | 1× | assert!(report.results.iter().all(|r| r.ok), "{:?}", report.results); |
| 537 | 1× | assert_eq!(host.bookmark_heads().unwrap(), vec![hash]); |
| 538 | 1× | assert!(matches!( |
| 539 | 1× | host.authenticate_change(&hash).unwrap(), |
| 540 | 1× | SignatureStatus::Valid { key, sealed: false, .. } if key == agent |
| 541 | )); | |
| 542 | // A seal by the agent is outside its scope. | |
| 543 | 1× | let change2 = host |
| 544 | 1× | .build_change( |
| 545 | 1× | &[hash], |
| 546 | 1× | &[FileEdit { |
| 547 | 1× | path: "a.txt".into(), |
| 548 | 1× | content: Some(b"sealed by the agent\n".to_vec()), |
| 549 | 1× | }], |
| 550 | 1× | "Sealed", |
| 551 | 1× | author, |
| 552 | ) | |
| 553 | 1× | .unwrap(); |
| 554 | 1× | let h2 = host.store().put_change(&change2).unwrap(); |
| 555 | 1× | let seal = host |
| 556 | 1× | .store() |
| 557 | 1× | .put_attest(&Attest::seal(host.id().unwrap(), vec![h2])) |
| 558 | 1× | .unwrap(); |
| 559 | 1× | host.store() |
| 560 | 1× | .put_signature(&seal, &agent_sk.sign(Domain::Attest, &agent, &seal)) |
| 561 | 1× | .unwrap(); |
| 562 | 1× | host.store().index_seal(&h2, &seal).unwrap(); |
| 563 | 1× | assert!(matches!( |
| 564 | 1× | host.authenticate_change(&h2).unwrap(), |
| 565 | 1× | SignatureStatus::Invalid(e) if e.contains("may not seal") |
| 566 | )); | |
| 567 | 1× | } |
| 568 | | |
| 569 | /// Agent doc §2 end to end from the command line's point of view: the | |
| 570 | /// agent's key is a changes-only delegate, and `author.signer` makes | |
| 571 | /// the owner's checkout sign with it while `author.key` stays the | |
| 572 | /// owner, so a commit carries the owner as author and the agent's | |
| 573 | /// signature, and a reference attestation by the agent is refused. | |
| 574 | #[test] | |
| 575 | 1× | fn an_agent_signs_its_owner_s_changes_through_author_signer() { |
| 576 | 1× | let dir = tempfile::tempdir().unwrap(); |
| 577 | 1× | let (host, mut user, _, _, _) = setup(dir.path(), true, false); |
| 578 | 1× | let owner = user.config.author_key.unwrap(); |
| 579 | // The agent makes its own key in its own home; the owner delegates. | |
| 580 | 1× | let agent_home = dir.path().join("agent-home"); |
| 581 | 1× | let mut agent_repo = Repo::init(&dir.path().join("agent-work")) |
| 582 | 1× | .unwrap() |
| 583 | 1× | .with_home(agent_home.clone()); |
| 584 | 1× | let (agent, agent_id) = agent_repo |
| 585 | 1× | .create_identity("aider on a laptop", "agent@example", IdentityScope::User) |
| 586 | 1× | .unwrap(); |
| 587 | 1× | user.delegate_add(agent_id.clone(), Scope::CHANGES, None) |
| 588 | 1× | .unwrap(); |
| 589 | 1× | assert!(user.push("origin", false).unwrap().iter().all(|x| x.ok)); |
| 590 | 1× | let owner = { |
| 591 | 1× | let _ = owner; |
| 592 | 1× | user.config.author_key.unwrap() |
| 593 | }; | |
| 594 | // The owner's checkout, as the agent runs it: the agent's keys, | |
| 595 | // the owner as author, the agent as signer. | |
| 596 | // (The signer is the agent's own setting, in its user config: the | |
| 597 | // checkout's `author.key` is the owner's, in the repository.) | |
| 598 | 1× | let work = dir.path().join("work"); |
| 599 | 1× | let mut as_agent = Repo::open(&work).unwrap().with_home(agent_home); |
| 600 | 1× | as_agent |
| 601 | 1× | .set_user_config("author.signer", &agent.to_hex()) |
| 602 | 1× | .unwrap(); |
| 603 | 1× | as_agent.set_user_config("core.sign", "change").unwrap(); |
| 604 | 1× | assert_eq!(as_agent.config.author_key, Some(owner)); |
| 605 | 1× | assert_eq!(as_agent.signing_key(), Some(agent)); |
| 606 | 1× | write(&work, "a.txt", b"by the agent\n"); |
| 607 | 1× | as_agent.snapshot("t").unwrap(); |
| 608 | 1× | let h = as_agent.commit("Agent: edit a", &[]).unwrap(); |
| 609 | 1× | let meta = as_agent.store().get_change_meta(&h).unwrap(); |
| 610 | 1× | assert_eq!(meta.author, owner, "the owner stays the author"); |
| 611 | 1× | assert!(matches!( |
| 612 | 1× | as_agent.verify_change(&h).unwrap(), |
| 613 | 1× | SignatureStatus::Valid { key, .. } if key == agent |
| 614 | )); | |
| 615 | 1× | assert_eq!(as_agent.revset("delegated()").unwrap().hashes, vec![h]); |
| 616 | // The agent cannot push: its attestation of the reference is | |
| 617 | // refused under the policy. The owner can. | |
| 618 | 1× | let r = as_agent.push("origin", false).unwrap(); |
| 619 | 1× | assert!(r.iter().all(|x| !x.ok), "{r:?}"); |
| 620 | 1× | assert!(!host.bookmark_heads().unwrap().contains(&h)); |
| 621 | // The owner's own handle (its key directory was fixed at `setup`; | |
| 622 | // the environment's `OXY_HOME` may have moved since). | |
| 623 | 1× | let r = user.push("origin", false).unwrap(); |
| 624 | 1× | assert!(r.iter().all(|x| x.ok), "{r:?}"); |
| 625 | 1× | assert!(host.bookmark_heads().unwrap().contains(&h)); |
| 626 | 1× | assert!(matches!( |
| 627 | 1× | host.authenticate_change(&h).unwrap(), |
| 628 | 1× | SignatureStatus::Valid { key, .. } if key == agent |
| 629 | )); | |
| 630 | 1× | } |
| 631 | | |
| 632 | /// A delegation past its expiry authenticates nothing made after it, | |
| 633 | /// and the delegator may revoke the key; a stranger's key, they may not. | |
| 634 | #[test] | |
| 635 | 1× | fn an_expired_delegation_is_refused_and_the_delegator_may_revoke() { |
| 636 | 1× | let dir = tempfile::tempdir().unwrap(); |
| 637 | 1× | let (_, mut user, _, _, _) = setup(dir.path(), false, false); |
| 638 | 1× | let agent_sk = SecretKey::generate(); |
| 639 | 1× | let agent_id = agent_sk.identity("agent", "agent@example", 1); |
| 640 | 1× | let agent = agent_id.hash(); |
| 641 | 1× | let yesterday = Hlc::now().wall_ms - 24 * 3600 * 1000; |
| 642 | 1× | user.delegate_add(agent_id, Scope::CHANGES, Some(yesterday)) |
| 643 | 1× | .unwrap(); |
| 644 | 1× | user.config.set("core.sign", "seal").unwrap(); |
| 645 | 1× | user.save_config().unwrap(); |
| 646 | 1× | write(&dir.path().join("work"), "a.txt", b"later\n"); |
| 647 | 1× | user.snapshot("t").unwrap(); |
| 648 | 1× | let h = user.commit("after the delegation ended", &[]).unwrap(); |
| 649 | 1× | user.store() |
| 650 | 1× | .put_signature(&h, &agent_sk.sign(Domain::Change, &agent, &h)) |
| 651 | 1× | .unwrap(); |
| 652 | 1× | let status = user.verify_change(&h).unwrap(); |
| 653 | 1× | assert!( |
| 654 | 1× | matches!(&status, SignatureStatus::Invalid(e) if e.contains("expired")), |
| 655 | "{status:?}" | |
| 656 | ); | |
| 657 | // No policy here, so the user is no root: only delegation lets them revoke. | |
| 658 | 1× | let stranger = SecretKey::generate().identity("x", "x@example", 1).hash(); |
| 659 | 1× | assert!(user.revoke_identity(&stranger, "").is_err()); |
| 660 | 1× | assert!(user.revoke_identity(&agent, "session over").is_ok()); |
| 661 | 1× | assert!(user.is_revoked(&agent).unwrap()); |
| 662 | 1× | let r = user.delegate_remove(&agent).unwrap(); |
| 663 | 1× | assert!(user.identity(&r.new).unwrap().delegates.is_empty()); |
| 664 | 1× | } |
| 665 | | |
| 666 | #[test] | |
| 667 | 1× | fn a_host_edit_that_removes_markers_resolves_the_conflict() { |
| 668 | 1× | let dir = tempfile::tempdir().unwrap(); |
| 669 | 1× | let (host, mut user, hub_key, hub_sk, hub_id) = setup(dir.path(), false, true); |
| 670 | 1× | let author = user.config.author_key.unwrap(); |
| 671 | 1× | let base = host.bookmark_heads().unwrap(); |
| 672 | // Two independent edits of the same line: a conflict in the union. | |
| 673 | 1× | let a = host |
| 674 | 1× | .build_change( |
| 675 | 1× | &base, |
| 676 | 1× | &[FileEdit { |
| 677 | 1× | path: "a.txt".into(), |
| 678 | 1× | content: Some(b"ONE\ntwo\n".to_vec()), |
| 679 | 1× | }], |
| 680 | 1× | "A", |
| 681 | 1× | author, |
| 682 | ) | |
| 683 | 1× | .unwrap(); |
| 684 | 1× | let b = host |
| 685 | 1× | .build_change( |
| 686 | 1× | &base, |
| 687 | 1× | &[FileEdit { |
| 688 | 1× | path: "a.txt".into(), |
| 689 | 1× | content: Some(b"uno\ntwo\n".to_vec()), |
| 690 | 1× | }], |
| 691 | 1× | "B", |
| 692 | 1× | author, |
| 693 | ) | |
| 694 | 1× | .unwrap(); |
| 695 | 1× | let (ha, hb) = (a.hash(), b.hash()); |
| 696 | 1× | let ids = vec![hub_id.clone(), user.identity(&author).unwrap()]; |
| 697 | 2× | for (c, h) in [(a, ha), (b, hb)] { |
| 698 | 2× | let sig = hub_sk.sign(Domain::Change, &hub_key, &h); |
| 699 | 2× | let mut heads = host.bookmark_heads().unwrap(); |
| 700 | 2× | heads.retain(|x| !base.contains(x)); |
| 701 | 2× | heads.push(h); |
| 702 | 2× | let update = host |
| 703 | 2× | .host_ref_update("main", &heads, Some((&hub_key, &hub_sk))) |
| 704 | 2× | .unwrap(); |
| 705 | 2× | let r = host |
| 706 | 2× | .apply_hosted(Some((c, vec![sig])), &ids, update) |
| 707 | 2× | .unwrap(); |
| 708 | 2× | assert!(r.results.iter().all(|x| x.ok), "{:?}", r.results); |
| 709 | } | |
| 710 | 1× | let heads = host.bookmark_heads().unwrap(); |
| 711 | 1× | assert_eq!(heads.len(), 2); |
| 712 | 1× | let marked = String::from_utf8(host.file_at("a.txt", Some(&heads)).unwrap()).unwrap(); |
| 713 | 1× | assert!(marked.contains("<<<<<<< oxy conflict"), "{marked}"); |
| 714 | // Saving with markers left in is refused; saving without them resolves. | |
| 715 | 1× | assert!(host |
| 716 | 1× | .build_change( |
| 717 | 1× | &heads, |
| 718 | 1× | &[FileEdit { |
| 719 | 1× | path: "a.txt".into(), |
| 720 | 1× | content: Some(format!("{marked}x\n").into_bytes()) |
| 721 | 1× | }], |
| 722 | 1× | "half", |
| 723 | 1× | author |
| 724 | 1× | ) |
| 725 | 1× | .is_err()); |
| 726 | 1× | let res = host |
| 727 | 1× | .build_change( |
| 728 | 1× | &heads, |
| 729 | 1× | &[FileEdit { |
| 730 | 1× | path: "a.txt".into(), |
| 731 | 1× | content: Some(b"One\ntwo\n".to_vec()), |
| 732 | 1× | }], |
| 733 | 1× | "Resolve", |
| 734 | 1× | author, |
| 735 | ) | |
| 736 | 1× | .unwrap(); |
| 737 | 1× | assert_eq!(res.kind, ChangeKind::Resolution); |
| 738 | 1× | assert!( |
| 739 | 1× | res.deps.contains(&ha) && res.deps.contains(&hb), |
| 740 | "a resolution depends on both sides" | |
| 741 | ); | |
| 742 | 1× | let hr = res.hash(); |
| 743 | 1× | let sig = hub_sk.sign(Domain::Change, &hub_key, &hr); |
| 744 | 1× | let update = host |
| 745 | 1× | .host_ref_update("main", &[hr], Some((&hub_key, &hub_sk))) |
| 746 | 1× | .unwrap(); |
| 747 | 1× | let r = host |
| 748 | 1× | .apply_hosted(Some((res, vec![sig])), &ids, update) |
| 749 | 1× | .unwrap(); |
| 750 | 1× | assert!(r.results.iter().all(|x| x.ok), "{:?}", r.results); |
| 751 | 1× | assert_eq!(host.file_at("a.txt", Some(&[hr])).unwrap(), b"One\ntwo\n"); |
| 752 | 1× | assert!(!host |
| 753 | 1× | .load_state(&[hr]) |
| 754 | 1× | .unwrap() |
| 755 | 1× | .materialize() |
| 756 | 1× | .is_conflicted()); |
| 757 | 1× | let _ = &mut user; |
| 758 | 1× | } |
| 759 | } |