chadtmiller.com/monolith
An atproto PDS
0
0
//! Requests for other services, as `spec/PROXY.md` says: a method the server
//! does not serve is forwarded to the service its `atproto-proxy` header
//! names, or to the default AppView, with a service token the account's
//! signing key signs; `getServiceAuth` makes such tokens; and the
//! `app.bsky` methods the server serves itself and forwards: `getRecord` of
//! a repository not here, `getFeed`, `registerPush` and `unregisterPush`.
//! Read-after-write is `read_after_write`, preferences `preferences`, and
//! what every request to a service goes out through `outbound`.
//!
//! A service's DID document is kept for `DOC_LIFE` once read. Nothing here
//! holds a lock across a request to a service.
use crate::outbound::Fetched;
use crate::scopes::Need;
use crate::sessions::{self, ACCESS, APP_PASS_PRIVILEGED, Bearer, Token};
use crate::{
Answer, Call, Handler, Resp, Service, Shared, Shown, XrpcError, err, internal, invalid,
invalid_token, opt, read, required, write,
};
use axum::body::Bytes;
use axum::http::{HeaderName, HeaderValue, Method, header};
use base64::Engine;
use base64::engine::general_purpose::URL_SAFE_NO_PAD as B64;
use k256::ecdsa::signature::Signer;
use pds_repo::data::valid_did;
use serde_json::{Map, Value as Json, json};
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};
/// The default AppView: where a request goes with no `atproto-proxy` header.
#[derive(Clone, Debug)]
pub(crate) struct AppView {
pub url: String,
pub did: String,
}
/// What `spec/PROXY.md`'s variables configure, but SSRF protection, which
/// is the server's (`Config::ssrf_protection`).
#[derive(Clone, Debug, Default)]
pub(crate) struct Settings {
pub appview: Option<AppView>,
/// The URL of an image on the AppView's CDN, with three `%s`.
pub cdn: Option<String>,
}
impl Settings {
pub fn from_env() -> Result<Settings, String> {
let var = |n: &str| std::env::var(n).ok().filter(|v| !v.is_empty());
let appview = match (var("PDS_BSKY_APP_VIEW_URL"), var("PDS_BSKY_APP_VIEW_DID")) {
(None, None) => None,
(Some(_), None) => {
return Err("PDS_BSKY_APP_VIEW_URL is set without PDS_BSKY_APP_VIEW_DID".into());
}
(None, Some(_)) => {
return Err("PDS_BSKY_APP_VIEW_DID is set without PDS_BSKY_APP_VIEW_URL".into());
}
(Some(url), Some(did)) => {
if !url::Url::parse(&url).is_ok_and(|u| matches!(u.scheme(), "http" | "https")) {
return Err(format!(
"PDS_BSKY_APP_VIEW_URL is {url:?}, not an http or https URL"
));
}
if !valid_did(&did) {
return Err(format!("PDS_BSKY_APP_VIEW_DID is {did:?}, not a DID"));
}
Some(AppView { url, did })
}
};
Ok(Settings {
appview,
cdn: var("PDS_BSKY_APP_VIEW_CDN_URL_PATTERN"),
})
}
}
/// The methods only an account's own server may serve, never forwarded.
pub(crate) const NEVER_FORWARDED: &[&str] = &[
"com.atproto.admin.sendEmail",
"com.atproto.identity.requestPlcOperationSignature",
"com.atproto.identity.signPlcOperation",
"com.atproto.identity.updateHandle",
"com.atproto.server.activateAccount",
"com.atproto.server.confirmEmail",
"com.atproto.server.createAppPassword",
"com.atproto.server.deactivateAccount",
"com.atproto.server.getAccountInviteCodes",
"com.atproto.server.getSession",
"com.atproto.server.listAppPasswords",
"com.atproto.server.requestAccountDelete",
"com.atproto.server.requestEmailConfirmation",
"com.atproto.server.requestEmailUpdate",
"com.atproto.server.revokeAppPassword",
"com.atproto.server.updateEmail",
];
fn never_forwarded(name: &str) -> bool {
NEVER_FORWARDED.iter().any(|n| n.eq_ignore_ascii_case(name))
}
/// Whether a session may call privileged methods: its account's password's,
/// or a privileged app password's. An OAuth session's scope says instead
/// (`spec/OAUTH-SCOPES.md`): `transition:chat.bsky` or an `rpc` permission.
fn privileged(t: &Token, b: Bearer) -> bool {
matches!(b, Bearer::OAuth(_)) || t.scope == ACCESS || t.scope == APP_PASS_PRIVILEGED
}
/// The requests the server forwards and those it serves, and the app methods it serves itself.
pub(crate) fn handler(nsid: &str) -> Option<Handler> {
Some(match nsid {
"com.atproto.server.getServiceAuth" => Handler::Query(get_service_auth),
"app.bsky.actor.getProfile"
| "app.bsky.actor.getProfiles"
| "app.bsky.feed.getTimeline"
| "app.bsky.feed.getAuthorFeed"
| "app.bsky.feed.getActorLikes"
| "app.bsky.feed.getPostThread" => {
Handler::Proxy(|s, r, _| Box::pin(crate::read_after_write::answer(s, r)))
}
"app.bsky.feed.getFeed" => Handler::Proxy(|s, r, p| Box::pin(get_feed(s, r, p))),
"app.bsky.notification.registerPush" => {
Handler::Authed(sessions::access_token, |s, t, b| {
Box::pin(push(s, t, b, "app.bsky.notification.registerPush"))
})
}
"app.bsky.notification.unregisterPush" => {
Handler::Authed(sessions::access_token, |s, t, b| {
Box::pin(push(s, t, b, "app.bsky.notification.unregisterPush"))
})
}
_ => return None,
})
}
/// Whether a method `handler` names checks the caller's access token first:
/// all but `getRecord`, which a repository here answers to anyone.
pub(crate) fn needs_token(nsid: &str) -> bool {
nsid != "com.atproto.repo.getRecord"
}
/// Whether what follows `/xrpc/` names a method as the server reads one:
/// runs of ASCII letters and digits joined by single `.` or `-`, with an
/// optional `/` after.
fn valid_name(s: &str) -> bool {
let s = s.strip_suffix('/').unwrap_or(s);
!s.is_empty()
&& s.split(['.', '-'])
.all(|run| !run.is_empty() && run.bytes().all(|c| c.is_ascii_alphanumeric()))
}
/// The method the server serves that a request names, in any case, as its
/// lexicon names it; None if the server serves none, and the request is to
/// be forwarded. The `app.bsky` methods are served only with a default AppView.
pub(crate) fn served(svc: &Service, name: &str) -> Result<Option<&'static str>, XrpcError> {
if !valid_name(name) {
return invalid(format!("Not a method: {name}"));
}
let name = name.strip_suffix('/').unwrap_or(name);
let Some(m) = pds_lexicon::methods()
.iter()
.find(|m| m.nsid.eq_ignore_ascii_case(name))
else {
return Ok(None);
};
if crate::handler(&m.nsid).is_none()
|| (m.nsid.starts_with("app.bsky.") && svc.config.proxy.appview.is_none())
{
return Ok(None);
}
Ok(Some(m.nsid.as_str()))
}
/// The `Accept-Language` of a request the server makes of its own, not
/// forwarded (getFeed's record, registerPush), as the reference's HTTP client
/// sends it whatever the client's.
const OWN_LANGUAGE: &str = "*";
/// The request headers forwarded as they came.
fn passes(name: &HeaderName) -> bool {
let n = name.as_str();
n == "accept-language"
|| n == "atproto-accept-labelers"
|| n == "x-bsky-topics"
|| n.starts_with("x-atproto-")
}
/// A request to forward, apart from the connection it came on.
pub(crate) struct Request {
/// The method's NSID: as the lexicon names it, for one the server serves.
pub nsid: String,
/// The method's name as the request's path has it.
pub lxm: String,
pub method: Method,
/// `/xrpc/<name>?<query>`, as received.
pub path: String,
/// What the `Authorization` header said, as `Bearer::of` reads it.
authorization: Option<String>,
/// The OAuth session of a call whose token and proof passed (`scopes::look`).
grant: Option<crate::scopes::Grant>,
/// The `atproto-proxy` header, without white space around it; None if absent or empty.
pub proxy: Option<String>,
/// The headers that go on as they came, in order.
passed: Vec<(HeaderName, HeaderValue)>,
accept_encoding: Option<HeaderValue>,
content_type: Option<HeaderValue>,
content_encoding: Option<HeaderValue>,
pub body: Bytes,
pub params: Vec<(String, String)>,
}
impl Request {
/// A call, as a request to forward. A call made as an MCP tool has no
/// headers but its `Authorization`, and its path is made from its parameters.
pub fn of(call: &Call) -> Request {
let one = |n: HeaderName| call.headers.get(n).cloned();
let (path, lxm) = match call.http {
Some(h) => {
let path = h
.uri
.path_and_query()
.map_or_else(|| h.uri.path().to_string(), |p| p.as_str().to_string());
(
path,
h.uri
.path()
.strip_prefix("/xrpc/")
.unwrap_or_default()
.trim_end_matches('/')
.to_string(),
)
}
None => {
let mut q = url::form_urlencoded::Serializer::new(String::new());
for (k, v) in call.params {
q.append_pair(k, v);
}
let q = q.finish();
(
if q.is_empty() {
format!("/xrpc/{}", call.nsid)
} else {
format!("/xrpc/{}?{q}", call.nsid)
},
call.nsid.to_string(),
)
}
};
let authorization = match call.bearer {
Bearer::None | Bearer::OAuth(_) => None,
Bearer::Token(t) => Some(format!("Bearer {t}")),
Bearer::Basic(c) => Some(format!("Basic {c}")),
Bearer::DPoP(t) => Some(format!("DPoP {t}")),
Bearer::Malformed => Some(String::new()),
};
Request {
nsid: call.nsid.to_string(),
lxm,
method: call.method.clone(),
path,
authorization,
grant: match call.bearer {
Bearer::OAuth(g) => Some(g.clone()),
_ => None,
},
proxy: proxy_header(call.headers),
passed: call
.headers
.iter()
.filter(|(n, _)| passes(n))
.map(|(n, v)| (n.clone(), v.clone()))
.collect(),
accept_encoding: one(header::ACCEPT_ENCODING),
content_type: one(header::CONTENT_TYPE),
content_encoding: one(header::CONTENT_ENCODING),
params: call.params.to_vec(),
body: call
.http
.map_or_else(|| Bytes::copy_from_slice(call.body), |h| h.body.clone()),
}
}
pub fn bearer(&self) -> Bearer<'_> {
match &self.grant {
Some(g) => Bearer::OAuth(g),
None => Bearer::of(self.authorization.as_deref()),
}
}
/// The call it was, for a method the server answers itself.
fn call(&self, f: impl FnOnce(&Call) -> Result<Resp, XrpcError>) -> Result<Resp, XrpcError> {
let headers = axum::http::HeaderMap::new();
let call = Call {
nsid: &self.nsid,
method: &self.method,
params: &self.params,
body: &self.body,
bearer: self.bearer(),
headers: &headers,
// Its rate limits are paid already.
client: String::new(),
charged: std::sync::OnceLock::new(),
http: None,
};
f(&call)
}
}
/// A request's `atproto-proxy` header, without white space around it; None if absent or empty.
fn proxy_header(headers: &axum::http::HeaderMap) -> Option<String> {
headers
.get("atproto-proxy")
.map(|v| String::from_utf8_lossy(v.as_bytes()).trim().to_string())
.filter(|v| !v.is_empty())
}
/// The service a call goes to as an `rpc` scope names it (`spec/OAUTH-SCOPES.md`):
/// its `atproto-proxy` header as given, or the default AppView's DID and `#bsky_appview`.
fn audience(svc: &Service, header: Option<&str>) -> String {
match (header, &svc.config.proxy.appview) {
(Some(h), _) => h.to_string(),
(None, Some(a)) => format!("{}#bsky_appview", a.did),
(None, None) => String::new(),
}
}
/// What a method the server serves by asking the AppView needs of an OAuth
/// scope, once its token is checked: `rpc` of the method, and for `getFeed`
/// of `getFeedSkeleton` too, to the service its `atproto-proxy` header names
/// or the default AppView.
pub(crate) fn scope(svc: &Service, call: &Call) -> Result<(), XrpcError> {
if !matches!(call.bearer, Bearer::OAuth(_)) || !needs_token(call.nsid) {
return Ok(());
}
let aud = audience(svc, proxy_header(call.headers).as_deref());
crate::scopes::may(
call.bearer,
&Need::Rpc {
lxm: call.nsid,
aud: &aud,
},
)?;
if call.nsid == "app.bsky.feed.getFeed" {
crate::scopes::may(
call.bearer,
&Need::Rpc {
lxm: "app.bsky.feed.getFeedSkeleton",
aud: &aud,
},
)?;
}
Ok(())
}
/// Whether a call of a method the server does not serve passes what is
/// checked before its token (`forward_any`): its HTTP method, a method that
/// is never forwarded, and the service it goes to. One that does not is
/// refused as it is, its token never looked at.
pub(crate) async fn forwards(svc: &Service, call: &Call<'_>) -> bool {
let lxm = call.nsid.trim_end_matches('/');
matches!(*call.method, Method::GET | Method::HEAD | Method::POST)
&& !never_forwarded(lxm)
&& target(svc, proxy_header(call.headers).as_deref())
.await
.is_ok()
}
// ---- Targets -------------------------------------------------------------
/// Where a request goes: the service's DID, and its endpoint as its DID document names it.
pub(crate) struct Target {
pub did: String,
pub endpoint: String,
}
/// How long a DID document read is kept.
const DOC_LIFE: Duration = Duration::from_secs(300);
/// How many are kept at most.
const DOCS_KEPT: usize = 4096;
/// The DID documents read lately, by DID.
#[derive(Default)]
pub(crate) struct DidDocs(RwLock<HashMap<String, (Instant, Arc<Json>)>>);
fn is_default(svc: &Service, header: &str) -> bool {
svc.config
.proxy
.appview
.as_ref()
.is_some_and(|a| header.strip_suffix("#bsky_appview") == Some(a.did.as_str()))
}
/// Whether an `atproto-proxy` header names a service other than the default AppView.
pub(crate) fn names_other(svc: &Service, header: Option<&str>) -> bool {
header.is_some_and(|h| !is_default(svc, h))
}
fn default_target(svc: &Service) -> Result<Target, XrpcError> {
match &svc.config.proxy.appview {
Some(a) => Ok(Target {
did: a.did.clone(),
endpoint: a.url.clone(),
}),
None => invalid("No service configured for this request"),
}
}
/// The target a request's `atproto-proxy` header names, or the default AppView without one.
pub(crate) async fn target(svc: &Service, header: Option<&str>) -> Result<Target, XrpcError> {
let Some(h) = header else {
return default_target(svc);
};
if h.starts_with('#') {
return invalid("atproto-proxy names no DID");
}
let Some((did, id)) = h.split_once('#').filter(|(_, id)| !id.is_empty()) else {
return invalid("atproto-proxy names no service id");
};
if id.contains('#') || h.contains(' ') {
return invalid("atproto-proxy is not <did>#<service id>");
}
if is_default(svc, h) {
return default_target(svc);
}
let doc = resolve_did(svc, did).await?;
match service_endpoint(&doc, did, id, None) {
Some(endpoint) => Ok(Target {
did: did.to_string(),
endpoint,
}),
None => invalid(format!("{did} names no service #{id}")),
}
}
/// A service's DID document, read from the PLC directory or its `did:web`
/// host, and checked to be the DID's, kept for `DOC_LIFE` (`read_did`).
pub(crate) async fn resolve_did(svc: &Service, did: &str) -> Result<Arc<Json>, XrpcError> {
resolve_did_within(svc, did, DOC_LIFE).await
}
/// A DID's document as `resolve_did` reads it, but kept only for `life`:
/// one read longer ago than that is read again.
pub(crate) async fn resolve_did_within(
svc: &Service,
did: &str,
life: Duration,
) -> Result<Arc<Json>, XrpcError> {
let kept = read(&svc.did_docs.0)
.get(did)
.filter(|(at, _)| at.elapsed() < life)
.map(|(_, d)| d.clone());
if let Some(doc) = kept {
return Ok(doc);
}
let doc = Arc::new(read_did(svc, did).await?);
let mut docs = write(&svc.did_docs.0);
if docs.len() >= DOCS_KEPT {
docs.retain(|_, (at, _)| at.elapsed() < DOC_LIFE);
if docs.len() >= DOCS_KEPT {
docs.clear();
}
}
docs.insert(did.to_string(), (Instant::now(), doc.clone()));
Ok(doc)
}
/// A DID's document, read now from the PLC directory or its `did:web`
/// host, and checked to be the DID's: `400 InvalidRequest` for one that
/// resolves to nothing, `500 InternalServerError` for one that cannot be
/// read or is not a document of the DID. A `did:web` document is read
/// through the guard (`outbound::Outbound::did_web`): an answer other than
/// `2xx` or `3xx` is `400`; a redirect, one longer than its limit, or none
/// at all, `500`.
pub(crate) async fn read_did(svc: &Service, did: &str) -> Result<Json, XrpcError> {
let doc: Json = if did.starts_with("did:plc:") && valid_did(did) {
match crate::plc::document(
&svc.http,
&format!("{}/{did}", svc.config.plc_url.trim_end_matches('/')),
)
.await
{
Ok(Some(doc)) => doc,
Ok(None) => return invalid(format!("Could not resolve DID {did}")),
Err(_) => return internal(),
}
} else if did.starts_with("did:web:")
&& valid_did(did)
&& crate::outbound::did_web_url(did, false).is_some()
{
let Ok(got) = svc.outbound.did_web(did).await else {
return internal();
};
if !(200..300).contains(&got.status) {
return invalid(format!("Could not resolve DID {did}"));
}
let Ok(doc) = serde_json::from_slice(&got.body) else {
return internal();
};
doc
} else {
return internal();
};
if !is_document(&doc, did) {
return internal();
}
Ok(doc)
}
/// Forget the document kept of `did`, so that `resolve_did` reads it again:
/// a service token's signature that does not verify is tried once more
/// against its issuer's document as served now (`spec/MIGRATION.md`).
pub(crate) fn forget_did(svc: &Service, did: &str) {
write(&svc.did_docs.0).remove(did);
}
/// Whether `doc` is a DID document of `did`: an object with that `id`, and
/// a `service`, if any, that is a list of entries each with a string `id`
/// and `type`, and a `serviceEndpoint` that is a string or an object.
fn is_document(doc: &Json, did: &str) -> bool {
let entry = |s: &Json| {
s["id"].is_string()
&& s["type"].is_string()
&& (s["serviceEndpoint"].is_string() || s["serviceEndpoint"].is_object())
};
doc.is_object()
&& doc["id"] == did
&& match doc.get("service") {
None => true,
Some(Json::Array(services)) => services.iter().all(entry),
Some(_) => false,
}
}
/// The endpoint of the first service of `doc` whose `id` is `#<id>` or
/// `<did>#<id>`, of type `kind` if given: a string that starts with
/// `http://` or `https://` and parses as a URL.
pub(crate) fn service_endpoint(
doc: &Json,
did: &str,
id: &str,
kind: Option<&str>,
) -> Option<String> {
let (short, long) = (format!("#{id}"), format!("{did}#{id}"));
let s = doc["service"]
.as_array()?
.iter()
.find(|s| s["id"] == short.as_str() || s["id"] == long.as_str())?;
if kind.is_some_and(|k| s["type"] != k) {
return None;
}
let e = s["serviceEndpoint"].as_str()?;
((e.starts_with("http://") || e.starts_with("https://")) && url::Url::parse(e).is_ok())
.then(|| e.to_string())
}
// ---- Service tokens ------------------------------------------------------
/// A JWT signed with `key` in ES256K, as `atproto/xrpc.md` says.
pub(crate) fn sign_jwt(key: &k256::ecdsa::SigningKey, claims: &Json) -> String {
let header = B64.encode(r#"{"typ":"JWT","alg":"ES256K"}"#);
let message = format!("{header}.{}", B64.encode(claims.to_string()));
let sig: k256::ecdsa::Signature = key.sign(message.as_bytes());
let sig = sig.normalize_s();
format!("{message}.{}", B64.encode(sig.to_bytes()))
}
/// A service token from account `iss` for `aud`, signed with the account's
/// signing key: for `lxm` if given, until `exp` or a minute from now.
pub(crate) fn service_token(
svc: &Service,
iss: &str,
aud: &str,
lxm: Option<&str>,
exp: Option<i64>,
) -> Result<String, XrpcError> {
speaks(svc, iss)?;
let Some(account) = svc.account(iss) else {
return internal();
};
let key = read(&account.store).repo.signing_key().clone();
let iat = svc.sessions.now() as i64;
let mut claims = json!({ "iat": iat, "iss": iss, "aud": aud, "exp": exp.unwrap_or(iat + 60), "jti": hex::encode(rand::random::<[u8; 16]>()) });
if let Some(lxm) = lxm {
claims["lxm"] = json!(lxm);
}
Ok(sign_jwt(&key, &claims))
}
/// Whether an account may speak to other services, as a service token is
/// made for it (`spec/LIFECYCLE.md`): taken down is `401 AccountTakedown`;
/// a deleted one has no key to sign with, `500`; a deactivated one speaks
/// as an active one.
fn speaks(svc: &Service, did: &str) -> Result<(), XrpcError> {
taken_down(svc, did)?;
match svc.account(did).as_deref().map(crate::lifecycle::status) {
None | Some(Some("deleted")) => internal(),
_ => Ok(()),
}
}
/// `getServiceAuth` and a forwarded call refuse a taken-down account just
/// after the token, whatever the method (`spec/PROXY.md`).
fn taken_down(svc: &Service, did: &str) -> Result<(), XrpcError> {
match svc.account(did).as_deref().map(crate::lifecycle::status) {
Some(Some("takendown")) => err(401, "AccountTakedown", "Account has been taken down"),
_ => Ok(()),
}
}
/// Whether `getServiceAuth` takes an `aud`: an atproto DID, a `did:plc` or a
/// `did:web` with no path and no port but on `localhost`, optionally
/// followed by `#` and a fragment of the characters a DID may hold.
fn valid_audience(aud: &str) -> bool {
let did = match aud.split_once('#') {
Some((did, fragment))
if !fragment.is_empty()
&& fragment
.bytes()
.all(|c| c.is_ascii_alphanumeric() || b"._:%-".contains(&c)) =>
{
did
}
Some(_) => return false,
None => aud,
};
if let Some(id) = did.strip_prefix("did:plc:") {
return id.len() == 24
&& id
.bytes()
.all(|c| c.is_ascii_lowercase() || (b'2'..=b'7').contains(&c));
}
crate::outbound::atproto_did_web(did)
}
fn get_service_auth(
s: &Service,
call: &Call,
_: &mut Shown,
p: &Map<String, Json>,
) -> Result<Resp, XrpcError> {
let t = sessions::access_token(s, call.bearer)?;
let aud = required(p, "aud")?;
let lxm = opt(p, "lxm");
// An OAuth session's scope is asked first, for any method if none is named.
crate::scopes::may(
call.bearer,
&Need::Rpc {
lxm: lxm.unwrap_or("*"),
aud,
},
)?;
taken_down(s, &t.did)?;
if !valid_audience(aud) {
return invalid(format!("Not an atproto DID: {aud}"));
}
let privileged_method = |l: &str| {
let l = l.to_ascii_lowercase();
l.starts_with("chat.bsky.") || l == "com.atproto.server.createaccount"
};
if lxm.is_some_and(privileged_method) && !privileged(&t, call.bearer) {
return invalid(format!("Bad token method: {}", lxm.unwrap_or_default()));
}
let exp = p.get("exp").and_then(Json::as_i64);
if let Some(e) = exp.filter(|e| *e != 0) {
let now = s.sessions.now() as i64;
if e < now {
return err(400, "BadExpiration", "expiration is in past");
}
if e - now > 60 * 60 {
return err(
400,
"BadExpiration",
"cannot request a token with an expiration more than an hour in the future",
);
}
if lxm.is_none() && e - now > 60 {
return err(
400,
"BadExpiration",
"cannot request a method-less token with an expiration more than a minute in the future",
);
}
}
if lxm.is_some_and(never_forwarded) {
return invalid(format!(
"Cannot request a token for {}",
lxm.unwrap_or_default()
));
}
Ok(Resp::Json(
json!({ "token": service_token(s, &t.did, aud, lxm, exp)? }),
))
}
// ---- Forwarding ----------------------------------------------------------
/// An answer of a service's, as the server answers it.
pub(crate) struct Upstream {
pub status: u16,
pub headers: Vec<(HeaderName, HeaderValue)>,
pub body: Bytes,
}
fn upstream_failure<T>() -> Result<T, XrpcError> {
err(502, "UpstreamFailure", "Upstream Failure")
}
/// The URL of `path` on the service at `endpoint`, which must be an origin
/// alone: one with a path, a query or a fragment cannot be reached.
fn url_at(endpoint: &str, path: &str) -> Result<url::Url, XrpcError> {
let Ok(e) = url::Url::parse(endpoint) else {
return upstream_failure();
};
if e.path() != "/" || e.query().is_some() || e.fragment().is_some() {
return upstream_failure();
}
url::Url::parse(&format!("{}{path}", e.origin().ascii_serialization()))
.or_else(|_| upstream_failure())
}
/// Send a request to a service and read its answer, up to `FORWARD_MAX`
/// bytes; one that cannot be had, or is longer, is `502 UpstreamFailure`.
async fn fetch(svc: &Service, req: reqwest::RequestBuilder) -> Result<Fetched, XrpcError> {
match svc
.outbound
.fetch(
crate::ops::Target::Service,
req,
crate::outbound::FORWARD_MAX,
)
.await
{
Ok(got) => Ok(got),
Err(_) => upstream_failure(),
}
}
/// Whether a `Content-Type` is JSON: `application/json` or `application/<x>+json`.
pub(crate) fn is_json(content_type: &HeaderValue) -> bool {
let t = content_type.to_str().unwrap_or_default();
let t = t
.split(';')
.next()
.unwrap_or_default()
.trim()
.to_ascii_lowercase();
t == "application/json" || (t.starts_with("application/") && t.ends_with("+json"))
}
/// The response headers of a service's that go on with a success.
const PASSED_BACK: [HeaderName; 6] = [
header::CONTENT_TYPE,
header::CONTENT_LANGUAGE,
header::CONTENT_ENCODING,
HeaderName::from_static("atproto-repo-rev"),
HeaderName::from_static("atproto-content-labelers"),
header::RETRY_AFTER,
];
/// The names and texts of the error statuses XRPC names.
const STATUSES: &[(u16, &str, &str)] = &[
(400, "InvalidRequest", "Invalid Request"),
(401, "AuthenticationRequired", "Authentication Required"),
(403, "Forbidden", "Forbidden"),
(404, "XRPCNotSupported", "XRPC Not Supported"),
(406, "NotAcceptable", "Not Acceptable"),
(413, "PayloadTooLarge", "Payload Too Large"),
(415, "UnsupportedMediaType", "Unsupported Media Type"),
(429, "RateLimitExceeded", "Rate Limit Exceeded"),
(501, "MethodNotImplemented", "Method Not Implemented"),
(502, "UpstreamFailure", "Upstream Failure"),
(503, "NotEnoughResources", "Not Enough Resources"),
(504, "UpstreamTimeout", "Upstream Timeout"),
];
fn json_answer(status: u16, body: &Json) -> Resp {
Resp::Upstream(Upstream {
status,
headers: vec![(
header::CONTENT_TYPE,
HeaderValue::from_static("application/json"),
)],
body: Bytes::from(body.to_string()),
})
}
/// A service's answer as the server answers it. Below 400, its status (or
/// `200` for a method the server serves), its body, and the headers that
/// go back; from 400, an error of its status (`500` as `502`), named and
/// said as the service did if it answered JSON, else, for a status XRPC
/// names, as XRPC names it.
pub(crate) fn answered(got: Fetched, served: bool) -> Resp {
if got.status < 400 {
let headers = PASSED_BACK
.iter()
.flat_map(|n| {
got.headers
.get_all(n)
.iter()
.map(|v| (n.clone(), v.clone()))
})
.collect();
return Resp::Upstream(Upstream {
status: if served { 200 } else { got.status },
headers,
body: got.body,
});
}
let status = if got.status == 500 { 502 } else { got.status };
let parsed = match got.headers.get(header::CONTENT_TYPE) {
Some(t) if !is_json(t) => None,
_ => serde_json::from_slice::<Json>(&got.body).ok(),
};
let said = |k: &str| {
parsed
.as_ref()
.and_then(|j| j.get(k))
.and_then(Json::as_str)
.map(str::to_string)
};
let mut out = Map::new();
match STATUSES.iter().find(|(s, ..)| *s == status) {
Some((_, name, text)) => {
out.insert(
"error".into(),
json!(said("error").unwrap_or_else(|| name.to_string())),
);
out.insert(
"message".into(),
json!(said("message").unwrap_or_else(|| text.to_string())),
);
}
None => {
for k in ["error", "message"] {
if let Some(v) = said(k) {
out.insert(k.into(), json!(v));
}
}
}
}
json_answer(status, &Json::Object(out))
}
/// Forward a request for a method the server does not serve, as `spec/PROXY.md` says.
pub(crate) async fn forward(svc: &Shared, call: &Call<'_>) -> Result<Resp, XrpcError> {
let r = Request::of(call);
crate::ops::spawn(forward_any(svc.clone(), r))
.await
.unwrap_or_else(|_| internal())
}
async fn forward_any(svc: Shared, r: Request) -> Result<Resp, XrpcError> {
if !matches!(r.method, Method::GET | Method::HEAD | Method::POST) {
return invalid(format!("Method {} is not forwarded", r.method));
}
if never_forwarded(&r.lxm) {
return invalid_token();
}
let target = target(&svc, r.proxy.as_deref()).await?;
let t = sessions::access_token(&svc, r.bearer())?;
crate::scopes::may(
r.bearer(),
&Need::Rpc {
lxm: &r.lxm,
aud: &audience(&svc, r.proxy.as_deref()),
},
)?;
taken_down(&svc, &t.did)?;
// Read only so far (`limits::read_body`): one longer is not sent on cut short.
if r.body.len() > crate::limits::body_limit(&r.lxm) {
return crate::limits::too_large();
}
if r.lxm.to_ascii_lowercase().starts_with("chat.bsky.") && !privileged(&t, r.bearer()) {
return invalid_token();
}
let url = url_at(&target.endpoint, &r.path)?;
let token = service_token(&svc, &t.did, &target.did, Some(&r.lxm), None)?;
let mut req = svc.outbound.client().request(r.method.clone(), url);
for (n, v) in &r.passed {
req = req.header(n.clone(), v.clone());
}
req = req
.header(
header::ACCEPT_ENCODING,
r.accept_encoding
.clone()
.unwrap_or(HeaderValue::from_static("identity")),
)
.header(header::AUTHORIZATION, format!("Bearer {token}"));
if r.method == Method::POST {
for (n, v) in [
(header::CONTENT_TYPE, &r.content_type),
(header::CONTENT_ENCODING, &r.content_encoding),
] {
if let Some(v) = v {
req = req.header(n, v.clone());
}
}
req = req.body(r.body.clone());
}
Ok(answered(fetch(&svc, req).await?, false))
}
/// Forward a query the server serves to `target`, by `GET`, with the
/// request's path and query and the headers that pass, `identity` its
/// `Accept-Encoding`, and a service token from `iss` for `aud` and `lxm` if given.
pub(crate) async fn forward_query(
svc: &Service,
r: &Request,
target: &Target,
token: Option<(&str, &str, &str)>,
) -> Result<Fetched, XrpcError> {
let url = url_at(&target.endpoint, &r.path)?;
let mut req = svc.outbound.client().get(url);
for (n, v) in &r.passed {
req = req.header(n.clone(), v.clone());
}
req = req.header(header::ACCEPT_ENCODING, "identity");
if let Some((iss, aud, lxm)) = token {
req = req.header(
header::AUTHORIZATION,
format!("Bearer {}", service_token(svc, iss, aud, Some(lxm), None)?),
);
}
fetch(svc, req).await
}
/// `com.atproto.repo.getRecord`: of a repository here of an active account,
/// its record; of any other, one of an account not active too
/// (`spec/LIFECYCLE.md`), forwarded with no `Authorization`.
pub(crate) fn get_record(s: Shared, r: Request, p: Map<String, Json>) -> Answer {
Box::pin(async move {
if s.account(required(&p, "repo")?)
.is_some_and(|a| crate::lifecycle::status(&a).is_none())
{
let mut shown = Shown::default();
let out = r.call(|call| crate::repo::get_record(&s, call, &mut shown, &p));
shown.durable().await?;
return out;
}
if s.config.proxy.appview.is_none() {
return invalid("Could not find repo");
}
let target = target(&s, r.proxy.as_deref()).await?;
Ok(answered(forward_query(&s, &r, &target, None).await?, true))
})
}
/// `app.bsky.feed.getFeed`: the feed's record, from the default AppView,
/// names the feed generator the token is for.
async fn get_feed(s: Shared, r: Request, p: Map<String, Json>) -> Result<Resp, XrpcError> {
let t = sessions::access_token(&s, r.bearer())?;
let Some(appview) = s.config.proxy.appview.clone() else {
return internal();
};
let feed = required(&p, "feed")?;
let mut parts = feed.strip_prefix("at://").unwrap_or_default().split('/');
let (Some(repo), Some(collection), Some(rkey)) = (parts.next(), parts.next(), parts.next())
else {
return internal();
};
if [repo, collection, rkey].contains(&"") {
return internal();
}
let query = url::form_urlencoded::Serializer::new(String::new())
.append_pair("repo", repo)
.append_pair("collection", collection)
.append_pair("rkey", rkey)
.finish();
let url = url_at(
&appview.url,
&format!("/xrpc/com.atproto.repo.getRecord?{query}"),
)?;
let got = fetch(
&s,
s.outbound
.client()
.get(url)
.header(header::ACCEPT_LANGUAGE, OWN_LANGUAGE),
)
.await?;
if got.status >= 400 {
return Ok(answered(got, true));
}
let record = serde_json::from_slice::<Json>(&got.body).ok().filter(|j| {
pds_lexicon::method("com.atproto.repo.getRecord").is_some_and(|m| m.check_output(j).is_ok())
});
let Some(record) = record else {
return err(
502,
"InvalidResponse",
"Invalid response from the feed's record",
);
};
let Some(aud) = record["value"]["did"].as_str() else {
return err(400, "InvalidRequestError", "The feed's record names no DID");
};
let target = target(&s, r.proxy.as_deref()).await?;
Ok(answered(
forward_query(
&s,
&r,
&target,
Some((&t.did, aud, "app.bsky.feed.getFeedSkeleton")),
)
.await?,
true,
))
}
/// `registerPush` and `unregisterPush`: the input, sent on to the notification service `serviceDid` names.
async fn push(
s: Shared,
t: Token,
input: Map<String, Json>,
nsid: &'static str,
) -> Result<Resp, XrpcError> {
let service = required(&input, "serviceDid")?.to_string();
let endpoint = match &s.config.proxy.appview {
Some(a) if a.did == service => a.url.clone(),
_ => {
let doc = resolve_did(&s, &service).await?;
match service_endpoint(
&doc,
&service,
"bsky_notif",
Some("BskyNotificationService"),
) {
Some(e) => e,
None => return invalid(format!("{service} names no notification service")),
}
}
};
let url = url_at(&endpoint, &format!("/xrpc/{nsid}"))?;
let token = service_token(&s, &t.did, &service, Some(nsid), None)?;
let req = s
.outbound
.client()
.post(url)
.header(header::CONTENT_TYPE, "application/json")
.header(header::ACCEPT_LANGUAGE, OWN_LANGUAGE)
.header(header::AUTHORIZATION, format!("Bearer {token}"))
.body(Json::Object(input).to_string());
let got = fetch(&s, req).await?;
if got.status >= 400 {
return Ok(answered(got, true));
}
if got.headers.get(header::CONTENT_TYPE).is_some_and(is_json)
&& serde_json::from_slice::<Json>(&got.body).is_err()
{
return err(
502,
"InvalidResponse",
"The notification service answered JSON that is not",
);
}
Ok(Resp::Nothing)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::testing::*;
use axum::extract::State;
use axum::http::{HeaderMap, StatusCode, Uri};
use axum::response::{IntoResponse, Response};
use k256::ecdsa::signature::Verifier;
use std::sync::Mutex;
/// A request a stub service got.
struct Got {
method: Method,
/// Its path and query, as sent.
path: String,
headers: HeaderMap,
body: Bytes,
}
#[derive(Clone)]
struct Canned {
status: u16,
headers: Vec<(&'static str, String)>,
body: Vec<u8>,
}
/// A service on `localhost`, `did:web:localhost%3A<port>`, which serves its
/// DID document and records every other request it gets, answering what
/// is canned for its path (or for `""`), or `200 {}`.
#[derive(Clone)]
struct Stub {
url: String,
did: String,
got: Arc<Mutex<Vec<Got>>>,
answers: Arc<Mutex<HashMap<String, Canned>>>,
doc: Arc<Mutex<Json>>,
}
async fn stub_answer(
State(st): State<Stub>,
method: Method,
uri: Uri,
headers: HeaderMap,
body: Bytes,
) -> Response {
if uri.path() == "/.well-known/did.json" {
let doc = st.doc.lock().unwrap().clone();
return if doc.is_null() {
StatusCode::NOT_FOUND.into_response()
} else {
axum::Json(doc).into_response()
};
}
let path = uri.path_and_query().unwrap().as_str().to_string();
st.got.lock().unwrap().push(Got {
method,
path,
headers,
body,
});
let answers = st.answers.lock().unwrap();
let a = answers.get(uri.path()).or(answers.get("")).cloned();
drop(answers);
let a = a.unwrap_or(Canned {
status: 200,
headers: vec![("content-type", "application/json".into())],
body: b"{}".to_vec(),
});
let mut r = Response::new(axum::body::Body::from(a.body));
*r.status_mut() = StatusCode::from_u16(a.status).unwrap();
for (n, v) in a.headers {
r.headers_mut().append(n, v.parse().unwrap());
}
r
}
impl Stub {
async fn start() -> Stub {
let listener = tokio::net::TcpListener::bind(("127.0.0.1", 0))
.await
.unwrap();
let port = listener.local_addr().unwrap().port();
let (url, did) = (
format!("http://127.0.0.1:{port}"),
format!("did:web:localhost%3A{port}"),
);
let service = |id: &str, endpoint: Json| json!({ "id": id, "type": "X", "serviceEndpoint": endpoint });
let doc = json!({ "id": did, "service": [
{ "id": "#bsky_appview", "type": "BskyAppView", "serviceEndpoint": url },
{ "id": "#bsky_notif", "type": "BskyNotificationService", "serviceEndpoint": url },
service("#bsky_x", json!(url)),
service(&format!("{did}#full"), json!(url)),
service("#path", json!(format!("{url}/x"))),
service("#query", json!(format!("{url}/?a=b"))),
service("#down", json!("http://127.0.0.1:1")),
service("#ftp", json!("ftp://127.0.0.1/")),
service("#upper", json!(url.replace("http", "HTTP"))),
service("#object", json!({ "uri": url })),
]});
let stub = Stub {
url,
did,
got: Default::default(),
answers: Default::default(),
doc: Arc::new(Mutex::new(doc)),
};
let app = axum::Router::new()
.fallback(stub_answer)
.with_state(stub.clone());
tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
stub
}
fn answer(&self, path: &str, status: u16, headers: &[(&'static str, &str)], body: &[u8]) {
let headers = headers.iter().map(|(n, v)| (*n, v.to_string())).collect();
self.answers.lock().unwrap().insert(
path.to_string(),
Canned {
status,
headers,
body: body.to_vec(),
},
);
}
fn take(&self) -> Vec<Got> {
std::mem::take(&mut *self.got.lock().unwrap())
}
}
/// The server, configured with `appview` as its default AppView, listening on a port; and its URL.
async fn server(
name: &str,
plc: &str,
appview: Option<&Stub>,
protect: bool,
) -> (Shared, String) {
let mut c = config(plc);
c.proxy = Settings {
appview: appview.map(|a| AppView {
url: a.url.clone(),
did: a.did.clone(),
}),
cdn: Some("http://cdn.test/%s/%s/%s".into()),
};
c.ssrf_protection = protect;
let s = Arc::new(Service::open(&scratch(name), c).unwrap());
let listener = tokio::net::TcpListener::bind(("127.0.0.1", 0))
.await
.unwrap();
let url = format!("http://{}", listener.local_addr().unwrap());
let app = crate::router(s.clone());
tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
(s, url)
}
/// A request to the server as sent: its status, headers and body.
async fn send(
base: &str,
method: Method,
path: &str,
headers: &[(&str, &str)],
body: &[u8],
) -> (u16, HeaderMap, Bytes) {
let mut req = reqwest::Client::builder()
.no_proxy()
.build()
.unwrap()
.request(method, format!("{base}{path}"));
for (n, v) in headers {
req = req.header(*n, *v);
}
let res = req.body(body.to_vec()).send().await.unwrap();
(
res.status().as_u16(),
res.headers().clone(),
res.bytes().await.unwrap(),
)
}
fn error(body: &Bytes) -> String {
serde_json::from_slice::<Json>(body).unwrap()["error"]
.as_str()
.unwrap_or_default()
.to_string()
}
/// A service token's claims, once its header and signature by `did`'s signing key are checked.
fn claims(s: &Service, token: &str, did: &str) -> Map<String, Json> {
let token = token.strip_prefix("Bearer ").unwrap_or(token);
let parts: Vec<&str> = token.split('.').collect();
assert_eq!(parts.len(), 3, "{token}");
let header: Json = serde_json::from_slice(&B64.decode(parts[0]).unwrap()).unwrap();
assert_eq!(header, json!({ "typ": "JWT", "alg": "ES256K" }));
let sig = k256::ecdsa::Signature::from_slice(&B64.decode(parts[2]).unwrap()).unwrap();
assert!(sig.normalize_s() == sig, "a low-S signature");
let key = repo_of(s, did, |r| *r.verifying_key());
key.verify(format!("{}.{}", parts[0], parts[1]).as_bytes(), &sig)
.unwrap();
let Json::Object(c) = serde_json::from_slice(&B64.decode(parts[1]).unwrap()).unwrap()
else {
panic!("claims that are not an object")
};
let jti = c["jti"].as_str().unwrap();
assert!(
jti.len() == 32 && jti.bytes().all(|b| b.is_ascii_hexdigit()),
"{jti}"
);
let now = s.sessions.now() as i64;
assert!((now - c["iat"].as_i64().unwrap()).abs() <= 2);
c
}
fn keys(c: &Map<String, Json>) -> Vec<&str> {
let mut k: Vec<&str> = c.keys().map(String::as_str).collect();
k.sort();
k
}
#[test]
fn method_names() {
for ok in ["foo", "a.b", "9a.b.c", "a-b.c", "a.b/", "App.Bsky.X"] {
assert!(valid_name(ok), "{ok}");
}
for bad in [
"", "a..b", "a.b/c", ".a", "a.", "a b", "a%2Eb", "a_b", "/", "a.b//",
] {
assert!(!valid_name(bad), "{bad}");
}
}
#[test]
fn audiences() {
for ok in [
"did:plc:abcdefghijklmnopqrstuvwx",
"did:plc:abcdefghijklmnopqrstuvwx#atproto_labeler",
"did:web:api.bsky.app",
"did:web:localhost%3A2583",
"did:web:x.test#bsky_chat",
] {
assert!(valid_audience(ok), "{ok}");
}
for bad in [
"did:key:zQ3sh",
"did:example:x",
"did:plc:abc",
"did:plc:abcdefghijklmnopqrstuvw1",
"did:web:a.test#",
"did:web:a.test#a b",
"did:web:a.test#a#b",
"did:web:a.test:path",
"did:web:a.test%3A443",
"notadid",
"",
] {
assert!(!valid_audience(bad), "{bad}");
}
}
#[test]
fn documents() {
let did = "did:web:x.test";
let entry = |s: Json| json!({ "id": did, "service": [s] });
assert!(is_document(&json!({ "id": did }), did));
assert!(is_document(
&entry(json!({ "id": "#a", "type": "T", "serviceEndpoint": {} })),
did
));
for bad in [
json!([]),
json!({ "id": "did:web:y.test" }),
json!({ "id": did, "service": {} }),
entry(json!({ "id": "#a", "serviceEndpoint": "https://x.test" })),
entry(json!({ "id": 5, "type": "T", "serviceEndpoint": "https://x.test" })),
entry(json!({ "id": "#a", "type": "T", "serviceEndpoint": 5 })),
entry(json!({ "id": "#a", "type": "T" })),
] {
assert!(!is_document(&bad, did), "{bad}");
}
let doc = json!({ "id": did, "service": [
{ "id": "#a", "type": "T", "serviceEndpoint": "https://a.test" },
{ "id": "#a", "type": "T", "serviceEndpoint": "https://second.test" },
{ "id": format!("{did}#b"), "type": "U", "serviceEndpoint": "http://b.test" },
{ "id": "#c", "type": "T", "serviceEndpoint": "HTTPS://c.test" },
]});
assert_eq!(
service_endpoint(&doc, did, "a", None).as_deref(),
Some("https://a.test")
);
assert_eq!(
service_endpoint(&doc, did, "b", None).as_deref(),
Some("http://b.test")
);
assert_eq!(service_endpoint(&doc, did, "b", Some("T")), None);
assert_eq!(service_endpoint(&doc, did, "c", None), None);
assert_eq!(service_endpoint(&doc, did, "A", None), None);
}
/// A request the server does not serve goes to the default AppView, with
/// the path, query and body as sent, only the headers that go on, and a
/// service token; its answer comes back with only the headers that do.
#[tokio::test]
async fn forwards_with_a_service_token() {
let plc = plc().await;
let appview = Stub::start().await;
let (s, base) = server("forward", &plc, Some(&appview), false).await;
let (did, token) = create(&s, "alice").await;
let auth = format!("Bearer {token}");
let headers = [
("authorization", auth.as_str()),
("accept-language", "fr"),
("x-atproto-foo", "1"),
("X-Bsky-Topics", "t"),
("atproto-accept-labelers", "did:plc:l;redact"),
("cookie", "c=1"),
("origin", "http://evil.test"),
("referer", "http://evil.test/"),
("user-agent", "ua"),
("dpop", "d"),
("x-not-forwarded", "n"),
("atproto-proxy", ""),
];
let canned = [
("content-type", "application/json"),
("atproto-repo-rev", "3abc"),
("content-language", "en"),
("x-other", "y"),
("retry-after", "5"),
];
appview.answer("", 201, &canned, b"{\"a\":1}");
let path = "/xrpc/app.bsky.feed.getPosts?uris=at%3A%2F%2Fx&b=%27&b=2";
let (status, h, body) = send(&base, Method::GET, path, &headers, b"").await;
assert_eq!((status, &body[..]), (201, &b"{\"a\":1}"[..]));
for (n, v) in [
("content-type", "application/json"),
("atproto-repo-rev", "3abc"),
("content-language", "en"),
("retry-after", "5"),
] {
assert_eq!(h.get(n).map(|v| v.to_str().unwrap()), Some(v), "{n}");
}
assert!(h.get("x-other").is_none());
let got = appview.take();
assert_eq!(got.len(), 1);
let g = &got[0];
assert_eq!((&g.method, g.path.as_str()), (&Method::GET, path));
for (n, v) in [
("accept-language", "fr"),
("x-atproto-foo", "1"),
("x-bsky-topics", "t"),
("atproto-accept-labelers", "did:plc:l;redact"),
("accept-encoding", "identity"),
] {
assert_eq!(
g.headers.get(n).map(|v| v.to_str().unwrap()),
Some(v),
"{n}"
);
}
for n in [
"cookie",
"origin",
"referer",
"user-agent",
"dpop",
"x-not-forwarded",
"atproto-proxy",
"content-type",
] {
assert!(g.headers.get(n).is_none(), "{n}");
}
assert!(g.body.is_empty());
let c = claims(&s, g.headers["authorization"].to_str().unwrap(), &did);
assert_eq!(keys(&c), ["aud", "exp", "iat", "iss", "jti", "lxm"]);
assert_eq!(
(&c["iss"], &c["aud"], &c["lxm"]),
(
&json!(did),
&json!(appview.did),
&json!("app.bsky.feed.getPosts")
)
);
assert_eq!(c["exp"].as_i64().unwrap() - c["iat"].as_i64().unwrap(), 60);
// A `POST`, in odd case, to a service the header names: the body as
// sent, with its type and encoding, and the client's Accept-Encoding.
appview.answer("", 200, &[], b"");
let body: &[u8] = &[0x1f, 0x8b, 0, 1, 2, 255];
let proxy = format!("{}#full", appview.did);
let headers = [
("authorization", auth.as_str()),
("content-type", "application/octet-stream"),
("content-encoding", "gzip"),
("accept-encoding", "gzip, br"),
("atproto-proxy", proxy.as_str()),
];
let (status, h, answer) = send(
&base,
Method::POST,
"/xrpc/App.Bsky.Graph.MuteActor",
&headers,
body,
)
.await;
assert_eq!(
(status, answer.len(), h.get("content-type")),
(200, 0, None)
);
let got = appview.take();
let g = &got[0];
assert_eq!(
(&g.method, g.path.as_str(), &g.body[..]),
(&Method::POST, "/xrpc/App.Bsky.Graph.MuteActor", body)
);
for (n, v) in [
("content-type", "application/octet-stream"),
("content-encoding", "gzip"),
("accept-encoding", "gzip, br"),
] {
assert_eq!(
g.headers.get(n).map(|v| v.to_str().unwrap()),
Some(v),
"{n}"
);
}
let c = claims(&s, g.headers["authorization"].to_str().unwrap(), &did);
assert_eq!(
(&c["aud"], &c["lxm"]),
(&json!(appview.did), &json!("App.Bsky.Graph.MuteActor"))
);
// Every token is new.
let other = service_token(&s, &did, "did:web:x.test", None, None)
.ok()
.unwrap();
assert_ne!(c["jti"], claims(&s, &other, &did)["jti"]);
// `HEAD`, and a redirect, answered as it is.
appview.answer(
"",
302,
&[
("location", "https://elsewhere.test/"),
("content-type", "text/plain"),
],
b"moved",
);
let (status, h, _) = send(
&base,
Method::HEAD,
"/xrpc/com.example.thing",
&[("authorization", auth.as_str())],
b"",
)
.await;
assert_eq!(
(
status,
h.get("location"),
h.get("content-type").map(|v| v.to_str().unwrap())
),
(302, None, Some("text/plain"))
);
assert_eq!(appview.take()[0].method, Method::HEAD);
}
/// A service's error is answered with its status, `500` as `502`, and its
/// `error` and `message` where it gives them as JSON.
#[tokio::test]
async fn errors_as_the_service_says() {
let plc = plc().await;
let appview = Stub::start().await;
let (s, base) = server("upstream-errors", &plc, Some(&appview), false).await;
let (_, token) = create(&s, "alice").await;
let auth = format!("Bearer {token}");
let json_type = ("content-type", "application/json");
type Case = (
u16,
Vec<(&'static str, &'static str)>,
&'static [u8],
u16,
Json,
);
let cases: Vec<Case> = vec![
(
500,
vec![json_type],
br#"{"error":"Boom","message":"it broke"}"#,
502,
json!({ "error": "Boom", "message": "it broke" }),
),
(
404,
vec![("content-type", "text/plain")],
br#"{"error":"X"}"#,
404,
json!({ "error": "XRPCNotSupported", "message": "XRPC Not Supported" }),
),
(
400,
vec![],
br#"{"error":5,"message":"why"}"#,
400,
json!({ "error": "InvalidRequest", "message": "why" }),
),
(
429,
vec![
("content-type", "application/problem+json"),
("retry-after", "9"),
],
br#"{"error":"Slow"}"#,
429,
json!({ "error": "Slow", "message": "Rate Limit Exceeded" }),
),
(
418,
vec![json_type],
br#"{"error":"Tea","message":"pot"}"#,
418,
json!({ "error": "Tea", "message": "pot" }),
),
(
418,
vec![json_type],
br#"{"error":5,"message":"pot"}"#,
418,
json!({ "message": "pot" }),
),
(418, vec![json_type], b"not json", 418, json!({})),
(
504,
vec![json_type],
b"",
504,
json!({ "error": "UpstreamTimeout", "message": "Upstream Timeout" }),
),
];
for (status, headers, body, want_status, want) in cases {
appview.answer("", status, &headers, body);
let (got, h, answer) = send(
&base,
Method::GET,
"/xrpc/app.bsky.feed.getPosts",
&[("authorization", auth.as_str())],
b"",
)
.await;
assert_eq!(
(got, serde_json::from_slice::<Json>(&answer).unwrap()),
(want_status, want),
"{status}"
);
assert!(h.get("retry-after").is_none());
}
// A service that cannot be reached.
let (status, _, body) = send(
&base,
Method::GET,
"/xrpc/a.b",
&[
("authorization", auth.as_str()),
("atproto-proxy", &format!("{}#down", appview.did)),
],
b"",
)
.await;
assert_eq!((status, error(&body)), (502, "UpstreamFailure".into()));
}
/// What is refused before anything is sent, in the order it is checked.
#[tokio::test]
async fn refusals() {
let plc = plc().await;
let appview = Stub::start().await;
let (s, base) = server("refusals", &plc, Some(&appview), false).await;
let (_, token) = create(&s, "alice").await;
let auth = format!("Bearer {token}");
let ask = |method: Method, path: &str, headers: Vec<(&'static str, String)>| {
let (base, path) = (base.clone(), path.to_string());
async move {
let h: Vec<(&str, &str)> = headers.iter().map(|(n, v)| (*n, v.as_str())).collect();
let (status, _, body) = send(&base, method, &path, &h, b"").await;
(status, error(&body))
}
};
let with = |proxy: &str| {
vec![
("authorization", auth.clone()),
("atproto-proxy", proxy.to_string()),
]
};
let bad = |status: u16, name: &str| (status, name.to_string());
for m in [Method::PUT, Method::DELETE, Method::PATCH] {
assert_eq!(
ask(m, "/xrpc/app.bsky.feed.getPosts", with("")).await,
bad(400, "InvalidRequest")
);
}
for path in ["/xrpc/a..b", "/xrpc/a.b/c", "/xrpc/a%2Eb", "/xrpc/.a"] {
assert_eq!(
ask(Method::GET, path, with("")).await,
bad(400, "InvalidRequest"),
"{path}"
);
}
// A method never forwarded that is not served is refused as it is.
assert_eq!(
ask(Method::POST, "/xrpc/com.atproto.admin.sendemail", vec![]).await,
bad(400, "InvalidToken")
);
// A method served here, in another case, is served (`activateAccount`,
// spec/LIFECYCLE.md; the PLC operations, spec/MIGRATION.md).
for name in [
"com.atproto.identity.REQUESTPLCOPERATIONSIGNATURE",
"com.atproto.identity.signPlcOperation",
] {
assert_eq!(
ask(Method::POST, &format!("/xrpc/{name}"), vec![]).await,
bad(401, "AuthMissing"),
"{name}"
);
}
assert_eq!(
ask(
Method::POST,
"/xrpc/com.atproto.server.ACTIVATEACCOUNT",
vec![]
)
.await,
bad(401, "AuthMissing")
);
let d = &appview.did;
for (proxy, want) in [
("#bsky_appview".to_string(), bad(400, "InvalidRequest")),
(d.clone(), bad(400, "InvalidRequest")),
(format!("{d}#"), bad(400, "InvalidRequest")),
(format!("{d}#a#b"), bad(400, "InvalidRequest")),
(format!("{d}#bsky x"), bad(400, "InvalidRequest")),
(format!("{d}#nope"), bad(400, "InvalidRequest")),
(format!("{d}#BSKY_X"), bad(400, "InvalidRequest")),
(format!("{d}#ftp"), bad(400, "InvalidRequest")),
(format!("{d}#upper"), bad(400, "InvalidRequest")),
(format!("{d}#object"), bad(400, "InvalidRequest")),
(
"did:plc:aaaaaaaaaaaaaaaaaaaaaaaa#x".into(),
bad(400, "InvalidRequest"),
),
(
"did:web:localhost%3A1#x".into(),
bad(500, "InternalServerError"),
),
(
"did:key:zQ3shokFTS3brHcDQrn82RUDfCZESWL1ZdCEJwekUDPQiYBme#x".into(),
bad(500, "InternalServerError"),
),
("did:example:x#x".into(), bad(500, "InternalServerError")),
("notadid#x".into(), bad(500, "InternalServerError")),
(
"DID:PLC:aaaaaaaaaaaaaaaaaaaaaaaa#x".into(),
bad(500, "InternalServerError"),
),
(
"did:web:example.com:a#x".into(),
bad(500, "InternalServerError"),
),
(format!("{d}#path"), bad(502, "UpstreamFailure")),
(format!("{d}#query"), bad(502, "UpstreamFailure")),
(format!("{d}#down"), bad(502, "UpstreamFailure")),
] {
assert_eq!(
ask(Method::GET, "/xrpc/app.bsky.feed.getPosts", with(&proxy)).await,
want,
"{proxy}"
);
}
// The target's errors come before the token's.
let none = |proxy: &str| vec![("atproto-proxy", proxy.to_string())];
assert_eq!(
ask(Method::GET, "/xrpc/x.y", none("notadid#x")).await,
bad(500, "InternalServerError")
);
assert_eq!(
ask(Method::GET, "/xrpc/x.y", none(&format!("{d}#bsky_x"))).await,
bad(401, "AuthMissing")
);
assert_eq!(
ask(
Method::GET,
"/xrpc/x.y",
vec![("authorization", "Basic YTpi".into())]
)
.await,
bad(400, "InvalidToken")
);
assert_eq!(
ask(
Method::GET,
"/xrpc/x.y",
vec![("authorization", "Bearer nope".into())]
)
.await,
bad(400, "InvalidToken")
);
let refresh = call(
&s,
"com.atproto.server.createSession",
None,
json!({ "identifier": "alice.test", "password": "alice-pw" }),
)
.await
.unwrap();
let refresh = format!("Bearer {}", refresh["refreshJwt"].as_str().unwrap());
assert_eq!(
ask(Method::GET, "/xrpc/x.y", vec![("authorization", refresh)]).await,
bad(400, "InvalidToken")
);
assert!(appview.take().is_empty(), "nothing was sent");
// A `chat.bsky` method needs a privileged session.
let app_session = |privileged: bool| {
let (s, token) = (s.clone(), token.clone());
async move {
let name = format!("app{privileged}");
let p = call(
&s,
"com.atproto.server.createAppPassword",
Some(&token),
json!({ "name": name, "privileged": privileged }),
)
.await
.unwrap();
let login = json!({ "identifier": "alice.test", "password": p["password"] });
format!(
"Bearer {}",
call(&s, "com.atproto.server.createSession", None, login)
.await
.unwrap()["accessJwt"]
.as_str()
.unwrap()
)
}
};
let (plain, privileged) = (app_session(false).await, app_session(true).await);
assert_eq!(
ask(
Method::GET,
"/xrpc/CHAT.bsky.convo.listConvos",
vec![("authorization", plain.clone())]
)
.await,
bad(400, "InvalidToken")
);
assert!(appview.take().is_empty());
assert_eq!(
ask(
Method::GET,
"/xrpc/chat.bsky.convo.listConvos",
vec![("authorization", privileged)]
)
.await
.0,
200
);
assert_eq!(
ask(
Method::GET,
"/xrpc/app.bsky.feed.getPosts",
vec![("authorization", plain)]
)
.await
.0,
200
);
assert_eq!(appview.take().len(), 2);
// A method served, in another case, is served.
let (status, _, body) = send(
&base,
Method::GET,
"/xrpc/com.atproto.server.GETSESSION",
&[("authorization", auth.as_str())],
b"",
)
.await;
assert_eq!(
(
status,
serde_json::from_slice::<Json>(&body).unwrap()["handle"].clone()
),
(200, json!("alice.test"))
);
assert!(appview.take().is_empty());
// A document that is not the DID's.
*appview.doc.lock().unwrap() = json!({ "id": "did:web:other.test", "service": [] });
let other = Stub::start().await;
*other.doc.lock().unwrap() = json!({ "id": other.did, "service": [{ "id": "#bsky_x", "serviceEndpoint": other.url }] });
assert_eq!(
ask(
Method::GET,
"/xrpc/x.y",
with(&format!("{}#bsky_x", other.did))
)
.await,
bad(500, "InternalServerError")
);
}
/// Without a default AppView, every request that would go to it is
/// refused, and the `app.bsky` methods are forwarded as any other.
#[tokio::test]
async fn without_an_appview() {
let plc = plc().await;
let service = Stub::start().await;
let (s, base) = server("no-appview", &plc, None, false).await;
let (_, token) = create(&s, "alice").await;
let auth = format!("Bearer {token}");
for path in [
"/xrpc/app.bsky.feed.getPosts",
"/xrpc/app.bsky.actor.getPreferences",
"/xrpc/com.atproto.repo.getRecord?repo=bob.test&collection=a.b.c&rkey=x",
] {
let (status, _, body) = send(
&base,
Method::GET,
path,
&[("authorization", auth.as_str())],
b"",
)
.await;
assert_eq!(
(status, error(&body)),
(400, "InvalidRequest".into()),
"{path}"
);
}
// With a header, getPreferences is forwarded, its success status passed on.
service.answer(
"",
203,
&[("content-type", "application/json")],
b"{\"preferences\":[]}",
);
let proxy = format!("{}#bsky_appview", service.did);
let (status, _, _) = send(
&base,
Method::POST,
"/xrpc/app.bsky.actor.getPreferences",
&[
("authorization", auth.as_str()),
("atproto-proxy", proxy.as_str()),
],
b"x",
)
.await;
assert_eq!(status, 203);
let got = service.take();
assert_eq!(
(&got[0].method, &got[0].body[..]),
(&Method::POST, &b"x"[..])
);
}
/// A forwarded body is held to the cap of a method with a JSON input
/// (`spec/LIMITS.md`), read as `limits::read_body` reads every body: one
/// longer is `413` once the token is checked, and nothing is sent on.
#[tokio::test]
async fn forwarded_bodies_are_capped() {
let plc = plc().await;
let appview = Stub::start().await;
let (s, base) = server("forward-cap", &plc, Some(&appview), false).await;
let (_, token) = create(&s, "alice").await;
let auth = format!("Bearer {token}");
let path = "/xrpc/app.bsky.graph.muteActor";
let at_cap = vec![b'x'; crate::limits::MAX_BODY];
let (status, _, _) = send(
&base,
Method::POST,
path,
&[("authorization", auth.as_str())],
&at_cap,
)
.await;
assert_eq!(status, 200);
assert_eq!(appview.take()[0].body.len(), crate::limits::MAX_BODY);
let over = vec![b'x'; crate::limits::MAX_BODY + 1];
let (status, _, body) = send(
&base,
Method::POST,
path,
&[("authorization", auth.as_str())],
&over,
)
.await;
assert_eq!((status, error(&body)), (413, "PayloadTooLargeError".into()));
// A bad token is its own refusal, before the size.
let (status, _, body) = send(
&base,
Method::POST,
path,
&[("authorization", "Bearer nope")],
&over,
)
.await;
assert_eq!((status, error(&body)), (400, "InvalidToken".into()));
assert!(appview.take().is_empty());
}
/// With SSRF protection, no request goes to this machine.
#[tokio::test]
async fn ssrf_protection() {
let plc = plc().await;
let appview = Stub::start().await;
let (s, base) = server("ssrf", &plc, Some(&appview), true).await;
let (_, token) = create(&s, "alice").await;
let auth = format!("Bearer {token}");
let (status, _, body) = send(
&base,
Method::GET,
"/xrpc/app.bsky.feed.getPosts",
&[("authorization", auth.as_str())],
b"",
)
.await;
assert_eq!((status, error(&body)), (502, "UpstreamFailure".into()));
let proxy = format!("{}#bsky_x", appview.did);
let (status, _, body) = send(
&base,
Method::GET,
"/xrpc/app.bsky.feed.getPosts",
&[
("authorization", auth.as_str()),
("atproto-proxy", proxy.as_str()),
],
b"",
)
.await;
assert_eq!((status, error(&body)), (500, "InternalServerError".into()));
assert!(appview.take().is_empty());
}
/// A taken-down account's token makes no service token, by
/// `getServiceAuth` or for a forwarded call, which is sent nowhere; a
/// deactivated account's calls are forwarded (`spec/LIFECYCLE.md`).
#[tokio::test]
async fn accounts_not_active_and_other_services() {
use crate::space_credentials::tests::{deactivate, take_down};
let plc = plc().await;
let appview = Stub::start().await;
let (s, base) = server("forward-state", &plc, Some(&appview), false).await;
let (did, token) = create(&s, "alice").await;
let auth = format!("Bearer {token}");
let forward = || {
let (base, auth) = (base.clone(), auth.clone());
async move {
send(
&base,
Method::GET,
"/xrpc/app.bsky.feed.getPosts",
&[("authorization", auth.as_str())],
b"",
)
.await
}
};
let service_auth = |lxm: Option<&str>| {
let mut path =
"/xrpc/com.atproto.server.getServiceAuth?aud=did:web:svc.example".to_string();
if let Some(l) = lxm {
path.push_str(&format!("&lxm={l}"));
}
let (base, auth) = (base.clone(), auth.clone());
async move {
send(
&base,
Method::GET,
&path,
&[("authorization", auth.as_str())],
b"",
)
.await
}
};
deactivate(&s, &token, true).await;
assert_eq!(forward().await.0, 200);
assert_eq!(service_auth(None).await.0, 200);
assert_eq!(appview.take().len(), 1);
deactivate(&s, &token, false).await;
take_down(&s, &did, true).await;
let (status, _, body) = forward().await;
assert_eq!((status, error(&body)), (401, "AccountTakedown".into()));
for lxm in [None, Some("app.bsky.feed.getPosts")] {
let (status, _, body) = service_auth(lxm).await;
assert_eq!(
(status, error(&body)),
(401, "AccountTakedown".into()),
"{lxm:?}"
);
}
assert!(appview.take().is_empty(), "nothing sent");
}
/// What is read of a service (`spec/PROXY.md`, "SSRF protection", with
/// protection off): a `did:web` document that redirects is `500`, its
/// target never asked; one past 65536 bytes is `500`; an answer past
/// 16 MiB is `502 UpstreamFailure`.
#[tokio::test]
async fn what_is_read_of_a_service() {
let plc = plc().await;
let appview = Stub::start().await;
let (s, base) = server("forward-sizes", &plc, Some(&appview), false).await;
let (_, token) = create(&s, "alice").await;
let auth = format!("Bearer {token}");
let listener = tokio::net::TcpListener::bind(("127.0.0.1", 0))
.await
.unwrap();
let port = listener.local_addr().unwrap().port();
let asked = Arc::new(Mutex::new(Vec::<String>::new()));
let seen = asked.clone();
let app = axum::Router::new().fallback(move |uri: Uri| {
let seen = seen.clone();
async move {
seen.lock().unwrap().push(uri.path().to_string());
match uri.path() {
"/.well-known/did.json" => axum::response::Redirect::temporary("/real").into_response(),
_ => axum::Json(json!({ "id": format!("did:web:localhost%3A{port}"), "service": [{ "id": "#x", "type": "X", "serviceEndpoint": "http://127.0.0.1:1" }] })).into_response(),
}
}
});
tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
let ask = |proxy: String| {
let (base, auth) = (base.clone(), auth.clone());
async move {
send(
&base,
Method::GET,
"/xrpc/app.bsky.feed.getPosts",
&[
("authorization", auth.as_str()),
("atproto-proxy", proxy.as_str()),
],
b"",
)
.await
}
};
let (status, _, body) = ask(format!("did:web:localhost%3A{port}#x")).await;
assert_eq!((status, error(&body)), (500, "InternalServerError".into()));
assert_eq!(*asked.lock().unwrap(), ["/.well-known/did.json"]);
// A document of 70000 bytes is none; of 60000, read.
let padded = |n: usize| {
let mut doc = appview.doc.lock().unwrap().clone();
doc["padding"] = json!("");
let len = doc.to_string().len();
doc["padding"] = json!("x".repeat(n - len));
doc
};
*appview.doc.lock().unwrap() = padded(70000);
let (status, _, body) = ask(format!("{}#bsky_x", appview.did)).await;
assert_eq!((status, error(&body)), (500, "InternalServerError".into()));
assert!(appview.take().is_empty());
*appview.doc.lock().unwrap() = padded(60000);
let (status, _, _) = ask(format!("{}#bsky_x", appview.did)).await;
assert_eq!(status, 200);
appview.take();
// An answer of 17 MiB is 502; of exactly 16 MiB, answered.
appview.answer(
"/xrpc/app.bsky.feed.getPosts",
200,
&[("content-type", "application/octet-stream")],
&vec![b'x'; 17 << 20],
);
let (status, _, body) = send(
&base,
Method::GET,
"/xrpc/app.bsky.feed.getPosts",
&[("authorization", auth.as_str())],
b"",
)
.await;
assert_eq!((status, error(&body)), (502, "UpstreamFailure".into()));
appview.answer(
"/xrpc/app.bsky.feed.getPosts",
200,
&[("content-type", "application/octet-stream")],
&vec![b'x'; crate::outbound::FORWARD_MAX],
);
let (status, _, body) = send(
&base,
Method::GET,
"/xrpc/app.bsky.feed.getPosts",
&[("authorization", auth.as_str())],
b"",
)
.await;
assert_eq!((status, body.len()), (200, crate::outbound::FORWARD_MAX));
}
#[tokio::test]
async fn get_service_auth() {
let plc = plc().await;
let s = open(&scratch("service-auth"), &plc);
let (did, token) = create(&s, "alice").await;
let ask = |token: Option<String>, p: &[(&str, &str)]| {
let s = s.clone();
let p: Vec<(String, String)> = p
.iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect();
async move {
call_with(
&s,
"com.atproto.server.getServiceAuth",
token.as_deref(),
&p,
Json::Null,
)
.await
.map(|j| j["token"].as_str().unwrap().to_string())
}
};
let tok = Some(token.clone());
let got = ask(tok.clone(), &[("aud", "did:web:api.bsky.app#bsky_appview")])
.await
.unwrap();
let c = claims(&s, &got, &did);
assert_eq!(keys(&c), ["aud", "exp", "iat", "iss", "jti"]);
assert_eq!(
(&c["aud"], &c["iss"]),
(&json!("did:web:api.bsky.app#bsky_appview"), &json!(did))
);
assert_eq!(c["exp"].as_i64().unwrap() - c["iat"].as_i64().unwrap(), 60);
let now = s.sessions.now() as i64;
let exp = (now + 1800).to_string();
let c = claims(
&s,
&ask(
tok.clone(),
&[
("aud", "did:plc:abcdefghijklmnopqrstuvwx"),
("lxm", "com.atproto.repo.uploadBlob"),
("exp", &exp),
],
)
.await
.unwrap(),
&did,
);
assert_eq!(
(&c["lxm"], c["exp"].as_i64().unwrap()),
(&json!("com.atproto.repo.uploadBlob"), now + 1800)
);
let c = claims(
&s,
&ask(
tok.clone(),
&[("aud", "did:web:localhost%3A2583"), ("exp", "0")],
)
.await
.unwrap(),
&did,
);
assert_eq!(c["exp"], 0);
let refused = |r: Result<String, String>| r.err().unwrap();
// The parameters are checked before the token.
assert_eq!(
refused(ask(None, &[("aud", "x"), ("lxm", "*")]).await),
"InvalidRequest"
);
assert_eq!(refused(ask(None, &[("exp", "1")]).await), "InvalidRequest");
assert_eq!(
refused(ask(None, &[("aud", "x"), ("exp", "soon")]).await),
"InvalidRequest"
);
assert_eq!(refused(ask(None, &[("aud", "x")]).await), "AuthMissing");
assert_eq!(
refused(ask(Some("nope".into()), &[("aud", "x")]).await),
"InvalidToken"
);
for aud in [
"did:key:zQ3sh",
"did:example:x",
"did:plc:abc",
"did:web:x.test#",
"did:web:x.test%3A443",
] {
assert_eq!(
refused(ask(tok.clone(), &[("aud", aud)]).await),
"InvalidRequest",
"{aud}"
);
}
let (aud, past, far, minute) = (
"did:web:x.test",
(now - 10).to_string(),
(now + 3700).to_string(),
(now + 120).to_string(),
);
for exp in [&past, &far, &minute] {
assert_eq!(
refused(ask(tok.clone(), &[("aud", aud), ("exp", exp)]).await),
"BadExpiration",
"{exp}"
);
}
ask(
tok.clone(),
&[
("aud", aud),
("exp", &minute),
("lxm", "app.bsky.feed.getTimeline"),
],
)
.await
.unwrap();
for lxm in [
"com.atproto.server.getSession",
"COM.ATPROTO.SERVER.UPDATEEMAIL",
] {
assert_eq!(
refused(ask(tok.clone(), &[("aud", aud), ("lxm", lxm)]).await),
"InvalidRequest",
"{lxm}"
);
}
// A privileged method needs a privileged session.
let p = call(
&s,
"com.atproto.server.createAppPassword",
Some(&token),
json!({ "name": "p" }),
)
.await
.unwrap();
let login = json!({ "identifier": "alice.test", "password": p["password"] });
let app = call(&s, "com.atproto.server.createSession", None, login)
.await
.unwrap()["accessJwt"]
.as_str()
.unwrap()
.to_string();
for lxm in [
"chat.bsky.convo.listConvos",
"com.atproto.server.CREATEACCOUNT",
] {
assert_eq!(
refused(ask(Some(app.clone()), &[("aud", aud), ("lxm", lxm)]).await),
"InvalidRequest",
"{lxm}"
);
ask(tok.clone(), &[("aud", aud), ("lxm", lxm)])
.await
.unwrap();
}
ask(
Some(app),
&[("aud", aud), ("lxm", "app.bsky.feed.getTimeline")],
)
.await
.unwrap();
}
/// The app's reads show the account's records the AppView has not seen.
#[tokio::test]
async fn read_after_write() {
let plc = plc().await;
let appview = Stub::start().await;
let (s, base) = server("read-after-write", &plc, Some(&appview), false).await;
let (did, token) = create(&s, "alice").await;
let auth = format!("Bearer {token}");
let write = |collection: &str, rkey: Option<&str>, record: Json| {
let (s, token, did) = (s.clone(), token.clone(), did.clone());
let mut body = json!({ "repo": did, "collection": collection, "record": record });
if let Some(k) = rkey {
body["rkey"] = json!(k);
}
async move {
call(&s, "com.atproto.repo.createRecord", Some(&token), body)
.await
.unwrap()["commit"]["rev"]
.as_str()
.unwrap()
.to_string()
}
};
let post = |text: &str| json!({ "$type": "app.bsky.feed.post", "text": text, "createdAt": "2026-01-01T00:00:00.000Z" });
let before = write("app.bsky.feed.post", None, post("p0")).await;
write(
"app.bsky.actor.profile",
Some("self"),
json!({ "$type": "app.bsky.actor.profile", "displayName": "Alice" }),
)
.await;
write("app.bsky.feed.post", None, post("p1")).await;
let view = json!({ "did": did, "handle": "alice.test", "displayName": "Old", "followersCount": 1 });
let typed = |rev: &str| {
vec![
("content-type", "application/json".to_string()),
("atproto-repo-rev", rev.to_string()),
("content-language", "en".to_string()),
]
};
let canned = |path: &str, rev: &str, body: &Json| {
let h = typed(rev);
let h: Vec<(&'static str, &str)> = h.iter().map(|(n, v)| (*n, v.as_str())).collect();
appview.answer(path, 203, &h, body.to_string().as_bytes());
};
let get = |path: &str| {
let (base, auth, path) = (base.clone(), auth.clone(), path.to_string());
async move {
send(
&base,
Method::GET,
&path,
&[("authorization", auth.as_str())],
b"",
)
.await
}
};
let profile = format!("/xrpc/app.bsky.actor.getProfile?actor={did}");
canned("/xrpc/app.bsky.actor.getProfile", &before, &view);
let (status, h, body) = get(&profile).await;
assert_eq!(status, 200);
assert_eq!(h["content-type"], "application/json");
assert!(
h.get("atproto-upstream-lag").is_some()
&& h.get("atproto-repo-rev").is_none()
&& h.get("content-language").is_none()
);
assert_eq!(
serde_json::from_slice::<Json>(&body).unwrap(),
json!({ "did": did, "handle": "alice.test", "displayName": "Alice", "followersCount": 1 })
);
let got = appview.take();
assert_eq!(
(
got[0].path.as_str(),
got[0].headers["accept-encoding"].to_str().unwrap()
),
(profile.as_str(), "identity")
);
let c = claims(&s, got[0].headers["authorization"].to_str().unwrap(), &did);
assert_eq!(
(&c["aud"], &c["lxm"]),
(&json!(appview.did), &json!("app.bsky.actor.getProfile"))
);
// A rev before every record, or an answer not valid by the lexicon, is answered as it is.
for (rev, body) in [
("2222222222222", view.clone()),
(before.as_str(), json!({ "did": did })),
] {
canned("/xrpc/app.bsky.actor.getProfile", rev, &body);
let (status, h, answer) = get(&profile).await;
assert_eq!(
(status, serde_json::from_slice::<Json>(&answer).unwrap()),
(200, body)
);
assert_eq!(
(
h["atproto-repo-rev"].to_str().unwrap(),
h.get("atproto-upstream-lag")
),
(rev, None)
);
}
// The timeline gets the post the AppView has not seen.
canned(
"/xrpc/app.bsky.feed.getTimeline",
&before,
&json!({ "feed": [] }),
);
let (status, _, body) = get("/xrpc/app.bsky.feed.getTimeline").await;
let body: Json = serde_json::from_slice(&body).unwrap();
assert_eq!((status, body["feed"].as_array().unwrap().len()), (200, 1));
let p = &body["feed"][0]["post"];
assert_eq!(
(&p["record"]["text"], &p["author"]),
(
&json!("p1"),
&json!({ "did": did, "handle": "alice.test", "displayName": "Alice" })
)
);
// getPostThread is forwarded and answered as it is.
canned(
"/xrpc/app.bsky.feed.getPostThread",
&before,
&json!({ "thread": {} }),
);
let (status, h, _) = get(&format!(
"/xrpc/app.bsky.feed.getPostThread?uri=at://{did}/app.bsky.feed.post/x"
))
.await;
assert_eq!(
(status, h.get("atproto-upstream-lag").is_none()),
(200, true)
);
// Parameters are checked before anything is sent.
let (status, _, _) = get("/xrpc/app.bsky.actor.getProfile").await;
assert_eq!(status, 400);
appview.take();
// getRecord of a repository not here is forwarded without a token.
appview.answer(
"",
201,
&[("content-type", "application/json")],
br#"{"uri":"at://did:plc:bbbbbbbbbbbbbbbbbbbbbbbb/a.b.c/x","value":{}}"#,
);
let path = "/xrpc/com.atproto.repo.getRecord?repo=did:plc:bbbbbbbbbbbbbbbbbbbbbbbb&collection=a.b.c&rkey=x";
let (status, _, _) = get(path).await;
let got = appview.take();
assert_eq!(
(
status,
got[0].path.as_str(),
got[0].headers.get("authorization")
),
(200, path, None)
);
// One of a repository here is not.
let (status, _, body) = get(&format!("/xrpc/com.atproto.repo.getRecord?repo={did}&collection=app.bsky.actor.profile&rkey=self")).await;
assert_eq!(
(
status,
serde_json::from_slice::<Json>(&body).unwrap()["value"]["displayName"].clone()
),
(200, json!("Alice"))
);
assert!(appview.take().is_empty());
// One of an account here that is not active is, as of one not here (spec/LIFECYCLE.md).
call(
&s,
"com.atproto.server.deactivateAccount",
Some(&token),
json!({}),
)
.await
.unwrap();
appview.answer(
"",
201,
&[("content-type", "application/json")],
br#"{"uri":"at://did:plc:bbbbbbbbbbbbbbbbbbbbbbbb/a.b.c/x","value":{}}"#,
);
let mine = format!(
"/xrpc/com.atproto.repo.getRecord?repo={did}&collection=app.bsky.actor.profile&rkey=self"
);
let (status, _, _) = get(&mine).await;
let got = appview.take();
assert_eq!(
(
status,
got[0].path.as_str(),
got[0].headers.get("authorization")
),
(200, mine.as_str(), None)
);
}
#[tokio::test]
async fn get_feed() {
let plc = plc().await;
let appview = Stub::start().await;
let (s, base) = server("get-feed", &plc, Some(&appview), false).await;
let (did, token) = create(&s, "alice").await;
let auth = format!("Bearer {token}");
let get = |path: String| {
let (base, auth) = (base.clone(), auth.clone());
async move {
send(
&base,
Method::GET,
&path,
&[("authorization", auth.as_str()), ("accept-language", "fr")],
b"",
)
.await
}
};
let record = |value: Json| {
json!({ "uri": "at://did:plc:aaaaaaaaaaaaaaaaaaaaaaaa/app.bsky.feed.generator/hot", "value": value }).to_string()
};
let json_type = [("content-type", "application/json")];
appview.answer(
"/xrpc/com.atproto.repo.getRecord",
200,
&json_type,
record(json!({ "did": "did:web:feeds.test" })).as_bytes(),
);
appview.answer(
"/xrpc/app.bsky.feed.getFeed",
200,
&json_type,
br#"{"feed":[]}"#,
);
let feed = "/xrpc/app.bsky.feed.getFeed?feed=at://alice.test/app.bsky.feed.generator/hot";
let (status, _, body) = get(feed.into()).await;
assert_eq!((status, &body[..]), (200, &br#"{"feed":[]}"#[..]));
let got = appview.take();
assert_eq!(
got[0].path,
"/xrpc/com.atproto.repo.getRecord?repo=alice.test&collection=app.bsky.feed.generator&rkey=hot"
);
// None of the client's headers: `Accept-Language` is the server's own.
assert!(got[0].headers.get("authorization").is_none());
assert_eq!(
got[0].headers["accept-language"].to_str().unwrap(),
OWN_LANGUAGE
);
assert_eq!(
(
got[1].path.as_str(),
got[1].headers["accept-language"].to_str().unwrap()
),
(feed, "fr")
);
let c = claims(&s, got[1].headers["authorization"].to_str().unwrap(), &did);
assert_eq!(
(&c["aud"], &c["lxm"]),
(
&json!("did:web:feeds.test"),
&json!("app.bsky.feed.getFeedSkeleton")
)
);
let refused = |path: String| {
let get = get(path);
async move {
let (status, _, body) = get.await;
(status, error(&body))
}
};
let feed = |uri: &str| format!("/xrpc/app.bsky.feed.getFeed?feed={uri}");
for uri in ["at://alice.test", "at://alice.test/app.bsky.feed.generator"] {
assert_eq!(
refused(feed(uri)).await,
(500, "InternalServerError".into()),
"{uri}"
);
}
// A trailing `/` makes it no AT URI (VALIDATION.md): the parameter is refused.
assert_eq!(
refused(feed("at://alice.test/app.bsky.feed.generator/")).await,
(400, "InvalidRequest".into())
);
assert!(appview.take().is_empty());
appview.answer(
"/xrpc/com.atproto.repo.getRecord",
200,
&json_type,
record(json!({ "did": 5 })).as_bytes(),
);
assert_eq!(
refused(feed("at://alice.test/a.b.c/k")).await,
(400, "InvalidRequestError".into())
);
appview.answer(
"/xrpc/com.atproto.repo.getRecord",
200,
&json_type,
br#"{"uri":"at://x"}"#,
);
assert_eq!(
refused(feed("at://alice.test/a.b.c/k")).await,
(502, "InvalidResponse".into())
);
appview.answer(
"/xrpc/com.atproto.repo.getRecord",
400,
&json_type,
br#"{"error":"RecordNotFound"}"#,
);
assert_eq!(
refused(feed("at://alice.test/a.b.c/k")).await,
(400, "RecordNotFound".into())
);
assert_eq!(appview.take().len(), 3, "only the feed's record was read");
}
#[tokio::test]
async fn push_registration() {
let plc = plc().await;
let (appview, notif) = (Stub::start().await, Stub::start().await);
let (s, base) = server("push", &plc, Some(&appview), false).await;
let (did, token) = create(&s, "alice").await;
let auth = format!("Bearer {token}");
let register = |service: &str, nsid: &str| {
let input = json!({ "serviceDid": service, "token": "t", "platform": "ios", "appId": "app", "extra": [1] });
let (base, auth, path) = (base.clone(), auth.clone(), format!("/xrpc/{nsid}"));
async move {
let h = [
("authorization", auth.as_str()),
("content-type", "application/json"),
("accept-language", "fr"),
("x-atproto-x", "1"),
];
let (status, _, body) =
send(&base, Method::POST, &path, &h, input.to_string().as_bytes()).await;
(
status,
if body.is_empty() {
String::new()
} else {
error(&body)
},
input,
)
}
};
notif.answer(
"",
200,
&[("content-type", "application/json")],
b"{\"ignored\":true}",
);
let (status, name, input) =
register(¬if.did, "app.bsky.notification.registerPush").await;
assert_eq!((status, name), (200, String::new()));
let got = notif.take();
let g = &got[0];
assert_eq!(
(&g.method, g.path.as_str()),
(&Method::POST, "/xrpc/app.bsky.notification.registerPush")
);
assert_eq!(
(
g.headers["content-type"].to_str().unwrap(),
serde_json::from_slice::<Json>(&g.body).unwrap()
),
("application/json", input)
);
assert!(g.headers.get("x-atproto-x").is_none());
assert_eq!(g.headers["accept-language"].to_str().unwrap(), OWN_LANGUAGE);
let c = claims(&s, g.headers["authorization"].to_str().unwrap(), &did);
assert_eq!(
(&c["aud"], &c["lxm"]),
(
&json!(notif.did),
&json!("app.bsky.notification.registerPush")
)
);
// The default AppView's DID is its URL, unread.
assert_eq!(
register(&appview.did, "app.bsky.notification.unregisterPush")
.await
.0,
200
);
assert_eq!(
appview.take()[0].path,
"/xrpc/app.bsky.notification.unregisterPush"
);
// The service's answers.
notif.answer("", 200, &[("content-type", "application/json")], b"");
assert_eq!(
register(¬if.did, "app.bsky.notification.registerPush")
.await
.1,
"InvalidResponse"
);
notif.answer("", 200, &[], b"not json");
assert_eq!(
register(¬if.did, "app.bsky.notification.registerPush")
.await
.0,
200
);
notif.answer("", 500, &[], b"");
assert_eq!(
register(¬if.did, "app.bsky.notification.registerPush")
.await
.1,
"UpstreamFailure"
);
// A service with no notification service of that type, or no DID document.
let other = Stub::start().await;
*other.doc.lock().unwrap() = json!({ "id": other.did, "service": [{ "id": "#bsky_notif", "type": "Other", "serviceEndpoint": other.url }] });
assert_eq!(
register(&other.did, "app.bsky.notification.registerPush")
.await
.1,
"InvalidRequest"
);
let gone = Stub::start().await;
*gone.doc.lock().unwrap() = Json::Null;
assert_eq!(
register(&gone.did, "app.bsky.notification.registerPush")
.await
.1,
"InvalidRequest"
);
assert_eq!(
register(
"did:plc:aaaaaaaaaaaaaaaaaaaaaaaa",
"app.bsky.notification.registerPush"
)
.await
.1,
"InvalidRequest"
);
// A taken-down account's is refused where its service token would be made, and sent nowhere.
let admin = format!(
"Basic {}",
base64::Engine::encode(
&base64::engine::general_purpose::STANDARD,
format!("admin:{}", crate::testing::ADMIN_PASSWORD)
)
);
let down = json!({ "subject": { "$type": "com.atproto.admin.defs#repoRef", "did": did }, "takedown": { "applied": true } });
crate::testing::call_raw(
&s,
"com.atproto.admin.updateSubjectStatus",
crate::sessions::Bearer::of(Some(&admin)),
&[],
&down.to_string(),
)
.await
.unwrap();
let _ = (notif.take(), appview.take());
for (service, nsid) in [
(¬if.did, "app.bsky.notification.registerPush"),
(&appview.did, "app.bsky.notification.unregisterPush"),
] {
assert_eq!(register(service, nsid).await.1, "AccountTakedown", "{nsid}");
}
let sent: Vec<String> = notif
.take()
.into_iter()
.chain(appview.take())
.map(|g| g.path)
.collect();
assert!(sent.iter().all(|p| !p.starts_with("/xrpc/")), "{sent:?}");
}
}