commit 0a2e37767d11194b28dda94a6669adb9502219c5
parent 6327747249839969be3d420a6fddee35a005eaa9
Author: Antoine A <>
Date: Thu, 19 Nov 2026 13:06:34 +0100
taler-api: let routes answer OPTIONS requests that are not CORS
preflights
Diffstat:
4 files changed, 75 insertions(+), 25 deletions(-)
diff --git a/Cargo.lock b/Cargo.lock
@@ -2405,7 +2405,6 @@ dependencies = [
"taler-test-utils",
"thiserror",
"tokio",
- "tower-http",
"tracing",
"url",
"zlib-rs",
@@ -2754,21 +2753,6 @@ dependencies = [
]
[[package]]
-name = "tower-http"
-version = "0.7.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "08a05a66a4fdd61cbbe0a1d755ffe0ca6aba159dd4820936a0ff8a8278245b9c"
-dependencies = [
- "bitflags",
- "bytes",
- "http",
- "percent-encoding",
- "pin-project-lite",
- "tower-layer",
- "tower-service",
-]
-
-[[package]]
name = "tower-layer"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/common/taler-api/Cargo.toml b/common/taler-api/Cargo.toml
@@ -19,7 +19,6 @@ http-body-util.workspace = true
hyper.workspace = true
hyper-util = { workspace = true, features = ["server-auto", "service", "tokio"] }
zlib-rs = "0.6.3"
-tower-http = { version = "0.7", features = ["cors"] }
tokio = { workspace = true, features = ["signal"] }
taler-macros.workspace = true
rand.workspace = true
diff --git a/common/taler-api/src/api.rs b/common/taler-api/src/api.rs
@@ -24,8 +24,9 @@ use std::{
use axum::{
extract::{MatchedPath, OriginalUri, Request, State},
+ http::{HeaderValue, Method, header},
middleware::{self, Next},
- response::Response,
+ response::{IntoResponse as _, Response},
};
use compact_str::CompactString;
use rand::RngExt as _;
@@ -36,7 +37,6 @@ use taler_common::{
types::amount::{Amount, Currency},
};
use tokio::signal;
-use tower_http::cors::{Any, CorsLayer};
use tracing::{Level, debug, info};
use wire::WireGateway;
@@ -124,12 +124,7 @@ impl TalerRouter for Router {
fn finalize(self) -> Router {
self.method_not_allowed_fallback(async || failure_code(ErrorCode::GENERIC_METHOD_INVALID))
.fallback(async || failure_code(ErrorCode::GENERIC_ENDPOINT_UNKNOWN))
- .layer(
- CorsLayer::new()
- .allow_origin(Any)
- .allow_methods(Any)
- .allow_headers(Any),
- )
+ .layer(middleware::from_fn(cors_middleware))
.layer(middleware::from_fn(logger_middleware))
}
@@ -158,6 +153,28 @@ impl TalerRouter for Router {
}
}
+async fn cors_middleware(request: Request, next: Next) -> Response {
+ let headers = request.headers();
+ if request.method() == Method::OPTIONS
+ && headers.contains_key(header::ORIGIN)
+ && headers.contains_key(header::ACCESS_CONTROL_REQUEST_METHOD)
+ {
+ return [
+ (header::ACCESS_CONTROL_ALLOW_ORIGIN, "*"),
+ (header::ACCESS_CONTROL_ALLOW_METHODS, "*"),
+ (header::ACCESS_CONTROL_ALLOW_HEADERS, "*"),
+ ]
+ .into_response();
+ }
+
+ let mut response = next.run(request).await;
+ response.headers_mut().insert(
+ header::ACCESS_CONTROL_ALLOW_ORIGIN,
+ HeaderValue::from_static("*"),
+ );
+ response
+}
+
struct LifetimeMiddlewareState {
lifetime: AtomicU32,
notify: Arc<tokio::sync::Notify>,
diff --git a/common/taler-api/src/test.rs b/common/taler-api/src/test.rs
@@ -98,6 +98,56 @@ async fn setup(db: &PgPool) -> Router {
test_api(db.clone(), "EUR".parse().unwrap()).finalize()
}
+#[tokio::test]
+async fn options_requests() {
+ use axum::{
+ body::Body,
+ http::{Method, Request},
+ routing::options,
+ };
+ use hyper::service::Service as _;
+ use hyper_util::service::TowerToHyperService;
+
+ let server = TowerToHyperService::new(
+ Router::new()
+ .route("/resource", options(async || StatusCode::NO_CONTENT))
+ .finalize(),
+ );
+
+ for (origin, request_method) in [(false, false), (true, false), (false, true), (true, true)] {
+ let mut request = Request::builder().method(Method::OPTIONS).uri("/resource");
+ if origin {
+ request = request.header(header::ORIGIN, "https://example.com");
+ }
+ if request_method {
+ request = request.header(header::ACCESS_CONTROL_REQUEST_METHOD, "OPTIONS");
+ }
+ let response = server
+ .call(request.body(Body::empty()).unwrap())
+ .await
+ .unwrap();
+ assert_eq!(response.headers()[header::ACCESS_CONTROL_ALLOW_ORIGIN], "*");
+ if origin && request_method {
+ assert_eq!(response.status(), StatusCode::OK);
+ assert_eq!(
+ response.headers()[header::ACCESS_CONTROL_ALLOW_METHODS],
+ "*"
+ );
+ assert_eq!(
+ response.headers()[header::ACCESS_CONTROL_ALLOW_HEADERS],
+ "*"
+ );
+ } else {
+ assert_eq!(response.status(), StatusCode::NO_CONTENT);
+ assert!(
+ !response
+ .headers()
+ .contains_key(header::ACCESS_CONTROL_ALLOW_METHODS)
+ );
+ }
+ }
+}
+
#[db_test(sql_dir = "db", component = "taler-api")]
async fn body_parsing(db: PgPool) {
let server = setup(&db).await;