Skip to content
Merged
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: 6 additions & 3 deletions packages/zingod-grpc/src/admission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
17 changes: 10 additions & 7 deletions packages/zingod-grpc/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -231,7 +233,7 @@ 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 / retry pushback exposed
fn cors(enabled: bool) -> tower_http::cors::CorsLayer {
use tower_http::cors::{Any, CorsLayer};
if !enabled {
Expand All @@ -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))
}

Expand Down Expand Up @@ -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");
Expand Down
6 changes: 3 additions & 3 deletions packages/zingod-grpc/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<OriginGate>)`, 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
Expand Down
Loading