Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ async fn main() -> anyhow::Result<()> {
),
)
.layer(RequestBodyLimitLayer::new(1024 * 1024))
.layer(axum::middleware::from_fn(middleware::security_headers))
.layer(TraceLayer::new_for_http());

let port = std::env::var("PORT")
Expand Down
69 changes: 63 additions & 6 deletions crates/api/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use axum::{
body::Body,
extract::State,
http::{Request, StatusCode},
http::{HeaderName, HeaderValue, Request, StatusCode},
middleware::Next,
response::{IntoResponse, Response},
Json,
Expand Down Expand Up @@ -41,6 +41,30 @@ pub async fn require_admin_key(
next.run(req).await
}

pub async fn security_headers(req: Request<Body>, next: Next) -> Response {
let mut response = next.run(req).await;
let headers = response.headers_mut();

headers.insert(
HeaderName::from_static("x-content-type-options"),
HeaderValue::from_static("nosniff"),
);
headers.insert(
HeaderName::from_static("x-frame-options"),
HeaderValue::from_static("DENY"),
);
headers.insert(
HeaderName::from_static("referrer-policy"),
HeaderValue::from_static("no-referrer"),
);
headers.insert(
HeaderName::from_static("content-security-policy"),
HeaderValue::from_static("frame-ancestors 'none'"),
);

response
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -63,12 +87,15 @@ mod tests {
api_port: 8081,
};

let app = Router::new()
.route("/admin", get(|| async { "ok" }))
.layer(axum::middleware::from_fn_with_state(
AppState::new(config, sqlx::PgPool::connect_lazy("postgres://localhost/waveflow").unwrap()),
let app = Router::new().route("/admin", get(|| async { "ok" })).layer(
axum::middleware::from_fn_with_state(
AppState::new(
config,
sqlx::PgPool::connect_lazy("postgres://localhost/waveflow").unwrap(),
),
require_admin_key,
));
),
);

let response = app
.oneshot(
Expand All @@ -82,4 +109,34 @@ mod tests {

assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
}

#[tokio::test]
async fn applies_security_headers_without_changing_json_body() {
let app = Router::new()
.route("/json", get(|| async { Json(json!({ "status": "ok" })) }))
.layer(axum::middleware::from_fn(security_headers));

let response = app
.oneshot(Request::builder().uri("/json").body(Body::empty()).unwrap())
.await
.unwrap();

assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.headers().get("x-content-type-options").unwrap(),
"nosniff"
);
assert_eq!(response.headers().get("x-frame-options").unwrap(), "DENY");
assert_eq!(
response.headers().get("referrer-policy").unwrap(),
"no-referrer"
);
assert_eq!(
response.headers().get("content-security-policy").unwrap(),
"frame-ancestors 'none'"
);

let body = response.into_body().collect().await.unwrap().to_bytes();
assert_eq!(&body[..], br#"{"status":"ok"}"#);
}
}
2 changes: 1 addition & 1 deletion crates/api/src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use axum::{
use metrics::counter;
use serde_json::json;
use uuid::Uuid;
use waveflow_shared::{ContributorRecord, PayoutRecord, ProgramRecord, ProgramStatus, WaveFlowError, WaveFlowResult};
use waveflow_shared::{ContributorRecord, PayoutRecord, ProgramRecord, ProgramStatus, WaveFlowError};

use crate::state::AppState;

Expand Down
1 change: 1 addition & 0 deletions docs/security-checklist.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- [ ] Configure non-default `API_ADMIN_KEYS`
- [ ] Terminate TLS at load balancer
- [ ] Rate-limit public endpoints
- [x] Send `X-Content-Type-Options`, `X-Frame-Options`, `Referrer-Policy`, and frame-ancestor CSP headers on API responses

## Contract operations

Expand Down