Coverage / crates/oxy-core/src/repo/hosted.rs 90.4%
797 of 882 lines run, 38 of 63 functions · run #384 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× | self.build_change_with(heads, edits, &[], message, author) |
| 30 | 16× | } |
| 31 | | |
| 32 | /// Build (but do not store or sign) a change that moves the subrepo at | |
| 33 | /// `path` in the state with `heads` to `pin`: a hub moving a consumer's | |
| 34 | /// pin for Diffusion (oxygenhub#148). The path must already be a | |
| 35 | /// subrepo; nothing else changes. | |
| 36 | 3× | pub fn build_pin_change( |
| 37 | 3× | &self, |
| 38 | 3× | heads: &[ChangeHash], |
| 39 | 3× | path: &str, |
| 40 | 3× | pin: &crate::subrepo::Pin, |
| 41 | 3× | message: &str, |
| 42 | 3× | author: KeyId, |
| 43 | 3× | ) -> Result<Change> { |
| 44 | 3× | self.build_change_with( |
| 45 | 3× | heads, |
| 46 | 3× | &[], |
| 47 | 3× | &[(path.to_string(), pin.clone())], |
| 48 | 3× | message, |
| 49 | 3× | author, |
| 50 | ) | |
| 51 | 3× | } |
| 52 | | |
| 53 | 19× | fn build_change_with( |
| 54 | 19× | &self, |
| 55 | 19× | heads: &[ChangeHash], |
| 56 | 19× | edits: &[FileEdit], |
| 57 | 19× | pins: &[(String, crate::subrepo::Pin)], |
| 58 | 19× | message: &str, |
| 59 | 19× | author: KeyId, |
| 60 | 19× | ) -> Result<Change> { |
| 61 | 19× | if message.trim().is_empty() { |
| 62 | 0× | return fail("a change message is required"); |
| 63 | 19× | } |
| 64 | 19× | if edits.is_empty() && pins.is_empty() { |
| 65 | 0× | return fail("nothing to change"); |
| 66 | 19× | } |
| 67 | 19× | let base = self.load_state(heads)?; |
| 68 | 19× | let base_snapshot = base.materialize(); |
| 69 | 19× | let mut entries: BTreeMap<String, FsEntry> = BTreeMap::new(); |
| 70 | 36× | for (path, f) in &base_snapshot.files { |
| 71 | 36× | let entry = match (&f.kind, &f.content) { |
| 72 | 7× | (NodeKind::Dir, _) | (_, FileContent::Dir) => continue, |
| 73 | 24× | (kind, FileContent::Text { bytes, .. }) => { |
| 74 | 24× | FsEntry::new(*kind, f.mode.exec, bytes.clone()) |
| 75 | } | |
| 76 | 5× | (kind, FileContent::Opaque { blobs }) => FsEntry { |
| 77 | 5× | blob: blobs.first().copied(), |
| 78 | 5× | ..FsEntry::new(*kind, f.mode.exec, vec![]) |
| 79 | 5× | }, |
| 80 | }; | |
| 81 | 29× | entries.insert(path.clone(), entry); |
| 82 | } | |
| 83 | 19× | for e in edits { |
| 84 | 17× | let path = e.path.trim_matches('/'); |
| 85 | 17× | if path.is_empty() |
| 86 | 17× | || path |
| 87 | 17× | .split('/') |
| 88 | 19× | .any(|c| c.is_empty() || c == "." || c == ".." || c == OXY_DIR) |
| 89 | { | |
| 90 | 1× | return fail(format!("{:?} is not a valid path", e.path)); |
| 91 | 16× | } |
| 92 | 16× | match &e.content { |
| 93 | 15× | Some(bytes) => { |
| 94 | 15× | let exec = entries.get(path).map(|x| x.exec).unwrap_or(false); |
| 95 | // A file cannot sit where a directory is, or inside a file. | |
| 96 | 15× | if base_snapshot |
| 97 | 15× | .files |
| 98 | 15× | .get(path) |
| 99 | 15× | .is_some_and(|f| f.kind == NodeKind::Dir) |
| 100 | { | |
| 101 | 0× | return fail(format!("{path} is a directory")); |
| 102 | 15× | } |
| 103 | 15× | let mut parent = path; |
| 104 | 16× | while let Some((p, _)) = parent.rsplit_once('/') { |
| 105 | 2× | if base_snapshot |
| 106 | 2× | .files |
| 107 | 2× | .get(p) |
| 108 | 2× | .is_some_and(|f| f.kind != NodeKind::Dir) |
| 109 | { | |
| 110 | 1× | return fail(format!("{p} is a file, not a directory")); |
| 111 | 1× | } |
| 112 | 1× | parent = p; |
| 113 | } | |
| 114 | 14× | entries.insert( |
| 115 | 14× | path.to_string(), |
| 116 | 14× | FsEntry::new(NodeKind::File, exec, bytes.clone()), |
| 117 | ); | |
| 118 | } | |
| 119 | None => { | |
| 120 | 1× | if entries.remove(path).is_none() { |
| 121 | // Removing a directory removes everything below it. | |
| 122 | 1× | let prefix = format!("{path}/"); |
| 123 | 1× | let before = entries.len(); |
| 124 | 2× | entries.retain(|p, _| !p.starts_with(&prefix)); |
| 125 | 1× | if entries.len() == before { |
| 126 | 0× | return fail(format!("{path} does not exist")); |
| 127 | 1× | } |
| 128 | 0× | } |
| 129 | } | |
| 130 | } | |
| 131 | } | |
| 132 | 17× | for (path, pin) in pins { |
| 133 | 3× | let path = path.trim_matches('/'); |
| 134 | 3× | match base_snapshot.files.get(path) { |
| 135 | 2× | Some(f) if f.kind == NodeKind::Subrepo => {} |
| 136 | 1× | Some(_) => return fail(format!("{path} is not a subrepo")), |
| 137 | 1× | None => return fail(format!("{path} does not exist")), |
| 138 | } | |
| 139 | 1× | entries.insert( |
| 140 | 1× | path.to_string(), |
| 141 | 1× | FsEntry::new(NodeKind::Subrepo, false, pin.encode()), |
| 142 | ); | |
| 143 | } | |
| 144 | // A path the user writes settles its replace conflict, if any: they | |
| 145 | // chose that content (spec §9.3). | |
| 146 | 15× | let written: std::collections::HashSet<String> = edits |
| 147 | 15× | .iter() |
| 148 | 15× | .filter(|e| e.content.is_some()) |
| 149 | 15× | .map(|e| e.path.trim_matches('/').to_string()) |
| 150 | 15× | .chain(pins.iter().map(|(p, _)| p.trim_matches('/').to_string())) |
| 151 | 15× | .collect(); |
| 152 | 15× | let built = self.build_from_entries_resolving( |
| 153 | 15× | &base, |
| 154 | 15× | &base_snapshot, |
| 155 | 15× | HashMap::new(), |
| 156 | 15× | &entries, |
| 157 | 15× | &written, |
| 158 | 0× | )?; |
| 159 | 15× | if let Some(s) = built |
| 160 | 15× | .statuses |
| 161 | 15× | .iter() |
| 162 | 16× | .find(|s| s.status == ChangeStatus::ConflictEdited) |
| 163 | { | |
| 164 | 1× | return fail(format!("{} still contains conflict markers", s.path)); |
| 165 | 14× | } |
| 166 | 14× | if built.tree_ops.is_empty() && built.edits.is_empty() { |
| 167 | 1× | return fail("the edits change nothing"); |
| 168 | 13× | } |
| 169 | 13× | let mut change = Change { |
| 170 | version: crate::objects::CHANGE_VERSION, | |
| 171 | 13× | change_id: ChangeId::random(), |
| 172 | 13× | deps: vec![], |
| 173 | 13× | author, |
| 174 | 13× | hlc: Hlc::now(), |
| 175 | 13× | message: message.trim_end().to_string(), |
| 176 | 13× | tree_ops: built.tree_ops, |
| 177 | 13× | edits: built.edits, |
| 178 | 13× | kind: if built.resolves { |
| 179 | 3× | ChangeKind::Resolution |
| 180 | } else { | |
| 181 | 10× | ChangeKind::Normal |
| 182 | }, | |
| 183 | }; | |
| 184 | 13× | let mut deps: BTreeSet<ChangeHash> = referenced_changes(&change, &base); |
| 185 | 13× | deps.extend(built.contributors.iter().copied()); |
| 186 | 13× | change.deps = deps.into_iter().collect(); |
| 187 | 13× | change.normalize(); |
| 188 | 13× | Ok(change) |
| 189 | 19× | } |
| 190 | | |
| 191 | /// A reference update of `bookmark` to `heads`, attested by `signer` | |
| 192 | /// when given (the attestation a push would carry, spec §14.4). | |
| 193 | 10× | pub fn host_ref_update( |
| 194 | 10× | &self, |
| 195 | 10× | bookmark: &str, |
| 196 | 10× | heads: &[ChangeHash], |
| 197 | 10× | signer: Option<(&KeyId, &SecretKey)>, |
| 198 | 10× | ) -> Result<RefUpdate> { |
| 199 | 10× | let view = self.view()?; |
| 200 | 10× | let empty = self.store.put_state(&StateObject::new(vec![]))?; |
| 201 | 10× | let old_state = view.bookmarks.get(bookmark).copied().unwrap_or(empty); |
| 202 | 10× | let new_state = self.store.put_state(&StateObject::new(heads.to_vec()))?; |
| 203 | 10× | let attestation = match signer { |
| 204 | 9× | Some((key, sk)) => { |
| 205 | 9× | let serial = self |
| 206 | 9× | .latest_ref_attest(bookmark)? |
| 207 | 9× | .and_then(|(_, a)| match a.attest.body { |
| 208 | 9× | AttestBody::Ref { serial, .. } => Some(serial), |
| 209 | 0× | _ => None, |
| 210 | 9× | }) |
| 211 | 9× | .map(|s| s + 1) |
| 212 | 9× | .unwrap_or(1); |
| 213 | 9× | let attest = Attest { |
| 214 | 9× | repo: self.id()?, |
| 215 | 9× | hlc: Hlc::now(), |
| 216 | 9× | body: AttestBody::Ref { |
| 217 | 9× | name: bookmark.to_string(), |
| 218 | 9× | old_state, |
| 219 | 9× | new_state, |
| 220 | 9× | serial, |
| 221 | 9× | }, |
| 222 | }; | |
| 223 | 9× | let r = attest.hash(); |
| 224 | 9× | let sig = sk.sign(Domain::Attest, key, &r); |
| 225 | 9× | Some(SignedAttest { |
| 226 | 9× | attest, |
| 227 | 9× | sigs: vec![sig], |
| 228 | 9× | }) |
| 229 | } | |
| 230 | 1× | None => None, |
| 231 | }; | |
| 232 | 10× | Ok(RefUpdate { |
| 233 | 10× | name: bookmark.to_string(), |
| 234 | 10× | old_state, |
| 235 | 10× | new_state, |
| 236 | 10× | force: false, |
| 237 | 10× | attestation, |
| 238 | 10× | }) |
| 239 | 10× | } |
| 240 | | |
| 241 | /// Create tag `name` for the state of `heads` as a host does for a | |
| 242 | /// signed-in user (oxygenhub#295): attested by `signer` (a delegate of | |
| 243 | /// the user, or the host's own key), with `identities` it needs, and | |
| 244 | /// received as a pushed tag is, so the same policy checks apply. | |
| 245 | 0× | pub fn host_tag_create( |
| 246 | 0× | &self, |
| 247 | 0× | name: &str, |
| 248 | 0× | heads: &[ChangeHash], |
| 249 | 0× | message: &str, |
| 250 | 0× | signer: (&KeyId, &SecretKey), |
| 251 | 0× | identities: &[Identity], |
| 252 | 0× | ) -> Result<PushReport> { |
| 253 | 0× | crate::objects::validate_name(name) |
| 254 | 0× | .map_err(|e| RepoError::Msg(format!("tag name: {e}")))?; |
| 255 | 0× | let st = self.load_state_history(heads)?; |
| 256 | 0× | let heads = st.maximal(heads); |
| 257 | 0× | if heads.is_empty() { |
| 258 | 0× | return fail("a tag needs at least one change"); |
| 259 | 0× | } |
| 260 | 0× | let state_object = StateObject::new(heads.clone()); |
| 261 | 0× | let state = state_object.hash(); |
| 262 | 0× | let body = AttestBody::Tag { |
| 263 | 0× | name: name.into(), |
| 264 | 0× | state, |
| 265 | 0× | message: message.into(), |
| 266 | 0× | }; |
| 267 | 0× | let mut bundle = self.hosted_bundle(identities); |
| 268 | 0× | bundle |
| 269 | 0× | .objects |
| 270 | 0× | .push((ObjectType::State, state_object.encode())); |
| 271 | 0× | let t = crate::protocol::TagRef::new( |
| 272 | 0× | name.into(), |
| 273 | 0× | state, |
| 274 | 0× | heads, |
| 275 | 0× | Some(self.host_attest(body, signer)?), |
| 276 | ); | |
| 277 | 0× | self.receive_push_report(bundle, vec![], None, vec![t]) |
| 278 | 0× | } |
| 279 | | |
| 280 | /// Withdraw tag `name` as a host does for a signed-in user | |
| 281 | /// (oxygenhub#301): an `untag` attestation by `signer`, received as a | |
| 282 | /// pushed withdrawal is. | |
| 283 | 0× | pub fn host_tag_withdrawal( |
| 284 | 0× | &self, |
| 285 | 0× | name: &str, |
| 286 | 0× | reason: &str, |
| 287 | 0× | signer: (&KeyId, &SecretKey), |
| 288 | 0× | identities: &[Identity], |
| 289 | 0× | ) -> Result<PushReport> { |
| 290 | 0× | let Some(state) = self.view()?.tags.get(name).copied() else { |
| 291 | 0× | return fail(format!("no tag {name:?}")); |
| 292 | }; | |
| 293 | 0× | let body = AttestBody::Untag { |
| 294 | 0× | name: name.into(), |
| 295 | 0× | state, |
| 296 | 0× | reason: reason.into(), |
| 297 | 0× | }; |
| 298 | 0× | let t = crate::protocol::TagRef::new( |
| 299 | 0× | name.into(), |
| 300 | 0× | state, |
| 301 | 0× | vec![], |
| 302 | 0× | Some(self.host_attest(body, signer)?), |
| 303 | ); | |
| 304 | 0× | self.receive_push_report(self.hosted_bundle(identities), vec![], None, vec![t]) |
| 305 | 0× | } |
| 306 | | |
| 307 | 0× | fn host_attest( |
| 308 | 0× | &self, |
| 309 | 0× | body: AttestBody, |
| 310 | 0× | (key, sk): (&KeyId, &SecretKey), |
| 311 | 0× | ) -> Result<SignedAttest> { |
| 312 | 0× | let attest = Attest { |
| 313 | 0× | repo: self.id()?, |
| 314 | 0× | hlc: Hlc::now(), |
| 315 | 0× | body, |
| 316 | }; | |
| 317 | 0× | let sig = sk.sign(Domain::Attest, key, &attest.hash()); |
| 318 | 0× | Ok(SignedAttest { |
| 319 | 0× | attest, |
| 320 | 0× | sigs: vec![sig], |
| 321 | 0× | }) |
| 322 | 0× | } |
| 323 | | |
| 324 | 0× | fn hosted_bundle(&self, identities: &[Identity]) -> Bundle { |
| 325 | 0× | let mut bundle = Bundle::default(); |
| 326 | 0× | for id in identities { |
| 327 | 0× | bundle.objects.push((ObjectType::Identity, id.encode())); |
| 328 | 0× | } |
| 329 | 0× | bundle |
| 330 | 0× | } |
| 331 | | |
| 332 | /// Apply a host-made update as if it had been pushed: the new change | |
| 333 | /// (with its signatures) if any, the identities it needs, and the | |
| 334 | /// reference update, all through `receive_push_report`, so the same | |
| 335 | /// authentication, compare-and-swap and policy checks apply. | |
| 336 | 10× | pub fn apply_hosted( |
| 337 | 10× | &self, |
| 338 | 10× | change: Option<(Change, Vec<Signature>)>, |
| 339 | 10× | identities: &[Identity], |
| 340 | 10× | update: RefUpdate, |
| 341 | 10× | ) -> Result<PushReport> { |
| 342 | 10× | let mut bundle = Bundle::default(); |
| 343 | 17× | for id in identities { |
| 344 | 17× | bundle.objects.push((ObjectType::Identity, id.encode())); |
| 345 | 17× | } |
| 346 | 10× | if let Ok(st) = self.store.get_state(&update.new_state) { |
| 347 | 10× | bundle.objects.push((ObjectType::State, st.encode())); |
| 348 | 10× | } |
| 349 | 10× | if let Some((c, sigs)) = change { |
| 350 | 9× | bundle.changes.push((c, sigs)); |
| 351 | 9× | } |
| 352 | 10× | self.receive_push_report(bundle, vec![update], None, vec![]) |
| 353 | 10× | } |
| 354 | } | |
| 355 | ||
| 356 | #[cfg(test)] | |
| 357 | mod tests { | |
| 358 | use super::*; | |
| 359 | use crate::identity::SecretKey; | |
| 360 | | |
| 361 | 10× | fn hermetic() { |
| 362 | static ONCE: std::sync::Once = std::sync::Once::new(); | |
| 363 | static DIR: std::sync::OnceLock<tempfile::TempDir> = std::sync::OnceLock::new(); | |
| 364 | 10× | ONCE.call_once(|| { |
| 365 | 10× | let d = tempfile::tempdir().unwrap(); |
| 366 | 10× | std::env::set_var("OXY_HOME", d.path()); |
| 367 | 10× | DIR.set(d).ok(); |
| 368 | 10× | }); |
| 369 | 10× | } |
| 370 | | |
| 371 | 12× | fn write(root: &Path, rel: &str, content: &[u8]) { |
| 372 | 12× | let p = root.join(rel); |
| 373 | 12× | fs::create_dir_all(p.parent().unwrap()).unwrap(); |
| 374 | 12× | fs::write(p, content).unwrap(); |
| 375 | 12× | } |
| 376 | | |
| 377 | /// A user repository pushed to a bare host, and a hub key the user | |
| 378 | /// has delegated to. Returns (host, user, hub key, hub secret). | |
| 379 | 10× | fn setup( |
| 380 | 10× | root: &Path, |
| 381 | 10× | with_policy: bool, |
| 382 | 10× | delegate: bool, |
| 383 | 10× | ) -> (Repo, Repo, KeyId, SecretKey, Identity) { |
| 384 | 10× | hermetic(); |
| 385 | 10× | let host_dir = root.join("host"); |
| 386 | 10× | Repo::init_bare_unbound(&host_dir).unwrap(); |
| 387 | 10× | let work = root.join("work"); |
| 388 | 10× | let mut user = Repo::init(&work).unwrap(); |
| 389 | 10× | user.create_identity("Andy", "[email protected]", IdentityScope::Repo) |
| 390 | 10× | .unwrap(); |
| 391 | 10× | user.config.set("core.sign", "change").unwrap(); |
| 392 | 10× | user.config |
| 393 | 10× | .set("remote.origin.url", host_dir.to_str().unwrap()) |
| 394 | 10× | .unwrap(); |
| 395 | 10× | user.save_config().unwrap(); |
| 396 | 10× | let hub_sk = SecretKey::generate(); |
| 397 | 10× | let hub_id = hub_sk.identity("Hub", "hub@example", 1); |
| 398 | 10× | let hub_key = hub_id.hash(); |
| 399 | 10× | if with_policy { |
| 400 | 5× | user.policy_init().unwrap(); |
| 401 | 5× | } |
| 402 | 10× | if delegate { |
| 403 | 3× | user.delegate_add(hub_id.clone(), Scope::CHANGES | Scope::REFS, None) |
| 404 | 3× | .unwrap(); |
| 405 | 7× | } |
| 406 | 10× | write(&work, "a.txt", b"one\ntwo\n"); |
| 407 | 10× | user.snapshot("t").unwrap(); |
| 408 | 10× | user.commit("first", &[]).unwrap(); |
| 409 | 10× | let r = user.push("origin", false).unwrap(); |
| 410 | 10× | assert!(r.iter().all(|x| x.ok), "{r:?}"); |
| 411 | 10× | ( |
| 412 | 10× | Repo::open(&host_dir).unwrap(), |
| 413 | 10× | user, |
| 414 | 10× | hub_key, |
| 415 | 10× | hub_sk, |
| 416 | 10× | hub_id, |
| 417 | 10× | ) |
| 418 | 10× | } |
| 419 | | |
| 420 | #[test] | |
| 421 | 1× | fn a_host_edit_signed_by_a_delegate_lands() { |
| 422 | 1× | let dir = tempfile::tempdir().unwrap(); |
| 423 | 1× | let (host, user, hub_key, hub_sk, hub_id) = setup(dir.path(), false, true); |
| 424 | 1× | let author = user.config.author_key.unwrap(); |
| 425 | 1× | let heads = host.bookmark_heads().unwrap(); |
| 426 | 1× | let change = host |
| 427 | 1× | .build_change( |
| 428 | 1× | &heads, |
| 429 | 1× | &[ |
| 430 | 1× | FileEdit { |
| 431 | 1× | path: "a.txt".into(), |
| 432 | 1× | content: Some(b"one\ntwo\nthree\n".to_vec()), |
| 433 | 1× | }, |
| 434 | 1× | FileEdit { |
| 435 | 1× | path: "docs/new.md".into(), |
| 436 | 1× | content: Some(b"# New\n".to_vec()), |
| 437 | 1× | }, |
| 438 | 1× | ], |
| 439 | 1× | "Edit on the web", |
| 440 | 1× | author, |
| 441 | ) | |
| 442 | 1× | .unwrap(); |
| 443 | 1× | assert_eq!(change.author, author); |
| 444 | 1× | assert_eq!(change.kind, ChangeKind::Normal); |
| 445 | 1× | let hash = change.hash(); |
| 446 | 1× | let sig = hub_sk.sign(Domain::Change, &hub_key, &hash); |
| 447 | 1× | let mut new_heads = heads.clone(); |
| 448 | 1× | new_heads.push(hash); |
| 449 | 1× | let update = host |
| 450 | 1× | .host_ref_update("main", &[hash], Some((&hub_key, &hub_sk))) |
| 451 | 1× | .unwrap(); |
| 452 | 1× | let ids = vec![hub_id, user.identity(&author).unwrap()]; |
| 453 | 1× | let report = host |
| 454 | 1× | .apply_hosted(Some((change, vec![sig])), &ids, update) |
| 455 | 1× | .unwrap(); |
| 456 | 1× | assert!(report.results.iter().all(|r| r.ok), "{:?}", report.results); |
| 457 | 1× | assert_eq!(host.bookmark_heads().unwrap(), vec![hash]); |
| 458 | 1× | assert_eq!( |
| 459 | 1× | host.file_at("a.txt", Some(&[hash])).unwrap(), |
| 460 | b"one\ntwo\nthree\n" | |
| 461 | ); | |
| 462 | 1× | assert_eq!( |
| 463 | 1× | host.file_at("docs/new.md", Some(&[hash])).unwrap(), |
| 464 | b"# New\n" | |
| 465 | ); | |
| 466 | // Authored by the user, authenticated by the hub key as delegate. | |
| 467 | 1× | assert!( |
| 468 | 1× | matches!(host.authenticate_change(&hash).unwrap(), SignatureStatus::Valid { key, name, .. } if key == hub_key && name == "Hub") |
| 469 | ); | |
| 470 | 1× | let _ = new_heads; |
| 471 | // Bad paths and empty edits are refused. | |
| 472 | 1× | assert!(host |
| 473 | 1× | .build_change( |
| 474 | 1× | &[hash], |
| 475 | 1× | &[FileEdit { |
| 476 | 1× | path: "../x".into(), |
| 477 | 1× | content: Some(vec![]) |
| 478 | 1× | }], |
| 479 | 1× | "m", |
| 480 | 1× | author |
| 481 | 1× | ) |
| 482 | 1× | .is_err()); |
| 483 | 1× | assert!( |
| 484 | 1× | host.build_change( |
| 485 | 1× | &[hash], |
| 486 | 1× | &[FileEdit { |
| 487 | 1× | path: "a.txt".into(), |
| 488 | 1× | content: Some(b"one\ntwo\nthree\n".to_vec()) |
| 489 | 1× | }], |
| 490 | 1× | "m", |
| 491 | 1× | author |
| 492 | 1× | ) |
| 493 | 1× | .is_err(), |
| 494 | "no-op edits are refused" | |
| 495 | ); | |
| 496 | 1× | assert!( |
| 497 | 1× | host.build_change( |
| 498 | 1× | &[hash], |
| 499 | 1× | &[FileEdit { |
| 500 | 1× | path: "a.txt/b".into(), |
| 501 | 1× | content: Some(vec![]) |
| 502 | 1× | }], |
| 503 | 1× | "m", |
| 504 | 1× | author |
| 505 | 1× | ) |
| 506 | 1× | .is_err(), |
| 507 | "a file inside a file" | |
| 508 | ); | |
| 509 | 1× | let rm = host |
| 510 | 1× | .build_change( |
| 511 | 1× | &[hash], |
| 512 | 1× | &[FileEdit { |
| 513 | 1× | path: "docs".into(), |
| 514 | 1× | content: None, |
| 515 | 1× | }], |
| 516 | 1× | "Remove docs", |
| 517 | 1× | author, |
| 518 | ) | |
| 519 | 1× | .unwrap(); |
| 520 | 1× | assert!(!rm.tree_ops.is_empty()); |
| 521 | 1× | } |
| 522 | | |
| 523 | #[test] | |
| 524 | 1× | fn a_host_edit_by_a_key_that_is_not_a_delegate_is_refused() { |
| 525 | 1× | let dir = tempfile::tempdir().unwrap(); |
| 526 | 1× | let (host, user, hub_key, hub_sk, hub_id) = setup(dir.path(), false, false); |
| 527 | 1× | let author = user.config.author_key.unwrap(); |
| 528 | 1× | let heads = host.bookmark_heads().unwrap(); |
| 529 | 1× | let change = host |
| 530 | 1× | .build_change( |
| 531 | 1× | &heads, |
| 532 | 1× | &[FileEdit { |
| 533 | 1× | path: "a.txt".into(), |
| 534 | 1× | content: Some(b"x\n".to_vec()), |
| 535 | 1× | }], |
| 536 | 1× | "Edit", |
| 537 | 1× | author, |
| 538 | ) | |
| 539 | 1× | .unwrap(); |
| 540 | 1× | let hash = change.hash(); |
| 541 | 1× | let sig = hub_sk.sign(Domain::Change, &hub_key, &hash); |
| 542 | 1× | let update = host |
| 543 | 1× | .host_ref_update("main", &[hash], Some((&hub_key, &hub_sk))) |
| 544 | 1× | .unwrap(); |
| 545 | 1× | let report = host |
| 546 | 1× | .apply_hosted( |
| 547 | 1× | Some((change, vec![sig])), |
| 548 | 1× | &[hub_id, user.identity(&author).unwrap()], |
| 549 | 1× | update, |
| 550 | ) | |
| 551 | 1× | .unwrap(); |
| 552 | // Without a policy the host accepts unauthenticated changes, but the | |
| 553 | // signature does not verify as the author's: it stays unauthenticated. | |
| 554 | 1× | assert!(report.results.iter().all(|r| r.ok)); |
| 555 | 1× | assert!( |
| 556 | 1× | matches!(host.authenticate_change(&hash).unwrap(), SignatureStatus::Invalid(why) if why.contains("neither the author")) |
| 557 | ); | |
| 558 | 1× | } |
| 559 | | |
| 560 | #[test] | |
| 561 | 1× | fn under_a_policy_a_delegate_may_update_the_reference_and_a_stranger_may_not() { |
| 562 | 1× | let dir = tempfile::tempdir().unwrap(); |
| 563 | 1× | let (host, user, hub_key, hub_sk, hub_id) = setup(dir.path(), true, true); |
| 564 | 1× | assert!(host.current_policy().unwrap().is_some()); |
| 565 | 1× | let author = user.config.author_key.unwrap(); |
| 566 | 1× | let heads = host.bookmark_heads().unwrap(); |
| 567 | 1× | let change = host |
| 568 | 1× | .build_change( |
| 569 | 1× | &heads, |
| 570 | 1× | &[FileEdit { |
| 571 | 1× | path: "a.txt".into(), |
| 572 | 1× | content: Some(b"one\n".to_vec()), |
| 573 | 1× | }], |
| 574 | 1× | "Trim", |
| 575 | 1× | author, |
| 576 | ) | |
| 577 | 1× | .unwrap(); |
| 578 | 1× | let hash = change.hash(); |
| 579 | 1× | let sig = hub_sk.sign(Domain::Change, &hub_key, &hash); |
| 580 | 1× | let ids = vec![hub_id.clone(), user.identity(&author).unwrap()]; |
| 581 | 1× | let update = host |
| 582 | 1× | .host_ref_update("main", &[hash], Some((&hub_key, &hub_sk))) |
| 583 | 1× | .unwrap(); |
| 584 | 1× | let report = host |
| 585 | 1× | .apply_hosted(Some((change, vec![sig])), &ids, update) |
| 586 | 1× | .unwrap(); |
| 587 | 1× | assert!(report.results.iter().all(|r| r.ok), "{:?}", report.results); |
| 588 | 1× | assert_eq!(host.bookmark_heads().unwrap(), vec![hash]); |
| 589 | | |
| 590 | // A stranger's key cannot move the reference, even with the user as author. | |
| 591 | 1× | let stranger = SecretKey::generate(); |
| 592 | 1× | let sid = stranger.identity("Mallory", "m@example", 1); |
| 593 | 1× | let skey = sid.hash(); |
| 594 | 1× | let change2 = host |
| 595 | 1× | .build_change( |
| 596 | 1× | &[hash], |
| 597 | 1× | &[FileEdit { |
| 598 | 1× | path: "a.txt".into(), |
| 599 | 1× | content: Some(b"evil\n".to_vec()), |
| 600 | 1× | }], |
| 601 | 1× | "Evil", |
| 602 | 1× | author, |
| 603 | ) | |
| 604 | 1× | .unwrap(); |
| 605 | 1× | let h2 = change2.hash(); |
| 606 | 1× | let sig2 = stranger.sign(Domain::Change, &skey, &h2); |
| 607 | 1× | let update = host |
| 608 | 1× | .host_ref_update("main", &[h2], Some((&skey, &stranger))) |
| 609 | 1× | .unwrap(); |
| 610 | 1× | let report = host |
| 611 | 1× | .apply_hosted(Some((change2, vec![sig2])), &[sid], update) |
| 612 | 1× | .unwrap(); |
| 613 | 1× | assert!(report.results.iter().all(|r| !r.ok), "{:?}", report.results); |
| 614 | 1× | assert_eq!( |
| 615 | 1× | host.bookmark_heads().unwrap(), |
| 616 | 1× | vec![hash], |
| 617 | "the reference did not move" | |
| 618 | ); | |
| 619 | // An unsigned update is refused under a policy too. | |
| 620 | 1× | let update = host.host_ref_update("main", &[hash], None).unwrap(); |
| 621 | 1× | let report = host.apply_hosted(None, &[], update).unwrap(); |
| 622 | 1× | assert!(report.results.iter().all(|r| !r.ok)); |
| 623 | 1× | } |
| 624 | | |
| 625 | /// Agent doc §2: an agent's key signs changes for its owner and nothing | |
| 626 | /// more. Its reference attestation is refused even though its owner's | |
| 627 | /// would pass, and a seal it makes authenticates nothing. | |
| 628 | #[test] | |
| 629 | 1× | fn a_changes_only_delegate_signs_changes_but_cannot_seal_or_move_a_reference() { |
| 630 | 1× | let dir = tempfile::tempdir().unwrap(); |
| 631 | 1× | let (host, mut user, _, _, _) = setup(dir.path(), true, false); |
| 632 | 1× | let agent_sk = SecretKey::generate(); |
| 633 | 1× | let agent_id = agent_sk.identity("aider on a laptop", "agent@example", 1); |
| 634 | 1× | let agent = agent_id.hash(); |
| 635 | 1× | user.delegate_add(agent_id.clone(), Scope::CHANGES, None) |
| 636 | 1× | .unwrap(); |
| 637 | // The successor identity reaches the host with the rotated policy. | |
| 638 | 1× | assert!(user.push("origin", false).unwrap().iter().all(|x| x.ok)); |
| 639 | 1× | let author = user.config.author_key.unwrap(); |
| 640 | 1× | let ids = vec![agent_id.clone(), user.identity(&author).unwrap()]; |
| 641 | 1× | let heads = host.bookmark_heads().unwrap(); |
| 642 | 1× | let change = host |
| 643 | 1× | .build_change( |
| 644 | 1× | &heads, |
| 645 | 1× | &[FileEdit { |
| 646 | 1× | path: "a.txt".into(), |
| 647 | 1× | content: Some(b"by the agent\n".to_vec()), |
| 648 | 1× | }], |
| 649 | 1× | "Agent edit", |
| 650 | 1× | author, |
| 651 | ) | |
| 652 | 1× | .unwrap(); |
| 653 | 1× | let hash = change.hash(); |
| 654 | 1× | let sig = agent_sk.sign(Domain::Change, &agent, &hash); |
| 655 | // Attested by the agent: refused, the reference stays. | |
| 656 | 1× | let update = host |
| 657 | 1× | .host_ref_update("main", &[hash], Some((&agent, &agent_sk))) |
| 658 | 1× | .unwrap(); |
| 659 | 1× | let report = host |
| 660 | 1× | .apply_hosted(Some((change.clone(), vec![sig.clone()])), &ids, update) |
| 661 | 1× | .unwrap(); |
| 662 | 1× | assert!(report.results.iter().all(|r| !r.ok), "{:?}", report.results); |
| 663 | 1× | assert_eq!(host.bookmark_heads().unwrap(), heads); |
| 664 | // Attested by the owner: the agent-signed change lands. | |
| 665 | 1× | let (owner, owner_sk) = user.secret_key().unwrap().unwrap(); |
| 666 | 1× | let update = host |
| 667 | 1× | .host_ref_update("main", &[hash], Some((&owner, &owner_sk))) |
| 668 | 1× | .unwrap(); |
| 669 | 1× | let report = host |
| 670 | 1× | .apply_hosted(Some((change, vec![sig])), &ids, update) |
| 671 | 1× | .unwrap(); |
| 672 | 1× | assert!(report.results.iter().all(|r| r.ok), "{:?}", report.results); |
| 673 | 1× | assert_eq!(host.bookmark_heads().unwrap(), vec![hash]); |
| 674 | 1× | assert!(matches!( |
| 675 | 1× | host.authenticate_change(&hash).unwrap(), |
| 676 | 1× | SignatureStatus::Valid { key, sealed: false, .. } if key == agent |
| 677 | )); | |
| 678 | // A seal by the agent is outside its scope. | |
| 679 | 1× | let change2 = host |
| 680 | 1× | .build_change( |
| 681 | 1× | &[hash], |
| 682 | 1× | &[FileEdit { |
| 683 | 1× | path: "a.txt".into(), |
| 684 | 1× | content: Some(b"sealed by the agent\n".to_vec()), |
| 685 | 1× | }], |
| 686 | 1× | "Sealed", |
| 687 | 1× | author, |
| 688 | ) | |
| 689 | 1× | .unwrap(); |
| 690 | 1× | let h2 = host.store().put_change(&change2).unwrap(); |
| 691 | 1× | let seal = host |
| 692 | 1× | .store() |
| 693 | 1× | .put_attest(&Attest::seal(host.id().unwrap(), vec![h2])) |
| 694 | 1× | .unwrap(); |
| 695 | 1× | host.store() |
| 696 | 1× | .put_signature(&seal, &agent_sk.sign(Domain::Attest, &agent, &seal)) |
| 697 | 1× | .unwrap(); |
| 698 | 1× | host.store().index_seal(&h2, &seal).unwrap(); |
| 699 | 1× | assert!(matches!( |
| 700 | 1× | host.authenticate_change(&h2).unwrap(), |
| 701 | 1× | SignatureStatus::Invalid(e) if e.contains("may not seal") |
| 702 | )); | |
| 703 | 1× | } |
| 704 | | |
| 705 | /// Agent doc §2 end to end from the command line's point of view: the | |
| 706 | /// agent's key is a changes-only delegate, and `author.signer` makes | |
| 707 | /// the owner's checkout sign with it while `author.key` stays the | |
| 708 | /// owner, so a commit carries the owner as author and the agent's | |
| 709 | /// signature, and a reference attestation by the agent is refused. | |
| 710 | #[test] | |
| 711 | 1× | fn an_agent_signs_its_owner_s_changes_through_author_signer() { |
| 712 | 1× | let dir = tempfile::tempdir().unwrap(); |
| 713 | 1× | let (host, mut user, _, _, _) = setup(dir.path(), true, false); |
| 714 | 1× | let owner = user.config.author_key.unwrap(); |
| 715 | // The agent makes its own key in its own home; the owner delegates. | |
| 716 | 1× | let agent_home = dir.path().join("agent-home"); |
| 717 | 1× | let mut agent_repo = Repo::init(&dir.path().join("agent-work")) |
| 718 | 1× | .unwrap() |
| 719 | 1× | .with_home(agent_home.clone()); |
| 720 | 1× | let (agent, agent_id) = agent_repo |
| 721 | 1× | .create_identity("aider on a laptop", "agent@example", IdentityScope::User) |
| 722 | 1× | .unwrap(); |
| 723 | 1× | user.delegate_add(agent_id.clone(), Scope::CHANGES, None) |
| 724 | 1× | .unwrap(); |
| 725 | 1× | assert!(user.push("origin", false).unwrap().iter().all(|x| x.ok)); |
| 726 | 1× | host.refresh().unwrap(); |
| 727 | 1× | let owner = { |
| 728 | 1× | let _ = owner; |
| 729 | 1× | user.config.author_key.unwrap() |
| 730 | }; | |
| 731 | // The owner's checkout, as the agent runs it: the agent's keys, | |
| 732 | // the owner as author, the agent as signer. | |
| 733 | // (The signer is the agent's own setting, in its user config: the | |
| 734 | // checkout's `author.key` is the owner's, in the repository.) | |
| 735 | 1× | let work = dir.path().join("work"); |
| 736 | 1× | let mut as_agent = Repo::open(&work).unwrap().with_home(agent_home); |
| 737 | 1× | as_agent |
| 738 | 1× | .set_user_config("author.signer", &agent.to_hex()) |
| 739 | 1× | .unwrap(); |
| 740 | 1× | as_agent.set_user_config("core.sign", "change").unwrap(); |
| 741 | 1× | assert_eq!(as_agent.config.author_key, Some(owner)); |
| 742 | 1× | assert_eq!(as_agent.signing_key(), Some(agent)); |
| 743 | 1× | write(&work, "a.txt", b"by the agent\n"); |
| 744 | 1× | as_agent.snapshot("t").unwrap(); |
| 745 | 1× | let h = as_agent.commit("Agent: edit a", &[]).unwrap(); |
| 746 | 1× | let meta = as_agent.store().get_change_meta(&h).unwrap(); |
| 747 | 1× | assert_eq!(meta.author, owner, "the owner stays the author"); |
| 748 | 1× | assert!(matches!( |
| 749 | 1× | as_agent.verify_change(&h).unwrap(), |
| 750 | 1× | SignatureStatus::Valid { key, .. } if key == agent |
| 751 | )); | |
| 752 | 1× | assert_eq!(as_agent.revset("delegated()").unwrap().hashes, vec![h]); |
| 753 | // The agent cannot push: its attestation of the reference is | |
| 754 | // refused under the policy. The owner can. | |
| 755 | 1× | let r = as_agent.push("origin", false).unwrap(); |
| 756 | 1× | host.refresh().unwrap(); |
| 757 | 1× | assert!(r.iter().all(|x| !x.ok), "{r:?}"); |
| 758 | 1× | assert!(!host.bookmark_heads().unwrap().contains(&h)); |
| 759 | // The owner's own handle (its key directory was fixed at `setup`; | |
| 760 | // the environment's `OXY_HOME` may have moved since), which reads | |
| 761 | // the head again to see the agent's commit. | |
| 762 | 1× | user.refresh().unwrap(); |
| 763 | 1× | let r = user.push("origin", false).unwrap(); |
| 764 | 1× | host.refresh().unwrap(); |
| 765 | 1× | assert!(r.iter().all(|x| x.ok), "{r:?}"); |
| 766 | 1× | assert!(host.bookmark_heads().unwrap().contains(&h)); |
| 767 | 1× | assert!(matches!( |
| 768 | 1× | host.authenticate_change(&h).unwrap(), |
| 769 | 1× | SignatureStatus::Valid { key, .. } if key == agent |
| 770 | )); | |
| 771 | 1× | } |
| 772 | | |
| 773 | /// oxygenhub#129: a delegate that may move references makes a bookmark | |
| 774 | /// at its owner's change, signing no change of its own. The push | |
| 775 | /// carries its identity, so the host can check the move (it was | |
| 776 | /// refused as "unknown key" until the delegate had pushed a change). | |
| 777 | #[test] | |
| 778 | 1× | fn a_delegate_s_first_push_of_a_bookmark_carries_its_identity() { |
| 779 | 1× | let dir = tempfile::tempdir().unwrap(); |
| 780 | 1× | let (host, mut user, _, _, _) = setup(dir.path(), true, false); |
| 781 | 1× | let agent_home = dir.path().join("agent-home"); |
| 782 | 1× | let mut agent_repo = Repo::init(&dir.path().join("agent-work")) |
| 783 | 1× | .unwrap() |
| 784 | 1× | .with_home(agent_home.clone()); |
| 785 | 1× | let (agent, agent_id) = agent_repo |
| 786 | 1× | .create_identity("agent", "agent@example", IdentityScope::User) |
| 787 | 1× | .unwrap(); |
| 788 | 1× | user.delegate_add(agent_id, Scope::CHANGES | Scope::REFS, None) |
| 789 | 1× | .unwrap(); |
| 790 | 1× | assert!(user.push("origin", false).unwrap().iter().all(|x| x.ok)); |
| 791 | 1× | let work = dir.path().join("work"); |
| 792 | 1× | let mut as_agent = Repo::open(&work).unwrap().with_home(agent_home); |
| 793 | 1× | as_agent |
| 794 | 1× | .set_user_config("author.signer", &agent.to_hex()) |
| 795 | 1× | .unwrap(); |
| 796 | 1× | assert!( |
| 797 | 1× | host.store().get_identity(&agent).is_err(), |
| 798 | "the host has never seen the agent" | |
| 799 | ); | |
| 800 | 1× | let heads = as_agent.bookmark_heads().unwrap(); |
| 801 | 1× | as_agent.bookmark_create("feature", Some(&heads)).unwrap(); |
| 802 | 1× | let r = as_agent.push_bookmark("origin", "feature", false).unwrap(); |
| 803 | 1× | assert!(r.iter().all(|x| x.ok), "{r:?}"); |
| 804 | 1× | assert!( |
| 805 | 1× | host.store().get_identity(&agent).is_ok(), |
| 806 | "its identity came with the move" | |
| 807 | ); | |
| 808 | 1× | } |
| 809 | | |
| 810 | /// oxygenhub#266: a bookmark whose latest attestation a delegate made, | |
| 811 | /// signing no change, is fetched by a third party. The fetch bundle | |
| 812 | /// carries the delegate's identity even when it asks for no change, | |
| 813 | /// so a client as it was (import the bundle, then check each | |
| 814 | /// bookmark's attestation) verifies the move instead of failing the | |
| 815 | /// whole fetch with "unknown key". | |
| 816 | #[test] | |
| 817 | 1× | fn a_bookmark_attested_by_a_key_that_signed_no_change_is_fetched() { |
| 818 | 1× | let dir = tempfile::tempdir().unwrap(); |
| 819 | 1× | let (host, mut user, _, _, _) = setup(dir.path(), true, false); |
| 820 | 1× | let agent_home = dir.path().join("agent-home"); |
| 821 | 1× | let mut agent_repo = Repo::init(&dir.path().join("agent-work")) |
| 822 | 1× | .unwrap() |
| 823 | 1× | .with_home(agent_home.clone()); |
| 824 | 1× | let (agent, agent_id) = agent_repo |
| 825 | 1× | .create_identity("agent", "agent@example", IdentityScope::User) |
| 826 | 1× | .unwrap(); |
| 827 | 1× | user.delegate_add(agent_id, Scope::CHANGES | Scope::REFS, None) |
| 828 | 1× | .unwrap(); |
| 829 | 1× | assert!(user.push("origin", false).unwrap().iter().all(|x| x.ok)); |
| 830 | 1× | let mut as_agent = Repo::open(&dir.path().join("work")) |
| 831 | 1× | .unwrap() |
| 832 | 1× | .with_home(agent_home); |
| 833 | 1× | as_agent |
| 834 | 1× | .set_user_config("author.signer", &agent.to_hex()) |
| 835 | 1× | .unwrap(); |
| 836 | 1× | let heads = as_agent.bookmark_heads().unwrap(); |
| 837 | 1× | as_agent.bookmark_create("feature", Some(&heads)).unwrap(); |
| 838 | 1× | let r = as_agent.push_bookmark("origin", "feature", false).unwrap(); |
| 839 | 1× | assert!(r.iter().all(|x| x.ok), "{r:?}"); |
| 840 | 1× | host.refresh().unwrap(); |
| 841 | 1× | let (_, sa) = host.latest_ref_attest("feature").unwrap().unwrap(); |
| 842 | 1× | assert_eq!(sa.sigs[0].key, agent, "the agent attested the move"); |
| 843 | | |
| 844 | // What a fetch that names no change brings: the agent's identity. | |
| 845 | 1× | let remote = crate::protocol::LocalRemote::open(&dir.path().join("host")).unwrap(); |
| 846 | 2× | let carries = |b: &Bundle| { |
| 847 | 2× | b.objects |
| 848 | 2× | .iter() |
| 849 | 4× | .any(|(t, body)| *t == ObjectType::Identity && hash_object(*t, body) == agent) |
| 850 | 2× | }; |
| 851 | 1× | assert!(carries( |
| 852 | 1× | &crate::protocol::Remote::fetch(&remote, &[], None).unwrap() |
| 853 | 1× | )); |
| 854 | 1× | assert!(carries(&host.bundle_for_filtered(&[], None).unwrap())); |
| 855 | | |
| 856 | // A third party's clone takes both bookmarks, verified. | |
| 857 | 1× | let theirs = Repo::clone( |
| 858 | 1× | dir.path().join("host").to_str().unwrap(), |
| 859 | 1× | &dir.path().join("theirs"), |
| 860 | ) | |
| 861 | 1× | .unwrap(); |
| 862 | 1× | let tracked = theirs.view().unwrap().remotes["origin"].clone(); |
| 863 | 1× | assert_eq!( |
| 864 | 1× | tracked.get("feature"), |
| 865 | 1× | host.view().unwrap().bookmarks.get("feature") |
| 866 | ); | |
| 867 | 1× | assert!(theirs.store().get_identity(&agent).is_ok()); |
| 868 | 1× | let report = theirs.fetch("origin").unwrap(); |
| 869 | 1× | assert!(report.skipped.is_empty(), "{:?}", report.skipped); |
| 870 | 1× | } |
| 871 | | |
| 872 | /// Make `bookmark`'s latest attestation on `host` one signed as `key` | |
| 873 | /// with `sk`: by a key the host never saw (as a host that did not send | |
| 874 | /// the signer's identity looked), or a bad signature. | |
| 875 | 3× | fn reattest(host: &Repo, bookmark: &str, key: KeyId, sk: &SecretKey) { |
| 876 | 3× | let state = host.view().unwrap().bookmarks[bookmark]; |
| 877 | 3× | let (_, last) = host.latest_ref_attest(bookmark).unwrap().unwrap(); |
| 878 | let AttestBody::Ref { | |
| 879 | 3× | old_state, serial, .. |
| 880 | 3× | } = last.attest.body |
| 881 | else { | |
| 882 | 0× | panic!("not a ref attestation") |
| 883 | }; | |
| 884 | 3× | let attest = Attest { |
| 885 | 3× | repo: host.id().unwrap(), |
| 886 | 3× | hlc: Hlc::now(), |
| 887 | 3× | body: AttestBody::Ref { |
| 888 | 3× | name: bookmark.into(), |
| 889 | 3× | old_state, |
| 890 | 3× | new_state: state, |
| 891 | 3× | serial: serial + 1, |
| 892 | 3× | }, |
| 893 | 3× | }; |
| 894 | 3× | let r = host.store().put_attest(&attest).unwrap(); |
| 895 | 3× | host.store() |
| 896 | 3× | .put_signature(&r, &sk.sign(Domain::Attest, &key, &attest.hash())) |
| 897 | 3× | .unwrap(); |
| 898 | 3× | host.set_ref_attest(bookmark, &r).unwrap(); |
| 899 | 3× | } |
| 900 | | |
| 901 | /// oxygenhub#266: a bookmark whose attestation a client cannot verify | |
| 902 | /// (its signer unknown, or its signature bad) is left as it was, named | |
| 903 | /// with why, and the rest are fetched. The current bookmark is not | |
| 904 | /// skipped: a clone, fetch or pull that would take it fails. | |
| 905 | #[test] | |
| 906 | 1× | fn an_unverifiable_bookmark_is_skipped_but_never_the_current_one() { |
| 907 | 1× | let dir = tempfile::tempdir().unwrap(); |
| 908 | 1× | let (host, user, _, _, _) = setup(dir.path(), false, false); |
| 909 | 1× | let author = user.config.author_key.unwrap(); |
| 910 | 1× | let heads = user.bookmark_heads().unwrap(); |
| 911 | 3× | for b in ["unknown", "forged", "fine"] { |
| 912 | 3× | user.bookmark_create(b, Some(&heads)).unwrap(); |
| 913 | 3× | let r = user.push_bookmark("origin", b, false).unwrap(); |
| 914 | 3× | assert!(r.iter().all(|x| x.ok), "{r:?}"); |
| 915 | } | |
| 916 | 1× | host.refresh().unwrap(); |
| 917 | 1× | let stranger_sk = SecretKey::generate(); |
| 918 | 1× | let stranger = stranger_sk.identity("s", "s@example", 1).hash(); |
| 919 | 1× | reattest(&host, "unknown", stranger, &stranger_sk); |
| 920 | 1× | reattest(&host, "forged", author, &stranger_sk); |
| 921 | | |
| 922 | 1× | let url = dir.path().join("host").to_string_lossy().to_string(); |
| 923 | 1× | let (theirs, skipped) = |
| 924 | 1× | Repo::clone_reporting(&url, &dir.path().join("theirs"), None, &[], None).unwrap(); |
| 925 | 2× | let names: Vec<&str> = skipped.iter().map(|(n, _)| n.as_str()).collect(); |
| 926 | 1× | assert_eq!(names, ["forged", "unknown"]); |
| 927 | 1× | assert!(skipped[1].1.contains("unknown key"), "{skipped:?}"); |
| 928 | 1× | assert!(skipped[1].1.contains("origin/unknown"), "{skipped:?}"); |
| 929 | 1× | let tracked = theirs.view().unwrap().remotes["origin"].clone(); |
| 930 | 1× | assert!(tracked.contains_key("main") && tracked.contains_key("fine")); |
| 931 | 1× | assert!(!tracked.contains_key("unknown") && !tracked.contains_key("forged")); |
| 932 | 1× | assert_eq!(theirs.config.bookmark, "main"); |
| 933 | 1× | assert_eq!(theirs.fetch("origin").unwrap().skipped.len(), 2); |
| 934 | 1× | assert_eq!(theirs.pull("origin").unwrap().skipped.len(), 2); |
| 935 | | |
| 936 | // The current bookmark's attestation unverifiable: a fetch and a | |
| 937 | // pull fail, and so does a fresh clone. | |
| 938 | 1× | reattest(&host, "main", stranger, &stranger_sk); |
| 939 | 3× | for err in [ |
| 940 | 1× | theirs.fetch("origin").unwrap_err(), |
| 941 | 1× | theirs.pull("origin").unwrap_err(), |
| 942 | 1× | Repo::clone(&url, &dir.path().join("again")) |
| 943 | 1× | .err() |
| 944 | 1× | .expect("the clone fails"), |
| 945 | 1× | ] { |
| 946 | 3× | let err = err.to_string(); |
| 947 | 3× | assert!( |
| 948 | 3× | err.contains("attestation for origin/main: unknown key"), |
| 949 | "{err}" | |
| 950 | ); | |
| 951 | } | |
| 952 | 1× | } |
| 953 | | |
| 954 | /// A delegation past its expiry authenticates nothing made after it, | |
| 955 | /// and the delegator may revoke the key; a stranger's key, they may not. | |
| 956 | #[test] | |
| 957 | 1× | fn an_expired_delegation_is_refused_and_the_delegator_may_revoke() { |
| 958 | 1× | let dir = tempfile::tempdir().unwrap(); |
| 959 | 1× | let (_, mut user, _, _, _) = setup(dir.path(), false, false); |
| 960 | 1× | let agent_sk = SecretKey::generate(); |
| 961 | 1× | let agent_id = agent_sk.identity("agent", "agent@example", 1); |
| 962 | 1× | let agent = agent_id.hash(); |
| 963 | 1× | let yesterday = Hlc::now().wall_ms - 24 * 3600 * 1000; |
| 964 | 1× | user.delegate_add(agent_id, Scope::CHANGES, Some(yesterday)) |
| 965 | 1× | .unwrap(); |
| 966 | 1× | user.config.set("core.sign", "seal").unwrap(); |
| 967 | 1× | user.save_config().unwrap(); |
| 968 | 1× | write(&dir.path().join("work"), "a.txt", b"later\n"); |
| 969 | 1× | user.snapshot("t").unwrap(); |
| 970 | 1× | let h = user.commit("after the delegation ended", &[]).unwrap(); |
| 971 | 1× | user.store() |
| 972 | 1× | .put_signature(&h, &agent_sk.sign(Domain::Change, &agent, &h)) |
| 973 | 1× | .unwrap(); |
| 974 | 1× | let status = user.verify_change(&h).unwrap(); |
| 975 | 1× | assert!( |
| 976 | 1× | matches!(&status, SignatureStatus::Invalid(e) if e.contains("expired")), |
| 977 | "{status:?}" | |
| 978 | ); | |
| 979 | // No policy here, so the user is no root: only delegation lets them revoke. | |
| 980 | 1× | let stranger = SecretKey::generate().identity("x", "x@example", 1).hash(); |
| 981 | 1× | assert!(user.revoke_identity(&stranger, "").is_err()); |
| 982 | 1× | assert!(user.revoke_identity(&agent, "session over").is_ok()); |
| 983 | 1× | assert!(user.is_revoked(&agent).unwrap()); |
| 984 | 1× | let r = user.delegate_remove(&agent).unwrap(); |
| 985 | 1× | assert!(user.identity(&r.new).unwrap().delegates.is_empty()); |
| 986 | 1× | } |
| 987 | | |
| 988 | #[test] | |
| 989 | 1× | fn a_host_edit_that_removes_markers_resolves_the_conflict() { |
| 990 | 1× | let dir = tempfile::tempdir().unwrap(); |
| 991 | 1× | let (host, mut user, hub_key, hub_sk, hub_id) = setup(dir.path(), false, true); |
| 992 | 1× | let author = user.config.author_key.unwrap(); |
| 993 | 1× | let base = host.bookmark_heads().unwrap(); |
| 994 | // Two independent edits of the same line: a conflict in the union. | |
| 995 | 1× | let a = host |
| 996 | 1× | .build_change( |
| 997 | 1× | &base, |
| 998 | 1× | &[FileEdit { |
| 999 | 1× | path: "a.txt".into(), |
| 1000 | 1× | content: Some(b"ONE\ntwo\n".to_vec()), |
| 1001 | 1× | }], |
| 1002 | 1× | "A", |
| 1003 | 1× | author, |
| 1004 | ) | |
| 1005 | 1× | .unwrap(); |
| 1006 | 1× | let b = host |
| 1007 | 1× | .build_change( |
| 1008 | 1× | &base, |
| 1009 | 1× | &[FileEdit { |
| 1010 | 1× | path: "a.txt".into(), |
| 1011 | 1× | content: Some(b"uno\ntwo\n".to_vec()), |
| 1012 | 1× | }], |
| 1013 | 1× | "B", |
| 1014 | 1× | author, |
| 1015 | ) | |
| 1016 | 1× | .unwrap(); |
| 1017 | 1× | let (ha, hb) = (a.hash(), b.hash()); |
| 1018 | 1× | let ids = vec![hub_id.clone(), user.identity(&author).unwrap()]; |
| 1019 | 2× | for (c, h) in [(a, ha), (b, hb)] { |
| 1020 | 2× | let sig = hub_sk.sign(Domain::Change, &hub_key, &h); |
| 1021 | 2× | let mut heads = host.bookmark_heads().unwrap(); |
| 1022 | 2× | heads.retain(|x| !base.contains(x)); |
| 1023 | 2× | heads.push(h); |
| 1024 | 2× | let update = host |
| 1025 | 2× | .host_ref_update("main", &heads, Some((&hub_key, &hub_sk))) |
| 1026 | 2× | .unwrap(); |
| 1027 | 2× | let r = host |
| 1028 | 2× | .apply_hosted(Some((c, vec![sig])), &ids, update) |
| 1029 | 2× | .unwrap(); |
| 1030 | 2× | assert!(r.results.iter().all(|x| x.ok), "{:?}", r.results); |
| 1031 | } | |
| 1032 | 1× | let heads = host.bookmark_heads().unwrap(); |
| 1033 | 1× | assert_eq!(heads.len(), 2); |
| 1034 | 1× | let marked = String::from_utf8(host.file_at("a.txt", Some(&heads)).unwrap()).unwrap(); |
| 1035 | 1× | assert!(marked.contains("<<<<<<< oxy conflict"), "{marked}"); |
| 1036 | // Saving with markers left in is refused; saving without them resolves. | |
| 1037 | 1× | assert!(host |
| 1038 | 1× | .build_change( |
| 1039 | 1× | &heads, |
| 1040 | 1× | &[FileEdit { |
| 1041 | 1× | path: "a.txt".into(), |
| 1042 | 1× | content: Some(format!("{marked}x\n").into_bytes()) |
| 1043 | 1× | }], |
| 1044 | 1× | "half", |
| 1045 | 1× | author |
| 1046 | 1× | ) |
| 1047 | 1× | .is_err()); |
| 1048 | 1× | let res = host |
| 1049 | 1× | .build_change( |
| 1050 | 1× | &heads, |
| 1051 | 1× | &[FileEdit { |
| 1052 | 1× | path: "a.txt".into(), |
| 1053 | 1× | content: Some(b"One\ntwo\n".to_vec()), |
| 1054 | 1× | }], |
| 1055 | 1× | "Resolve", |
| 1056 | 1× | author, |
| 1057 | ) | |
| 1058 | 1× | .unwrap(); |
| 1059 | 1× | assert_eq!(res.kind, ChangeKind::Resolution); |
| 1060 | 1× | assert!( |
| 1061 | 1× | res.deps.contains(&ha) && res.deps.contains(&hb), |
| 1062 | "a resolution depends on both sides" | |
| 1063 | ); | |
| 1064 | 1× | let hr = res.hash(); |
| 1065 | 1× | let sig = hub_sk.sign(Domain::Change, &hub_key, &hr); |
| 1066 | 1× | let update = host |
| 1067 | 1× | .host_ref_update("main", &[hr], Some((&hub_key, &hub_sk))) |
| 1068 | 1× | .unwrap(); |
| 1069 | 1× | let r = host |
| 1070 | 1× | .apply_hosted(Some((res, vec![sig])), &ids, update) |
| 1071 | 1× | .unwrap(); |
| 1072 | 1× | assert!(r.results.iter().all(|x| x.ok), "{:?}", r.results); |
| 1073 | 1× | assert_eq!(host.file_at("a.txt", Some(&[hr])).unwrap(), b"One\ntwo\n"); |
| 1074 | 1× | assert!(!host |
| 1075 | 1× | .load_state(&[hr]) |
| 1076 | 1× | .unwrap() |
| 1077 | 1× | .materialize() |
| 1078 | 1× | .is_conflicted()); |
| 1079 | 1× | let _ = &mut user; |
| 1080 | 1× | } |
| 1081 | } |