fix: require sign-in for the OpenAPI document unless TESSERA_PUBLIC_OPENAPI is set
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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<SocketAddr>,
|
||||
State(state): State<HubState>,
|
||||
headers: HeaderMap,
|
||||
) -> Result<Json<utoipa::openapi::OpenApi>, 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::<SocketAddr>().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();
|
||||
|
||||
Reference in New Issue
Block a user