From 03b11e48bdddf21aab36e5d77ef6b98b8ea0f7a2 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Tue, 24 Nov 2026 00:10:34 +0100 Subject: [PATCH 2/2] taler-api: allow a preflight the request headers it names Browsers do not count Authorization as covered by the wildcard in Access-Control-Allow-Headers (Fetch standard, "CORS non-wildcard request-header name"), so with "*" alone every cross-origin request with a bearer token is refused. A preflight now gets the headers it names in Access-Control-Request-Headers, as tower-http's CorsLayer does with mirror_request; one that names none still gets "*". Assisted by LLM --- common/taler-api/src/api.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/common/taler-api/src/api.rs b/common/taler-api/src/api.rs index 9215277..8e718a6 100644 --- a/common/taler-api/src/api.rs +++ b/common/taler-api/src/api.rs @@ -159,12 +159,27 @@ async fn cors_middleware(request: Request, next: Next) -> Response { && headers.contains_key(header::ORIGIN) && headers.contains_key(header::ACCESS_CONTROL_REQUEST_METHOD) { - return [ + // The wildcard does not cover Authorization (Fetch standard, "CORS + // non-wildcard request-header name"), so a preflight is allowed the + // headers it names; with "*" alone, a browser refuses every request + // with a bearer token from another origin. + let requested = headers.get_all(header::ACCESS_CONTROL_REQUEST_HEADERS); + let mut response = [ (header::ACCESS_CONTROL_ALLOW_ORIGIN, "*"), (header::ACCESS_CONTROL_ALLOW_METHODS, "*"), - (header::ACCESS_CONTROL_ALLOW_HEADERS, "*"), ] .into_response(); + let allowed = response.headers_mut(); + for value in requested { + allowed.append(header::ACCESS_CONTROL_ALLOW_HEADERS, value.clone()); + } + if !allowed.contains_key(header::ACCESS_CONTROL_ALLOW_HEADERS) { + allowed.insert( + header::ACCESS_CONTROL_ALLOW_HEADERS, + HeaderValue::from_static("*"), + ); + } + return response; } let mut response = next.run(request).await; -- 2.47.3