Skip to content

Commit 55eaea6

Browse files
authored
Merge pull request #442 from kelly-musk/fix/373-security-headers-middleware
fix: mount security headers middleware globally (#373)
2 parents a04020a + 4f7e54a commit 55eaea6

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

services/api/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use db::Database;
2929
use email::{queue::EmailQueue, service::EmailService, webhook::WebhookHandler};
3030
use metrics::Metrics;
3131
use newsletter::IpRateLimiter;
32-
use security::{ApiKeyAuth, IpWhitelist, RateLimiter};
32+
use security::{ApiKeyAuth, IpWhitelist, RateLimiter, TrustProxy};
3333
use shutdown::ShutdownCoordinator;
3434
use tokio::net::TcpListener;
3535
use tower_http::{

services/api/src/security.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,30 @@ mod tests {
464464
h
465465
}
466466

467-
// ── existing behaviour (trust_proxy = true) ───────────────────────────
467+
// ── security headers middleware ───────────────────────────────────────
468+
469+
#[tokio::test]
470+
async fn security_headers_middleware_sets_required_headers() {
471+
use axum::{body::Body, http::Request, middleware, routing::get, Router};
472+
use tower::ServiceExt;
473+
474+
let app = Router::new()
475+
.route("/", get(|| async { "ok" }))
476+
.layer(middleware::from_fn(super::security_headers_middleware));
477+
478+
let response = app
479+
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
480+
.await
481+
.unwrap();
482+
483+
let headers = response.headers();
484+
assert!(headers.contains_key("content-security-policy"));
485+
assert!(headers.contains_key("strict-transport-security"));
486+
assert!(headers.contains_key("x-frame-options"));
487+
assert!(headers.contains_key("referrer-policy"));
488+
assert_eq!(headers["x-frame-options"], "DENY");
489+
assert_eq!(headers["x-content-type-options"], "nosniff");
490+
}
468491

469492
#[test]
470493
fn test_extract_client_ip_precedence() {

0 commit comments

Comments
 (0)