[Security] Restrict default CORS origins (#61009)

Signed-off-by: Juan Pérez de Algaba <jperezde@redhat.com>
Co-authored-by: Cyrus Leung <tlleungac@connect.ust.hk>
This commit is contained in:
Juan Pérez de Algaba
2026-10-11 08:26:02 +00:00
committed by GitHub
co-authored by Cyrus Leung
parent 26a488ad93
commit fcae5c9887
8 changed files with 124 additions and 37 deletions
+2 -2
View File
@@ -65,7 +65,7 @@ pub struct ApiServerOptions {
pub sse_keep_alive_interval: Option<Duration>,
}
/// CORS settings mirroring Python's `CORSMiddleware`; the default is permissive.
/// CORS settings mirroring Python's `CORSMiddleware`; origins are denied by default.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct CorsConfig {
/// Allowed origins. `["*"]` allows any origin.
@@ -81,7 +81,7 @@ pub struct CorsConfig {
impl Default for CorsConfig {
fn default() -> Self {
Self {
allow_origins: vec!["*".to_string()],
allow_origins: Vec::new(),
allow_methods: vec!["*".to_string()],
allow_headers: vec!["*".to_string()],
allow_credentials: false,
+1 -1
View File
@@ -105,7 +105,7 @@ pub fn cors_layer(cfg: &CorsConfig) -> CorsLayer {
// Emit `Vary: Origin` only when the allow-origin is dynamic (explicit
// origins, or credentials); the wildcard + no-credentials case emits no
// `Vary` at all, and an empty list disables the header here.
// `Vary` at all. An empty origin list never permits an origin.
let vary: Vec<HeaderName> = if !wildcard_origins || cfg.allow_credentials {
vec![header::ORIGIN]
} else {
+35 -9
View File
@@ -1784,7 +1784,7 @@ async fn api_key_auth_allows_unguarded_route_without_token() {
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[serial]
async fn cors_default_simple_request_allows_any_origin() {
async fn cors_default_simple_request_omits_allow_origin() {
let (mut app, _engine_task) = test_app_with_cors(CorsConfig::default()).await;
let response = app
.call(
@@ -1799,17 +1799,12 @@ async fn cors_default_simple_request_allows_any_origin() {
.expect("call app");
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
header_value(&response, "access-control-allow-origin"),
Some("*")
);
// Wildcard origins without credentials emit no `Vary` (Starlette parity).
assert_eq!(header_value(&response, "vary"), None);
assert_eq!(header_value(&response, "access-control-allow-origin"), None);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[serial]
async fn cors_default_preflight_returns_explicit_methods_and_max_age() {
async fn cors_default_preflight_omits_allow_origin() {
let (mut app, _engine_task) = test_app_with_cors(CorsConfig::default()).await;
let response = app
.call(
@@ -1825,6 +1820,32 @@ async fn cors_default_preflight_returns_explicit_methods_and_max_age() {
.await
.expect("call app");
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(header_value(&response, "access-control-allow-origin"), None);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[serial]
async fn cors_wildcard_preflight_returns_explicit_methods_and_max_age() {
let cors = CorsConfig {
allow_origins: vec!["*".to_string()],
..CorsConfig::default()
};
let (mut app, _engine_task) = test_app_with_cors(cors).await;
let response = app
.call(
Request::builder()
.method("OPTIONS")
.uri("/v1/chat/completions")
.header("origin", "http://example.com")
.header("access-control-request-method", "POST")
.header("access-control-request-headers", "content-type")
.body(Body::empty())
.expect("build request"),
)
.await
.expect("call app");
assert_eq!(response.status(), StatusCode::OK);
// `*` methods expand to the explicit method list, matching Starlette
// (never the literal `*`).
@@ -1917,6 +1938,7 @@ async fn cors_explicit_origin_disallowed_omits_allow_origin() {
#[serial]
async fn cors_wildcard_with_credentials_reflects_origin_without_panic() {
let cors = CorsConfig {
allow_origins: vec!["*".to_string()],
allow_credentials: true,
..CorsConfig::default()
};
@@ -1971,8 +1993,12 @@ async fn cors_unauthorized_response_has_no_cors_headers() {
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[serial]
async fn cors_preflight_bypasses_auth_and_returns_cors_headers() {
let cors = CorsConfig {
allow_origins: vec!["*".to_string()],
..CorsConfig::default()
};
let (mut app, _engine_task) =
test_app_with_cors_and_keys(CorsConfig::default(), vec!["secret".to_string()]).await;
test_app_with_cors_and_keys(cors, vec!["secret".to_string()]).await;
let response = app
.call(
Request::builder()