From 833c713dd06927e571d72975f498226491ea3952 Mon Sep 17 00:00:00 2001 From: Eli Barbieri Date: Thu, 19 Nov 2026 13:43:22 -0800 Subject: [PATCH] fix(zingod-grpc): expose grpc-retry-pushback-ms to gRPC-web clients Co-authored-by: Daniel Gorgonha --- packages/zingod-grpc/src/admission.rs | 9 ++++++--- packages/zingod-grpc/src/transport.rs | 17 ++++++++++------- packages/zingod-grpc/usage.md | 6 +++--- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/packages/zingod-grpc/src/admission.rs b/packages/zingod-grpc/src/admission.rs index a27ca2711..0965f0aa0 100644 --- a/packages/zingod-grpc/src/admission.rs +++ b/packages/zingod-grpc/src/admission.rs @@ -14,7 +14,7 @@ use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::sync::{Arc, Mutex}; use std::time::Duration; -use http::{HeaderValue, Response}; +use http::{HeaderName, HeaderValue, Response}; use tokio_metrics::TaskMonitor; use tokio_util::sync::CancellationToken; use tonic::Status; @@ -28,8 +28,11 @@ use crate::stall::Client; /// Refusal retry hint, ms (held streams = wallet syncs: a slot frees well under a second) const PUSHBACK_MS: &str = "250"; -/// gRPC's own "come back later" header (grpc-core) -const PUSHBACK_HEADER: &str = "grpc-retry-pushback-ms"; +/// gRPC headers a refusal carries (status by tonic, pushback = grpc-core's "come back later") +pub(crate) const STATUS_HEADER: HeaderName = HeaderName::from_static("grpc-status"); +pub(crate) const MESSAGE_HEADER: HeaderName = HeaderName::from_static("grpc-message"); +pub(crate) const DETAILS_HEADER: HeaderName = HeaderName::from_static("grpc-status-details-bin"); +pub(crate) const PUSHBACK_HEADER: HeaderName = HeaderName::from_static("grpc-retry-pushback-ms"); /// Work streams holding a slot, the adaptive limit and its outcomes, shared by every connection /// ([`GrpcService::admission`](crate::GrpcService::admission) = its readiness view) diff --git a/packages/zingod-grpc/src/transport.rs b/packages/zingod-grpc/src/transport.rs index b9c893124..0ed0764ea 100644 --- a/packages/zingod-grpc/src/transport.rs +++ b/packages/zingod-grpc/src/transport.rs @@ -25,7 +25,9 @@ use tracing::{debug, info, warn, Instrument as _, Span}; use zingod_persistence::PinnedView; use zingod_source::ChainDataSource; -use crate::admission::AdmissionState; +use crate::admission::{ + AdmissionState, DETAILS_HEADER, MESSAGE_HEADER, PUSHBACK_HEADER, STATUS_HEADER, +}; use crate::connections::{ConnectionCaps, Reserved}; use crate::guard::{Guard, Protocol}; use crate::health::{Health, HealthRoute}; @@ -231,7 +233,7 @@ impl GrpcService { /// `Access-Control-Allow-Origin` and is refused; native and non-browser clients unaffected) /// /// - `*` origin + `Any` headers (public endpoint; the `Origin` gate does the refusing, not CORS) -/// - grpc-status / grpc-message exposed (browser reads a gRPC trailer off the body) +/// - grpc-status / grpc-message / retry pushback exposed fn cors(enabled: bool) -> tower_http::cors::CorsLayer { use tower_http::cors::{Any, CorsLayer}; if !enabled { @@ -241,11 +243,7 @@ fn cors(enabled: bool) -> tower_http::cors::CorsLayer { .allow_origin(Any) .allow_methods(Any) .allow_headers(Any) - .expose_headers([ - http::header::HeaderName::from_static("grpc-status"), - http::header::HeaderName::from_static("grpc-message"), - http::header::HeaderName::from_static("grpc-status-details-bin"), - ]) + .expose_headers([STATUS_HEADER, MESSAGE_HEADER, DETAILS_HEADER, PUSHBACK_HEADER]) .max_age(Duration::from_secs(3600)) } @@ -1125,6 +1123,11 @@ mod tests { .expect("read"); let response = String::from_utf8_lossy(&response).to_ascii_lowercase(); assert!(response.contains("grpc-status: 14"), "gRPC-web answered UNAVAILABLE:\n{response}"); + assert!( + response.lines().any(|line| line.starts_with("access-control-expose-headers:") + && line.contains("grpc-retry-pushback-ms")), + "refusal pushback readable by a browser:\n{response}", + ); cancel.cancel(); serving.await.expect("the serve task ran"); diff --git a/packages/zingod-grpc/usage.md b/packages/zingod-grpc/usage.md index c316482ad..f36724193 100644 --- a/packages/zingod-grpc/usage.md +++ b/packages/zingod-grpc/usage.md @@ -157,9 +157,9 @@ stall-watch ticket and its close-out metrics, so all three end when hyper drops is `405`, any other path over HTTP/1.x `404` — forwarded to the gRPC-web stack instead when `with_grpc_web(true)` (browser gRPC-web and its CORS preflight ride HTTP/1.1). - **Browser gRPC-web** (`with_grpc_web(true)`): `tower-http` CORS (`*`, grpc-status / grpc-message - exposed) answers the preflight, `tonic-web` translates gRPC-web framing; native gRPC is - untouched (CORS short-circuits only an `OPTIONS` preflight). Off = no CORS, so a browser is - refused. Per-origin policy is the gate below. + / `grpc-retry-pushback-ms` exposed) answers the preflight, `tonic-web` translates gRPC-web + framing; native gRPC is untouched (CORS short-circuits only an `OPTIONS` preflight). Off = no + CORS, so a browser is refused. Per-origin policy is the gate below. - **Origin gate** (`with_origin_gate(Arc)`, read in `guard.rs`): a request's `Origin` → `block` (`PermissionDenied` before admission), else the normal admission path; `allow` only carves a host out of a blocked parent, never lifts a cap (`Origin` is client-settable). A