diff --git a/docs/4-implementation/swarm/credentials.md b/docs/4-implementation/swarm/credentials.md index bf1703aeb..24844404b 100644 --- a/docs/4-implementation/swarm/credentials.md +++ b/docs/4-implementation/swarm/credentials.md @@ -142,6 +142,25 @@ the create that makes a new key. No principal hyperhive configures may delete either path's metadata, so run the two deletes with a token that may, such as the root token. +## What authenticates a callout request + +The responder never reads the signature segment of the request JWT. +`request::decode` splits the three segments and decodes the claims, and +nothing checks the third. The responder takes a request as the server's when +it opens: the server seals the request (a curve25519 box) to the callout +xkey, and the `Nats-Server-Xkey` header carries the key it sealed with. The +responder uses that key as sent, compares it with no key it holds, and seals +the reply back to it. A request without the header, or one that doesn't open, +gets no reply. + +The signature and the sender key stay unchecked on purpose, for three reasons. + +- Only the responder's own key can publish to `$SYS.REQ.USER.AUTH`. It's the one member of the callout account (`AUTH`) and the one `auth_users` bypass in `swarm-nats.nix`. The server has no system account, and no account shares anything with `AUTH`. Whoever holds that seed can already read the issuer seed and mint any user JWT, so a forged request gives them nothing new. +- A forger with a valid bearer gets what a normal connect gets: the responder decides the permissions from the bearer inside the request. +- Pinning has no key to pin. `nats-server` 2.14.4 makes a new random nkey and xkey at each start and has no option to set either, so the responder holds no server key to compare the header with. Under FIPS mode the server makes no xkey, so no header arrives and the responder refuses the request. + +The guard is the module-eval case `the callout account has exactly one member and no system account is configured` in `nix/module-eval/nats-authelia.nix`. An extra member of `AUTH` could subscribe to the subject and read every client's token, which no signature check would stop. Revisit this decision if that case changes: when `AUTH` gains a member, or the server gets a system account. + ## Per-principal identities Bao matches a cert-auth role on the certificate's subject, so a certificate is an diff --git a/swarm-nats-auth/src/main.rs b/swarm-nats-auth/src/main.rs index 408822b2d..1ca5f7f1c 100644 --- a/swarm-nats-auth/src/main.rs +++ b/swarm-nats-auth/src/main.rs @@ -16,9 +16,9 @@ //! //! Every request is opened with the callout xkey, and its reply is sealed //! back to the server; see `xkey`. A request that does not open gets no -//! reply: the server this answers always seals. The `ensure-callout-keys` -//! verb creates that key and the two nkeys (see `nkey`) in the secret store, -//! and runs before the server does. +//! reply: the server this answers always seals. The signature is never read, on +//! purpose (see `docs/4-implementation/swarm/credentials.md`). `ensure-callout-keys` +//! first creates that key and the two nkeys (see `nkey`) in the store. //! //! # Secrets //! diff --git a/swarm-nats-auth/src/oidc_secret.rs b/swarm-nats-auth/src/oidc_secret.rs index 3f2ca1c37..657744a01 100644 --- a/swarm-nats-auth/src/oidc_secret.rs +++ b/swarm-nats-auth/src/oidc_secret.rs @@ -26,7 +26,7 @@ use crate::keystore::KeyStore; /// The path under the store's KV mount holding `client_id`'s secret. /// Authelia's mint (`authelia-swarm-secrets`) writes it, at the path -/// `swarm-secret-publisher.nix` lists, and `natsAuthReaders` in `swarm-bao.nix` +/// `swarm-secret-publisher.nix` lists, and `natsAuthReaders` in `swarm-bao-readers.nix` /// grants this responder's role the read. /// /// # Errors diff --git a/swarm-nats-auth/src/request.rs b/swarm-nats-auth/src/request.rs index 1504bcdf7..43b4ecb80 100644 --- a/swarm-nats-auth/src/request.rs +++ b/swarm-nats-auth/src/request.rs @@ -63,8 +63,13 @@ pub struct Claims { /// Split a NATS JWT and deserialize its claim set. /// -/// The signature is **not** verified here — see the module docs on -/// `main.rs` for why that is a decision and not an oversight. +/// The signature is **not** verified here, on purpose: only the responder's own +/// key can publish to the request subject, and a forger gains nothing the key +/// holder lacks. The guard is the module-eval case "the callout account has +/// exactly one member and no system account is configured"; revisit this if +/// `AUTH` gains a member or a system account appears. The reasoning is in +/// `docs/4-implementation/swarm/credentials.md`, "What authenticates a callout +/// request". pub fn decode(payload: &[u8]) -> Result { let text = std::str::from_utf8(payload).context("auth request is not utf-8")?; let mut parts = text.split('.'); @@ -81,10 +86,8 @@ pub fn decode(payload: &[u8]) -> Result { serde_json::from_slice(&raw).context("claim segment is not the expected JSON") } -/// base64url without padding, as JWT uses. Written out rather than pulled in: -/// `data-encoding` is already in the tree via `nkeys`, but its no-pad URL -/// alphabet spec is one line either way and this keeps the direct dependency -/// list honest about what this crate actually needs. +/// base64url without padding, as JWT uses, with `data-encoding`'s no-pad URL +/// alphabet (a direct dependency of this crate). fn base64_url_decode(s: &str) -> Result> { data_encoding::BASE64URL_NOPAD .decode(s.as_bytes()) diff --git a/swarm-nats-auth/src/respond.rs b/swarm-nats-auth/src/respond.rs index 0c20a3cf2..c3da3892f 100644 --- a/swarm-nats-auth/src/respond.rs +++ b/swarm-nats-auth/src/respond.rs @@ -79,8 +79,8 @@ struct AuthResponse { version: i64, } -/// Sign a claim set the way `nats-jwt` does. `now` is injected so the test -/// can pin it; production passes the wall clock. +/// Sign a claim set the way `nats-jwt` does. The claims already carry their +/// times (`iat`, `exp`), set by the caller, so signing reads no clock. fn sign(mut claims: Claims, key: &KeyPair) -> String { claims.jti = String::new(); let unhashed = serde_json::to_string(&claims).expect("claims serialisation cannot fail"); diff --git a/swarm-nats-auth/src/xkey.rs b/swarm-nats-auth/src/xkey.rs index 399966eaf..9436c5bae 100644 --- a/swarm-nats-auth/src/xkey.rs +++ b/swarm-nats-auth/src/xkey.rs @@ -101,6 +101,12 @@ fn from_seed(seed: &str) -> Result { /// `payload` opened with `own`, and the server key `server_key` names, which /// the reply is sealed to. /// +/// That key is taken as the header gives it, compared with none: `nats-server` +/// makes a new xkey at each start and has no option to set it, so there is +/// none to pin. (Under FIPS mode it makes none, so no header arrives and the +/// request is refused.) See `docs/4-implementation/swarm/credentials.md`, "What +/// authenticates a callout request". +/// /// # Errors /// When there is no [`SERVER_XKEY_HEADER`], when it is not a public xkey, and /// when the payload does not open with `own`.