aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-13 03:16:33 +0000
committertv <tv@krebsco.de>2026-11-13 03:16:33 +0000
commit47d88d224c5c8fa42b4f30b8ec37c7e5deb83ea2 (patch)
treebecf56fd584d9ed37fbce0a4708b1dd17e26debd /src
parent6cb4d3fd8f5024c340bd9321a434c8579cce81e7 (diff)
screen: a twin of this node's actor switches it only signed with its key
A node switched to a fresh actor whenever a peer showed it a change of its own actor that it didn't hold, signed or not. So any peer made a node start a writer at will, by sending a copy of its last change with another value: and every writer costs every node memory in proportion to the document's history. Copies of a node's data directory hold its key, and sign their changes with it; a stranger can't. A twin switches the node only where a signature by the node's own key of it is kept or waiting. Unsigned, it is refused as forged as before, and nothing switches.
Diffstat (limited to 'src')
-rw-r--r--src/access/screen.rs76
-rw-r--r--src/document/signatures.rs11
2 files changed, 68 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);
}
diff --git a/src/document/signatures.rs b/src/document/signatures.rs
index 79c8569..f9dd94b 100644
--- a/src/document/signatures.rs
+++ b/src/document/signatures.rs
@@ -594,6 +594,17 @@ impl Signatures {
signed
}
+ /// Whether a signature of `hash` by the node whose public key is `key` is kept or
+ /// waiting for its change (`pending`): checked here, the change not needed.
+ pub(crate) fn signed_by(&self, hash: &ChangeHash, key: &[u8; 32]) -> bool {
+ let message = message(hash);
+ self.by_change
+ .get(hash)
+ .into_iter()
+ .chain(self.pending.by_change.get(hash).into_iter().flatten())
+ .any(|signature| crate::identity::verify(key, &message, signature))
+ }
+
/// Whether a valid signature of `hash` is kept: it vouches for its change and the
/// changes of its actor it builds on.
pub(crate) fn signed(&self, hash: &ChangeHash) -> bool {