diff options
| author | tv <tv@krebsco.de> | 2026-11-15 12:28:33 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-15 12:28:33 +0000 |
| commit | 6be5646df342b21fe82c63f59cfec5a369ab54f8 (patch) | |
| tree | 48ede9923ab4fa762623a6e32b9b18a088276b98 /src/api/store.rs | |
| parent | d1d081cf8e0e8bb5809c1c82bbdfec6bd7ad0285 (diff) | |
signed values: value and meta apart, numbered; the highest seq is read
A member could put a user's older signed value back beside a newer
one: a change made with dependencies from before both is judged where
the place was empty, so the older envelope (`prev` empty) is taken;
and of values written at once, the one read was the one with the
highest operation, which whoever writes the change steers. A revoked
capability, a lifted ban or an earlier policy came back that way,
signed by whoever signed it then.
An envelope is now `cv2.<value>.<meta>.<signature>`: the value as its
user wrote it, and apart from it what continuum adds (swarm, path,
user, at, seq, prev), the signature over the value's line and the
meta's. `seq` is one more than that of the envelope it replaces, 0
where it replaces none; every node checks it with `prev`.
Of the values a place holds, written at once, signed ones are read
before any other, the highest `seq` first, of equal ones the lower
digest (`store::candidates`): reads, snapshots and writes all go by
it. An older envelope beside a newer one changes nothing.
`application/continuum-signed` is the value's line, the meta's, then
the signature; `scripts/continuum-signed` writes and checks it so.
The wire version goes up: nodes must agree on what a place's value is.
Diffstat (limited to 'src/api/store.rs')
| -rw-r--r-- | src/api/store.rs | 37 |
1 files changed, 25 insertions, 12 deletions
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> { |
