You've already forked authentication-service
mirror of
https://github.com/matrix-org/matrix-authentication-service.git
synced 2025-08-09 04:22:45 +03:00
Add CORS headers to API-like routes
This commit is contained in:
@@ -13,6 +13,7 @@ http = "0.2.6"
|
||||
http-body = "0.4.4"
|
||||
hyper = "0.14.18"
|
||||
hyper-rustls = { version = "0.23.0", features = ["http1", "http2"] }
|
||||
once_cell = "1.10.0"
|
||||
opentelemetry = "0.17.0"
|
||||
opentelemetry-http = "0.6.0"
|
||||
opentelemetry-semantic-conventions = "0.9.0"
|
||||
@@ -23,6 +24,6 @@ serde_json = "1.0.79"
|
||||
thiserror = "1.0.30"
|
||||
tokio = { version = "1.17.0", features = ["sync", "parking_lot"] }
|
||||
tower = { version = "0.4.12", features = ["timeout", "limit"] }
|
||||
tower-http = { version = "0.2.5", features = ["follow-redirect", "decompression-full", "set-header", "compression-full"] }
|
||||
tower-http = { version = "0.2.5", features = ["follow-redirect", "decompression-full", "set-header", "compression-full", "cors"] }
|
||||
tracing = "0.1.32"
|
||||
tracing-opentelemetry = "0.17.2"
|
||||
|
@@ -12,8 +12,53 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use http::header::HeaderName;
|
||||
use once_cell::sync::OnceCell;
|
||||
use tower_http::cors::CorsLayer;
|
||||
|
||||
use crate::layers::json::Json;
|
||||
|
||||
static PROPAGATOR_HEADERS: OnceCell<Vec<HeaderName>> = OnceCell::new();
|
||||
|
||||
/// Notify the CORS layer what opentelemetry propagators are being used. This
|
||||
/// helps whitelisting headers in CORS requests.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// When called twice
|
||||
pub fn set_propagator(propagator: &dyn opentelemetry::propagation::TextMapPropagator) {
|
||||
let headers = propagator
|
||||
.fields()
|
||||
.map(|h| HeaderName::try_from(h).unwrap())
|
||||
.collect();
|
||||
|
||||
tracing::debug!(
|
||||
?headers,
|
||||
"Headers allowed in CORS requests for trace propagators set"
|
||||
);
|
||||
PROPAGATOR_HEADERS
|
||||
.set(headers)
|
||||
.expect(concat!(module_path!(), "::set_propagator was called twice"));
|
||||
}
|
||||
|
||||
pub trait CorsLayerExt {
|
||||
#[must_use]
|
||||
fn allow_otel_headers<H>(self, headers: H) -> Self
|
||||
where
|
||||
H: IntoIterator<Item = HeaderName>;
|
||||
}
|
||||
|
||||
impl CorsLayerExt for CorsLayer {
|
||||
fn allow_otel_headers<H>(self, headers: H) -> Self
|
||||
where
|
||||
H: IntoIterator<Item = HeaderName>,
|
||||
{
|
||||
let base = PROPAGATOR_HEADERS.get().cloned().unwrap_or_default();
|
||||
let headers: Vec<_> = headers.into_iter().chain(base.into_iter()).collect();
|
||||
self.allow_headers(headers)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ServiceExt: Sized {
|
||||
fn json<T>(self) -> Json<Self, T>;
|
||||
}
|
||||
|
@@ -47,7 +47,7 @@ mod future_service;
|
||||
mod layers;
|
||||
|
||||
pub use self::{
|
||||
ext::ServiceExt as HttpServiceExt,
|
||||
ext::{set_propagator, CorsLayerExt, ServiceExt as HttpServiceExt},
|
||||
future_service::FutureService,
|
||||
layers::{client::ClientLayer, json::JsonResponseLayer, otel, server::ServerLayer},
|
||||
};
|
||||
|
Reference in New Issue
Block a user