diff --git a/.github/workflows/security.yml b/.github/workflows/security.yml index ba7b8b6..a51279e 100644 --- a/.github/workflows/security.yml +++ b/.github/workflows/security.yml @@ -63,5 +63,8 @@ jobs: # PRs stay bounded; the weekly schedule exercises a much larger corpus. PROPTEST_CASES: ${{ github.event_name == 'schedule' && '4096' || '256' }} + - name: Test frontend telemetry session delivery + run: bash tests/run-frontend-telemetry.sh + - name: Build release targets run: cargo build --release --all-features diff --git a/src/handlers/frontend_telemetry.rs b/src/handlers/frontend_telemetry.rs new file mode 100644 index 0000000..ec815a5 --- /dev/null +++ b/src/handlers/frontend_telemetry.rs @@ -0,0 +1,567 @@ +//! Session-only operational telemetry for the Rush frontend, never general ingestion. +use super::rum::{RumIngestEvent, RumIngestPayload}; +use crate::{AppState, RequestIdentity, TenantContext}; +use axum::{ + Extension, + extract::State, + http::{HeaderMap, StatusCode, header}, + response::{IntoResponse, Response}, +}; +use serde_json::{Map, Value}; +use std::time::{Duration, Instant}; + +pub const MAX_BODY_BYTES: usize = 64 * 1024; +const MAX_EVENTS: usize = 1000; +const USER_REQUESTS_PER_MINUTE: u64 = 120; +const TENANT_REQUESTS_PER_MINUTE: u64 = 1200; +const MAX_COUNTER: f64 = 9_007_199_254_740_991.0; +const MAX_DURATION_MS: f64 = 86_400_000.0; +type Rejection = (StatusCode, String); +fn denied(status: StatusCode, message: &str) -> Rejection { + (status, message.into()) +} + +pub async fn ingest( + State(state): State, + Extension(tenant): Extension, + Extension(identity): Extension, + headers: HeaderMap, + body: axum::body::Bytes, +) -> Result { + if !session_identity(&identity, &tenant.tenant_id) { + return Err(denied( + StatusCode::UNAUTHORIZED, + "frontend telemetry requires a browser session", + )); + } + // Use the same request-scoped session validation as tenant middleware. Neither + // a query key nor an unverified header may turn into a frontend ingest grant. + let caller = super::users::require_auth(&state, &headers).await?; + let session_tenant = state + .config_db + .resolve_session_tenant(&caller.0, &caller.4, &caller.3, None) + .await + .map_err(|_| { + denied( + StatusCode::SERVICE_UNAVAILABLE, + "session tenant unavailable", + ) + })? + .ok_or_else(|| denied(StatusCode::FORBIDDEN, "session tenant unavailable"))?; + if session_tenant != tenant.tenant_id || caller.0 != identity.actor_id { + return Err(denied( + StatusCode::FORBIDDEN, + "frontend telemetry cannot change the session tenant", + )); + } + if let Err(retry_after) = consume_rate_limits( + &state.ingest_key_limiter, + &identity.actor_id, + &tenant.tenant_id, + Instant::now(), + ) { + return Ok(( + StatusCode::TOO_MANY_REQUESTS, + [(header::RETRY_AFTER, retry_after.to_string())], + "frontend telemetry rate limit exceeded", + ) + .into_response()); + } + state.ingest_limits.check_body("rum", &body)?; + state.ingest_limits.check_decompressed("rum", body.len())?; + if body.len() > MAX_BODY_BYTES { + return Err(denied( + StatusCode::PAYLOAD_TOO_LARGE, + "frontend telemetry payload too large", + )); + } + if !super::rum::rum_enabled(&state).await { + return Err(denied(StatusCode::FORBIDDEN, "RUM ingestion is disabled")); + } + let payload: RumIngestPayload = serde_json::from_slice(&body).map_err(|_| { + denied( + StatusCode::BAD_REQUEST, + "invalid frontend telemetry payload", + ) + })?; + state + .ingest_limits + .check_entities("rum", payload.events.len())?; + let payload = sanitize_payload(payload)?; + if payload.events.is_empty() { + return Ok(axum::Json(serde_json::json!({"accepted":0})).into_response()); + } + // Existing tenant signal gate, durable writer/backpressure, usage metering, + // and finite-label RUM ingest metrics remain the sole write path. + match super::rum::ingest_events(state, tenant, payload, body.len()).await { + Ok(response) => Ok(response.into_response()), + Err((status, message)) + if matches!( + status, + StatusCode::TOO_MANY_REQUESTS | StatusCode::SERVICE_UNAVAILABLE + ) => + { + Ok((status, [(header::RETRY_AFTER, "10")], message).into_response()) + } + Err(error) => Err(error), + } +} +fn session_identity(identity: &RequestIdentity, tenant: &str) -> bool { + identity.authenticated + && identity.credential_type == "session" + && identity.actor_type == "user" + && !identity.actor_id.is_empty() + && identity.tenant_id == tenant +} +fn consume_rate_limits( + limiter: &dashmap::DashMap, + user: &str, + tenant: &str, + now: Instant, +) -> Result<(), u64> { + for (key, limit) in [ + ( + format!("frontend-user:{tenant}:{user}"), + USER_REQUESTS_PER_MINUTE, + ), + ( + format!("frontend-tenant:{tenant}"), + TENANT_REQUESTS_PER_MINUTE, + ), + ] { + if limiter.len() >= 100_000 && !limiter.contains_key(&key) { + limiter.retain(|_, (_, started)| { + now.saturating_duration_since(*started) < Duration::from_secs(60) + }); + if limiter.len() >= 100_000 { + return Err(60); + } + } + let mut entry = limiter.entry(key).or_insert((0, now)); + let elapsed = now.saturating_duration_since(entry.1); + if elapsed >= Duration::from_secs(60) { + *entry = (0, now); + } + if entry.0 >= limit { + return Err(60 - elapsed.as_secs().min(59)); + } + entry.0 += 1; + } + Ok(()) +} +fn safe_name(value: &str) -> bool { + !value.is_empty() + && value.len() <= 64 + && value + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b"._-".contains(&b)) +} +fn category_path(path: &str) -> String { + let category = path + .split(['?', '#']) + .next() + .unwrap_or("") + .split('/') + .nth(1) + .unwrap_or(""); + match category { + "" => "/", + "apm" | "logs" | "metrics" | "rum" | "dashboards" | "notebooks" | "anomalies" | "slos" + | "monitors" | "profiles" | "services" | "investigate" | "control" + | "telemetry-quality" | "noise-waste" | "capacity" => return format!("/{category}"), + _ => "/other", + } + .into() +} +fn sanitize_payload(mut payload: RumIngestPayload) -> Result { + if payload.events.len() > MAX_EVENTS { + return Err(denied( + StatusCode::PAYLOAD_TOO_LARGE, + "frontend telemetry contains too many events", + )); + } + let meta = &mut payload.meta; + if !safe_name(&meta.app_name) { + return Err(denied( + StatusCode::BAD_REQUEST, + "invalid frontend application name", + )); + } + if meta.app_name != "rush-ui" { + meta.app_name = format!("rush-ui:{}", meta.app_name); + } + if meta.app_version.is_empty() + || meta.app_version.len() > 64 + || !meta + .app_version + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b"._-+".contains(&b)) + { + meta.app_version = "unknown".into(); + } + if !matches!( + meta.environment.as_str(), + "production" | "development" | "test" | "staging" + ) { + meta.environment = "unknown".into(); + } + meta.page_path = category_path(&meta.page_path); + meta.session_id.clear(); + meta.user_id.clear(); + meta.page_url.clear(); + meta.referrer.clear(); + meta.view_name.clear(); + // These optional metadata strings are not needed for operational health. + meta.browser_name.clear(); + meta.browser_version.clear(); + meta.os_name.clear(); + meta.os_version.clear(); + if !matches!(meta.device_type.as_str(), "desktop" | "mobile" | "tablet") { + meta.device_type = "unknown".into(); + } + meta.screen_width = meta.screen_width.min(16384); + meta.screen_height = meta.screen_height.min(16384); + payload.events = payload + .events + .into_iter() + .filter_map(sanitize_event) + .collect(); + Ok(payload) +} +fn sanitize_event(mut event: RumIngestEvent) -> Option { + match event.event_type.as_str() { + "custom" + if matches!( + event.event_name.as_str(), + "frontend_started" + | "query_request_cache" + | "polling_refresh" + | "frontend_api_request" + | "frontend_api_attempt" + | "frontend_api_retry" + | "frontend_api_body" + | "frontend_api_probe" + | "frontend_api_outage" + | "frontend_exception" + | "frontend_navigation" + | "frontend_investigation_stream" + | "frontend_investigation_first_response" + | "frontend_investigation_discarded" + | "frontend_telemetry_dropped" + ) => {} + "web_vital" + if matches!( + event.vital_name.as_str(), + "LCP" | "CLS" | "INP" | "FCP" | "TTFB" + ) => + { + event.event_name.clear(); + } + "pageview" => { + event.event_name = "pageview".into(); + } + "error" | "frustration" | "interaction" | "long_task" | "loaf" => { + event.event_name.clear(); + } + _ => return None, + } + if event.event_type != "web_vital" { + event.vital_name.clear(); + event.vital_rating.clear(); + event.vital_value = 0.0; + } + event.timestamp = None; + event.trace_id.clear(); + event.span_id.clear(); + event.duration_ms = event.duration_ms.clamp(0.0, MAX_DURATION_MS); + event.vital_value = event.vital_value.clamp(0.0, MAX_DURATION_MS); + if !matches!( + event.vital_rating.as_str(), + "good" | "needs-improvement" | "poor" + ) { + event.vital_rating.clear(); + } + if event.event_type == "error" { + event.error_message = "Client error".into(); + if !matches!( + event.error_type.as_str(), + "Error" + | "TypeError" + | "RangeError" + | "ReferenceError" + | "SyntaxError" + | "URIError" + | "EvalError" + | "UnhandledRejection" + ) { + event.error_type = "Error".into(); + } + } else { + event.error_message.clear(); + event.error_type.clear(); + } + event.error_stack.clear(); + let tag = event + .interaction_target + .split(|c: char| !c.is_ascii_alphabetic()) + .next() + .unwrap_or("") + .to_ascii_lowercase(); + event.interaction_target = match tag.as_str() { + "button" | "a" | "input" | "textarea" | "select" | "canvas" | "div" | "span" | "label" + | "form" => tag, + _ => String::new(), + }; + if !matches!( + event.interaction_type.as_str(), + "click" | "rage_click" | "dead_click" | "error_click" + ) { + event.interaction_type.clear(); + } + event.attributes = sanitize_attributes(&event.attributes); + Some(event) +} +fn sanitize_attributes(raw: &str) -> String { + let Ok(Value::Object(attributes)) = serde_json::from_str::(raw) else { + return "{}".into(); + }; + let mut safe = Map::new(); + for (key, value) in attributes { + if numeric_attribute(&key) { + if let Some(number) = value.as_f64().filter(|n| n.is_finite() && *n >= 0.0) { + safe.insert( + key, + if number >= MAX_COUNTER { + serde_json::json!(9_007_199_254_740_991_u64) + } else { + value + }, + ); + } + } else if let Some(allowed) = string_attribute(&key) + && let Some(value) = value.as_str() + { + safe.insert( + key, + Value::String( + if allowed.contains(&value) { + value + } else { + "other" + } + .into(), + ), + ); + } + } + Value::Object(safe).to_string() +} +fn numeric_attribute(key: &str) -> bool { + matches!( + key, + "count" + | "duration_count" + | "duration_ms_sum" + | "duration_ms_max" + | "startup_duration_ms" + | "cache_hits" + | "fresh_hits" + | "stale_hits" + | "revalidation_failures" + | "oversized_bypasses" + | "cache_entries" + | "cache_bytes" + | "in_flight" + | "in_flight_deduplications" + | "superseded_or_aborted" + | "cache_misses" + | "cache_evictions" + | "refreshes" + | "failures" + | "skipped_overlaps" + | "backoffs" + | "average_duration_ms" + | "maximum_duration_ms" + | "maximum_backoff_ms" + | "start" + | "blockingDuration" + | "scripts" + ) || matches!( + key, + "duration_ms_le_50" + | "duration_ms_le_100" + | "duration_ms_le_250" + | "duration_ms_le_500" + | "duration_ms_le_1000" + | "duration_ms_le_2500" + | "duration_ms_le_5000" + | "duration_ms_le_10000" + | "duration_ms_le_30000" + | "duration_ms_le_60000" + ) +} +fn string_attribute(key: &str) -> Option<&'static [&'static str]> { + Some(match key { + "cancel_reason" => &[ + "caller", + "none", + "session_changed", + "superseded_or_invalidated", + ], + "category" => &[ + "alert_list", + "anomaly", + "capacity", + "dashboard", + "explore_live", + "integration_activity", + "notebook", + "service_graph", + "slo_detail", + "slo_list", + ], + "kind" => &["evidence", "report"], + "method" => &[ + "DELETE", "GET", "HEAD", "OPTIONS", "OTHER", "PATCH", "POST", "PUT", + ], + "operation" => &[ + "anomalies", + "anomaly-events", + "apikeys", + "auth", + "bubbleup", + "dashboards", + "explore", + "export", + "exports", + "features", + "frontend", + "groups", + "integrations", + "investigate", + "license", + "logs", + "monitors", + "notebooks", + "other", + "profiles", + "prometheus", + "query", + "retention", + "roles", + "rum", + "services", + "sessions", + "settings", + "slo", + "slos", + "stats", + "suggest", + "system", + "tenants", + "traces", + "users", + ], + "outcome" => &[ + "agent_error", + "body_error", + "cancelled", + "completed", + "connection_error", + "decode_error", + "duplicated", + "failed", + "http_5xx", + "http_error", + "idle_timeout", + "interrupted", + "invalid_response", + "limit_exceeded", + "network", + "network_error", + "offline", + "other_error", + "reachable", + "redirected", + "stream_error", + "success", + "timeout", + ], + "reason" => &[ + "event_limit", + "field_truncated", + "http_5xx", + "http_error", + "invalid_response", + "malformed", + "network", + "offline", + "oversized", + "timeout", + "unknown_event", + ], + "route" => &[ + "alert-channel-add", + "alerts", + "anomaly", + "anomaly-add", + "anomaly-detail", + "argocd-detail", + "audit", + "capacity", + "dashboard", + "dashboards", + "deploys", + "explore", + "fluxcd-detail", + "funnels", + "integration-page", + "integrations", + "investigate", + "kubernetes-detail", + "log-views", + "login", + "logs", + "metric-detail", + "metrics", + "monitor-create", + "monitor-detail", + "monitor-edit", + "noise-waste", + "notebook", + "notebooks", + "other", + "profiles", + "rum", + "rum-detail", + "service-detail", + "services", + "settings", + "slo-detail", + "slos", + "sre-agent", + "sre-agent-session", + "sso-setup", + "telemetry-quality", + "trace", + "traces", + "usage", + ], + "source" => &["bootstrap", "router", "vue"], + "stage" => &[ + "event", + "lazy_chunk", + "mount", + "other", + "render", + "setup", + "watcher", + ], + "status_class" => &["1xx", "2xx", "3xx", "4xx", "5xx", "none"], + _ => return None, + }) +} + +#[cfg(test)] +#[path = "frontend_telemetry_tests.rs"] +mod tests; diff --git a/src/handlers/frontend_telemetry_tests.rs b/src/handlers/frontend_telemetry_tests.rs new file mode 100644 index 0000000..15b9761 --- /dev/null +++ b/src/handlers/frontend_telemetry_tests.rs @@ -0,0 +1,158 @@ +use super::*; +use serde_json::json; +fn payload(events: Value) -> RumIngestPayload { + serde_json::from_value(json!({"meta":{"app_name":"rush-ui","app_version":"0.1.13","environment":"production","session_id":"sensitive-session","user_id":"sensitive-user","page_url":"https://rush.example/logs?secret=sensitive-query","page_path":"/logs/sensitive-id?secret=sensitive-query","view_name":"sensitive-title","referrer":"sensitive-referrer"},"events":events})).unwrap() +} +#[test] +fn privacy_is_enforced_without_losing_bounded_health_signals() { + let input = payload(json!([ + {"event_type":"custom","event_name":"frontend_api_request","timestamp":123,"trace_id":"sensitive-trace","span_id":"sensitive-span","vital_name":"sensitive-vital","attributes":json!({"operation":"query","method":"GET","outcome":"decode_error","count":2,"duration_ms_sum":12.5,"status_class":"2xx","query":"sensitive-query","password":"sensitive-password","route":"sensitive-route"}).to_string()}, + {"event_type":"pageview","event_name":"sensitive-title","trace_id":"sensitive-trace","span_id":"sensitive-span"}, + {"event_type":"error","error_message":"sensitive-password","error_stack":"sensitive-stack","error_type":"sensitive-type"}, + {"event_type":"custom","event_name":"arbitrary-log-relay","attributes":"sensitive-payload"}, + {"event_type":"resource","event_name":"https://sensitive-url/"} + ])); + let output = sanitize_payload(input).unwrap(); + assert_eq!(output.events.len(), 3); + assert_eq!(output.meta.page_path, "/logs"); + let encoded = serde_json::to_string(&output).unwrap(); + assert!(!encoded.contains("sensitive")); + assert!(!encoded.contains("arbitrary-log-relay")); + assert_eq!(output.events[0].timestamp, None); + let attrs: Value = serde_json::from_str(&output.events[0].attributes).unwrap(); + assert_eq!(attrs["operation"], "query"); + assert_eq!(attrs["count"].as_f64(), Some(2.0)); + assert_eq!(attrs["duration_ms_sum"].as_f64(), Some(12.5)); + assert_eq!(attrs["route"], "other"); + assert_eq!(output.events[1].event_name, "pageview"); + assert!( + output + .events + .iter() + .all(|e| e.trace_id.is_empty() && e.span_id.is_empty()) + ); + assert_eq!(output.events[2].error_message, "Client error"); + assert_eq!(output.events[2].error_type, "Error"); +} +#[test] +fn real_sdk_fixture_preserves_the_allowed_event_and_attribute_contract() { + let mut payloads: Vec = serde_json::from_str(include_str!( + "../../tests/fixtures/frontend-health-sdk.json" + )) + .unwrap(); + payloads.push( + serde_json::from_str(include_str!( + "../../tests/fixtures/frontend-health-all-events.json" + )) + .unwrap(), + ); + let mut accepted = Vec::new(); + for original in payloads { + let events = original + .events + .iter() + .map(|event| { + ( + event.event_type.clone(), + event.event_name.clone(), + event.attributes.clone(), + ) + }) + .collect::>(); + let sanitized = sanitize_payload(original).unwrap(); + assert_eq!(sanitized.events.len(), events.len()); + for (event, (kind, name, attrs)) in sanitized.events.iter().zip(events) { + assert_eq!(event.event_type, kind); + if kind == "custom" { + assert_eq!(event.event_name, name); + let before: Value = serde_json::from_str(&attrs).unwrap(); + let after: Value = serde_json::from_str(&event.attributes).unwrap(); + for (key, value) in before.as_object().unwrap() { + let retained = &after[key]; + if value.is_number() { + assert_eq!(retained.as_f64(), value.as_f64(), "{name}.{key}"); + } else { + assert_eq!(retained, value, "{name}.{key}"); + } + } + } + } + accepted.push(sanitized); + } + if let Ok(path) = std::env::var("RUSH_FRONTEND_SANITIZED_FIXTURE_OUTPUT") { + std::fs::write(path, serde_json::to_vec_pretty(&accepted).unwrap()).unwrap(); + } +} +#[test] +fn identity_checks_accept_only_a_validated_same_tenant_session() { + let identity = RequestIdentity { + tenant_id: "tenant-a".into(), + authenticated: true, + actor_id: "user-1".into(), + actor_name: "viewer".into(), + actor_type: "user".into(), + credential_type: "session".into(), + }; + assert!(session_identity(&identity, "tenant-a")); + assert!(!session_identity(&identity, "tenant-b")); + for kind in ["anonymous", "query_key", "ingest_key", "internal_sre"] { + let mut changed = identity.clone(); + changed.credential_type = kind.into(); + assert!(!session_identity(&changed, "tenant-a")); + } + let mut changed = identity.clone(); + changed.authenticated = false; + assert!(!session_identity(&changed, "tenant-a")); + changed = identity; + changed.actor_id.clear(); + assert!(!session_identity(&changed, "tenant-a")); +} +#[test] +fn user_and_tenant_limits_are_independent_and_reset() { + let limiter = dashmap::DashMap::new(); + let now = Instant::now(); + for _ in 0..USER_REQUESTS_PER_MINUTE { + consume_rate_limits(&limiter, "u1", "t1", now).unwrap(); + } + assert_eq!(consume_rate_limits(&limiter, "u1", "t1", now), Err(60)); + consume_rate_limits(&limiter, "u2", "t1", now).unwrap(); + consume_rate_limits(&limiter, "u1", "t1", now + Duration::from_secs(60)).unwrap(); + let limiter = dashmap::DashMap::new(); + for index in 0..TENANT_REQUESTS_PER_MINUTE { + consume_rate_limits(&limiter, &format!("u{index}"), "t1", now).unwrap(); + } + assert!(consume_rate_limits(&limiter, "another", "t1", now).is_err()); + consume_rate_limits(&limiter, "another", "t2", now).unwrap(); +} +#[test] +fn payload_and_attribute_values_remain_bounded() { + let event = json!({"event_type":"custom","event_name":"frontend_started","duration_ms":-1,"attributes":json!({"startup_duration_ms":1e30,"count":-1,"method":"sensitive-url","arbitrary":"sensitive-token"}).to_string()}); + let result = sanitize_payload(payload(json!([event.clone()]))).unwrap(); + let attrs: Value = serde_json::from_str(&result.events[0].attributes).unwrap(); + assert_eq!(attrs["startup_duration_ms"].as_f64(), Some(MAX_COUNTER)); + assert!(attrs.get("count").is_none()); + assert_eq!(attrs["method"], "other"); + assert!(attrs.get("arbitrary").is_none()); + let too_many = Value::Array(vec![event; MAX_EVENTS + 1]); + assert_eq!( + sanitize_payload(payload(too_many)).unwrap_err().0, + StatusCode::PAYLOAD_TOO_LARGE + ); + let mut invalid = payload(json!([])); + invalid.meta.app_name = "secret?token=value".into(); + assert_eq!( + sanitize_payload(invalid).unwrap_err().0, + StatusCode::BAD_REQUEST + ); +} + +#[test] +fn namespace_and_build_metadata_are_preserved_with_bounded_application_identity() { + let mut input = payload(json!([])); + input.meta.app_name = "canary".into(); + input.meta.app_version = "1.2.3+abc123".into(); + let output = sanitize_payload(input).unwrap(); + assert_eq!(output.meta.app_name, "rush-ui:canary"); + assert_eq!(output.meta.app_version, "1.2.3+abc123"); + assert_eq!(output.meta.environment, "production"); +} diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index af54a3f..2f3ef13 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -69,3 +69,5 @@ pub mod usage; pub mod usage_metering; pub mod users; pub mod whoami; + +pub mod frontend_telemetry; diff --git a/src/handlers/rum.rs b/src/handlers/rum.rs index f0ba2c9..a5bb6ef 100644 --- a/src/handlers/rum.rs +++ b/src/handlers/rum.rs @@ -260,7 +260,7 @@ pub struct RumSessionRow { /// serde_json::to_string solely to count bytes. /// Whether RUM is enabled (admin toggle in Settings → General). Defaults ON; /// only an explicit "false" disables it. Gates both RUM ingest endpoints. -async fn rum_enabled(state: &AppState) -> bool { +pub(super) async fn rum_enabled(state: &AppState) -> bool { state .config_db .get_setting("rum_enabled") @@ -283,6 +283,16 @@ pub async fn ingest( state.ingest_limits.check_decompressed("rum", body.len())?; let payload: RumIngestPayload = serde_json::from_slice(&body) .map_err(|_| state.ingest_limits.malformed("rum", "invalid RUM payload"))?; + ingest_events(state, tenant, payload, body.len()).await +} + +/// Shared durable RUM write path after the route-specific credential and payload checks. +pub(super) async fn ingest_events( + state: AppState, + tenant: TenantContext, + payload: RumIngestPayload, + wire_bytes: usize, +) -> Result { state .ingest_limits .check_entities("rum", payload.events.len())?; @@ -420,7 +430,7 @@ pub async fn ingest( tenant_id, "rum", payload.events.len() as u64, - body.len() as u64, + wire_bytes as u64, ); tracing::info!( diff --git a/src/main.rs b/src/main.rs index 0525e8e..2a5a833 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1705,6 +1705,16 @@ fn consume_ingest_rate_limit(state: &AppState, key_id: &str, limit: u64) -> Resu Ok(()) } +fn should_reject_frontend_telemetry( + method: &Method, + path: &str, + credential: &CredentialKind, +) -> bool { + method == Method::POST + && path == "/api/v1/frontend/telemetry" + && *credential != CredentialKind::Session +} + fn credential_route_denial( credential: &CredentialKind, ingest_route: bool, @@ -1988,6 +1998,16 @@ async fn enforce_tenant_auth_middleware( _ => {} } + if should_reject_frontend_telemetry(&method, &path, &resolution.credential) { + // This narrow telemetry path deliberately rejects unauthenticated users + // even when a tenant allows anonymous queries. No generic ingest grant. + return ( + StatusCode::UNAUTHORIZED, + "frontend telemetry requires a browser session", + ) + .into_response(); + } + let agent_key = resolution .api_key .as_ref() @@ -3890,6 +3910,13 @@ async fn main() -> anyhow::Result<()> { get(handlers::groups::get_user_groups) .put(handlers::groups::set_user_groups), ) + // Dedicated session-authenticated, restricted health telemetry for this UI. + // General RUM/telemetry ingestion continues to require ingest credentials. + .route( + "/api/v1/frontend/telemetry", + post(handlers::frontend_telemetry::ingest) + .layer(DefaultBodyLimit::max(handlers::frontend_telemetry::MAX_BODY_BYTES)), + ) // RUM (Real User Monitoring) .route("/api/v1/rum/ingest", post(handlers::rum::ingest)) .route("/api/v1/rum/apps", get(handlers::rum::list_apps)) @@ -4510,6 +4537,42 @@ mod tenant_auth_tests { ); } + #[test] + fn frontend_health_is_a_separate_session_only_contract() { + let path = "/api/v1/frontend/telemetry"; + assert_eq!(ingest_signal_for_route(&Method::POST, path), None); + assert!(!super::should_reject_frontend_telemetry( + &Method::POST, + path, + &CredentialKind::Session + )); + for kind in [ + CredentialKind::Anonymous, + CredentialKind::QueryKey, + CredentialKind::IngestKey, + CredentialKind::InternalSre, + ] { + assert!(super::should_reject_frontend_telemetry( + &Method::POST, + path, + &kind + )); + } + assert!(requires_csrf_origin( + &Method::POST, + path, + Some(&CredentialKind::Session) + )); + assert_eq!( + credential_route_denial(&CredentialKind::Session, true, false), + Some("ingest_key_required") + ); + assert_eq!( + credential_route_denial(&CredentialKind::Session, true, true), + Some("ingest_key_required") + ); + } + #[test] fn startup_validates_api_key_secret_before_running_migrations() { let source = include_str!("main.rs"); diff --git a/tests/fixtures/frontend-clickhouse.xml b/tests/fixtures/frontend-clickhouse.xml new file mode 100644 index 0000000..afe43d5 --- /dev/null +++ b/tests/fixtures/frontend-clickhouse.xml @@ -0,0 +1,19 @@ + + rush_ + + + + local + /var/lib/clickhouse/frontend_test_cold/ + + + + + + default + frontend_test_cold + + + + + diff --git a/tests/fixtures/frontend-health-all-events.json b/tests/fixtures/frontend-health-all-events.json new file mode 100644 index 0000000..f5eb81c --- /dev/null +++ b/tests/fixtures/frontend-health-all-events.json @@ -0,0 +1,112 @@ +{ + "meta": { + "app_name": "health-smoke", + "app_version": "0.1.17", + "environment": "test", + "session_id": "fixture-session-0", + "user_id": "fixture-user", + "page_url": "https://frontend.example.test/logs/fixture?secret=fixture", + "page_path": "/logs/fixture?secret=fixture", + "view_name": "Fixture page", + "referrer": "https://frontend.example.test/fixture", + "browser_name": "Chrome", + "browser_version": "157.0.0.0", + "os_name": "macOS", + "os_version": "10.15.7", + "device_type": "desktop", + "screen_width": 1280, + "screen_height": 720 + }, + "events": [ + { + "event_type": "custom", + "event_name": "frontend_api_request", + "timestamp": 1794780000000000000, + "attributes": "{\"operation\":\"auth\",\"method\":\"GET\",\"outcome\":\"success\",\"status_class\":\"1xx\",\"cancel_reason\":\"none\",\"count\":1,\"duration_count\":1,\"duration_ms_sum\":42,\"duration_ms_max\":42,\"duration_ms_le_50\":1,\"duration_ms_le_100\":1,\"duration_ms_le_250\":1,\"duration_ms_le_500\":1,\"duration_ms_le_1000\":1,\"duration_ms_le_2500\":1,\"duration_ms_le_5000\":1,\"duration_ms_le_10000\":1,\"duration_ms_le_30000\":1,\"duration_ms_le_60000\":1}" + }, + { + "event_type": "custom", + "event_name": "frontend_api_attempt", + "timestamp": 1794780000000000000, + "attributes": "{\"operation\":\"auth\",\"method\":\"GET\",\"outcome\":\"success\",\"status_class\":\"1xx\",\"cancel_reason\":\"none\",\"count\":1,\"duration_count\":1,\"duration_ms_sum\":42,\"duration_ms_max\":42,\"duration_ms_le_50\":1,\"duration_ms_le_100\":1,\"duration_ms_le_250\":1,\"duration_ms_le_500\":1,\"duration_ms_le_1000\":1,\"duration_ms_le_2500\":1,\"duration_ms_le_5000\":1,\"duration_ms_le_10000\":1,\"duration_ms_le_30000\":1,\"duration_ms_le_60000\":1}" + }, + { + "event_type": "custom", + "event_name": "frontend_api_retry", + "timestamp": 1794780000000000000, + "attributes": "{\"operation\":\"auth\",\"method\":\"GET\",\"outcome\":\"success\",\"status_class\":\"1xx\",\"cancel_reason\":\"none\",\"count\":1,\"duration_count\":1,\"duration_ms_sum\":42,\"duration_ms_max\":42,\"duration_ms_le_50\":1,\"duration_ms_le_100\":1,\"duration_ms_le_250\":1,\"duration_ms_le_500\":1,\"duration_ms_le_1000\":1,\"duration_ms_le_2500\":1,\"duration_ms_le_5000\":1,\"duration_ms_le_10000\":1,\"duration_ms_le_30000\":1,\"duration_ms_le_60000\":1}" + }, + { + "event_type": "custom", + "event_name": "frontend_api_body", + "timestamp": 1794780000000000000, + "attributes": "{\"operation\":\"auth\",\"method\":\"GET\",\"outcome\":\"success\",\"status_class\":\"1xx\",\"cancel_reason\":\"none\",\"count\":1,\"duration_count\":1,\"duration_ms_sum\":42,\"duration_ms_max\":42,\"duration_ms_le_50\":1,\"duration_ms_le_100\":1,\"duration_ms_le_250\":1,\"duration_ms_le_500\":1,\"duration_ms_le_1000\":1,\"duration_ms_le_2500\":1,\"duration_ms_le_5000\":1,\"duration_ms_le_10000\":1,\"duration_ms_le_30000\":1,\"duration_ms_le_60000\":1}" + }, + { + "event_type": "custom", + "event_name": "frontend_started", + "timestamp": 1794780000000000000, + "attributes": "{\"startup_duration_ms\":42}" + }, + { + "event_type": "custom", + "event_name": "frontend_telemetry_dropped", + "timestamp": 1794780000000000000, + "attributes": "{\"count\":1}" + }, + { + "event_type": "custom", + "event_name": "frontend_exception", + "timestamp": 1794780000000000000, + "attributes": "{\"source\":\"vue\",\"stage\":\"setup\",\"count\":1,\"duration_count\":1,\"duration_ms_sum\":42,\"duration_ms_max\":42,\"duration_ms_le_50\":1,\"duration_ms_le_100\":1,\"duration_ms_le_250\":1,\"duration_ms_le_500\":1,\"duration_ms_le_1000\":1,\"duration_ms_le_2500\":1,\"duration_ms_le_5000\":1,\"duration_ms_le_10000\":1,\"duration_ms_le_30000\":1,\"duration_ms_le_60000\":1}" + }, + { + "event_type": "custom", + "event_name": "frontend_navigation", + "timestamp": 1794780000000000000, + "attributes": "{\"route\":\"alert-channel-add\",\"outcome\":\"success\",\"count\":1,\"duration_count\":1,\"duration_ms_sum\":42,\"duration_ms_max\":42,\"duration_ms_le_50\":1,\"duration_ms_le_100\":1,\"duration_ms_le_250\":1,\"duration_ms_le_500\":1,\"duration_ms_le_1000\":1,\"duration_ms_le_2500\":1,\"duration_ms_le_5000\":1,\"duration_ms_le_10000\":1,\"duration_ms_le_30000\":1,\"duration_ms_le_60000\":1}" + }, + { + "event_type": "custom", + "event_name": "frontend_investigation_stream", + "timestamp": 1794780000000000000, + "attributes": "{\"outcome\":\"completed\",\"count\":1,\"duration_count\":1,\"duration_ms_sum\":42,\"duration_ms_max\":42,\"duration_ms_le_50\":1,\"duration_ms_le_100\":1,\"duration_ms_le_250\":1,\"duration_ms_le_500\":1,\"duration_ms_le_1000\":1,\"duration_ms_le_2500\":1,\"duration_ms_le_5000\":1,\"duration_ms_le_10000\":1,\"duration_ms_le_30000\":1,\"duration_ms_le_60000\":1}" + }, + { + "event_type": "custom", + "event_name": "frontend_investigation_first_response", + "timestamp": 1794780000000000000, + "attributes": "{\"kind\":\"report\",\"count\":1,\"duration_count\":1,\"duration_ms_sum\":42,\"duration_ms_max\":42,\"duration_ms_le_50\":1,\"duration_ms_le_100\":1,\"duration_ms_le_250\":1,\"duration_ms_le_500\":1,\"duration_ms_le_1000\":1,\"duration_ms_le_2500\":1,\"duration_ms_le_5000\":1,\"duration_ms_le_10000\":1,\"duration_ms_le_30000\":1,\"duration_ms_le_60000\":1}" + }, + { + "event_type": "custom", + "event_name": "frontend_investigation_discarded", + "timestamp": 1794780000000000000, + "attributes": "{\"reason\":\"malformed\",\"count\":1,\"duration_count\":1,\"duration_ms_sum\":42,\"duration_ms_max\":42,\"duration_ms_le_50\":1,\"duration_ms_le_100\":1,\"duration_ms_le_250\":1,\"duration_ms_le_500\":1,\"duration_ms_le_1000\":1,\"duration_ms_le_2500\":1,\"duration_ms_le_5000\":1,\"duration_ms_le_10000\":1,\"duration_ms_le_30000\":1,\"duration_ms_le_60000\":1}" + }, + { + "event_type": "custom", + "event_name": "frontend_api_probe", + "timestamp": 1794780000000000000, + "attributes": "{\"outcome\":\"reachable\",\"count\":1,\"duration_count\":1,\"duration_ms_sum\":42,\"duration_ms_max\":42,\"duration_ms_le_50\":1,\"duration_ms_le_100\":1,\"duration_ms_le_250\":1,\"duration_ms_le_500\":1,\"duration_ms_le_1000\":1,\"duration_ms_le_2500\":1,\"duration_ms_le_5000\":1,\"duration_ms_le_10000\":1,\"duration_ms_le_30000\":1,\"duration_ms_le_60000\":1}" + }, + { + "event_type": "custom", + "event_name": "frontend_api_outage", + "timestamp": 1794780000000000000, + "attributes": "{\"reason\":\"offline\",\"count\":1,\"duration_count\":1,\"duration_ms_sum\":42,\"duration_ms_max\":42,\"duration_ms_le_50\":1,\"duration_ms_le_100\":1,\"duration_ms_le_250\":1,\"duration_ms_le_500\":1,\"duration_ms_le_1000\":1,\"duration_ms_le_2500\":1,\"duration_ms_le_5000\":1,\"duration_ms_le_10000\":1,\"duration_ms_le_30000\":1,\"duration_ms_le_60000\":1}" + }, + { + "event_type": "custom", + "event_name": "query_request_cache", + "timestamp": 1794780000000000000, + "attributes": "{\"cache_hits\":1,\"fresh_hits\":1,\"stale_hits\":1,\"revalidation_failures\":1,\"oversized_bypasses\":1,\"cache_entries\":1,\"cache_bytes\":1,\"in_flight\":1,\"in_flight_deduplications\":1,\"superseded_or_aborted\":1,\"cache_misses\":1,\"cache_evictions\":1}" + }, + { + "event_type": "custom", + "event_name": "polling_refresh", + "timestamp": 1794780000000000000, + "attributes": "{\"category\":\"alert_list\",\"refreshes\":1,\"failures\":1,\"skipped_overlaps\":1,\"backoffs\":1,\"average_duration_ms\":1,\"maximum_duration_ms\":1,\"maximum_backoff_ms\":1}" + } + ] +} diff --git a/tests/fixtures/frontend-health-sdk.json b/tests/fixtures/frontend-health-sdk.json new file mode 100644 index 0000000..b9b4109 --- /dev/null +++ b/tests/fixtures/frontend-health-sdk.json @@ -0,0 +1,297 @@ +[ + { + "meta": { + "app_name": "health-smoke", + "app_version": "0.1.17", + "environment": "test", + "session_id": "fixture-session-0", + "user_id": "fixture-user", + "page_url": "https://frontend.example.test/logs/fixture?secret=fixture", + "page_path": "/logs/fixture?secret=fixture", + "view_name": "Fixture page", + "referrer": "https://frontend.example.test/fixture", + "browser_name": "Chrome", + "browser_version": "157.0.0.0", + "os_name": "macOS", + "os_version": "10.15.7", + "device_type": "desktop", + "screen_width": 1280, + "screen_height": 720 + }, + "events": [ + { + "event_type": "pageview", + "event_name": "Fixture page", + "duration_ms": 58.39999997615814, + "trace_id": "fixture-trace", + "span_id": "fixture-span", + "timestamp": 1794780651734400000 + }, + { + "event_type": "custom", + "event_name": "frontend_started", + "attributes": "{\"startup_duration_ms\":84}", + "timestamp": 1794780651734500000 + }, + { + "event_type": "web_vital", + "vital_name": "FCP", + "vital_value": 80, + "vital_rating": "good", + "timestamp": 1794780651741599700 + }, + { + "event_type": "web_vital", + "vital_name": "TTFB", + "vital_value": 4.799999982118607, + "vital_rating": "good", + "timestamp": 1794780651741800000 + }, + { + "event_type": "pageview", + "event_name": "Fixture page", + "duration_ms": 58.39999997615814, + "trace_id": "fixture-trace", + "span_id": "fixture-span", + "timestamp": 1794780651742900000 + } + ] + }, + { + "meta": { + "app_name": "health-smoke", + "app_version": "0.1.17", + "environment": "test", + "session_id": "fixture-session-1", + "user_id": "fixture-user", + "page_url": "https://frontend.example.test/logs/fixture?secret=fixture", + "page_path": "/logs/fixture?secret=fixture", + "view_name": "Fixture page", + "referrer": "https://frontend.example.test/fixture", + "browser_name": "Chrome", + "browser_version": "157.0.0.0", + "os_name": "macOS", + "os_version": "10.15.7", + "device_type": "desktop", + "screen_width": 1280, + "screen_height": 720 + }, + "events": [ + { + "event_type": "interaction", + "interaction_type": "click", + "interaction_target": "a", + "timestamp": 1794780652536400000 + }, + { + "event_type": "web_vital", + "vital_name": "LCP", + "vital_value": 128, + "vital_rating": "good", + "timestamp": 1794780652572500000 + } + ] + }, + { + "meta": { + "app_name": "health-smoke", + "app_version": "0.1.17", + "environment": "test", + "session_id": "fixture-session-2", + "user_id": "fixture-user", + "page_url": "https://frontend.example.test/logs/fixture?secret=fixture", + "page_path": "/logs/fixture?secret=fixture", + "view_name": "Fixture page", + "referrer": "https://frontend.example.test/fixture", + "browser_name": "Chrome", + "browser_version": "157.0.0.0", + "os_name": "macOS", + "os_version": "10.15.7", + "device_type": "desktop", + "screen_width": 1280, + "screen_height": 720 + }, + "events": [ + { + "event_type": "pageview", + "event_name": "Fixture page", + "duration_ms": 58.39999997615814, + "trace_id": "fixture-trace", + "span_id": "fixture-span", + "timestamp": 1794780652964000000 + } + ] + }, + { + "meta": { + "app_name": "health-smoke", + "app_version": "0.1.17", + "environment": "test", + "session_id": "fixture-session-3", + "user_id": "fixture-user", + "page_url": "https://frontend.example.test/logs/fixture?secret=fixture", + "page_path": "/logs/fixture?secret=fixture", + "view_name": "Fixture page", + "referrer": "https://frontend.example.test/fixture", + "browser_name": "Chrome", + "browser_version": "157.0.0.0", + "os_name": "macOS", + "os_version": "10.15.7", + "device_type": "desktop", + "screen_width": 1280, + "screen_height": 720 + }, + "events": [ + { + "event_type": "custom", + "event_name": "frontend_api_attempt", + "attributes": "{\"cancel_reason\":\"none\",\"method\":\"GET\",\"operation\":\"features\",\"outcome\":\"success\",\"status_class\":\"2xx\",\"count\":2,\"duration_count\":2,\"duration_ms_sum\":50.099999994039536,\"duration_ms_max\":44.5,\"duration_ms_le_50\":2,\"duration_ms_le_100\":2,\"duration_ms_le_250\":2,\"duration_ms_le_500\":2,\"duration_ms_le_1000\":2,\"duration_ms_le_2500\":2,\"duration_ms_le_5000\":2,\"duration_ms_le_10000\":2,\"duration_ms_le_30000\":2,\"duration_ms_le_60000\":2}", + "timestamp": 1794780652964899800 + }, + { + "event_type": "custom", + "event_name": "frontend_api_attempt", + "attributes": "{\"cancel_reason\":\"none\",\"method\":\"GET\",\"operation\":\"license\",\"outcome\":\"success\",\"status_class\":\"2xx\",\"count\":2,\"duration_count\":2,\"duration_ms_sum\":52.90000003576279,\"duration_ms_max\":47.10000002384186,\"duration_ms_le_50\":2,\"duration_ms_le_100\":2,\"duration_ms_le_250\":2,\"duration_ms_le_500\":2,\"duration_ms_le_1000\":2,\"duration_ms_le_2500\":2,\"duration_ms_le_5000\":2,\"duration_ms_le_10000\":2,\"duration_ms_le_30000\":2,\"duration_ms_le_60000\":2}", + "timestamp": 1794780652965000000 + }, + { + "event_type": "custom", + "event_name": "frontend_api_attempt", + "attributes": "{\"cancel_reason\":\"none\",\"method\":\"GET\",\"operation\":\"auth\",\"outcome\":\"success\",\"status_class\":\"2xx\",\"count\":1,\"duration_count\":1,\"duration_ms_sum\":46.900000005960464,\"duration_ms_max\":46.900000005960464,\"duration_ms_le_50\":1,\"duration_ms_le_100\":1,\"duration_ms_le_250\":1,\"duration_ms_le_500\":1,\"duration_ms_le_1000\":1,\"duration_ms_le_2500\":1,\"duration_ms_le_5000\":1,\"duration_ms_le_10000\":1,\"duration_ms_le_30000\":1,\"duration_ms_le_60000\":1}", + "timestamp": 1794780652965099800 + }, + { + "event_type": "custom", + "event_name": "frontend_api_body", + "attributes": "{\"cancel_reason\":\"none\",\"method\":\"GET\",\"operation\":\"features\",\"outcome\":\"completed\",\"status_class\":\"2xx\",\"count\":2,\"duration_count\":2,\"duration_ms_sum\":6.299999982118607,\"duration_ms_max\":4.799999982118607,\"duration_ms_le_50\":2,\"duration_ms_le_100\":2,\"duration_ms_le_250\":2,\"duration_ms_le_500\":2,\"duration_ms_le_1000\":2,\"duration_ms_le_2500\":2,\"duration_ms_le_5000\":2,\"duration_ms_le_10000\":2,\"duration_ms_le_30000\":2,\"duration_ms_le_60000\":2}", + "timestamp": 1794780652965099800 + }, + { + "event_type": "custom", + "event_name": "frontend_api_body", + "attributes": "{\"cancel_reason\":\"none\",\"method\":\"GET\",\"operation\":\"license\",\"outcome\":\"completed\",\"status_class\":\"2xx\",\"count\":2,\"duration_count\":2,\"duration_ms_sum\":2.9999999701976776,\"duration_ms_max\":1.699999988079071,\"duration_ms_le_50\":2,\"duration_ms_le_100\":2,\"duration_ms_le_250\":2,\"duration_ms_le_500\":2,\"duration_ms_le_1000\":2,\"duration_ms_le_2500\":2,\"duration_ms_le_5000\":2,\"duration_ms_le_10000\":2,\"duration_ms_le_30000\":2,\"duration_ms_le_60000\":2}", + "timestamp": 1794780652965099800 + }, + { + "event_type": "custom", + "event_name": "frontend_api_body", + "attributes": "{\"cancel_reason\":\"none\",\"method\":\"GET\",\"operation\":\"auth\",\"outcome\":\"completed\",\"status_class\":\"2xx\",\"count\":1,\"duration_count\":1,\"duration_ms_sum\":1.199999988079071,\"duration_ms_max\":1.199999988079071,\"duration_ms_le_50\":1,\"duration_ms_le_100\":1,\"duration_ms_le_250\":1,\"duration_ms_le_500\":1,\"duration_ms_le_1000\":1,\"duration_ms_le_2500\":1,\"duration_ms_le_5000\":1,\"duration_ms_le_10000\":1,\"duration_ms_le_30000\":1,\"duration_ms_le_60000\":1}", + "timestamp": 1794780652965099800 + }, + { + "event_type": "custom", + "event_name": "frontend_api_request", + "attributes": "{\"cancel_reason\":\"none\",\"method\":\"GET\",\"operation\":\"auth\",\"outcome\":\"success\",\"status_class\":\"2xx\",\"count\":1,\"duration_count\":1,\"duration_ms_sum\":49.29999998211861,\"duration_ms_max\":49.29999998211861,\"duration_ms_le_50\":1,\"duration_ms_le_100\":1,\"duration_ms_le_250\":1,\"duration_ms_le_500\":1,\"duration_ms_le_1000\":1,\"duration_ms_le_2500\":1,\"duration_ms_le_5000\":1,\"duration_ms_le_10000\":1,\"duration_ms_le_30000\":1,\"duration_ms_le_60000\":1}", + "timestamp": 1794780652965099800 + }, + { + "event_type": "custom", + "event_name": "frontend_api_probe", + "attributes": "{\"outcome\":\"reachable\",\"count\":2,\"duration_count\":2,\"duration_ms_sum\":56.599999994039536,\"duration_ms_max\":54,\"duration_ms_le_100\":2,\"duration_ms_le_250\":2,\"duration_ms_le_500\":2,\"duration_ms_le_1000\":2,\"duration_ms_le_2500\":2,\"duration_ms_le_5000\":2,\"duration_ms_le_10000\":2,\"duration_ms_le_30000\":2,\"duration_ms_le_60000\":2,\"duration_ms_le_50\":1}", + "timestamp": 1794780652965099800 + }, + { + "event_type": "custom", + "event_name": "frontend_api_attempt", + "attributes": "{\"cancel_reason\":\"none\",\"method\":\"GET\",\"operation\":\"tenants\",\"outcome\":\"success\",\"status_class\":\"2xx\",\"count\":1,\"duration_count\":1,\"duration_ms_sum\":5,\"duration_ms_max\":5,\"duration_ms_le_50\":1,\"duration_ms_le_100\":1,\"duration_ms_le_250\":1,\"duration_ms_le_500\":1,\"duration_ms_le_1000\":1,\"duration_ms_le_2500\":1,\"duration_ms_le_5000\":1,\"duration_ms_le_10000\":1,\"duration_ms_le_30000\":1,\"duration_ms_le_60000\":1}", + "timestamp": 1794780652965099800 + }, + { + "event_type": "custom", + "event_name": "frontend_api_body", + "attributes": "{\"cancel_reason\":\"none\",\"method\":\"GET\",\"operation\":\"tenants\",\"outcome\":\"completed\",\"status_class\":\"2xx\",\"count\":1,\"duration_count\":1,\"duration_ms_sum\":0.30000001192092896,\"duration_ms_max\":0.30000001192092896,\"duration_ms_le_50\":1,\"duration_ms_le_100\":1,\"duration_ms_le_250\":1,\"duration_ms_le_500\":1,\"duration_ms_le_1000\":1,\"duration_ms_le_2500\":1,\"duration_ms_le_5000\":1,\"duration_ms_le_10000\":1,\"duration_ms_le_30000\":1,\"duration_ms_le_60000\":1}", + "timestamp": 1794780652965099800 + }, + { + "event_type": "custom", + "event_name": "frontend_navigation", + "attributes": "{\"outcome\":\"success\",\"route\":\"investigate\",\"count\":1,\"duration_count\":1,\"duration_ms_sum\":61.5,\"duration_ms_max\":61.5,\"duration_ms_le_100\":1,\"duration_ms_le_250\":1,\"duration_ms_le_500\":1,\"duration_ms_le_1000\":1,\"duration_ms_le_2500\":1,\"duration_ms_le_5000\":1,\"duration_ms_le_10000\":1,\"duration_ms_le_30000\":1,\"duration_ms_le_60000\":1}", + "timestamp": 1794780652965099800 + }, + { + "event_type": "custom", + "event_name": "frontend_api_attempt", + "attributes": "{\"cancel_reason\":\"none\",\"method\":\"GET\",\"operation\":\"sessions\",\"outcome\":\"success\",\"status_class\":\"2xx\",\"count\":2,\"duration_count\":2,\"duration_ms_sum\":9.699999988079071,\"duration_ms_max\":6.699999988079071,\"duration_ms_le_50\":2,\"duration_ms_le_100\":2,\"duration_ms_le_250\":2,\"duration_ms_le_500\":2,\"duration_ms_le_1000\":2,\"duration_ms_le_2500\":2,\"duration_ms_le_5000\":2,\"duration_ms_le_10000\":2,\"duration_ms_le_30000\":2,\"duration_ms_le_60000\":2}", + "timestamp": 1794780652965200000 + }, + { + "event_type": "custom", + "event_name": "frontend_api_body", + "attributes": "{\"cancel_reason\":\"none\",\"method\":\"GET\",\"operation\":\"sessions\",\"outcome\":\"completed\",\"status_class\":\"2xx\",\"count\":2,\"duration_count\":2,\"duration_ms_sum\":0.20000001788139343,\"duration_ms_max\":0.10000002384185791,\"duration_ms_le_50\":2,\"duration_ms_le_100\":2,\"duration_ms_le_250\":2,\"duration_ms_le_500\":2,\"duration_ms_le_1000\":2,\"duration_ms_le_2500\":2,\"duration_ms_le_5000\":2,\"duration_ms_le_10000\":2,\"duration_ms_le_30000\":2,\"duration_ms_le_60000\":2}", + "timestamp": 1794780652965200000 + }, + { + "event_type": "custom", + "event_name": "frontend_api_request", + "attributes": "{\"cancel_reason\":\"none\",\"method\":\"GET\",\"operation\":\"sessions\",\"outcome\":\"success\",\"status_class\":\"2xx\",\"count\":2,\"duration_count\":2,\"duration_ms_sum\":9.900000005960464,\"duration_ms_max\":6.799999982118607,\"duration_ms_le_50\":2,\"duration_ms_le_100\":2,\"duration_ms_le_250\":2,\"duration_ms_le_500\":2,\"duration_ms_le_1000\":2,\"duration_ms_le_2500\":2,\"duration_ms_le_5000\":2,\"duration_ms_le_10000\":2,\"duration_ms_le_30000\":2,\"duration_ms_le_60000\":2}", + "timestamp": 1794780652965200000 + }, + { + "event_type": "custom", + "event_name": "frontend_api_attempt", + "attributes": "{\"cancel_reason\":\"none\",\"method\":\"POST\",\"operation\":\"investigate\",\"outcome\":\"success\",\"status_class\":\"2xx\",\"count\":1,\"duration_count\":1,\"duration_ms_sum\":2.9000000059604645,\"duration_ms_max\":2.9000000059604645,\"duration_ms_le_50\":1,\"duration_ms_le_100\":1,\"duration_ms_le_250\":1,\"duration_ms_le_500\":1,\"duration_ms_le_1000\":1,\"duration_ms_le_2500\":1,\"duration_ms_le_5000\":1,\"duration_ms_le_10000\":1,\"duration_ms_le_30000\":1,\"duration_ms_le_60000\":1}", + "timestamp": 1794780652965200000 + }, + { + "event_type": "custom", + "event_name": "frontend_api_body", + "attributes": "{\"cancel_reason\":\"none\",\"method\":\"POST\",\"operation\":\"investigate\",\"outcome\":\"completed\",\"status_class\":\"2xx\",\"count\":1,\"duration_count\":1,\"duration_ms_sum\":0.19999998807907104,\"duration_ms_max\":0.19999998807907104,\"duration_ms_le_50\":1,\"duration_ms_le_100\":1,\"duration_ms_le_250\":1,\"duration_ms_le_500\":1,\"duration_ms_le_1000\":1,\"duration_ms_le_2500\":1,\"duration_ms_le_5000\":1,\"duration_ms_le_10000\":1,\"duration_ms_le_30000\":1,\"duration_ms_le_60000\":1}", + "timestamp": 1794780652965200000 + }, + { + "event_type": "custom", + "event_name": "frontend_investigation_discarded", + "attributes": "{\"reason\":\"malformed\",\"count\":1}", + "timestamp": 1794780652965200000 + }, + { + "event_type": "custom", + "event_name": "frontend_investigation_first_response", + "attributes": "{\"kind\":\"evidence\",\"count\":1,\"duration_count\":1,\"duration_ms_sum\":3.199999988079071,\"duration_ms_max\":3.199999988079071,\"duration_ms_le_50\":1,\"duration_ms_le_100\":1,\"duration_ms_le_250\":1,\"duration_ms_le_500\":1,\"duration_ms_le_1000\":1,\"duration_ms_le_2500\":1,\"duration_ms_le_5000\":1,\"duration_ms_le_10000\":1,\"duration_ms_le_30000\":1,\"duration_ms_le_60000\":1}", + "timestamp": 1794780652965300000 + }, + { + "event_type": "custom", + "event_name": "frontend_investigation_stream", + "attributes": "{\"outcome\":\"completed\",\"count\":1,\"duration_count\":1,\"duration_ms_sum\":3.4000000059604645,\"duration_ms_max\":3.4000000059604645,\"duration_ms_le_50\":1,\"duration_ms_le_100\":1,\"duration_ms_le_250\":1,\"duration_ms_le_500\":1,\"duration_ms_le_1000\":1,\"duration_ms_le_2500\":1,\"duration_ms_le_5000\":1,\"duration_ms_le_10000\":1,\"duration_ms_le_30000\":1,\"duration_ms_le_60000\":1}", + "timestamp": 1794780652965300000 + }, + { + "event_type": "custom", + "event_name": "frontend_api_attempt", + "attributes": "{\"cancel_reason\":\"none\",\"method\":\"POST\",\"operation\":\"auth\",\"outcome\":\"success\",\"status_class\":\"2xx\",\"count\":1,\"duration_count\":1,\"duration_ms_sum\":35.79999998211861,\"duration_ms_max\":35.79999998211861,\"duration_ms_le_50\":1,\"duration_ms_le_100\":1,\"duration_ms_le_250\":1,\"duration_ms_le_500\":1,\"duration_ms_le_1000\":1,\"duration_ms_le_2500\":1,\"duration_ms_le_5000\":1,\"duration_ms_le_10000\":1,\"duration_ms_le_30000\":1,\"duration_ms_le_60000\":1}", + "timestamp": 1794780652965300000 + }, + { + "event_type": "custom", + "event_name": "frontend_api_body", + "attributes": "{\"cancel_reason\":\"none\",\"method\":\"POST\",\"operation\":\"auth\",\"outcome\":\"completed\",\"status_class\":\"2xx\",\"count\":1,\"duration_count\":1,\"duration_ms_sum\":0.7000000178813934,\"duration_ms_max\":0.7000000178813934,\"duration_ms_le_50\":1,\"duration_ms_le_100\":1,\"duration_ms_le_250\":1,\"duration_ms_le_500\":1,\"duration_ms_le_1000\":1,\"duration_ms_le_2500\":1,\"duration_ms_le_5000\":1,\"duration_ms_le_10000\":1,\"duration_ms_le_30000\":1,\"duration_ms_le_60000\":1}", + "timestamp": 1794780652965300000 + }, + { + "event_type": "custom", + "event_name": "frontend_api_request", + "attributes": "{\"cancel_reason\":\"none\",\"method\":\"POST\",\"operation\":\"auth\",\"outcome\":\"success\",\"status_class\":\"2xx\",\"count\":1,\"duration_count\":1,\"duration_ms_sum\":36.80000001192093,\"duration_ms_max\":36.80000001192093,\"duration_ms_le_50\":1,\"duration_ms_le_100\":1,\"duration_ms_le_250\":1,\"duration_ms_le_500\":1,\"duration_ms_le_1000\":1,\"duration_ms_le_2500\":1,\"duration_ms_le_5000\":1,\"duration_ms_le_10000\":1,\"duration_ms_le_30000\":1,\"duration_ms_le_60000\":1}", + "timestamp": 1794780652965300000 + }, + { + "event_type": "custom", + "event_name": "frontend_exception", + "attributes": "{\"source\":\"router\",\"stage\":\"lazy_chunk\",\"count\":1}", + "timestamp": 1794780652965300000 + }, + { + "event_type": "custom", + "event_name": "frontend_navigation", + "attributes": "{\"outcome\":\"failed\",\"route\":\"services\",\"count\":1,\"duration_count\":1,\"duration_ms_sum\":36.89999997615814,\"duration_ms_max\":36.89999997615814,\"duration_ms_le_50\":1,\"duration_ms_le_100\":1,\"duration_ms_le_250\":1,\"duration_ms_le_500\":1,\"duration_ms_le_1000\":1,\"duration_ms_le_2500\":1,\"duration_ms_le_5000\":1,\"duration_ms_le_10000\":1,\"duration_ms_le_30000\":1,\"duration_ms_le_60000\":1}", + "timestamp": 1794780652965300000 + }, + { + "event_type": "custom", + "event_name": "frontend_api_outage", + "attributes": "{\"reason\":\"offline\",\"count\":1,\"duration_count\":1,\"duration_ms_sum\":61.400000005960464,\"duration_ms_max\":61.400000005960464,\"duration_ms_le_100\":1,\"duration_ms_le_250\":1,\"duration_ms_le_500\":1,\"duration_ms_le_1000\":1,\"duration_ms_le_2500\":1,\"duration_ms_le_5000\":1,\"duration_ms_le_10000\":1,\"duration_ms_le_30000\":1,\"duration_ms_le_60000\":1}", + "timestamp": 1794780652965300000 + } + ] + } +] diff --git a/tests/frontend_telemetry_session.rs b/tests/frontend_telemetry_session.rs new file mode 100644 index 0000000..bad5057 --- /dev/null +++ b/tests/frontend_telemetry_session.rs @@ -0,0 +1,335 @@ +//! Exercises the real binary, session cookies and production middleware against a +//! disposable ClickHouse supplied through RUSH_TEST_CLICKHOUSE_URL. Never use a +//! development/production database: the API bootstraps its test admin and schema. +use anyhow::{Context, Result}; +use reqwest::{Client, StatusCode, header}; +use serde_json::{Value, json}; +use std::{ + path::PathBuf, + process::{Child, Command, Stdio}, + time::{Duration, Instant}, +}; +struct ApiProcess { + child: Child, + root: PathBuf, +} +impl Drop for ApiProcess { + fn drop(&mut self) { + let _ = self.child.kill(); + let _ = self.child.wait(); + let _ = std::fs::remove_dir_all(&self.root); + } +} +async fn login(client: &Client, base: &str, user: &str, password: &str) -> Result { + let response = client + .post(format!("{base}/api/v1/auth/login")) + .header(header::ORIGIN, base) + .json(&json!({"username":user,"password":password})) + .send() + .await?; + anyhow::ensure!( + response.status() == StatusCode::OK, + "test login failed: {}", + response.status() + ); + Ok(response + .headers() + .get(header::SET_COOKIE) + .context("session cookie")? + .to_str()? + .split(';') + .next() + .context("cookie value")? + .into()) +} +#[tokio::test] +#[ignore = "requires an empty disposable ClickHouse; starts the real API with isolated test-only secrets"] +async fn session_cookie_delivery_preserves_general_ingest_isolation() -> Result<()> { + let clickhouse = std::env::var("RUSH_TEST_CLICKHOUSE_URL")?; + let target = url::Url::parse(&clickhouse)?; + anyhow::ensure!( + target.scheme() == "http" + && target.host_str().is_some_and(|host| host == "localhost" + || host + .parse::() + .is_ok_and(|ip| ip.is_loopback())), + "test ClickHouse must be loopback HTTP" + ); + let probe = Client::builder().timeout(Duration::from_secs(5)).build()?; + let tables = probe.post(&clickhouse).body("SELECT count() FROM system.tables WHERE database NOT IN ('system', 'INFORMATION_SCHEMA', 'information_schema')").send().await?.error_for_status()?.text().await?; + anyhow::ensure!( + tables.trim() == "0", + "refusing to bootstrap a nonempty ClickHouse; run the disposable fixture script" + ); + + let root = std::env::temp_dir().join(format!("rush-frontend-auth-{}", uuid::Uuid::new_v4())); + std::fs::create_dir_all(&root)?; + let socket = std::net::TcpListener::bind("127.0.0.1:0")?; + let port = socket.local_addr()?.port(); + drop(socket); + let base = format!("http://127.0.0.1:{port}"); + let log = std::fs::File::create(root.join("api.log"))?; + let password = format!("Example-Widget-{}!", uuid::Uuid::new_v4()); + let mut command = Command::new(env!("CARGO_BIN_EXE_rush-api")); + command + .current_dir(&root) + .env_clear() + .stdout(Stdio::from(log.try_clone()?)) + .stderr(Stdio::from(log)); + for (key, value) in [ + ("RUSH_ENVIRONMENT", "test"), + ("RUSH_INSECURE_COOKIES", "true"), + ("RUSH_ALLOW_INSECURE_TENANT_READS", "true"), + ("RUSH_SSO_REPLAY_STORE", "local"), + ("RUSH_INGEST_BATCH_ROWS", "1"), + ("RUSH_CLICKHOUSE_ASYNC_INSERT", "false"), + ("RUSH_RUN_REPLAYER", "false"), + ("RUSH_RUN_SLO_ENGINE", "false"), + ("RUSH_RUN_ANOMALY_ENGINE", "false"), + ("RUSH_RUN_MONITOR_ENGINE", "false"), + ("RUSH_RUN_SIEM_ENGINE", "false"), + ("RUSH_RUN_TELEMETRY_QUALITY_ENGINE", "false"), + ("INITIAL_ADMIN_PASSWORD", password.as_str()), + ("RUST_LOG", "warn"), + ] { + command.env(key, value); + } + for key in [ + "RUSH_API_KEY_SECRET", + "RUSH_SESSION_HMAC_SECRET", + "RUSH_AUDIT_HMAC_SECRET", + "RUSH_CONFIG_ENCRYPTION_KEY", + "RUSH_SSO_TRANSACTION_SECRET", + ] { + command.env(key, uuid::Uuid::new_v4().to_string()); + } + command + .env("CLICKHOUSE_URL", &clickhouse) + .env("RUSH_PORT", port.to_string()) + .env("RUSH_BASE_URL", &base) + .env("TMPDIR", &root); + let mut api = ApiProcess { + child: command.spawn()?, + root, + }; + let client = Client::builder().timeout(Duration::from_secs(10)).build()?; + let deadline = Instant::now() + Duration::from_secs(120); + loop { + if let Some(status) = api.child.try_wait()? { + anyhow::bail!( + "isolated API exited {status}: {}", + std::fs::read_to_string(api.root.join("api.log"))? + ); + } + if client + .get(format!("{base}/healthz")) + .send() + .await + .is_ok_and(|r| r.status().is_success()) + { + break; + } + anyhow::ensure!(Instant::now() < deadline, "isolated API startup timeout"); + tokio::time::sleep(Duration::from_millis(200)).await; + } + let cookie = login(&client, &base, "admin", &password).await?; + let payload = json!({"meta":{"app_name":"rush-ui","app_version":"1.2.3","environment":"test","page_path":"/logs/private-id?token=private-query","page_url":"https://private-host/logs?token=private-query","session_id":"private-session","user_id":"private-user"},"events":[{"event_type":"custom","event_name":"frontend_api_request","attributes":json!({"operation":"query","method":"POST","outcome":"success","count":1,"duration_ms_sum":42,"query":"private-query"}).to_string()}]}); + let request = |path: &str| { + client + .post(format!("{base}{path}")) + .header(header::ORIGIN, &base) + .header(header::COOKIE, &cookie) + }; + for content_type in ["application/json", "text/plain;charset=UTF-8"] { + let response = request("/api/v1/frontend/telemetry") + .header(header::CONTENT_TYPE, content_type) + .body(payload.to_string()) + .send() + .await?; + anyhow::ensure!( + response.status() == StatusCode::OK, + "frontend health request failed: {}", + response.status() + ); + assert_eq!(response.json::().await?["accepted"], 1); + } + assert_eq!( + request("/api/v1/rum/ingest") + .json(&payload) + .send() + .await? + .status(), + StatusCode::FORBIDDEN + ); + for forged in ["", "rush_session=forged-session"] { + assert_eq!( + client + .post(format!("{base}/api/v1/frontend/telemetry")) + .header(header::ORIGIN, &base) + .header(header::COOKIE, forged) + .header("x-rush-credential-type", "session") + .json(&payload) + .send() + .await? + .status(), + StatusCode::UNAUTHORIZED + ); + } + for content_type in ["application/json", "text/plain;charset=UTF-8"] { + for origin in [Some("https://attacker.invalid"), None] { + let mut forged = client + .post(format!("{base}/api/v1/frontend/telemetry")) + .header(header::COOKIE, &cookie) + .header(header::CONTENT_TYPE, content_type) + .body(payload.to_string()); + if let Some(origin) = origin { + forged = forged.header(header::ORIGIN, origin); + } + assert_eq!(forged.send().await?.status(), StatusCode::FORBIDDEN); + } + } + let other = request("/api/v1/tenants") + .json(&json!({"name":"frontend-other"})) + .send() + .await?; + anyhow::ensure!(other.status().is_success(), "create isolated tenant failed"); + assert_eq!( + request("/api/v1/frontend/telemetry") + .header("x-rush-tenant", "frontend-other") + .json(&payload) + .send() + .await? + .status(), + StatusCode::FORBIDDEN + ); + assert_eq!( + request("/api/v1/frontend/telemetry") + .header(header::CONTENT_TYPE, "application/json") + .body("x".repeat(64 * 1024 + 1)) + .send() + .await? + .status(), + StatusCode::PAYLOAD_TOO_LARGE + ); + anyhow::ensure!( + client + .put(format!("{base}/api/v1/groups/viewers/tenants")) + .header(header::ORIGIN, &base) + .header(header::COOKIE, &cookie) + .json(&json!({"tenant_ids":["default"]})) + .send() + .await? + .status() + .is_success(), + "grant isolated viewer default-tenant access failed" + ); + let viewer_password = format!("Example-Viewer-{}!", uuid::Uuid::new_v4()); + let viewer = request("/api/v1/users") + .json(&json!({"username":"frontendviewer", "password":viewer_password})) + .send() + .await?; + anyhow::ensure!( + viewer.status().is_success(), + "create isolated viewer failed: {}", + viewer.status() + ); + let viewer_cookie = login(&client, &base, "frontendviewer", &viewer_password).await?; + let viewer_request = || { + client + .post(format!("{base}/api/v1/frontend/telemetry")) + .header(header::ORIGIN, &base) + .header(header::COOKIE, &viewer_cookie) + .json(&payload) + }; + assert_eq!(viewer_request().send().await?.status(), StatusCode::OK); + assert_eq!( + viewer_request() + .header("x-rush-tenant", "frontend-other") + .send() + .await? + .status(), + StatusCode::FORBIDDEN + ); + assert_eq!( + client + .post(format!("{base}/api/v1/rum/ingest")) + .header(header::ORIGIN, &base) + .header(header::COOKIE, &viewer_cookie) + .json(&payload) + .send() + .await? + .status(), + StatusCode::FORBIDDEN + ); + let rum_setting = |enabled: bool| { + client + .put(format!("{base}/api/v1/settings/rum")) + .header(header::ORIGIN, &base) + .header(header::COOKIE, &cookie) + .json(&json!({"enabled":enabled})) + }; + anyhow::ensure!( + rum_setting(false).send().await?.status().is_success(), + "disable isolated RUM failed" + ); + assert_eq!( + viewer_request().send().await?.status(), + StatusCode::FORBIDDEN + ); + anyhow::ensure!( + rum_setting(true).send().await?.status().is_success(), + "restore isolated RUM failed" + ); + let rum_signal = |enabled: bool| { + client + .put(format!("{base}/api/v1/tenants/default/signals")) + .header(header::ORIGIN, &base) + .header(header::COOKIE, &cookie) + .json(&json!({"rum":enabled})) + }; + anyhow::ensure!( + rum_signal(false).send().await?.status().is_success(), + "disable isolated tenant RUM signal failed" + ); + assert_eq!(viewer_request().send().await?.status(), StatusCode::OK); + anyhow::ensure!( + rum_signal(true).send().await?.status().is_success(), + "restore isolated tenant RUM signal failed" + ); + // Synchronous insertion lets this confirm actual sanitized delivery, not just HTTP acceptance. + let rows=client.post(&clickhouse).body("SELECT tenant_id, AppName, AppVersion, Environment, PagePath, PageUrl, SessionId, UserId, EventName, Attributes FROM observability.rum FORMAT JSONEachRow").send().await?.error_for_status()?.text().await?; + assert!(!rows.contains("private-"), "raw private data reached RUM"); + let rows = rows + .lines() + .map(serde_json::from_str::) + .collect::, _>>()?; + assert_eq!(rows.len(), 3); + for row in rows { + assert_eq!(row["tenant_id"], "default"); + assert_eq!(row["AppName"], "rush-ui"); + assert_eq!(row["AppVersion"], "1.2.3"); + assert_eq!(row["Environment"], "test"); + assert_eq!(row["PagePath"], "/logs"); + let attrs: Value = serde_json::from_str(row["Attributes"].as_str().unwrap())?; + assert_eq!(attrs["count"], 1); + assert_eq!(attrs["duration_ms_sum"], 42); + } + // Logging out revokes the exact cookie used by the next telemetry request. + anyhow::ensure!( + request("/api/v1/auth/logout") + .send() + .await? + .status() + .is_success(), + "logout failed" + ); + assert_eq!( + request("/api/v1/frontend/telemetry") + .json(&payload) + .send() + .await? + .status(), + StatusCode::UNAUTHORIZED + ); + Ok(()) +} diff --git a/tests/run-frontend-telemetry.sh b/tests/run-frontend-telemetry.sh new file mode 100644 index 0000000..243ea8e --- /dev/null +++ b/tests/run-frontend-telemetry.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +set -euo pipefail +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +cd "$repo_root" +container="rush-frontend-telemetry-test-$$" +cleanup() { docker rm -f "$container" >/dev/null 2>&1 || true; } +trap cleanup EXIT +# Pinned official ClickHouse 26.10.3.7; all API credentials are isolated test +# values generated inside the integration test, never read from the developer shell. +docker run -d --rm --name "$container" --memory=1536m --cpus=2 \ + -p 127.0.0.1::8123 -e CLICKHOUSE_SKIP_USER_SETUP=1 \ + --mount "type=bind,source=$repo_root/tests/fixtures/frontend-clickhouse.xml,target=/etc/clickhouse-server/config.d/frontend-test.xml,readonly" \ + clickhouse/clickhouse-server@sha256:739e099abd1157390960a59e4532d21781d8fde57e6e4151cb920ea02c30b1ce >/dev/null +address="$(docker port "$container" 8123/tcp)" +export RUSH_TEST_CLICKHOUSE_URL="http://$address" +ready=false +for attempt in {1..60}; do + if curl --fail --silent --max-time 2 "$RUSH_TEST_CLICKHOUSE_URL/ping" >/dev/null; then ready=true; break; fi + sleep 1 +done +if [[ "$ready" != true ]]; then docker logs "$container"; exit 1; fi +cargo test --test frontend_telemetry_session --all-features -- --ignored