~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
610 lines · 23.0 KB Raw History
//! Accounts moving between servers, as `spec/MIGRATION.md` says: the
//! service token `createAccount` takes from an account's current server
//! (`service_caller`), `importRepo` and `listMissingBlobs` for an account
//! moving here, `submitPlcOperation`, which points its DID here, and
//! `requestPlcOperationSignature` and `signPlcOperation` for one moving away.
//!
//! An import is one commit of the account's log, as a write is: the tree
//! the CAR brings, checked whole and built again from its records, becomes
//! the repository's by the puts and deletes that make it so, under a new
//! commit signed with the account's key. Its blobs it does not hold are
//! missing until they are uploaded (`blobs::Blobs::missing`).

use crate::email::{self, Purpose};
use crate::events;
use crate::sessions::{self, Bearer, Token};
use crate::space_credentials::jwt::Key;
use crate::{
    Call, Handler, Resp, Service, Shared, Shown, XrpcError, durable, err, internal, invalid,
    invalid_token, opt, read, write,
};
use base64::Engine;
use base64::engine::{DecodePaddingMode, GeneralPurpose, GeneralPurposeConfig};
use pds_repo::data::{V, decode, valid_did, valid_nsid, valid_rkey};
use pds_repo::{Op, Record, make_record};
use serde_json::{Map, Value as Json, json};
use std::collections::{BTreeMap, HashMap, HashSet};

/// `importRepo`'s NSID, whose body `limits::read_limit` reads up to
/// `PDS_MAX_REPO_IMPORT_SIZE`.
pub(crate) const IMPORT_REPO: &str = "com.atproto.repo.importRepo";

/// The method a service token for `createAccount` must name.
const CREATE_ACCOUNT: &str = "com.atproto.server.createAccount";

pub(crate) fn handler(nsid: &str) -> Option<Handler> {
    Some(match nsid {
        IMPORT_REPO => Handler::Call(|s, c, _| Box::pin(import_repo(s, c))),
        "com.atproto.repo.listMissingBlobs" => Handler::Query(list_missing_blobs),
        "com.atproto.identity.submitPlcOperation" => {
            Handler::Authed(sessions::access_token, |s, t, b| {
                Box::pin(submit_plc_operation(s, t, b))
            })
        }
        "com.atproto.identity.requestPlcOperationSignature" => {
            Handler::Authed(sessions::deactivating_token, |s, t, _| {
                Box::pin(request_signature(s, t))
            })
        }
        "com.atproto.identity.signPlcOperation" => {
            Handler::Authed(sessions::deactivating_token, |s, t, b| {
                Box::pin(sign_plc_operation(s, t, b))
            })
        }
        _ => return None,
    })
}

// ---- createAccount's service token -------------------------------------------

fn auth_required<T>() -> Result<T, XrpcError> {
    err(401, "AuthenticationRequired", "Authentication Required")
}

/// base64url, with or without padding, as a JWT's parts are read.
const B64: GeneralPurpose = GeneralPurpose::new(
    &base64::alphabet::URL_SAFE,
    GeneralPurposeConfig::new()
        .with_decode_padding_mode(DecodePaddingMode::Indifferent)
        .with_decode_allow_trailing_bits(true),
);

/// Whether a JSON value is one JavaScript takes as true.
fn truthy(v: &Json) -> bool {
    match v {
        Json::Null => false,
        Json::Bool(b) => *b,
        Json::Number(n) => n.as_f64().is_some_and(|f| f != 0.0),
        Json::String(s) => !s.is_empty(),
        _ => true,
    }
}

/// Whether the call's `Authorization` header is two words, one space
/// between them: one that is not is `400 InvalidToken`, as any header's
/// form (`spec/SESSIONS.md`).
fn two_words(call: &Call) -> bool {
    let h = call
        .headers
        .get(axum::http::header::AUTHORIZATION)
        .and_then(|v| v.to_str().ok())
        .unwrap_or_default();
    let words: Vec<&str> = h.split(' ').collect();
    words.len() == 2 && words.iter().all(|w| !w.is_empty())
}

/// The caller `createAccount`'s `Authorization` names, checked before its
/// input as `spec/MIGRATION.md` says: none without a header, or the `iss`
/// of a service token that passes, for `createAccount`, to this server.
pub(crate) async fn service_caller(
    s: &Service,
    call: &Call<'_>,
) -> Result<Option<String>, XrpcError> {
    let token = match call.bearer {
        Bearer::None => return Ok(None),
        Bearer::Token(t) => t,
        Bearer::Malformed if !two_words(call) => return invalid_token(),
        Bearer::Malformed | Bearer::Basic(_) | Bearer::DPoP(_) | Bearer::OAuth(_) => {
            return auth_required();
        }
    };
    let bad = |name: &'static str, why: &str| err(401, name, why);
    let parts: Vec<&str> = token.split('.').collect();
    let [h, p, sig] = parts[..] else {
        return bad("BadJwt", "Poorly formatted jwt");
    };
    let part = |x: &str| -> Option<Json> { serde_json::from_slice(&B64.decode(x).ok()?).ok() };
    let (Some(header), Some(payload)) = (part(h), part(p)) else {
        return internal();
    };
    let Some(alg) = header.get("alg").and_then(Json::as_str) else {
        return bad("BadJwt", "Jwt has no alg");
    };
    if header
        .get("typ")
        .and_then(Json::as_str)
        .is_some_and(|t| ["at+jwt", "refresh+jwt", "dpop+jwt"].contains(&t))
    {
        return bad("BadJwtType", "Invalid jwt type");
    }
    let text = |k: &str| payload.get(k).and_then(Json::as_str);
    let exp = payload
        .get("exp")
        .filter(|e| e.is_number())
        .and_then(Json::as_f64);
    let lxm = payload.get("lxm").filter(|l| truthy(l));
    let (Some(iss), Some(aud), Some(exp), true) = (
        text("iss"),
        text("aud"),
        exp,
        payload.is_object() && lxm.is_none_or(Json::is_string),
    ) else {
        return bad("BadJwt", "Poorly formatted jwt payload");
    };
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs_f64();
    if now > exp {
        return bad("JwtExpired", "jwt expired");
    }
    if lxm.and_then(Json::as_str) != Some(CREATE_ACCOUNT) {
        return bad(
            "BadJwtLexiconMethod",
            "missing jwt lexicon method (\"lxm\")",
        );
    }
    let (did, fragment) = match iss.split_once('#') {
        None => (iss, None),
        Some((d, f)) if !f.is_empty() && !f.contains('#') => (d, Some(f)),
        Some(_) => return bad("BadJwtIss", "Invalid jwt issuer"),
    };
    if !valid_did(did) {
        return bad("BadJwtIss", "Invalid jwt issuer");
    }
    let id = match fragment {
        Some("atproto_labeler") => "atproto_label",
        _ => "atproto",
    };
    let key = |doc: &Json| Key::of_document_named(doc, did, id);
    // Read as an account's is, held for `PDS_DID_CACHE_SECS`: one that cannot
    // be read, or names no key, is `401`; but a `did:web` whose host does not
    // answer, or answers what is no document of it, `500`.
    let life = std::time::Duration::from_secs(s.config.did_cache);
    let web = crate::identity::is_web(did) && crate::outbound::did_web_url(did, false).is_some();
    let found = |doc: Result<std::sync::Arc<Json>, XrpcError>| match doc {
        Ok(d) => Ok(key(&d)),
        Err(e) if web && e.status == 500 => Err(e),
        Err(_) => Ok(None),
    };
    let Some(k) = found(crate::proxy::resolve_did_within(s, did, life).await)? else {
        return auth_required();
    };
    let signed = format!("{h}.{p}");
    let signature = B64.decode(sig).unwrap_or_default();
    if !k.verifies(alg, signed.as_bytes(), &signature, true) {
        // A key rotated a moment ago: the document as the directory serves it now.
        crate::proxy::forget_did(s, did);
        let fresh = crate::proxy::resolve_did_within(s, did, life)
            .await
            .ok()
            .and_then(|d| key(&d));
        if !fresh.is_some_and(|k| k.verifies(alg, signed.as_bytes(), &signature, true)) {
            return bad("BadJwtSignature", "jwt signature does not match jwt issuer");
        }
    }
    if aud != format!("did:web:{}", s.config.hostname) {
        return bad("BadJwtAudience", "jwt audience does not match service did");
    }
    Ok(Some(iss.to_string()))
}

// ---- importRepo ---------------------------------------------------------------

/// A CAR file that cannot be read as a repository whole, as the reference answers it.
fn not_a_repo<T>(why: &str) -> Result<T, XrpcError> {
    crate::ops::failed("importRepo refused a CAR file", why);
    internal()
}

/// A repository read from a CAR file: its tree's root and its records, by path.
struct Imported {
    root: Vec<u8>,
    records: BTreeMap<String, Record>,
}

/// Read a CAR file as `did`'s repository, as `spec/MIGRATION.md` checks it:
/// `500` for a body that is no CAR, a root not in it or no commit, and a
/// block the commit does not reach; `400 InvalidRequest` for more than one
/// root, a commit of another DID, a tree not whole, an MST node reached
/// twice, a record that does not decode, and a tree not the one its records
/// make.
fn read_import(car: &[u8], did: &str) -> Result<Imported, XrpcError> {
    let Ok((roots, blocks)) = pds_repo::read_car(car) else {
        return not_a_repo("not a CAR file");
    };
    let root = match roots.as_slice() {
        [] => return not_a_repo("a CAR file with no root"),
        [root] => root,
        _ => return invalid("expected one root"),
    };
    let blocks: HashMap<Vec<u8>, Vec<u8>> = blocks.into_iter().collect();
    let Some(head) = blocks.get(root) else {
        return not_a_repo("a root not in the CAR file");
    };
    let commit = match decode(head) {
        Ok(V::Map(m)) => m,
        _ => return not_a_repo("a root that is no commit"),
    };
    let field = |k: &str| commit.iter().find(|(n, _)| n == k).map(|(_, v)| v);
    let (Some(V::Str(of)), Some(V::Link(data)), Some(V::Str(_)), Some(V::Int(2 | 3))) =
        (field("did"), field("data"), field("rev"), field("version"))
    else {
        return not_a_repo("a root that is no commit");
    };
    if of != did {
        return invalid(format!("A repository of another DID: {of}"));
    }
    let (entries, nodes) = pds_repo::mst::walk(data, |c| blocks.get(c).map(Vec::as_slice))
        .or_else(|why| invalid(format!("A tree that is not whole: {why}")))?;
    let mut records = BTreeMap::new();
    for (key, cid) in &entries {
        let path = std::str::from_utf8(key)
            .ok()
            .filter(|p| {
                p.split_once('/')
                    .is_some_and(|(c, r)| valid_nsid(c) && valid_rkey(r))
            })
            .ok_or_else(|| XrpcError {
                status: 400,
                name: "InvalidRequest",
                message: "A tree key that is no record's path".into(),
            })?;
        let Some(bytes) = blocks.get(cid) else {
            return invalid(format!("A record missing from the CAR file: {path}"));
        };
        let record = match decode(bytes) {
            Ok(v @ V::Map(_)) => make_record(v),
            _ => return invalid(format!("A record that does not decode: {path}")),
        };
        if record.cid != *cid {
            return invalid(format!("A record not in its canonical encoding: {path}"));
        }
        records.insert(path.to_string(), record);
    }
    let reached: HashSet<&[u8]> = std::iter::once(root.as_slice())
        .chain(nodes.iter().map(Vec::as_slice))
        .chain(entries.iter().map(|(_, c)| c.as_slice()))
        .collect();
    if blocks.keys().any(|c| !reached.contains(c.as_slice())) {
        return not_a_repo("a block the commit does not reach");
    }
    if pds_repo::mst::build_tree(&entries).cid != *data {
        return invalid("A tree that is not the one its records make");
    }
    Ok(Imported {
        root: data.clone(),
        records,
    })
}

/// Whether a request's `Content-Type` is `application/vnd.ipld.car`.
fn is_car(call: &Call) -> bool {
    call.headers
        .get(axum::http::header::CONTENT_TYPE)
        .and_then(|v| v.to_str().ok())
        .map(|t| t.split(';').next().unwrap_or_default().trim())
        .is_some_and(|t| t.eq_ignore_ascii_case("application/vnd.ipld.car"))
}

/// `importRepo` checks, in this order, its token (a session of the
/// account's password), what an OAuth scope needs, its account's state, its
/// `Content-Type` and the size of its body, then pays its rate limits and
/// reads the CAR file. The account's repository then becomes its tree, in
/// one commit, answered once it is on disk.
async fn import_repo(s: &Shared, call: &Call<'_>) -> Result<Resp, XrpcError> {
    let t = sessions::access_token(s, call.bearer)?;
    if !t.oauth && t.scope != sessions::ACCESS {
        return invalid_token();
    }
    crate::scopes::admit(call)?;
    crate::lifecycle::gate(s, call.nsid, &t.did)?;
    if !is_car(call) {
        return invalid("Wrong request encoding (Content-Type): expected application/vnd.ipld.car");
    }
    if call.body.len() as u64 > s.config.max_import {
        return err(413, "PayloadTooLarge", "request entity too large");
    }
    crate::limits::guard(s, call, Some(&t.did), None)?;
    let imported = read_import(call.body, &t.did)?;
    let Some(account) = s.account(&t.did) else {
        return crate::lifecycle::account_not_found();
    };
    let (shown, landing, doomed) = {
        let mut st = write(&account.store);
        let st = &mut *st;
        // Taken down or deleted while it waited for the lock.
        crate::lifecycle::gate(s, call.nsid, &account.did)?;
        crate::lifecycle::not_deleted(&account)?;
        st.repo.change = None;
        st.blobs.time(s.limits.clock.now(), s.config.blob_ttl);
        let Imported { root, mut records } = imported;
        let mut ops = Vec::new();
        let mut held = Vec::new();
        st.each_path(|p, cid, _| held.push((p.to_string(), cid.to_vec())));
        for (path, cid) in held {
            let (collection, rkey) = path.split_once('/').unwrap_or((&path, ""));
            let (collection, rkey) = (collection.to_string(), rkey.to_string());
            match records.remove(&path) {
                None => ops.push(Op::Delete { collection, rkey }),
                Some(r) if r.cid != cid => ops.push(Op::Update {
                    collection,
                    rkey,
                    value: r.value,
                }),
                Some(_) => {}
            }
        }
        for (path, r) in records {
            let (collection, rkey) = path.split_once('/').unwrap_or((&path, ""));
            ops.push(Op::Create {
                collection: collection.to_string(),
                rkey: Some(rkey.to_string()),
                value: r.value,
            });
        }
        crate::blobs::import(st, ops)?;
        let Some(mut c) = st.repo.change.take() else {
            return internal();
        };
        debug_assert_eq!(st.repo.root, root);
        let active = crate::lifecycle::status(&account).is_none();
        let landing = match crate::log::log_import(&s.events, &account.did, st, &c, active) {
            Ok(l) => l,
            Err(e) => {
                // Nothing of it reached the log: nothing of it stays.
                crate::ops::error(
                    "importRepo failed",
                    &[("did", json!(account.did)), ("error", json!(e.to_string()))],
                );
                if let Some(u) = c.undo.take() {
                    st.repo.undo(u);
                }
                st.blobs.undo();
                return internal();
            }
        };
        // A record deleted is no longer taken down.
        for path in &c.dels {
            st.takedowns.remove(path);
        }
        let shown = Shown::after(landing.as_ref(), vec![(st.wal.clone(), st.wal.written())]);
        (shown, landing, st.blobs.take_doomed())
    };
    // The import is in the log: the rest must happen even if the caller goes
    // away, or its event would never reach the firehose.
    let svc = s.clone();
    let finish = crate::ops::spawn(async move {
        let synced = shown.durable().await;
        if let Some(l) = landing {
            svc.events.land(l);
        }
        crate::blobs::forget(&svc, &account, doomed);
        synced
    });
    finish.await.unwrap_or_else(|_| internal())?;
    Ok(Resp::Nothing)
}

// ---- listMissingBlobs ---------------------------------------------------------

/// The blobs the token's account's records reference that it does not
/// hold, once the parameters are checked: of a deleted account `400 NotFound`.
fn list_missing_blobs(
    s: &Service,
    call: &Call,
    shown: &mut Shown,
    p: &Map<String, Json>,
) -> Result<Resp, XrpcError> {
    let t = sessions::access_token(s, call.bearer)?;
    let limit = p.get("limit").and_then(Json::as_u64).unwrap_or(500) as usize;
    let cursor = opt(p, "cursor");
    let missing = s.with_store(
        shown,
        &t.did,
        || XrpcError {
            status: 400,
            name: "NotFound",
            message: "Account not found".into(),
        },
        |a, st| {
            crate::lifecycle::not_deleted(a).map_err(|_| XrpcError {
                status: 400,
                name: "NotFound",
                message: "Account not found".into(),
            })?;
            Ok(st.blobs.missing(cursor, limit))
        },
    )?;
    let blobs: Vec<Json> = missing
        .iter()
        .map(|(cid, path)| json!({ "cid": cid, "recordUri": format!("at://{}/{path}", t.did) }))
        .collect();
    let mut out = json!({ "blobs": blobs });
    if let Some((cid, _)) = missing.last() {
        out["cursor"] = json!(cid);
    }
    Ok(Resp::Json(out))
}

// ---- submitPlcOperation -------------------------------------------------------

/// Whether an operation is a signed `plc_operation` of `atproto/did-plc.md`'s form.
fn of_the_form(op: &Map<String, Json>) -> bool {
    let strings = |v: Option<&Json>| {
        v.and_then(Json::as_array)
            .is_some_and(|a| a.iter().all(Json::is_string))
    };
    let services = op
        .get("services")
        .and_then(Json::as_object)
        .is_some_and(|m| {
            m.values()
                .all(|s| s["type"].is_string() && s["endpoint"].is_string())
        });
    op.get("type").and_then(Json::as_str) == Some("plc_operation")
        && strings(op.get("rotationKeys"))
        && op
            .get("verificationMethods")
            .and_then(Json::as_object)
            .is_some_and(|m| m.values().all(Json::is_string))
        && strings(op.get("alsoKnownAs"))
        && services
        && op.get("prev").is_some_and(|p| p.is_string() || p.is_null())
        && op.get("sig").is_some_and(Json::is_string)
}

async fn submit_plc_operation(
    s: Shared,
    t: Token,
    b: Map<String, Json>,
) -> Result<Resp, XrpcError> {
    let Some(op) = b.get("operation").and_then(Json::as_object) else {
        return invalid("operation must be an object");
    };
    if !of_the_form(op) {
        return invalid("Invalid operation");
    }
    let rotation = crate::plc::did_key(s.config.rotation_key.verifying_key());
    if !op["rotationKeys"]
        .as_array()
        .is_some_and(|k| k.iter().any(|k| *k == json!(rotation)))
    {
        return invalid("Rotation keys do not include server's rotation key");
    }
    let pds = &op["services"]["atproto_pds"];
    if pds["type"] != "AtprotoPersonalDataServer" {
        return invalid("Incorrect type on atproto_pds service");
    }
    if pds["endpoint"] != json!(s.config.public_url) {
        return invalid("Incorrect endpoint on atproto_pds service");
    }
    // A deleted account's key is gone.
    let Some(a) = s.account(&t.did) else {
        return internal();
    };
    let signing = crate::plc::did_key(read(&a.store).repo.verifying_key());
    if op["verificationMethods"]["atproto"] != json!(signing) {
        return invalid("Incorrect signing key");
    }
    if op["alsoKnownAs"][0] != json!(format!("at://{}", a.handle())) {
        return invalid("Incorrect handle in alsoKnownAs");
    }
    // A `did:web` is no directory's: its owner changes its document instead.
    if crate::identity::is_web(&a.did) {
        return invalid("A did:web has no PLC operations");
    }
    match crate::plc::post(
        &s.http,
        &s.config.plc_url,
        &a.did,
        &Json::Object(op.clone()),
    )
    .await
    {
        Ok(()) => {}
        Err(crate::plc::Upstream::Refused(why)) => return invalid(why),
        Err(crate::plc::Upstream::Unreachable { why, .. }) => {
            crate::ops::failed_for(
                crate::ops::Level::Error,
                "submitting a PLC operation failed",
                &a.did,
                why,
            );
            return err(
                502,
                "UpstreamFailure",
                "The PLC directory could not be reached",
            );
        }
    }
    // The document that operation made is the one served from now on.
    {
        let _fetching = a.fetching.lock().await;
        write(&a.did_doc).1 = None;
    }
    let _ = crate::identity::did_doc(&s, &a.did).await;
    crate::lifecycle::tell(&s, &a, |seq, time| events::did_event(&a.did, seq, time)).await?;
    Ok(Resp::Nothing)
}

// ---- Moving out ---------------------------------------------------------------

async fn request_signature(s: Shared, t: Token) -> Result<Resp, XrpcError> {
    let Some(a) = s.account(&t.did) else {
        return invalid("Account not found");
    };
    let out = {
        let mut m = email::lock(&a.mail);
        email::make(&s, &a.did, &mut m, Purpose::PlcOperation)?
    };
    email::deliver(&s, out).await?;
    Ok(Resp::Nothing)
}

async fn sign_plc_operation(s: Shared, t: Token, b: Map<String, Json>) -> Result<Resp, XrpcError> {
    let Some(token) = opt(&b, "token").filter(|t| !t.is_empty()) else {
        return invalid("email confirmation token required to sign PLC operations");
    };
    // A deleted account has no token.
    let Some(a) = s.account(&t.did) else {
        return invalid_token();
    };
    email::lock(&a.mail).check(Purpose::PlcOperation, token, s.sessions.now())?;
    // The directory knows no `did:web`.
    let last = if crate::identity::is_web(&a.did) {
        Err(crate::plc::Upstream::Refused(format!(
            "the directory has no operation for {}",
            a.did
        )))
    } else {
        crate::plc::last_operation(&s.http, &s.config.plc_url, &a.did).await
    };
    let last = match last {
        Ok(op) => op,
        Err(crate::plc::Upstream::Refused(why)) => return invalid(why),
        Err(crate::plc::Upstream::Unreachable { why, .. }) => {
            crate::ops::failed_for(
                crate::ops::Level::Error,
                "reading a DID's last operation failed",
                &a.did,
                why,
            );
            return err(
                502,
                "UpstreamFailure",
                "The PLC directory could not be reached",
            );
        }
    };
    if last["type"] == "plc_tombstone" {
        return invalid("Did is tombstoned");
    }
    let op = crate::plc::update_with(&s.config, &last, &b).or_else(invalid)?;
    // The token is used up, if it is still the account's.
    let n = {
        let mut m = email::lock(&a.mail);
        m.check(Purpose::PlcOperation, token, s.sessions.now())?;
        email::change(
            &s,
            &a.did,
            &mut m,
            &[email::use_entry(&a.did, Purpose::PlcOperation)],
        )?
    };
    durable(s.log.clone(), n).await?;
    Ok(Resp::Json(json!({ "operation": op })))
}

#[cfg(test)]
mod tests;