Skip to content

Commit 8bd7891

Browse files
thomasballingerConvex, Inc.
authored and
Convex, Inc.
committed
Allow any origin in CORS responses from more routes (#35761)
GitOrigin-RevId: a279eeb0ce37db610a94f30b0cd8b1c2653acc43
1 parent d560e75 commit 8bd7891

File tree

2 files changed

+7
-64
lines changed

2 files changed

+7
-64
lines changed

crates/common/src/http/mod.rs

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,7 @@ use http::{
6666
header::{
6767
HeaderName,
6868
HeaderValue,
69-
ACCEPT,
70-
ACCEPT_LANGUAGE,
71-
AUTHORIZATION,
72-
CONTENT_TYPE,
73-
REFERER,
74-
USER_AGENT,
7569
},
76-
request::Parts,
7770
HeaderMap,
7871
Method,
7972
StatusCode,
@@ -100,6 +93,7 @@ use tower::{
10093
ServiceBuilder,
10194
};
10295
use tower_http::cors::{
96+
AllowHeaders,
10397
AllowOrigin,
10498
CorsLayer,
10599
};
@@ -1217,17 +1211,7 @@ impl<T: fmt::Display> fmt::Display for LogOptFmt<T> {
12171211
// different headers.
12181212
pub fn cli_cors() -> CorsLayer {
12191213
CorsLayer::new()
1220-
.allow_headers(vec![
1221-
"baggage".parse().unwrap(),
1222-
"sentry-trace".parse().unwrap(),
1223-
ACCEPT,
1224-
ACCEPT_LANGUAGE,
1225-
AUTHORIZATION,
1226-
CONTENT_TYPE,
1227-
CONVEX_CLIENT_HEADER,
1228-
REFERER,
1229-
USER_AGENT,
1230-
])
1214+
.allow_headers(AllowHeaders::mirror_request())
12311215
.allow_credentials(true)
12321216
.allow_methods(vec![
12331217
Method::GET,
@@ -1236,13 +1220,7 @@ pub fn cli_cors() -> CorsLayer {
12361220
Method::OPTIONS,
12371221
Method::DELETE,
12381222
])
1239-
// `predicate` of `true` allows all origins without allow-origin *,
1240-
// since that wouldn't allow use of credentials.
1241-
.allow_origin(
1242-
AllowOrigin::predicate(|_origin: &HeaderValue, _request_head: &Parts| {
1243-
true
1244-
}),
1245-
)
1223+
.allow_origin(AllowOrigin::mirror_request())
12461224
.max_age(Duration::from_secs(86400))
12471225
}
12481226

crates/local_backend/src/router.rs

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ use axum::{
1919
Router,
2020
};
2121
use common::{
22-
http::{
23-
cli_cors,
24-
CONVEX_CLIENT_HEADER,
25-
},
22+
http::cli_cors,
2623
knobs::{
2724
AIRBYTE_STREAMING_IMPORT_REQUEST_SIZE_LIMIT,
2825
MAX_BACKEND_PUBLIC_API_REQUEST_SIZE,
@@ -32,23 +29,14 @@ use common::{
3229
},
3330
};
3431
use http::{
35-
header::{
36-
ACCEPT,
37-
ACCEPT_LANGUAGE,
38-
AUTHORIZATION,
39-
CONTENT_TYPE,
40-
REFERER,
41-
USER_AGENT,
42-
},
43-
request,
44-
HeaderValue,
4532
Method,
4633
StatusCode,
4734
};
4835
use metrics::SERVER_VERSION_STR;
4936
use tower::ServiceBuilder;
5037
use tower_http::{
5138
cors::{
39+
AllowHeaders,
5240
AllowOrigin,
5341
CorsLayer,
5442
},
@@ -412,17 +400,7 @@ where
412400

413401
pub fn cors() -> CorsLayer {
414402
CorsLayer::new()
415-
.allow_headers(vec![
416-
"baggage".parse().unwrap(),
417-
"sentry-trace".parse().unwrap(),
418-
ACCEPT,
419-
ACCEPT_LANGUAGE,
420-
AUTHORIZATION,
421-
CONTENT_TYPE,
422-
CONVEX_CLIENT_HEADER,
423-
REFERER,
424-
USER_AGENT,
425-
])
403+
.allow_headers(AllowHeaders::mirror_request())
426404
.allow_credentials(true)
427405
.allow_methods(vec![
428406
Method::GET,
@@ -432,19 +410,6 @@ pub fn cors() -> CorsLayer {
432410
Method::DELETE,
433411
Method::PUT,
434412
])
435-
// Don't use tower_http::cors::any(), it causes the server to respond with
436-
// Access-Control-Allow-Origin: *. Browsers restrict sending credentials to other domains
437-
// that reply to a CORS with allow-origin *.
438-
//
439-
// https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSNotSupportingCredentials
440-
//
441-
// Instead respond with Access-Control-Allow-Origin set to the submitted Origin header.
442-
//
443-
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin#directives
444-
.allow_origin(
445-
AllowOrigin::predicate(|_origin: &HeaderValue, _request_head: &request::Parts| {
446-
true
447-
}),
448-
)
413+
.allow_origin(AllowOrigin::mirror_request())
449414
.max_age(Duration::from_secs(86400))
450415
}

0 commit comments

Comments
 (0)