diff --git a/.changeset/grpc-web-expose-retry-pushback.md b/.changeset/grpc-web-expose-retry-pushback.md new file mode 100644 index 000000000..dd4da2ffb --- /dev/null +++ b/.changeset/grpc-web-expose-retry-pushback.md @@ -0,0 +1,9 @@ +--- +default: patch +--- + +# Expose the retry pushback to browser (gRPC-web) clients + +With `[serve] grpc_web = true`, a refusal's `grpc-retry-pushback-ms` is now listed in +`Access-Control-Expose-Headers` next to `grpc-status` and `grpc-message`, so a browser wallet can +read it and wait as long as the server asks. diff --git a/packages/zingod-grpc/src/admission.rs b/packages/zingod-grpc/src/admission.rs index 12ceda573..858c720e8 100644 --- a/packages/zingod-grpc/src/admission.rs +++ b/packages/zingod-grpc/src/admission.rs @@ -23,7 +23,7 @@ use crate::OverloadLimits; const PUSHBACK_MS: &str = "250"; /// gRPC's own "come back later" header (grpc-core) -const PUSHBACK_HEADER: &str = "grpc-retry-pushback-ms"; +pub(crate) const PUSHBACK_HEADER: &str = "grpc-retry-pushback-ms"; /// Cap a request counts against (each method's in [`Method::route`](crate::service::Method::route), /// unknown path = `Work`) diff --git a/packages/zingod-grpc/src/transport.rs b/packages/zingod-grpc/src/transport.rs index 4fc6ed927..affdf6458 100644 --- a/packages/zingod-grpc/src/transport.rs +++ b/packages/zingod-grpc/src/transport.rs @@ -192,7 +192,8 @@ 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 exposed (browser reads a gRPC trailer off the body), and the +/// refusal's retry pushback, so a browser can wait as long as the server asks fn cors(enabled: bool) -> tower_http::cors::CorsLayer { use tower_http::cors::{Any, CorsLayer}; if !enabled { @@ -206,6 +207,7 @@ fn cors(enabled: bool) -> tower_http::cors::CorsLayer { http::header::HeaderName::from_static("grpc-status"), http::header::HeaderName::from_static("grpc-message"), http::header::HeaderName::from_static("grpc-status-details-bin"), + http::header::HeaderName::from_static(crate::admission::PUSHBACK_HEADER), ]) .max_age(Duration::from_secs(3600)) } @@ -1015,6 +1017,35 @@ mod tests { serving.await.expect("the serve task ran"); } + /// gRPC-web on: a refusal's `grpc-retry-pushback-ms` is exposed to the browser, as the + /// status headers are (a Trailers-Only refusal carries both as HTTP headers) + #[tokio::test] + async fn grpc_web_exposes_the_refusal_retry_pushback() { + use tower::{Layer as _, ServiceExt as _}; + let refuse = tower::service_fn(|_: http::Request<()>| async { + Ok::<_, std::convert::Infallible>(crate::admission::refused( + crate::admission::Class::Work, + )) + }); + let request = http::Request::builder() + .method("POST") + .uri("/cash.z.wallet.sdk.rpc.CompactTxStreamer/GetLightdInfo") + .header("origin", "https://wallet.example") + .body(()) + .expect("request"); + let response = super::cors(true).layer(refuse).oneshot(request).await.expect("infallible"); + let exposed = response + .headers() + .get("access-control-expose-headers") + .and_then(|value| value.to_str().ok()) + .unwrap_or_default() + .to_ascii_lowercase(); + assert!( + exposed.split(',').any(|name| name.trim() == "grpc-retry-pushback-ms"), + "pushback readable by a browser, exposed: {exposed:?}", + ); + } + /// gRPC-web off (the default): the same preflight gets no CORS grant, so a browser is refused #[tokio::test] async fn grpc_web_off_refuses_a_preflight() { diff --git a/packages/zingod-grpc/usage.md b/packages/zingod-grpc/usage.md index 75537a837..11e0c221e 100644 --- a/packages/zingod-grpc/usage.md +++ b/packages/zingod-grpc/usage.md @@ -135,7 +135,7 @@ stream ending or its client disconnecting). What each `[grpc]` cap bounds: 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 + and a refusal's `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`