diff options
Diffstat (limited to 'src/access')
| -rw-r--r-- | src/access/screen.rs | 76 |
1 files changed, 57 insertions, 19 deletions
diff --git a/src/access/screen.rs b/src/access/screen.rs index 92cc360..cb088d5 100644 --- a/src/access/screen.rs +++ b/src/access/screen.rs @@ -721,17 +721,32 @@ pub(crate) fn apply_bytes( } let mut changes = crate::document::changes::changes_in(bytes) .ok_or_else(|| anyhow::anyhow!("Refused changes that don't load"))?; - // A change of the actor this node writes as, and not one it made: another node writes - // as it too (two started from one document file with `--keep-actor`, a copied data - // directory). Each would make changes the other's peers refuse as forged; the node - // writes on as a fresh actor once these are judged. They are judged as any other, - // under the actor still: one at a seq the node made a change at is forged - // (`Signatures::forks`). + // A change of the actor this node writes as, not one it made, and signed with this + // node's key: another node writes as it too (two started from one document file with + // `--keep-actor`, a copied data directory, which hold the key). Each would make + // changes the other's peers refuse as forged; the node writes on as a fresh actor + // once these are judged. They are judged as any other, under the actor still: one at + // a seq the node made a change at is forged (`Signatures::forks`). One nobody signed + // with this node's key is anyone's forgery, and switches nothing: else any peer + // would make the node start writers at will. + let own_key = doc + .get_author_for_actor(doc.get_actor()) + .and_then(|author| { + std::str::from_utf8(author.as_bytes()) + .ok() + .map(str::to_owned) + }) + .and_then(|author| crate::identity::public_of(&author)); let twin = changes .iter() .find(|change| { change.actor_id() == doc.get_actor() && !crate::document::changes::holds(&*doc, &change.hash()) + && own_key.is_some_and(|key| { + signatures + .as_deref() + .is_some_and(|signatures| signatures.signed_by(&change.hash(), &key)) + }) }) .map(Change::seq); let switch = |doc: &mut Document| { @@ -1146,13 +1161,31 @@ mod tests { /// A change of the node's own actor that it didn't make: refused as forged where the /// node made one at its seq, and it writes on as a fresh actor. Its own changes /// coming back change nothing. + /// A change of this node's actor it didn't make, signed with its key: another node + /// writes as it (its files copied), and it switches to a fresh actor. Unsigned, it is + /// anyone's forgery, and switches nothing. #[test] fn another_writing_as_this_node() { - let mut theirs = genesis(); + let key = crate::identity::NodeKey::from_seed([5; 32]); + let node = key.id(); + let mut theirs = Document::new(); + transact( + &mut theirs, + UnixMillis(1), + &Writer { + node: &node, + user: None, + }, + |doc| store::write_path(doc, &path("_continuum/swarm"), &json!("s")), + ) + .unwrap(); let mut ours = theirs.fork().with_actor(theirs.get_actor().clone()); let screening = std::sync::Mutex::new(Screening::default()); let mut signatures = Signatures::default(); - let mut take = |doc: &mut Document, from: &Document, since: &[ChangeHash]| { + let take = |doc: &mut Document, + signatures: &mut Signatures, + from: &Document, + since: &[ChangeHash]| { let bytes: Vec<u8> = received(from, since) .iter() .flat_map(|change| change.raw_bytes().to_vec()) @@ -1163,27 +1196,32 @@ mod tests { 0, &bytes, &screening, - Some(&mut signatures), + Some(signatures), &mut crate::document::PatchLog::inactive(), ) .unwrap() }; let heads = ours.get_heads(); - write(&mut ours, "a", None, "_continuum/members/a/alive", json!(1)); + write(&mut ours, &node, None, "notes/ours", json!(1)); let echo = ours.clone(); - assert!(take(&mut ours, &echo, &heads).fresh_actor.is_none()); + assert!( + take(&mut ours, &mut signatures, &echo, &heads) + .fresh_actor + .is_none() + ); let actor = ours.get_actor().clone(); let before = theirs.get_heads(); - write( - &mut theirs, - "a", - None, - "_continuum/members/a/alive", - json!(2), - ); - let outcome = take(&mut ours, &theirs, &before); + write(&mut theirs, &node, None, "notes/theirs", json!(2)); + let twin = theirs.get_last_local_change().unwrap().hash(); + // Nobody signed it with this node's key: forged, and nothing switches. + let outcome = take(&mut ours, &mut signatures, &theirs, &before); assert_eq!(outcome.forged, 1); + assert!(outcome.fresh_actor.is_none()); + assert_eq!(ours.get_actor(), &actor); + // Signed with it: the node's files were copied. + signatures.sign(&theirs, twin, &key); + let outcome = take(&mut ours, &mut signatures, &theirs, &before); assert_eq!(outcome.fresh_actor.as_ref(), Some(ours.get_actor())); assert_ne!(ours.get_actor(), &actor); } |
