~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0

Apps sign in to a group with OAuth, approved by a member in the console

Merged
Merged chadtmiller.com asked to merge into main from chadtmiller.com/group-host:host/oauth

What changes

The OAuth server. The host is the authorization server an app signs in to a group with, as atproto profiles OAuth (oauth.rs): its metadata at /.well-known/oauth-authorization-server and /.well-known/oauth-protected-resource; pushed requests at /oauth/par, required, with PKCE (S256) and a DPoP proof; /oauth/authorize, which sends the browser on to the console's /authorize (new CONSOLE_URL, the public URL by default); /oauth/token for a code (once, within a minute) or a refresh token (a new pair, the old one ended); and /oauth/revoke for either token. Every endpoint answers any origin, as a browser app reaches them.

Clients (oauth_clients.rs) are known by their metadata documents (https, no redirects, at most 64 KiB, kept ten minutes, checked as @atproto/oauth-provider checks them), or are the loopback client http://localhost. A confidential client signs each request with a key its metadata lists (private_key_jwt, inline or by jwks_uri) and must keep to that key.

DPoP (oauth_dpop.rs): ES256 or ES256K proofs; a nonce is an HMAC of the time, checked without being kept; each proof's jti, like each client assertion's, is spent once.

Grants (oauth_store.rs): tokens are opaque and kept only as their SHA-256. A grant records the member who approved it, and ends when they are ejected or leave.

Approving (authorization.rs): getAuthorization offers the groups the caller may sign the app in as (the one the app named in login_hint, by handle or DID, or each active group where they hold a role), with what the app would get and not get in each; approveAuthorization signs it in as the chosen group, for a member holding a role there, and the group the app named if it named one; declineAuthorization sends it back with access_denied.

What a token may do (oauth_scope.rs): the scope asked for is narrowed to the member's roles (repo collections and other services' methods by the permissions record's grants, the configure set for roles that configure, space scopes by each space's access record, space:* made one scope per space, blobs only beside a write), then made concrete: the configure set as its lexicon's space scopes, authority=self as the group, and a space scope naming no collection as naming every one.

atproto::jose: public keys as JWKs, RFC 7638 thumbprints, and ES256/ES256K JWTs signed and checked.

The design is in docs/design/signing-in.md.

Why

Step 6 of docs/design/README.md: apps act as a group through tokens a member approves, never a password. This is its first part. What the tokens do (writes as the group, group tokens, app passwords, connected apps) and provisioning come next.

What changed from the prototype:

  • Tokens are opaque and hashed, where the prototype signed access JWTs and kept refresh tokens in the clear. The host looks a token up on every use anyway, so a signature added nothing.
  • No lexicon is resolved at sign-in. The prototype withheld every permission set but the host's own, and resolved that one, and space types for a bare space scope's collections. Here the host's own set is read from its lexicon, and a bare space scope keeps every collection, which narrowing allows only to roles that may write every one there. That keeps sign-in off DNS and other servers, and never wider than what was approved.
  • DPoP and client assertions take ES256 and ES256K, the algorithms atproto names, not everything jose knows.
  • A grant records its member, not one of their roles.
  • The group's picture in getAuthorization is its getSpaceBlob URL.

How it was checked

  • crates/atproto/tests/jose.rs: the JWKs, thumbprints and proofs jose 5.10.0 makes of fixed P-256 and secp256k1 keys (tests/reference/jose.mjs → jose.json), RFC 9449's example thumbprint, and RFC 7636's PKCE example.
  • tests/oauth.rs: metadata, CORS, the nonce handshake, each refused request and proof, the browser's way to the console, codes (once, from their own client and key, not lapsed), refreshing, revoking by either token, and grants ending when a member is ejected. These port the prototype's authorization.test.ts sections on the pushed request, the token and the redirect.
  • tests/oauth_clients.rs: a confidential client end to end, inline and by jwks_uri; each refused assertion; holding a client to its first key; each metadata rule; reading documents only where it should; the loopback client; the cache. These port a confidential client and sameClientAuth.
  • tests/authorization.rs: getAuthorization, approveAuthorization and declineAuthorization, porting their sections.
  • tests/oauth_scope.rs: narrowing and making concrete, porting the narrowScope and self authority sections.

CI runs the 100% gate, clippy, and just lex.

Shortcomings

  • A token is not yet taken anywhere: the resource side (checking a DPoP-bound token on XRPC, and the writes it allows) is the next pull.
  • The console's /authorize page comes with step 7, so for now a member approves only through the methods.
  • space:* made concrete for a role granted * and a collection there lists both, the wider covering the narrower, as the prototype did.
Asked for changes

This pull adds the host's OAuth server: PAR, DPoP, PKCE and client metadata and assertions. It also adds opaque hashed grants, the approve/decline methods, scope narrowing and atproto::jose. The token and grant logic reads sound: codes are single-use, refresh rotation is atomic, and PKCE, key binding and same_auth are all checked. Two things need to change first.

  1. SSRF through client documents. fetch_json (oauth_clients.rs:236) fetches any https URL. The URL comes from the client_id of an unauthenticated PAR, or from a jwks_uri. It does not refuse loopback, private, link-local or metadata-service addresses, and redirects are off but the target is not filtered. Its errors also reach the caller (answered 404, too large, not a JSON object). That makes a blind probe of the host's internal network. @atproto/oauth-provider, which this claims to follow, refuses such hosts. Please check the resolved address before connecting and refuse non-public ones. In tests that use http on localhost, allow it only under the existing http_here condition. Add a test.
  2. The loopback redirect check is a prefix match. loopback() (oauth_clients.rs:101) uses starts_with("http://127.0.0.1"). http://127.0.0.1.evil.example/cb passes, so a client id of http://localhost?redirect_uri=http://127.0.0.1.evil.example/cb is accepted as "An app on this device" while redirecting off the machine. Parse the URL and compare host_str() to 127.0.0.1 or [::1], as registered() does. Add a test.

Smaller points, not blocking:

  • same_auth compares kid, and a missing kid becomes "". authenticate_client then tries every key in the set, so omitting kid defeats "must keep to that key". Require a kid, or compare the thumbprint of the key that verified.
  • nonce_at HMACs with the PLC rotation private key. A key for another purpose is safer, such as one derived from it or a separate secret.
  • A grant keeps the scope it was approved with, through refreshes of up to 90 days. If a member's roles shrink, the scope does not narrow. Make sure the resource-side pull re-narrows on use, and say so in signing-in.md.
  • code_verifier length (43–128 characters) is not checked.

I read the new source files in crates/atproto and crates/group-host/src (jose, oauth, clients, dpop, store, authorization). I did not read oauth_scope.rs, the tests, the docs or the reference fixtures. The test and check runs were still queued when I looked.

crates/group-host/src/oauth_clients.rsline 236Outdated
/// The JSON at `url`, an https URL (or http, while this host is on
/// http itself, as a network of test servers is).
async fn fetch_json(&self, url: &str, what: &str) -> Result<Value, OAuthError> {
review.knotless.dev

Fetches any https host, including private and loopback addresses, and the errors reach the caller. Refuse non-public addresses before connecting.

crates/group-host/src/oauth_clients.rsline 101Outdated
return Err(invalid_metadata("the loopback client id names one scope"));
}
for r in &redirects {
if !r.starts_with("http://127.0.0.1") && !r.starts_with("http://[::1]") {
review.knotless.dev

starts_with accepts http://127.0.0.1.evil.example/…. Compare the parsed host instead.

crates/group-host/src/oauth_clients.rsline 179Outdated
/// Whether a later request authenticates as the first did: the same
/// method, and for a confidential client, the same key.
pub fn same_auth(first: &Value, now: &Value) -> bool {
first["method"] == now["method"] && first["kid"] == now["kid"]
review.knotless.dev

A missing kid becomes "" and matches any key in the set, so this does not hold the client to its first key.

Fixed in 23f6158:

  1. A client document or key set is fetched only from a public address: the name is resolved first, every address must be public (not loopback, private, link-local, shared, reserved, documentation or multicast, v4-mapped included; oauth_clients::public), and the connection is pinned to those addresses, so a rebinding name is not followed elsewhere. Only a host on http itself (a test network) may fetch from anywhere. Tests: 127.0.0.1, localhost, 10.0.0.1, [::1] and 169.254.169.254 refused; knows_a_public_address over each range.
  2. A loopback client's redirects are parsed: http, and the host exactly 127.0.0.1 or [::1]. 127.0.0.1.evil.example, https and a non-URL are refused in tests.

The smaller points:

  • A confidential client is held to the thumbprint of the key that verified its assertion, not its kid: a test signs with the other key naming no kid and is refused, and the first key naming none passes.
  • Nonces HMAC under a key derived from the host's secret for nonces alone.
  • Re-narrowing on use: the resource-side pull will ask the member's roles again on each use; signing-in.md says so now.
  • A code_verifier must be 43 to 128 unreserved characters.

Also: a database failure answers server_error (tested), and the races in approve and decline return without a branch nothing reaches.

Approved

Both blocking findings from round 1 are fixed, and so are the smaller points.

  • SSRF: fetch_json (oauth_clients.rs) resolves the host first and refuses the fetch unless every address passes public(). public() covers loopback, private, link-local, shared, reserved, documentation and multicast ranges, with v4-mapped v6 included. The client then pins the connection to the addresses it checked, so DNS rebinding can't redirect it. Redirects stay off. Plain http to any address is allowed only when the host's own public_url is http. Tests cover the refused addresses.
  • Loopback check: registered() parses the URL and compares the host exactly. The loopback() client-id check was reported as fixed with tests, but I did not re-read it.
  • kid and same_auth: it now compares the key thumbprint (jkt), so omitting kid no longer defeats it.
  • Smaller points: nonces use a derived key, the code_verifier length is checked, and signing-in.md now says the resource side re-narrows on use. I did not re-read these.

All three workflows pass at 23f6158: test, check, and the two path-skipped ones. As in round 1, I did not read oauth_scope.rs, the tests or the docs.

Merged as 99eb1cb