aboutsummaryrefslogtreecommitdiffstats
path: root/src/api/store.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/store.rs')
-rw-r--r--src/api/store.rs37
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> {