autumn-plugin-graphql 0.2.0

Production-grade GraphQL for Autumn: mount any async-graphql schema with query limits, persisted queries, subscriptions, observability and error masking
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
//! The request pipeline every transport shares.
//!
//! ```text
//! prepare ─┬─ persisted-query resolution (APQ / trusted documents)
//!          ├─ text limits (size, lexical nesting)      ← before the parser
//!          ├─ parse (once; the parsed document is reused by the executor)
//!          ├─ transport policy (no mutations over GET)
//!          ├─ operation limits (depth, aliases, fields, …)
//!          └─ introspection policy
//! execute ──  the schema (with a timeout, for request/response transports)
//! finish  ─┬─ error codes + masking of unexpected errors
//!          └─ metrics + slow-operation log
//! ```
//!
//! HTTP handlers call [`Pipeline::execute_one`]; the WebSocket and SSE
//! transports go through [`GuardedExecutor`], which runs the same steps
//! inside async-graphql's [`Executor`] contract. No transport can reach the
//! schema without passing `prepare`.

use std::sync::Arc;
use std::time::Duration;

use async_graphql::parser::types::OperationType;
use async_graphql::{Data, Executor, IntrospectionMode, Request, Response, Value};
use futures_util::stream::{self, BoxStream, StreamExt};
use http::StatusCode;
use tokio::time::Instant;
use tracing::Instrument;

use crate::config::{LimitsConfig, PersistedQueryMode};
use crate::context::Transport;
use crate::error::{codes, finalize_errors, request_error};
use crate::limits::{self, AnalysisError, LimitViolation};
use crate::metrics::Metrics;
use crate::persisted::{PersistedQueryStore, TrustedDocuments, is_sha256_hex, sha256_hex};

/// Effective, context-resolved settings the pipeline runs with.
#[derive(Debug, Clone)]
pub struct PipelineSettings {
    /// Allow `__schema`/`__type`.
    pub introspection: bool,
    /// Mask uncoded resolver errors.
    pub mask_unexpected_errors: bool,
    /// Per-operation timeout for request/response transports.
    pub timeout: Option<Duration>,
    /// Log operations slower than this.
    pub slow_operation: Option<Duration>,
    /// Static-analysis limits.
    pub limits: LimitsConfig,
    /// Persisted-query mode.
    pub persisted_mode: PersistedQueryMode,
}

/// A request refused before execution.
#[derive(Debug, Clone)]
pub struct Rejection {
    /// `extensions.code`.
    pub code: &'static str,
    /// Client-facing message.
    pub message: String,
    /// HTTP status under `application/graphql-response+json`.
    pub status: StatusCode,
    /// Whether the status applies under `application/json` too (a
    /// transport-level refusal such as `405`).
    pub transport_level: bool,
    /// Extra `extensions` entries.
    pub extra: Vec<(&'static str, Value)>,
}

impl Rejection {
    pub(crate) fn new(code: &'static str, message: impl Into<String>, status: StatusCode) -> Self {
        Self {
            code,
            message: message.into(),
            status,
            transport_level: false,
            extra: Vec::new(),
        }
    }

    pub(crate) const fn transport_level(mut self) -> Self {
        self.transport_level = true;
        self
    }

    fn limit(violation: LimitViolation) -> Self {
        let status = if violation.limit == "query_bytes" {
            StatusCode::PAYLOAD_TOO_LARGE
        } else {
            StatusCode::BAD_REQUEST
        };
        let max = i64::try_from(violation.max).unwrap_or(i64::MAX);
        let actual = i64::try_from(violation.actual).unwrap_or(i64::MAX);
        Self {
            code: codes::QUERY_LIMIT_EXCEEDED,
            message: violation.to_string(),
            status,
            transport_level: false,
            extra: vec![
                ("limit", Value::from(violation.limit)),
                ("max", Value::from(max)),
                ("actual", Value::from(actual)),
            ],
        }
    }

    /// The rejection as a GraphQL response (no `data`, one coded error).
    #[must_use]
    pub fn into_response(self) -> Response {
        Response::from_errors(vec![request_error(
            self.code,
            self.message,
            self.status,
            &self.extra,
        )])
    }
}

/// What `prepare` learned about an operation.
#[derive(Debug, Clone)]
pub struct OperationInfo {
    /// `operationName`, if the request named one.
    pub name: Option<String>,
    /// The operation's type, when the document parsed.
    pub kind: Option<OperationType>,
    /// Whether the document parsed.
    pub parsed: bool,
    /// Short hash of the document, for logs.
    pub document_hash: String,
    /// The transport that carried it.
    pub transport: Transport,
    /// When preparation started.
    pub started: Instant,
}

/// Label for an operation type.
#[must_use]
pub const fn kind_label(kind: Option<OperationType>) -> &'static str {
    match kind {
        Some(OperationType::Query) => "query",
        Some(OperationType::Mutation) => "mutation",
        Some(OperationType::Subscription) => "subscription",
        None => "unknown",
    }
}

/// An executed (or refused) request/response operation.
pub struct Executed {
    /// The GraphQL response.
    pub response: Response,
    /// The HTTP status under `application/graphql-response+json`.
    pub status: StatusCode,
    /// A status that applies whatever the media type (e.g. `405`).
    pub transport_status: Option<StatusCode>,
    /// The operation's type, if known.
    pub kind: Option<OperationType>,
}

/// Shared per-endpoint pipeline state.
pub struct Pipeline {
    pub(crate) settings: PipelineSettings,
    pub(crate) store: Arc<dyn PersistedQueryStore>,
    pub(crate) trusted: Arc<TrustedDocuments>,
    pub(crate) metrics: Metrics,
}

impl Pipeline {
    /// Resolve persisted queries and enforce every pre-execution policy.
    ///
    /// # Errors
    ///
    /// Returns the [`Rejection`] to send instead of executing.
    pub async fn prepare(
        &self,
        request: &mut Request,
        document_id: Option<&str>,
        transport: Transport,
    ) -> Result<OperationInfo, Rejection> {
        let started = Instant::now();
        self.resolve_persisted(request, document_id).await?;
        limits::check_text(&request.query, &self.settings.limits).map_err(Rejection::limit)?;

        let name = request.operation_name.clone();
        let mut info = OperationInfo {
            name,
            kind: None,
            parsed: false,
            document_hash: sha256_hex(&request.query).chars().take(16).collect(),
            transport,
            started,
        };

        // Parse once; async-graphql reuses the parsed document. A syntax
        // error is left for the executor, which reports it in the standard
        // shape (and `finish` codes it GRAPHQL_PARSE_FAILED).
        let Ok(document) = request.parsed_query() else {
            return Ok(info);
        };
        info.parsed = true;
        info.kind = limits::operation_type(document, info.name.as_deref());

        if transport == Transport::HttpGet
            && matches!(
                info.kind,
                Some(OperationType::Mutation | OperationType::Subscription)
            )
        {
            return Err(Rejection::new(
                codes::METHOD_NOT_ALLOWED,
                format!(
                    "{} operations are not allowed over GET; use POST",
                    kind_label(info.kind)
                ),
                StatusCode::METHOD_NOT_ALLOWED,
            )
            .transport_level());
        }

        match limits::analyze(document, info.name.as_deref()) {
            Ok(stats) => {
                if stats.introspection && !self.settings.introspection {
                    return Err(Rejection::new(
                        codes::INTROSPECTION_DISABLED,
                        "introspection is disabled on this endpoint",
                        StatusCode::BAD_REQUEST,
                    ));
                }
                limits::check_stats(&stats, &self.settings.limits).map_err(Rejection::limit)?;
            }
            Err(AnalysisError::TooComplex) => {
                return Err(Rejection::new(
                    codes::QUERY_LIMIT_EXCEEDED,
                    "document is nested too deeply to analyse",
                    StatusCode::BAD_REQUEST,
                ));
            }
            // Cycles and a missing/ambiguous operation are validation errors
            // the executor reports with its own, better messages.
            Err(AnalysisError::FragmentCycle(_) | AnalysisError::NoOperation) => {}
        }

        // Defence in depth: documents the analyzer passes through (fragment
        // cycles, unselectable operations) still cannot introspect.
        if !self.settings.introspection {
            request.introspection_mode = IntrospectionMode::Disabled;
        }
        Ok(info)
    }

    async fn resolve_persisted(
        &self,
        request: &mut Request,
        document_id: Option<&str>,
    ) -> Result<(), Rejection> {
        let apq_hash = apq_hash(request)?;
        let id = document_id
            .map(|id| id.strip_prefix("sha256:").unwrap_or(id).to_owned())
            .or(apq_hash);
        let has_query = !request.query.trim().is_empty();
        let mode = self.settings.persisted_mode;

        match (mode, id) {
            (PersistedQueryMode::Disabled, Some(_)) if !has_query => Err(Rejection::new(
                codes::PERSISTED_QUERY_NOT_SUPPORTED,
                "PersistedQueryNotSupported",
                StatusCode::BAD_REQUEST,
            )),
            (PersistedQueryMode::Disabled, _) | (PersistedQueryMode::Automatic, None) => Ok(()),

            (PersistedQueryMode::Automatic, Some(id)) if has_query => {
                let trusted_match = self
                    .trusted
                    .get(&id)
                    .is_some_and(|doc| **doc == *request.query);
                if !trusted_match && sha256_hex(&request.query) != id.to_ascii_lowercase() {
                    return Err(Rejection::new(
                        codes::PERSISTED_QUERY_HASH_MISMATCH,
                        "provided sha does not match query",
                        StatusCode::BAD_REQUEST,
                    ));
                }
                if !trusted_match {
                    self.store
                        .put(&id.to_ascii_lowercase(), Arc::from(request.query.as_str()))
                        .await;
                }
                Ok(())
            }
            (PersistedQueryMode::Automatic, Some(id)) => {
                let found = match self.trusted.get(&id) {
                    Some(doc) => Some(Arc::clone(doc)),
                    None => self.store.get(&id.to_ascii_lowercase()).await,
                };
                found.map_or_else(
                    || Err(not_found()),
                    |doc| {
                        request.query = doc.to_string();
                        Ok(())
                    },
                )
            }

            (PersistedQueryMode::Trusted, Some(id)) => match self.trusted.get(&id) {
                Some(doc) if !has_query => {
                    request.query = doc.to_string();
                    Ok(())
                }
                Some(doc) if **doc == *request.query => Ok(()),
                Some(_) => Err(not_allowlisted()),
                None if has_query => Err(not_allowlisted()),
                None => Err(not_found()),
            },
            (PersistedQueryMode::Trusted, None) if !has_query => Ok(()),
            (PersistedQueryMode::Trusted, None) => {
                if self.trusted.contains_document(&request.query) {
                    Ok(())
                } else {
                    Err(not_allowlisted())
                }
            }
        }
    }

    /// Code errors, mask, and record metrics for a finished response.
    pub fn finish(&self, response: &mut Response, info: &OperationInfo) {
        let error_codes = finalize_errors(
            &mut response.errors,
            self.settings.mask_unexpected_errors,
            info.parsed,
        );
        let elapsed = info.started.elapsed();
        let outcome = if response.errors.is_empty() {
            "success"
        } else {
            "error"
        };
        self.metrics
            .operation(info.transport, info.kind, outcome, Some(elapsed));
        for code in &error_codes {
            self.metrics.error(code);
        }
        if let Some(slow) = self.settings.slow_operation
            && elapsed >= slow
        {
            tracing::warn!(
                operation.name = info.name.as_deref().unwrap_or("<anonymous>"),
                operation.kind = kind_label(info.kind),
                document = %info.document_hash,
                elapsed_ms = u64::try_from(elapsed.as_millis()).unwrap_or(u64::MAX),
                "slow GraphQL operation"
            );
        }
    }

    /// Record and render a rejection.
    pub fn reject(&self, rejection: Rejection, transport: Transport) -> Response {
        tracing::debug!(code = rejection.code, message = %rejection.message, "GraphQL request rejected");
        self.metrics.operation(transport, None, "rejected", None);
        self.metrics.error(rejection.code);
        rejection.into_response()
    }

    /// Prepare, execute (with the timeout) and finish one request/response
    /// operation.
    pub async fn execute_one<E: Executor>(
        &self,
        executor: &E,
        mut request: Request,
        document_id: Option<&str>,
        transport: Transport,
    ) -> Executed {
        let info = match self.prepare(&mut request, document_id, transport).await {
            Ok(info) => info,
            Err(rejection) => {
                let status = rejection.status;
                let transport_status = rejection.transport_level.then_some(status);
                return Executed {
                    response: self.reject(rejection, transport),
                    status,
                    transport_status,
                    kind: None,
                };
            }
        };

        let span = tracing::info_span!(
            "graphql.operation",
            otel.name = %format!("{} {}", kind_label(info.kind), info.name.as_deref().unwrap_or("")).trim_end(),
            graphql.operation.name = info.name.as_deref().unwrap_or(""),
            graphql.operation.type = kind_label(info.kind),
            graphql.document = %info.document_hash,
            transport = transport.as_str(),
        );
        let run = executor.execute(request).instrument(span);
        #[allow(clippy::option_if_let_else)] // two awaits read clearer as a match
        let (mut response, timed_out) = match self.settings.timeout {
            Some(limit) => match tokio::time::timeout(limit, run).await {
                Ok(response) => (response, false),
                Err(_) => (
                    Response::from_errors(vec![request_error(
                        codes::OPERATION_TIMEOUT,
                        format!("operation did not complete within {}ms", limit.as_millis()),
                        StatusCode::GATEWAY_TIMEOUT,
                        &[],
                    )]),
                    true,
                ),
            },
            None => (run.await, false),
        };
        self.finish(&mut response, &info);

        let status = if timed_out {
            StatusCode::GATEWAY_TIMEOUT
        } else if is_request_error(&response) {
            StatusCode::BAD_REQUEST
        } else {
            StatusCode::OK
        };
        Executed {
            response,
            status,
            transport_status: None,
            kind: info.kind,
        }
    }

    /// Prepare and start a streaming operation (SSE and WebSocket). Every
    /// item is finished like a single response.
    pub fn execute_stream<E: Executor>(
        self: &Arc<Self>,
        executor: &E,
        request: Request,
        session_data: Option<Arc<Data>>,
        transport: Transport,
    ) -> BoxStream<'static, Response> {
        let pipeline = Arc::clone(self);
        let executor = executor.clone();
        stream::once(async move {
            let mut request = request;
            match pipeline.prepare(&mut request, None, transport).await {
                Err(rejection) => {
                    stream::once(std::future::ready(pipeline.reject(rejection, transport))).boxed()
                }
                Ok(info) => {
                    pipeline
                        .metrics
                        .operation(transport, info.kind, "started", None);
                    executor
                        .execute_stream(request, session_data)
                        .map(move |mut response| {
                            let codes = finalize_errors(
                                &mut response.errors,
                                pipeline.settings.mask_unexpected_errors,
                                info.parsed,
                            );
                            for code in &codes {
                                pipeline.metrics.error(code);
                            }
                            response
                        })
                        .boxed()
                }
            }
        })
        .flatten()
        .boxed()
    }
}

/// A GraphQL-over-HTTP "request error": nothing executed (no `data`) and
/// every error is a pre-execution one.
#[must_use]
pub fn is_request_error(response: &Response) -> bool {
    matches!(response.data, Value::Null)
        && !response.errors.is_empty()
        && response.errors.iter().all(|e| e.path.is_empty())
}

fn not_found() -> Rejection {
    Rejection::new(
        codes::PERSISTED_QUERY_NOT_FOUND,
        "PersistedQueryNotFound",
        StatusCode::NOT_FOUND,
    )
}

fn not_allowlisted() -> Rejection {
    Rejection::new(
        codes::OPERATION_NOT_ALLOWLISTED,
        "this operation is not in the trusted-documents manifest",
        StatusCode::FORBIDDEN,
    )
}

/// Extract `extensions.persistedQuery.sha256Hash`, validating the shape.
fn apq_hash(request: &Request) -> Result<Option<String>, Rejection> {
    let Some(value) = request.extensions.get("persistedQuery") else {
        return Ok(None);
    };
    let bad = |message: &str| Rejection::new(codes::BAD_REQUEST, message, StatusCode::BAD_REQUEST);
    let Value::Object(object) = value else {
        return Err(bad("extensions.persistedQuery must be an object"));
    };
    match object.get("version") {
        Some(Value::Number(n)) if n.as_i64() == Some(1) => {}
        _ => return Err(bad("unsupported persisted query version")),
    }
    match object.get("sha256Hash") {
        Some(Value::String(hash)) if is_sha256_hex(hash) => Ok(Some(hash.to_ascii_lowercase())),
        _ => Err(bad(
            "extensions.persistedQuery.sha256Hash must be a SHA-256 hex digest",
        )),
    }
}

/// An [`Executor`] that runs the pipeline in front of `inner`.
///
/// The WebSocket and SSE transports hand this to async-graphql, so subscriptions
/// get exactly the same limits, persisted-query rules, introspection policy
/// and error masking as HTTP.
#[derive(Clone)]
pub struct GuardedExecutor<E> {
    pub(crate) inner: E,
    pub(crate) pipeline: Arc<Pipeline>,
    pub(crate) transport: Transport,
}

#[cfg(not(feature = "boxed-trait"))]
impl<E: Executor> Executor for GuardedExecutor<E> {
    fn execute(&self, request: Request) -> impl std::future::Future<Output = Response> + Send {
        let this = self.clone();
        async move {
            this.pipeline
                .execute_one(&this.inner, request, None, this.transport)
                .await
                .response
        }
    }

    fn execute_stream(
        &self,
        request: Request,
        session_data: Option<Arc<Data>>,
    ) -> BoxStream<'static, Response> {
        self.pipeline
            .execute_stream(&self.inner, request, session_data, self.transport)
    }
}

#[cfg(feature = "boxed-trait")]
#[async_trait::async_trait]
impl<E: Executor> Executor for GuardedExecutor<E> {
    async fn execute(&self, request: Request) -> Response {
        self.pipeline
            .execute_one(&self.inner, request, None, self.transport)
            .await
            .response
    }

    fn execute_stream(
        &self,
        request: Request,
        session_data: Option<Arc<Data>>,
    ) -> BoxStream<'static, Response> {
        self.pipeline
            .execute_stream(&self.inner, request, session_data, self.transport)
    }
}