diff --git a/README.md b/README.md index 4adf06b..d53cdbf 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ export TESSERA_VAULT_PASSPHRASE='choose a long private passphrase' ./target/release/tessera-hub ``` -The hub listens on **127.0.0.1:8787** unless `TESSERA_LISTEN` is set. Open [http://127.0.0.1:8787](http://127.0.0.1:8787). The OpenAPI document is at `/api-doc/openapi.json`. There is no default passphrase, and new vaults refuse the old published value `dev-vault-key`. Leave `TESSERA_SEED_DEMO` unset; `TESSERA_SEED_DEMO=1` writes demo backup history into the data directory. +The hub listens on **127.0.0.1:8787** unless `TESSERA_LISTEN` is set. Open [http://127.0.0.1:8787](http://127.0.0.1:8787). The OpenAPI document is at `/api-doc/openapi.json` for signed-in operators (set `TESSERA_PUBLIC_OPENAPI=true` to publish it). There is no default passphrase, and new vaults refuse the old published value `dev-vault-key`. Leave `TESSERA_SEED_DEMO` unset; `TESSERA_SEED_DEMO=1` writes demo backup history into the data directory. Authentication is required by default, including on loopback. Configure at least one of: the emergency operator (`TESSERA_OPERATOR_PASSWORD_HASH`), a shared API token (`TESSERA_ADMIN_TOKEN`, at least 32 characters, optionally with diff --git a/crates/tessera-hub/src/api/routes.rs b/crates/tessera-hub/src/api/routes.rs index 0f7fe06..d10b81d 100644 --- a/crates/tessera-hub/src/api/routes.rs +++ b/crates/tessera-hub/src/api/routes.rs @@ -422,13 +422,9 @@ pub fn router(state: HubState) -> Router { .merge(super::relations::routes()) .merge(super::refresh::routes()) .merge(super::restore::routes()) + .route("/api-doc/openapi.json", get(openapi_document)) .with_state(state); - let api = api.route( - "/api-doc/openapi.json", - get(|| async { Json(ApiDoc::openapi()) }), - ); - let hsts = matches!( std::env::var("TESSERA_HSTS").as_deref(), Ok("1" | "true") @@ -478,6 +474,23 @@ async fn security_headers( response } +/// The API description needs a signed-in operator or the admin token, unless +/// `TESSERA_PUBLIC_OPENAPI=true` publishes it. +async fn openapi_document( + ConnectInfo(peer): ConnectInfo, + State(state): State, + headers: HeaderMap, +) -> Result, StatusCode> { + let public = matches!( + std::env::var("TESSERA_PUBLIC_OPENAPI").as_deref(), + Ok("1" | "true") + ); + if !public { + authenticate(&state, peer, &headers, false)?; + } + Ok(Json(ApiDoc::openapi())) +} + #[utoipa::path(get, path = "/health", responses((status = 200, description = "ok")))] async fn health() -> &'static str { "vault online" @@ -1745,6 +1758,31 @@ mod tests { assert_eq!(content_type("noextension"), "application/octet-stream"); } + #[tokio::test] + async fn the_api_description_is_not_served_to_anonymous_callers() { + let dir = tempfile::tempdir().unwrap(); + let mut state = HubState::bootstrap(dir.path().to_str().unwrap(), "synthetic-vault") + .await + .unwrap(); + state.admin.token = Some("synthetic-admin-token-of-32-chars!".into()); + let app = router(state); + let request = |token: Option<&str>| { + let mut r = Request::get("/api-doc/openapi.json") + .extension(ConnectInfo("203.0.113.50:9000".parse::().unwrap())); + if let Some(token) = token { + r = r.header(header::AUTHORIZATION, format!("Bearer {token}")); + } + r.body(Body::empty()).unwrap() + }; + let anonymous = app.clone().oneshot(request(None)).await.unwrap(); + assert_eq!(anonymous.status(), StatusCode::UNAUTHORIZED); + let signed_in = app + .oneshot(request(Some("synthetic-admin-token-of-32-chars!"))) + .await + .unwrap(); + assert_eq!(signed_in.status(), StatusCode::OK); + } + #[tokio::test] async fn responses_carry_security_headers_and_assets_cache_for_a_year() { let dir = tempfile::tempdir().unwrap(); diff --git a/docs/auth-endpoint-contract.md b/docs/auth-endpoint-contract.md index cff50e8..96ca54f 100644 --- a/docs/auth-endpoint-contract.md +++ b/docs/auth-endpoint-contract.md @@ -16,7 +16,7 @@ admin/browser. Semua status/data job hanya di scope owner/admin yang berizin. | Route | Methods | Principal | Permission | Scope | Data yang boleh keluar | | --- | --- | --- | --- | --- | --- | | /health | GET | public | — | — | liveness statis | -| /api-doc/openapi.json | GET | public | — | — | schema tanpa config/secret | +| /api-doc/openapi.json | GET | human | — | — | perlu login atau token admin; publik hanya dengan TESSERA_PUBLIC_OPENAPI=true | | /api/v1/operator/session | GET | public | — | current session | status, actor, CSRF sesi sendiri; no-store | | /api/v1/operator/session | DELETE | human | session.self | current session | tidak ada; CSRF wajib | | /api/v1/me | GET | human | session.self | current principal | id, kind, auth method, display name, capabilities; no-store |