Skip to content

Commit e71f49e

Browse files
authored
Add cors debug mode (#5955)
* add cors debug mode * nits * fmt
1 parent 62e7923 commit e71f49e

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

quickwit/quickwit-serve/src/rest.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use tokio_util::either::Either;
2929
use tower::ServiceBuilder;
3030
use tower_http::compression::CompressionLayer;
3131
use tower_http::compression::predicate::{NotForContentType, Predicate, SizeAbove};
32-
use tower_http::cors::CorsLayer;
32+
use tower_http::cors::{AllowOrigin, CorsLayer};
3333
use tracing::{error, info};
3434
use warp::filters::log::Info;
3535
use warp::hyper::http::HeaderValue;
@@ -480,6 +480,25 @@ fn get_status_with_error(rejection: Rejection) -> Result<RestApiError, Rejection
480480
}
481481

482482
fn build_cors(cors_origins: &[String]) -> CorsLayer {
483+
let debug_mode = quickwit_common::get_bool_from_env("QW_ENABLE_CORS_DEBUG", false);
484+
if debug_mode {
485+
info!("CORS debug mode is enabled, localhost and 127.0.0.1 origins will be allowed");
486+
return CorsLayer::new()
487+
.allow_methods([
488+
Method::GET,
489+
Method::POST,
490+
Method::PUT,
491+
Method::PATCH,
492+
Method::DELETE,
493+
])
494+
.allow_origin(AllowOrigin::predicate(|origin, _parts| {
495+
[b"https://localhost:", b"https://127.0.0.1:"]
496+
.iter()
497+
.any(|prefix| origin.as_bytes().starts_with(*prefix))
498+
}))
499+
.allow_headers([http::header::CONTENT_TYPE]);
500+
}
501+
483502
let mut cors = CorsLayer::new().allow_methods([
484503
Method::GET,
485504
Method::POST,

0 commit comments

Comments
 (0)