aboutsummaryrefslogtreecommitdiffstats
path: root/src/document/testing.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-12 22:23:15 +0000
committertv <tv@krebsco.de>2026-11-12 22:23:15 +0000
commite519ade32a87811f287bb851e3e1dfcde6b064c9 (patch)
tree9fff94327105957f3a958d55a668cc3a5cf6bf9a /src/document/testing.rs
parentbfe93eea9b707b29b78e1f9a7a425e6f891701a0 (diff)
screen: refuse changes Automerge wouldn't have written
Automerge 0.12.0 applies a change that lists an actor none of its operations names, or whose first operation is numbered below its dependencies' or near 2^32. It then gives back another change than the one it was given and saves a document that doesn't load, or panics at the next write. A member sending one stops the nodes that take it. Every change from elsewhere is looked at before Automerge sees it (`document::changes::Written`, from `access::screen::apply`, with or without a policy), and refused like one breaking a rule unless: * encoded again, it has the hash it came with; * its first operation is numbered one after the highest of its dependencies, 1 without any, as a transaction at the document's heads numbers it; * every operation it names (an object, a list's element, an operation replaced) is its own, or was made before it, in the document or by a change of the batch looked at before.
Diffstat (limited to 'src/document/testing.rs')
-rw-r--r--src/document/testing.rs64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/document/testing.rs b/src/document/testing.rs
index cd6c15b..1e83db2 100644
--- a/src/document/testing.rs
+++ b/src/document/testing.rs
@@ -74,3 +74,67 @@ pub(crate) fn with_author_footer(raw: &[u8], author: &[u8]) -> Vec<u8> {
out.extend(body);
out
}
+
+/// `raw` (an uncompressed change chunk whose operations name no actor but its own)
+/// listing `spare` among its other actors, its checksum made right again: bytes that
+/// load as a change and aren't the ones Automerge encodes for it (`changes::Written`).
+pub(crate) fn with_spare_actor(raw: &[u8], spare: &[u8]) -> Vec<u8> {
+ use sha2::{Digest, Sha256};
+ fn leb(mut n: usize, out: &mut Vec<u8>) {
+ loop {
+ let byte = (n & 0x7f) as u8;
+ n >>= 7;
+ if n == 0 {
+ out.push(byte);
+ return;
+ }
+ out.push(byte | 0x80);
+ }
+ }
+ // A number (LEB128) at `at`, and how many bytes it takes.
+ fn read(bytes: &[u8], at: usize) -> (usize, usize) {
+ let (mut n, mut read, mut shift) = (0usize, 0, 0);
+ loop {
+ let byte = bytes[at + read];
+ n |= usize::from(byte & 0x7f) << shift;
+ read += 1;
+ if byte & 0x80 == 0 {
+ return (n, read);
+ }
+ shift += 7;
+ }
+ }
+ assert_eq!(raw[8], 1, "an uncompressed change chunk");
+ let (length, taken) = read(raw, 9);
+ let body = &raw[9 + taken..9 + taken + length];
+ // Its dependencies, its actor, its seq and first operation, its time, its message:
+ // then the other actors.
+ let (deps, taken) = read(body, 0);
+ let mut at = taken + 32 * deps;
+ let (actor, taken) = read(body, at);
+ at += taken + actor;
+ for _ in 0..3 {
+ at += read(body, at).1;
+ }
+ let (message, taken) = read(body, at);
+ at += taken + message;
+ let (others, taken) = read(body, at);
+ assert_eq!(others, 0, "a change naming no other actor");
+ let mut crafted = body[..at].to_vec();
+ crafted.push(1);
+ leb(spare.len(), &mut crafted);
+ crafted.extend_from_slice(spare);
+ crafted.extend_from_slice(&body[at + taken..]);
+ let mut header = Vec::new();
+ leb(crafted.len(), &mut header);
+ let mut summed = vec![1u8];
+ summed.extend(&header);
+ summed.extend(&crafted);
+ let sum = Sha256::digest(&summed);
+ let mut out = raw[..4].to_vec();
+ out.extend(&sum[..4]);
+ out.push(1);
+ out.extend(header);
+ out.extend(crafted);
+ out
+}