Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/grpc-web-expose-retry-pushback.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion packages/zingod-grpc/src/admission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
33 changes: 32 additions & 1 deletion packages/zingod-grpc/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ impl<S: ChainDataSource, V: PinnedView> GrpcService<S, V> {
/// `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 {
Expand All @@ -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))
}
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion packages/zingod-grpc/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<OriginGate>)`, read in `guard.rs`): a request's `Origin`
Expand Down