aboutsummaryrefslogtreecommitdiffstats
path: root/src/access
diff options
context:
space:
mode:
Diffstat (limited to 'src/access')
-rw-r--r--src/access/envelope.rs183
-rw-r--r--src/access/rules.rs5
-rw-r--r--src/access/screen.rs28
-rw-r--r--src/access/sealed.rs11
4 files changed, 185 insertions, 42 deletions
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());