diff options
| -rw-r--r-- | docs/access.md | 12 | ||||
| -rw-r--r-- | docs/api.md | 20 | ||||
| -rw-r--r-- | docs/openapi.yaml | 9 | ||||
| -rwxr-xr-x | scripts/continuum-signed | 39 | ||||
| -rw-r--r-- | src/access/envelope.rs | 183 | ||||
| -rw-r--r-- | src/access/rules.rs | 5 | ||||
| -rw-r--r-- | src/access/screen.rs | 28 | ||||
| -rw-r--r-- | src/access/sealed.rs | 11 | ||||
| -rw-r--r-- | src/api/store.rs | 37 | ||||
| -rw-r--r-- | src/api/users.rs | 2 | ||||
| -rw-r--r-- | src/document/store.rs | 109 | ||||
| -rw-r--r-- | src/simtest/policy.rs | 20 | ||||
| -rw-r--r-- | src/transport/wire.rs | 2 |
13 files changed, 370 insertions, 107 deletions
diff --git a/docs/access.md b/docs/access.md index c03f7c4..2f7fa5a 100644 --- a/docs/access.md +++ b/docs/access.md @@ -426,9 +426,15 @@ Signed then are what decides who may do what (every user's entry, every entry of `_continuum/access`, every ban), and the paths listed at `_continuum/access/userSigned`, a list of paths as capabilities name them, `*` for any one key: `[["config"], ["servers", "*", "secret"]]`. Each such value is an envelope, one string -(`cv1.<payload>.<signature>`): the payload names the path, the value (`null`: deleted), -the user, when, and the envelope it replaces (`prev`), and the signature is the user's -over it, as `ssh-keygen -Y sign -n continuum-value` makes it, with one of its `keys`. +(`cv2.<value>.<meta>.<signature>`): the value as its user wrote it (`null`: deleted); +apart from it, the meta, which names the swarm, the path, the user, when, the envelope +it replaces (`prev`) and its number (`seq`, one more than that one's, 0 where it +replaces none); and the user's signature over both, as +`ssh-keygen -Y sign -n continuum-value` makes it, with one of its `keys`. Where a place +holds several envelopes, written at once, the one with the highest `seq` is its value +(of equal ones, the one with the lower SHA-256), before anything that isn't one: so an +older envelope put back beside a newer one, by a change made not to see it, changes +nothing, whatever the order of the changes' operations. `scripts/continuum-signed` is a reference client, with `ssh-keygen`, `curl` and `jq`: it logs in by key, writes and deletes signed values (`put config '{"mode": "fast"}'`), and checks one against its user's keys (`verify config`). diff --git a/docs/api.md b/docs/api.md index 2665306..41a4f4d 100644 --- a/docs/api.md +++ b/docs/api.md @@ -164,7 +164,8 @@ way on every node: * **The same key**: one write wins, the same one everywhere. "Newest" means by Automerge's operation order (the write made after seeing more changes; between equals, by the writer's actor ID), not by any clock: of two truly concurrent writes, which wins - is arbitrary, but it is the same on every node. + is arbitrary, but it is the same on every node. Values their users signed come first + (Signed values, below): of those, the one its user numbered highest (`seq`). * **Maps written at the same key** merge into one: their keys are united, each key as above. * **A write and a delete** of the same key: the write stays (a delete removes only what @@ -312,17 +313,20 @@ document. ## Signed values Where the swarm has its users sign values ([`access.md`](access.md), Values signed by -their users), a user writes one by signing it with its SSH key: the payload, one line -of JSON, then the signature, as `application/continuum-signed`. +their users), a user writes one by signing it with its SSH key: the value, one line of +JSON; the meta, one line of JSON; then the signature of those two lines (without the +second's newline), as `application/continuum-signed`. ```sh # The swarm it is for; the envelope there, if any, and what replaces it names: its -# SHA-256 (none: ""). +# SHA-256 (none: "") and the number after its own (none: 0). swarm=$(curl -sf -u alice localhost:3000/auth/whoami | jq -r .swarm) read=$(curl -sf -u alice -H 'Accept: text/plain' 'localhost:3000/store/config?raw=true') prev=${read:+$(printf %s "$read" | sha256sum | cut -d' ' -f1)} -printf '{"swarm":"%s","path":["config"],"value":{"mode":"fast"},"user":"alice","at":%s,"prev":"%s"}' \ - "$swarm" "$(date +%s000)" "$prev" > payload +meta=$(printf %s "$read" | cut -d. -f3 | tr _- /+ | jq -Rr '@base64d') +seq=${meta:+$(( $(printf %s "$meta" | jq .seq) + 1 ))} +printf '{"mode":"fast"}\n{"swarm":"%s","path":["config"],"user":"alice","at":%s,"seq":%s,"prev":"%s"}' \ + "$swarm" "$(date +%s000)" "${seq:-0}" "$prev" > payload ssh-keygen -Y sign -f ~/.ssh/id_ed25519 -n continuum-value payload { cat payload; echo; cat payload.sig; } | curl -X PUT localhost:3000/store/config \ -u alice -H 'Content-Type: application/continuum-signed' --data-binary @- @@ -330,8 +334,8 @@ ssh-keygen -Y sign -f ~/.ssh/id_ed25519 -n continuum-value payload The node writes it as its envelope if the authenticated user signed it (`403` if not), for this swarm and this path, with one of its keys, may write there (`403`), and it replaces what is -there (`prev`, the SHA-256 of the envelope it replaces; `409` if it names another: -read again and sign anew), and only where values are signed (`409` elsewhere). It +there (`prev`, the SHA-256 of the envelope it replaces, and `seq`, one more than that +one's; `409` if it names another: read again and sign anew), and only where values are signed (`409` elsewhere). It answers with the value, as a `PUT` does; a signed `null` deletes, with `204`. It stays as an envelope (reads show nothing there), so the value written next names it in `prev`: the chain goes on through deletions, and an envelope from before one can't be diff --git a/docs/openapi.yaml b/docs/openapi.yaml index 1b4a199..5728da5 100644 --- a/docs/openapi.yaml +++ b/docs/openapi.yaml @@ -110,7 +110,7 @@ paths: - $ref: '#/components/parameters/at' - name: raw in: query - description: Values their users signed as stored, as envelopes (`cv1.<payload>.<signature>`), to check them. + description: Values their users signed as stored, as envelopes (`cv2.<value>.<meta>.<signature>`), to check them. schema: type: boolean default: false @@ -158,8 +158,8 @@ paths: `application/vnd.continuum.timestamp; format=rfc3339|unix|unix-ms|http-date`; a body that isn't one is `400`, saying why. - A value its user signed (`application/continuum-signed`: the payload's line of - JSON, then its armored SSH signature; `api.md`, Signed values) is written as its + A value its user signed (`application/continuum-signed`: the value's line of + JSON, the meta's, then the armored SSH signature of both; `api.md`, Signed values) is written as its envelope where values are signed: by the authenticated user, for this path, with one of its keys, replacing what is there. A signed `null` deletes (`204`). parameters: @@ -192,7 +192,8 @@ paths: schema: type: string example: | - {"swarm":"6f9a…","path":["config"],"value":{"mode":"fast"},"user":"alice","at":1762400000000,"prev":""} + {"mode":"fast"} + {"swarm":"6f9a…","path":["config"],"user":"alice","at":1762400000000,"seq":0,"prev":""} -----BEGIN SSH SIGNATURE----- U1NIU0lH… -----END SSH SIGNATURE----- diff --git a/scripts/continuum-signed b/scripts/continuum-signed index 21eb376..b66949f 100755 --- a/scripts/continuum-signed +++ b/scripts/continuum-signed @@ -94,22 +94,27 @@ raw() { "$url/store/$1?raw=true" || true } -# Writes JSON $2 at $1, signed: replacing the envelope there, if any (`prev`; $3, if -# given, instead of reading it). +# Writes JSON $2 at $1, signed: replacing the envelope there, if any (`prev`), and +# numbered after it (`seq`; $3, if given, instead of reading it). write() { [ -n "$user" ] || die "who signs? set CONTINUUM_USER" if [ $# -ge 3 ]; then current=$3; else current=$(raw "$1"); fi case $current in - cv1.*) prev=$(printf %s "$current" | sha256sum | cut -d' ' -f1) ;; - *) prev= ;; + cv2.*) + prev=$(printf %s "$current" | sha256sum | cut -d' ' -f1) + seq=$(( $(printf %s "$current" | cut -d. -f3 | unb64url | jq -r .seq) + 1 )) + ;; + *) prev= seq=0 ;; esac swarm=$(curl -sf -H "Authorization: Bearer $CONTINUUM_TOKEN" "$url/auth/whoami" | jq -r .swarm) || die "no swarm from $url" - # The exact bytes signed and sent: one line of JSON, no newline. - jq -cnj --arg swarm "$swarm" --arg path "$1" --argjson value "$2" --arg user "$user" \ - --argjson at "$(date +%s)000" --arg prev "$prev" \ - '{swarm: $swarm, path: ($path | split("/")), value: $value, user: $user, at: $at, prev: $prev}' \ - >"$work/payload" || die "not JSON: $2" + # The exact bytes signed and sent: the value's line of JSON, then the meta's, no + # newline after it. + { jq -cn --argjson value "$2" '$value' || die "not JSON: $2" + jq -cnj --arg swarm "$swarm" --arg path "$1" --arg user "$user" \ + --argjson at "$(date +%s)000" --argjson seq "$seq" --arg prev "$prev" \ + '{swarm: $swarm, path: ($path | split("/")), user: $user, at: $at, seq: $seq, prev: $prev}' + } >"$work/payload" sign "$work/payload" continuum-value { cat "$work/payload"; echo; cat "$work/payload.sig"; } | curl -sS --fail-with-body -X PUT "$url/store/$1" \ @@ -157,7 +162,7 @@ open_sealed() { command -v age >/dev/null || die "needs age" stored=$(raw "$1") case $stored in - cv1.*) sealed=$(printf %s "$stored" | cut -d. -f2 | unb64url | jq -r '.value // empty') ;; + cv2.*) sealed=$(printf %s "$stored" | cut -d. -f2 | unb64url | jq -r '. // empty') ;; *) sealed=$stored ;; esac case $sealed in @@ -172,16 +177,18 @@ open_sealed() { verify() { envelope=$(raw "$1") case $envelope in - cv1.*) ;; + cv2.*) ;; *) die "no signed value at $1" ;; esac - printf %s "$envelope" | cut -d. -f2 | unb64url >"$work/payload" - printf %s "$envelope" | cut -d. -f3 | unb64url | base64 | tr -d '\n' | fold -w 70 >"$work/blob" + # What was signed: the value, a newline, the meta. + printf %s "$envelope" | cut -d. -f3 | unb64url >"$work/meta" + { printf %s "$envelope" | cut -d. -f2 | unb64url; echo; cat "$work/meta"; } >"$work/payload" + printf %s "$envelope" | cut -d. -f4 | unb64url | base64 | tr -d '\n' | fold -w 70 >"$work/blob" echo >>"$work/blob" { echo '-----BEGIN SSH SIGNATURE-----'; cat "$work/blob" echo '-----END SSH SIGNATURE-----'; } >"$work/payload.sig" - signer=$(jq -r .user "$work/payload") - signed_for=$(jq -r '.path | join("/")' "$work/payload") + signer=$(jq -r .user "$work/meta") + signed_for=$(jq -r '.path | join("/")' "$work/meta") [ "$signed_for" = "$1" ] || die "signed for $signed_for, not $1" curl -sf -H "Authorization: Bearer $CONTINUUM_TOKEN" "$url/store/_continuum/users/$signer" | jq -r --arg user "$signer" \ @@ -190,7 +197,7 @@ verify() { ssh-keygen -q -Y verify -f "$work/allowed" -I "$signer" -n continuum-value \ -s "$work/payload.sig" <"$work/payload" >/dev/null 2>&1 || die "the signature of $1 doesn't check against $signer's keys" - echo "$1: signed by $signer at $(jq -r .at "$work/payload")" + echo "$1: signed by $signer at $(jq -r .at "$work/meta")" } # The user's own entry with key $1 set to the line in file $2, or removed (no $2), diff --git a/src/access/envelope.rs b/src/access/envelope.rs index 5b1b368..c12fd35 100644 --- a/src/access/envelope.rs +++ b/src/access/envelope.rs @@ -1,17 +1,22 @@ //! Values signed by their user: under a strict policy (`access::policy`), at what //! decides who may do what and at the paths the swarm lists as user-signed //! (`_continuum/access/userSigned`, `listed`; `signed_paths`), the value is a signed envelope, one -//! string, `cv1.<payload>.<signature>` (each base64url, unpadded). The payload is JSON: -//! the `path` it is for (segments), the `value` (`null`: deleted), the `user` who -//! signed it, when (`at`, Unix ms, by the signer's clock), and `prev`: the SHA-256 (hex) -//! of the envelope it replaces, empty if none. The signature is the user's, with one -//! of its keys (`registry::User::keys`), over the payload's bytes, as +//! string, `cv2.<value>.<meta>.<signature>` (each base64url, unpadded). The value is +//! JSON, as its user wrote it (`null`: deleted). What continuum adds is apart, in the +//! meta, JSON too: the `swarm` and the `path` it is for (segments), the `user` who +//! signed it, when (`at`, Unix ms, by the signer's clock), `seq`, and `prev`: the +//! SHA-256 (hex) of the envelope it replaces, empty if none. The signature is the +//! user's, with one of its keys (`registry::User::keys`), over the value's bytes, a +//! newline, and the meta's bytes (neither holds a newline), as //! `ssh-keygen -Y sign -n continuum-value` makes it (`access::sshsig`), its SSHSIG blob. //! //! It travels with the value, through replays and snapshots alike, and anyone can check -//! it (`check`): a node before it writes or applies one, a client reading it. `prev` -//! keeps a member from putting an older signed value back: an envelope only replaces -//! the one it names. A node never writes there but as its user signed it. +//! it (`check`): a node before it writes or applies one, a client reading it. An +//! envelope only replaces the one it names (`prev`), and its `seq` is one more than +//! that one's (0 where it replaces none). Of the envelopes a place holds, written at +//! once, the one with the highest `seq` is the place's value (`document::store`): so a +//! member that puts an older signed value back beside a newer one changes nothing. A +//! node never writes there but as its user signed it. use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD as B64URL}; use serde::{Deserialize, Serialize}; @@ -24,7 +29,7 @@ use crate::document::{ReadDoc, meta, store}; /// What a signature over a payload is for (`sshsig`). pub(crate) const NAMESPACE: &str = "continuum-value"; /// What every envelope starts with. -const PREFIX: &str = "cv1"; +const PREFIX: &str = "cv2"; /// Where the swarm lists the user-signed paths: a list of paths, each a list of /// segments. @@ -97,19 +102,56 @@ pub(crate) fn reaches_signed(signed: &[Vec<String>], path: &[String]) -> bool { signed.iter().any(|pattern| agree(pattern, path)) } -/// A signed value's payload. -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +/// What a signed value says: its value, and its meta. +#[derive(Debug, Clone, PartialEq)] pub(crate) struct Payload { + pub(crate) value: JsonValue, /// The swarm it is for: one user, with the same name and key in two swarms, signs /// for one only. pub(crate) swarm: String, pub(crate) path: Vec<String>, - pub(crate) value: JsonValue, pub(crate) user: String, pub(crate) at: u64, + /// One more than that of the envelope it replaces; 0 where it replaces none. + pub(crate) seq: u64, pub(crate) prev: String, } +/// An envelope's meta, as it is written. +#[derive(Serialize, Deserialize)] +struct Meta { + swarm: String, + path: Vec<String>, + user: String, + at: u64, + seq: u64, + prev: String, +} + +impl Payload { + /// The bytes its user signs (`seal`): the value, a newline, the meta. + #[cfg(test)] + pub(crate) fn signed(&self) -> Vec<u8> { + let meta = Meta { + swarm: self.swarm.clone(), + path: self.path.clone(), + user: self.user.clone(), + at: self.at, + seq: self.seq, + prev: self.prev.clone(), + }; + let mut bytes = serde_json::to_vec(&self.value).expect("a value is JSON"); + bytes.push(b'\n'); + bytes.extend(serde_json::to_vec(&meta).expect("a meta is JSON")); + bytes + } +} + +/// The `seq` of `envelope`, unchecked; None if it isn't one. +pub(crate) fn seq(envelope: &str) -> Option<u64> { + read(envelope).ok().map(|payload| payload.seq) +} + /// An envelope checked (`check`): what it says, and the key that signed it. #[derive(Debug, Clone, PartialEq)] pub(crate) struct Signed { @@ -127,35 +169,72 @@ pub(crate) fn digest(envelope: Option<&str>) -> String { }) } -/// The envelope of `payload` (its exact bytes) signed by `signature` (armored, as -/// `ssh-keygen -Y sign` writes it). -pub(crate) fn seal(payload: &[u8], armored: &str) -> Result<String, String> { +/// The envelope of `signed` (the exact bytes signed: the value's JSON, a newline, the +/// meta's JSON) signed by `signature` (armored, as `ssh-keygen -Y sign` writes it). +pub(crate) fn seal(signed: &[u8], armored: &str) -> Result<String, String> { let blob = sshsig::dearmor(armored)?; + let mut lines = signed.split(|byte| *byte == b'\n'); + let (Some(value), Some(meta), None) = (lines.next(), lines.next(), lines.next()) else { + return Err("its value's line, then its meta's".to_owned()); + }; Ok(format!( - "{PREFIX}.{}.{}", - B64URL.encode(payload), + "{PREFIX}.{}.{}.{}", + B64URL.encode(value), + B64URL.encode(meta), B64URL.encode(blob) )) } -/// What `envelope` holds, unchecked: its payload's bytes, the payload, the signature's +/// What `envelope` holds, unchecked: the bytes signed, what it says, the signature's /// blob. fn open(envelope: &str) -> Result<(Vec<u8>, Payload, Vec<u8>), String> { let mut parts = envelope.split('.'); - let (Some(PREFIX), Some(payload), Some(signature), None) = - (parts.next(), parts.next(), parts.next(), parts.next()) - else { - return Err("not a signed value (cv1.<payload>.<signature>)".to_owned()); + let (Some(PREFIX), Some(value), Some(meta), Some(signature), None) = ( + parts.next(), + parts.next(), + parts.next(), + parts.next(), + parts.next(), + ) else { + return Err("not a signed value (cv2.<value>.<meta>.<signature>)".to_owned()); + }; + let decoded = |part: &str, what: &str| { + let bytes = B64URL + .decode(part) + .map_err(|error| format!("its {what} doesn't decode: {error}"))?; + // One line each: the bytes signed part at the one newline between them. + if bytes.contains(&b'\n') { + return Err(format!("its {what} holds a newline")); + } + Ok(bytes) }; - let bytes = B64URL - .decode(payload) - .map_err(|error| format!("its payload doesn't decode: {error}"))?; - let parsed: Payload = serde_json::from_slice(&bytes) - .map_err(|error| format!("its payload isn't one: {error}"))?; + let mut signed = decoded(value, "value")?; + let value: JsonValue = serde_json::from_slice(&signed) + .map_err(|error| format!("its value isn't JSON: {error}"))?; + let meta = decoded(meta, "meta")?; + let Meta { + swarm, + path, + user, + at, + seq, + prev, + } = serde_json::from_slice(&meta).map_err(|error| format!("its meta isn't one: {error}"))?; + signed.push(b'\n'); + signed.extend(meta); let signature = B64URL .decode(signature) .map_err(|error| format!("its signature doesn't decode: {error}"))?; - Ok((bytes, parsed, signature)) + let payload = Payload { + value, + swarm, + path, + user, + at, + seq, + prev, + }; + Ok((signed, payload, signature)) } /// What `envelope` says, unchecked: for showing a value (`check` before taking it). @@ -165,8 +244,8 @@ pub(crate) fn read(envelope: &str) -> Result<Payload, String> { /// Checks `envelope` as the value at `path`, replacing `previous` (the envelope there /// before, if any), against the user registry `doc` holds (as the writer saw it): for -/// its swarm, that path, naming what it replaces, signed by one of its user's keys, a user who may -/// write there. +/// its swarm, that path, naming what it replaces and numbered after it, signed by one of +/// its user's keys, a user who may write there. pub(crate) fn check( doc: &impl ReadDoc, envelope: &str, @@ -193,6 +272,13 @@ pub(crate) fn check( if payload.prev != digest(previous) { return Err("it doesn't replace the value there (prev)".to_owned()); } + let next = match previous.map(seq) { + None => Some(0), + Some(before) => before.and_then(|before| before.checked_add(1)), + }; + if Some(payload.seq) != next { + return Err("it isn't numbered after the value there (seq)".to_owned()); + } let key = sshsig::verify_blob(&signature, NAMESPACE, &bytes)?; let user = registry::user(doc, &payload.user).ok_or_else(|| format!("no user {}", payload.user))?; @@ -275,17 +361,19 @@ mod tests { } fn signed(key: &SigningKey, payload: &Payload) -> String { - let bytes = serde_json::to_vec(payload).unwrap(); + let bytes = payload.signed(); seal(&bytes, &sshsig::sign(key, NAMESPACE, &bytes)).unwrap() } + /// The first at its place (`prev` empty), or the one after it. fn payload(user: &str, at: &str, value: JsonValue, prev: &str) -> Payload { Payload { + value, swarm: "s".to_owned(), path: path(at), - value, user: user.to_owned(), at: 1, + seq: u64::from(!prev.is_empty()), prev: prev.to_owned(), } } @@ -320,9 +408,36 @@ mod tests { assert!(check(&doc, &second, &path("config"), Some(&first)).is_ok()); assert!(check(&doc, &second, &path("config"), None).is_err()); assert!(check(&doc, &first, &path("config"), Some(&second)).is_err()); - // Tampered with. - let tampered = first.replacen("cv1.", "cv1.e30", 1); + // Numbered after it, and nothing else: not as the first, nor two on. + for seq in [0, 2] { + let misnumbered = signed( + &key, + &Payload { + seq, + ..payload("alice", "config", json!(null), &digest(Some(&first))) + }, + ); + assert!(check(&doc, &misnumbered, &path("config"), Some(&first)).is_err()); + } + let late = signed( + &key, + &Payload { + seq: 1, + ..payload("alice", "config", json!(1), "") + }, + ); + assert!(check(&doc, &late, &path("config"), None).is_err()); + // Tampered with: its value, its meta. + let tampered = first.replacen("cv2.", "cv2.e30", 1); assert!(check(&doc, &tampered, &path("config"), None).is_err()); + let mut parts: Vec<&str> = first.split('.').collect(); + let renumbered = B64URL.encode( + String::from_utf8(B64URL.decode(parts[2]).unwrap()) + .unwrap() + .replace("\"seq\":0", "\"seq\":7"), + ); + parts[2] = &renumbered; + assert!(check(&doc, &parts.join("."), &path("config"), None).is_err()); assert!(check(&doc, "plain", &path("config"), None).is_err()); // For another swarm. let elsewhere = signed( diff --git a/src/access/rules.rs b/src/access/rules.rs index 5c839af..738cdf8 100644 --- a/src/access/rules.rs +++ b/src/access/rules.rs @@ -964,14 +964,15 @@ mod tests { ); let sealed = |at: &str, prev: &str| { let payload = envelope::Payload { + value: json!({"mode": "fast"}), swarm: "s".to_owned(), path: path(at), - value: json!({"mode": "fast"}), user: "alice".to_owned(), at: 1, + seq: u64::from(!prev.is_empty()), prev: prev.to_owned(), }; - let bytes = serde_json::to_vec(&payload).unwrap(); + let bytes = payload.signed(); envelope::seal(&bytes, &sshsig::sign(&key, envelope::NAMESPACE, &bytes)).unwrap() }; let judged = |doc: &Document, ops: Vec<Op>| { diff --git a/src/access/screen.rs b/src/access/screen.rs index b616238..9ca8809 100644 --- a/src/access/screen.rs +++ b/src/access/screen.rs @@ -1064,7 +1064,8 @@ mod tests { } /// Under a strict policy, two signed values written one after the other at the same - /// path, received in one batch: both taken, the second judged with the first in. + /// path, received in one batch: both taken, the second judged with the first in. And + /// the first put back beside the second changes nothing. #[test] fn successive_signed_values_in_one_batch() { use crate::access::{envelope, sshsig}; @@ -1094,18 +1095,20 @@ mod tests { write(&mut theirs, "a", None, "_continuum/members/a", json!({})); let sealed = |prev: &str, value: serde_json::Value| { let payload = envelope::Payload { + value, swarm: "s".to_owned(), path: path("config"), - value, user: "alice".to_owned(), at: 1, + seq: u64::from(!prev.is_empty()), prev: prev.to_owned(), }; - let bytes = serde_json::to_vec(&payload).unwrap(); + let bytes = payload.signed(); envelope::seal(&bytes, &sshsig::sign(&key, envelope::NAMESPACE, &bytes)).unwrap() }; let first = sealed("", json!(1)); let second = sealed(&envelope::digest(Some(&first)), json!(2)); + let before = theirs.get_heads(); write(&mut theirs, "a", Some("alice"), "config", json!(first)); write(&mut theirs, "a", Some("alice"), "config", json!(second)); @@ -1117,6 +1120,25 @@ mod tests { store::read_path(&ours, &path("config")).unwrap(), Some(json!(second)) ); + + // The first put there again, by a change that saw neither: taken, as nothing + // was there at its history, and held beside the second, which stays the value + // (its `seq` is higher), whatever the order of the operations. + let mut side = theirs.fork_at(&before).unwrap(); + write(&mut side, "a", Some("alice"), "config", json!(first)); + theirs.merge(&mut side).unwrap(); + let mut ours = Document::new(); + let screened = screen(&mut ours, received(&theirs, &[]), &mut Waiting::default()); + let why: Vec<&str> = screened.refused.iter().map(|r| r.why.as_str()).collect(); + assert!(why.is_empty(), "{why:?}"); + assert_eq!( + store::read_candidates(&ours, &path("config")).unwrap(), + [json!(second), json!(first)] + ); + assert_eq!( + store::read_path(&ours, &path("config")).unwrap(), + Some(json!(second)) + ); } /// A refused change offered again: refused by its hash, not judged or kept again, diff --git a/src/access/sealed.rs b/src/access/sealed.rs index 7e48e34..545fe76 100644 --- a/src/access/sealed.rs +++ b/src/access/sealed.rs @@ -148,7 +148,7 @@ pub(crate) fn check( let JsonValue::String(text) = value else { return Err("a sealed value is a string (cs1.<age file>)".to_owned()); }; - let sealed = if text.starts_with("cv1.") { + let sealed = if text.starts_with(store::ENVELOPE) { match envelope::read(text)?.value { JsonValue::Null => return Ok(()), JsonValue::String(sealed) => sealed, @@ -340,8 +340,13 @@ mod tests { assert!(check(&doc, notes, &json!("plain")).is_err()); // Signed: holding a sealed value, or null. let signed = |value: JsonValue| { - let payload = json!({"swarm": "s", "path": ["notes"], "value": value, "user": "alice", "at": 1, "prev": ""}); - format!("cv1.{}.c2ln", B64URL.encode(payload.to_string())) + let meta = json!({"swarm": "s", "path": ["notes"], "user": "alice", "at": 1, "seq": 0, "prev": ""}); + format!( + "{}{}.{}.c2ln", + store::ENVELOPE, + B64URL.encode(value.to_string()), + B64URL.encode(meta.to_string()) + ) }; assert!(check(&doc, notes, &json!(signed(json!(sealed_for(&[key(9)]))))).is_ok()); assert!(check(&doc, notes, &json!(signed(JsonValue::Null))).is_ok()); diff --git a/src/api/store.rs b/src/api/store.rs index 00ced48..a7f4686 100644 --- a/src/api/store.rs +++ b/src/api/store.rs @@ -515,15 +515,16 @@ pub(crate) async fn put_store_entry( Ok(with_retyped(with_heads(read.respond()?, &heads), &retyped)) } -/// A signed value's media type (`access::envelope`): its payload's JSON, the exact bytes -/// signed, on the first line; then its user's signature of them, armored, as +/// A signed value's media type (`access::envelope`): its value's JSON on the first +/// line and its meta's on the second, the exact bytes signed (the two lines, without +/// the second's newline); then its user's signature of them, armored, as /// `ssh-keygen -Y sign -n continuum-value` writes it. const SIGNED: &str = "application/continuum-signed"; /// `PUT` of a value its user signed (`SIGNED`), at a path for one /// (`envelope::signed_paths`; `409` elsewhere): taken if the authenticated user signed it, -/// for this path, replacing what is there (`409` if it names another: read again and -/// sign anew), with one of its keys, and may write there (`envelope::check`; `403` if +/// for this path, replacing what is there and numbered after it (`409` if it names +/// another: read again and sign anew), with one of its keys, and may write there (`envelope::check`; `403` if /// not). Written as its envelope, and answered with the value, as `PUT` is. async fn put_signed( state: &AppState, @@ -535,10 +536,10 @@ async fn put_signed( ) -> Result<Response, ApiError> { let bad = |error: String| ApiError(StatusCode::BAD_REQUEST, Some(error)); let text = std::str::from_utf8(body).map_err(|_| bad("not UTF-8".to_owned()))?; - let (payload, armored) = text - .split_once('\n') - .ok_or_else(|| bad("its payload's line, then its signature".to_owned()))?; - let sealed = envelope::seal(payload.as_bytes(), armored).map_err(bad)?; + let lines = || bad("its value's line, its meta's, then its signature".to_owned()); + let (value, rest) = text.split_once('\n').ok_or_else(lines)?; + let (meta, armored) = rest.split_once('\n').ok_or_else(lines)?; + let sealed = envelope::seal(format!("{value}\n{meta}").as_bytes(), armored).map_err(bad)?; let signer = envelope::read(&sealed).map_err(bad)?.user; if signer != user.0 { return Err(ApiError( @@ -565,10 +566,16 @@ async fn put_signed( Some(JsonValue::String(text)) if envelope::read(&text).is_ok() => Some(text), _ => None, }; - if envelope::read(&sealed).map_err(bad)?.prev != envelope::digest(previous.as_deref()) { + let signed = envelope::read(&sealed).map_err(bad)?; + let next = previous + .as_deref() + .map_or(Some(0), |before| Some(envelope::seq(before)? + 1)); + if signed.prev != envelope::digest(previous.as_deref()) || Some(signed.seq) != next { return Err(ApiError( StatusCode::CONFLICT, - Some("it doesn't replace the value there (prev): read it again".to_owned()), + Some( + "it doesn't replace the value there (prev, seq): read it again".to_owned(), + ), )); } envelope::check(&*doc, &sealed, &path, previous.as_deref()) @@ -1331,8 +1338,14 @@ mod tests { fn envelope_of(value: JsonValue) -> String { use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD}; - let payload = json!({"swarm": "s", "path": ["x"], "value": value, "user": "alice", "at": 1, "prev": ""}); - format!("cv1.{}.c2ln", URL_SAFE_NO_PAD.encode(payload.to_string())) + let meta = + json!({"swarm": "s", "path": ["x"], "user": "alice", "at": 1, "seq": 0, "prev": ""}); + format!( + "{}{}.{}.c2ln", + store::ENVELOPE, + URL_SAFE_NO_PAD.encode(value.to_string()), + URL_SAFE_NO_PAD.encode(meta.to_string()) + ) } fn path(text: &str) -> Vec<String> { diff --git a/src/api/users.rs b/src/api/users.rs index fc3f6dc..66b6a8b 100644 --- a/src/api/users.rs +++ b/src/api/users.rs @@ -153,7 +153,7 @@ pub(crate) async fn own_keys( .flatten() .ok_or_else(|| ApiError(StatusCode::NOT_FOUND, Some("no entry of yours".to_owned())))?; let signed = match store::read_path(&replica.doc, &path) { - Ok(Some(JsonValue::String(text))) if text.starts_with("cv1.") => Some(text), + Ok(Some(JsonValue::String(text))) if text.starts_with(store::ENVELOPE) => Some(text), _ => None, }; Ok(Json(json!({ diff --git a/src/document/store.rs b/src/document/store.rs index fb28a4b..aa79e47 100644 --- a/src/document/store.rs +++ b/src/document/store.rs @@ -210,7 +210,33 @@ enum Resolved { Other(Value<'static>, ObjId), } -/// All concurrent values of `key` across a merged set of parent maps, newest first. +/// What every signed value starts with (`access::envelope`). +pub(crate) const ENVELOPE: &str = "cv2."; + +/// Where `value` stands among the values written at once at one place, if it is a +/// signed value (`access::envelope`), unchecked: its `seq`, and its digest. +fn signed_order(value: &Value<'_>) -> Option<(u64, [u8; 32])> { + use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD}; + use sha2::{Digest, Sha256}; + let Value::Scalar(scalar) = value else { + return None; + }; + let ScalarValue::Str(text) = scalar.as_ref() else { + return None; + }; + let meta = text.strip_prefix(ENVELOPE)?.split('.').nth(1)?; + let meta: JsonValue = serde_json::from_slice(&URL_SAFE_NO_PAD.decode(meta).ok()?).ok()?; + Some(( + meta.get("seq")?.as_u64()?, + Sha256::digest(text.as_bytes()).into(), + )) +} + +/// All concurrent values of `key` across a merged set of parent maps, the one that +/// counts first: signed values (`access::envelope`) before any other, the highest +/// `seq` first, of equal ones the lower digest; then the others, newest first. So +/// which signed value a place shows is its user's to say, not the order of operations, +/// which whoever writes a change steers. fn candidates( doc: &impl ReadDoc, parents: &[ObjId], @@ -223,6 +249,13 @@ fn candidates( } } all.sort_by(|a, b| op_order(&b.1).cmp(&op_order(&a.1))); + if all.len() > 1 { + // Stable: the others stay newest first. + all.sort_by_cached_key(|(value, _)| match signed_order(value) { + Some((seq, digest)) => (false, std::cmp::Reverse(seq), digest), + None => (true, std::cmp::Reverse(0), [0; 32]), + }); + } Ok(all) } @@ -469,20 +502,19 @@ pub(crate) fn read_plain( path: &[String], ) -> Result<Option<JsonValue>, StoreError> { Ok(match read_path(doc, path)? { - Some(JsonValue::String(text)) if text.starts_with("cv1.") => { + Some(JsonValue::String(text)) if text.starts_with(ENVELOPE) => { enveloped(&text).filter(|value| !value.is_null()) } other => other, }) } -/// The value an envelope (`cv1.<payload>.<signature>`) holds; None if it isn't one. +/// The value an envelope (`cv2.<value>.<meta>.<signature>`) holds; None if it isn't +/// one. fn enveloped(text: &str) -> Option<JsonValue> { use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD}; - let payload = text.split('.').nth(1)?; - let bytes = URL_SAFE_NO_PAD.decode(payload).ok()?; - let mut payload: JsonValue = serde_json::from_slice(&bytes).ok()?; - Some(payload.get_mut("value")?.take()) + let value = text.split('.').nth(1)?; + serde_json::from_slice(&URL_SAFE_NO_PAD.decode(value).ok()?).ok() } /// Reads a path; the empty path is the whole store. @@ -710,7 +742,8 @@ pub(crate) fn keys_at( }) } -/// Every concurrent value at `path`, newest first; empty if there is none. Where +/// Every concurrent value at `path`, the one read first (`candidates`); empty if there +/// is none. Where /// `read_path` picks one (or merges concurrent maps into one), this keeps them apart. pub(crate) fn read_candidates( doc: &impl ReadDoc, @@ -1704,20 +1737,66 @@ mod tests { at.split('/').map(str::to_owned).collect() } + /// An envelope holding `value`, numbered `seq`, its signature junk. + fn envelope_of(value: &JsonValue, seq: u64) -> String { + use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD}; + let meta = + json!({"swarm": "s", "path": ["a"], "user": "alice", "at": 1, "seq": seq, "prev": ""}); + format!( + "{ENVELOPE}{}.{}.c2ln", + URL_SAFE_NO_PAD.encode(value.to_string()), + URL_SAFE_NO_PAD.encode(meta.to_string()) + ) + } + + /// Of signed values written at once at one place, the highest `seq` is read, + /// whichever was written last, and before anything unsigned; a write replaces them + /// all. + #[test] + fn the_highest_seq_is_read() { + let doc = |actor: &[u8]| crate::document::Document::new().with_actor(ActorId::from(actor)); + let (mut ours, mut theirs, mut third) = (doc(b"a"), doc(b"b"), doc(b"c")); + // Ours: the older one, written after many operations, so that it is the newest. + try_change(&mut ours, |doc| { + for round in 0..20 { + write_path(doc, &path("padding"), &json!(round))?; + } + write_path(doc, &path("a"), &json!(envelope_of(&json!("old"), 0))) + }) + .unwrap(); + try_change(&mut theirs, |doc| { + write_path(doc, &path("a"), &json!(envelope_of(&json!("new"), 1))) + }) + .unwrap(); + try_change(&mut third, |doc| { + for round in 0..40 { + write_path(doc, &path("more"), &json!(round))?; + } + write_path(doc, &path("a"), &json!("unsigned")) + }) + .unwrap(); + ours.merge(&mut theirs).unwrap(); + assert_eq!(read_plain(&ours, &path("a")).unwrap(), Some(json!("new"))); + ours.merge(&mut third).unwrap(); + assert_eq!(read_plain(&ours, &path("a")).unwrap(), Some(json!("new"))); + try_change(&mut ours, |doc| { + write_path(doc, &path("a"), &json!("after")) + }) + .unwrap(); + assert_eq!(read_candidates(&ours, &path("a")).unwrap().len(), 1); + assert_eq!(read_plain(&ours, &path("a")).unwrap(), Some(json!("after"))); + } + /// An envelope reads as the value it holds, a signed `null` as nothing, anything /// else as it is. #[test] fn plain_reads_open_envelopes() { - use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD}; - let sealed = |value: JsonValue| { - let payload = json!({"swarm": "s", "path": ["a"], "value": value, "user": "alice", "at": 1, "prev": ""}); - format!("cv1.{}.c2ln", URL_SAFE_NO_PAD.encode(payload.to_string())) - }; + let sealed = |value: JsonValue| envelope_of(&value, 0); let mut doc = crate::document::Document::new(); try_change(&mut doc, |doc| { write_path(doc, &path("a"), &json!(sealed(json!({"mode": "fast"}))))?; write_path(doc, &path("gone"), &json!(sealed(JsonValue::Null)))?; - write_path(doc, &path("plain"), &json!("cv1-ish")) + write_path(doc, &path("plain"), &json!("cv2-ish")) }) .unwrap(); assert_eq!( @@ -1727,7 +1806,7 @@ mod tests { assert_eq!(read_plain(&doc, &path("gone")).unwrap(), None); assert_eq!( read_plain(&doc, &path("plain")).unwrap(), - Some(json!("cv1-ish")) + Some(json!("cv2-ish")) ); assert_eq!(read_plain(&doc, &path("none")).unwrap(), None); } diff --git a/src/simtest/policy.rs b/src/simtest/policy.rs index 4162a8c..4182b11 100644 --- a/src/simtest/policy.rs +++ b/src/simtest/policy.rs @@ -214,7 +214,9 @@ async fn write_signed( use crate::document::typed::{TimestampFormat, Val}; let at = path(at); let previous = match store::read_path(&node.doc.read().await.doc, &at) { - Ok(Some(serde_json::Value::String(text))) if text.starts_with("cv1.") => Some(text), + Ok(Some(serde_json::Value::String(text))) if text.starts_with(store::ENVELOPE) => { + Some(text) + } _ => None, }; let swarm = store::read_path(&node.doc.read().await.doc, &path("_continuum/swarm")) @@ -223,14 +225,18 @@ async fn write_signed( .and_then(|swarm| swarm.as_str().map(str::to_owned)) .unwrap_or_default(); let payload = envelope::Payload { + value, swarm, path: at.clone(), - value, user: user.to_owned(), at: 1, + seq: previous + .as_deref() + .and_then(envelope::seq) + .map_or(0, |seq| seq + 1), prev: envelope::digest(previous.as_deref()), }; - let bytes = serde_json::to_vec(&payload).unwrap(); + let bytes = payload.signed(); let sealed = envelope::seal(&bytes, &sshsig::sign(key, envelope::NAMESPACE, &bytes)).unwrap(); let sign = Sign::Write { path: at.clone(), @@ -371,14 +377,18 @@ async fn a_strict_swarm() { .flatten() .and_then(|value| value.as_str().map(str::to_owned)); let payload = envelope::Payload { + value: json!({"mode": "elsewhere"}), swarm: "elsewhere".to_owned(), path: path("config"), - value: json!({"mode": "elsewhere"}), user: "alice".to_owned(), at: 2, + seq: previous + .as_deref() + .and_then(envelope::seq) + .map_or(0, |seq| seq + 1), prev: envelope::digest(previous.as_deref()), }; - let bytes = serde_json::to_vec(&payload).unwrap(); + let bytes = payload.signed(); envelope::seal(&bytes, &sshsig::sign(&key, envelope::NAMESPACE, &bytes)).unwrap() }; forge(&c, "alice", "config", json!(elsewhere)).await; diff --git a/src/transport/wire.rs b/src/transport/wire.rs index c0db2e2..97bf6cc 100644 --- a/src/transport/wire.rs +++ b/src/transport/wire.rs @@ -14,7 +14,7 @@ use super::{EncodedMessage, MessageKind, ParsedMessage}; /// The wire format's version, the first byte of every payload. Bump it with every change /// to a message type or a codec (`golden_bytes` fails then). -pub(crate) const VERSION: u8 = 50; +pub(crate) const VERSION: u8 = 51; /// What the second byte of a payload says. fn tag(kind: MessageKind) -> u8 { |
