Skip to main content

alien_client_core/
request_utils.rs

1use crate::error::{ErrorData, Result};
2use alien_error::{AlienError, Context, IntoAlienError};
3use async_trait::async_trait;
4use backon::{ExponentialBuilder, Retryable};
5use serde::de::DeserializeOwned;
6use std::time::Duration;
7
8/// Extract request body as string from a reqwest::Request if available
9fn extract_request_body_string(request: &reqwest::Request) -> Option<String> {
10    request
11        .body()
12        .and_then(|body| body.as_bytes())
13        .map(|bytes| String::from_utf8_lossy(bytes).into_owned())
14}
15
16/// JSON key under which the captured request body lives in an error's `context` snapshot. The
17/// `AlienErrorData` derive builds `context` from the raw field name, so this must match the
18/// `http_request_text` field of [`ErrorData::HttpResponseError`] verbatim. The redaction tests below
19/// serialize the whole chain and assert the secret is gone, so a drift here fails loudly.
20const HTTP_REQUEST_TEXT_CONTEXT_KEY: &str = "http_request_text";
21
22/// Strips the captured HTTP request body from every layer of an error chain.
23///
24/// Cloud "create" requests carry secrets in the request body (e.g. a DB master password). On a
25/// non-2xx response the transport records that body in [`ErrorData::HttpResponseError`], in both the
26/// typed payload and the `context` JSON snapshot. A non-internal `.context(...)` wrapper does not
27/// sanitize its source, and the source chain is serialized verbatim into durable state and status
28/// responses — so the body could leak.
29///
30/// This scrubs both representations across the head and full `source` chain, keeping status, response
31/// text, URL, and chain intact. Response text is kept deliberately: RDS / Cloud SQL / Flexible Server
32/// error bodies don't echo the submitted password back, so it stays as a diagnostic — a future caller
33/// wiring this to an API that *does* reflect request fields in its error responses would need to scrub
34/// that too. Order-independent: works whether the HTTP error is still the head (AWS: redaction before
35/// mapping) or already wrapped into the source (GCP/Azure: transport maps first). Apply to the raw
36/// transport result of any request whose body contains a secret.
37pub fn redact_request_body<T>(result: Result<T>) -> Result<T> {
38    result.map_err(|mut e| {
39        // Head: drop the body from the typed payload when the head itself is the HTTP error...
40        if let Some(ErrorData::HttpResponseError {
41            http_request_text, ..
42        }) = e.error.as_mut()
43        {
44            *http_request_text = None;
45        }
46        // ...and from the head's `context` snapshot.
47        scrub_request_body(e.context.as_mut());
48        // Walk the source chain (each layer is type-erased to `GenericError`, so its typed payload no
49        // longer holds the body — only its `context` snapshot can) and scrub every layer.
50        let mut layer = e.source.as_deref_mut();
51        while let Some(err) = layer {
52            scrub_request_body(err.context.as_mut());
53            layer = err.source.as_deref_mut();
54        }
55        e
56    })
57}
58
59/// Removes the captured request body from a single error's `context` snapshot, if present.
60fn scrub_request_body(context: Option<&mut serde_json::Value>) {
61    if let Some(serde_json::Value::Object(map)) = context {
62        map.remove(HTTP_REQUEST_TEXT_CONTEXT_KEY);
63    }
64}
65
66/// Helper to build request and extract body before sending
67fn build_and_extract_body(
68    builder: reqwest::RequestBuilder,
69) -> Result<(reqwest::Client, reqwest::Request, Option<String>)> {
70    let (client, req_result) = builder.build_split();
71    let request = req_result
72        .into_alien_error()
73        .context(ErrorData::HttpRequestFailed {
74            message: "Failed to build request".to_string(),
75        })?;
76
77    let body_string = extract_request_body_string(&request);
78    Ok((client, request, body_string))
79}
80
81/// Handle an HTTP response by checking status and parsing JSON on success
82pub async fn handle_json_response<T: DeserializeOwned>(
83    response: reqwest::Response,
84    request_body: Option<String>,
85) -> Result<T> {
86    let status = response.status();
87    let url = response.url().to_string();
88    let response_text =
89        response
90            .text()
91            .await
92            .into_alien_error()
93            .context(ErrorData::HttpRequestFailed {
94                message: "Failed to read response body".to_string(),
95            })?;
96
97    if !status.is_success() {
98        return Err(AlienError::new(ErrorData::HttpResponseError {
99            message: format!(
100                "Request failed with HTTP {}: {}",
101                status.as_u16(),
102                status.canonical_reason().unwrap_or("Unknown error")
103            ),
104            url,
105            http_status: status.as_u16(),
106            http_request_text: request_body,
107            http_response_text: Some(response_text),
108        }));
109    }
110
111    // Parse the JSON response using serde_path_to_error for better error messages
112    let jd = &mut serde_json::Deserializer::from_str(&response_text);
113    let parsed_response: T = serde_path_to_error::deserialize(jd).map_err(|err| {
114        AlienError::new(ErrorData::HttpResponseError {
115            message: format!(
116                "Invalid JSON response at field '{}': {}",
117                err.path(),
118                err.inner()
119            ),
120            url,
121            http_status: status.as_u16(),
122            http_request_text: request_body,
123            http_response_text: Some(response_text),
124        })
125    })?;
126
127    Ok(parsed_response)
128}
129
130/// Handle an HTTP response by checking status and parsing XML on success
131pub async fn handle_xml_response<T: DeserializeOwned>(
132    response: reqwest::Response,
133    request_body: Option<String>,
134) -> Result<T> {
135    let status = response.status();
136    let url = response.url().to_string();
137    let response_text =
138        response
139            .text()
140            .await
141            .into_alien_error()
142            .context(ErrorData::HttpRequestFailed {
143                message: "Failed to read response body".to_string(),
144            })?;
145
146    if !status.is_success() {
147        return Err(AlienError::new(ErrorData::HttpResponseError {
148            message: format!(
149                "Request failed with HTTP {}: {}",
150                status.as_u16(),
151                status.canonical_reason().unwrap_or("Unknown error")
152            ),
153            url,
154            http_status: status.as_u16(),
155            http_request_text: request_body,
156            http_response_text: Some(response_text),
157        }));
158    }
159
160    // Parse the XML response using serde_path_to_error for better error messages
161    let mut xml_deserializer = quick_xml::de::Deserializer::from_str(&response_text);
162    let parsed_response: T =
163        serde_path_to_error::deserialize(&mut xml_deserializer).map_err(|err| {
164            AlienError::new(ErrorData::HttpResponseError {
165                message: format!(
166                    "Invalid XML response at field '{}': {}",
167                    err.path(),
168                    err.inner()
169                ),
170                url,
171                http_status: status.as_u16(),
172                http_request_text: request_body,
173                http_response_text: Some(response_text),
174            })
175        })?;
176
177    Ok(parsed_response)
178}
179
180/// Handle an HTTP response by checking status without parsing the body
181pub async fn handle_no_response(
182    response: reqwest::Response,
183    request_body: Option<String>,
184) -> Result<()> {
185    let status = response.status();
186    let url = response.url().to_string();
187
188    if !status.is_success() {
189        let response_text =
190            response
191                .text()
192                .await
193                .into_alien_error()
194                .context(ErrorData::HttpRequestFailed {
195                    message: "Failed to read error response body".to_string(),
196                })?;
197        return Err(AlienError::new(ErrorData::HttpResponseError {
198            message: format!(
199                "Request failed with HTTP {}: {}",
200                status.as_u16(),
201                status.canonical_reason().unwrap_or("Unknown error")
202            ),
203            url,
204            http_status: status.as_u16(),
205            http_request_text: request_body,
206            http_response_text: Some(response_text),
207        }));
208    }
209
210    // A successful response can still carry a body, such as SQS response metadata.
211    // Consume it before returning so HTTP/1 connections can be reused. Native responses are
212    // discarded a chunk at a time rather than buffered.
213    #[cfg(not(target_arch = "wasm32"))]
214    {
215        let mut response = response;
216        while response
217            .chunk()
218            .await
219            .into_alien_error()
220            .context(ErrorData::HttpResponseBodyReadFailed {
221                http_status: status.as_u16(),
222            })?
223            .is_some()
224        {}
225    }
226    #[cfg(target_arch = "wasm32")]
227    response
228        .bytes()
229        .await
230        .into_alien_error()
231        .context(ErrorData::HttpResponseBodyReadFailed {
232            http_status: status.as_u16(),
233        })?;
234
235    Ok(())
236}
237
238/// Extension trait for `reqwest::RequestBuilder` to add JSON and XML response handling
239#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
240#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
241pub trait RequestBuilderExt {
242    /// Enable retries with an exponential back-off strategy.
243    ///
244    /// Example:
245    /// ```ignore
246    /// let obj: MyType = client
247    ///     .get("https://api.example.com/obj")
248    ///     .with_retry()                // <- enable retries
249    ///     .send_json()                // <- deserialize JSON body
250    ///     .await?;
251    /// ```
252    fn with_retry(self) -> RetriableRequestBuilder;
253
254    /// Send the request and parse the response as JSON
255    async fn send_json<T: DeserializeOwned + 'static>(self) -> Result<T>;
256
257    /// Send the request and parse the response as XML
258    async fn send_xml<T: DeserializeOwned + 'static>(self) -> Result<T>;
259
260    /// Send the request without parsing the response body
261    async fn send_no_response(self) -> Result<()>;
262
263    /// Send the request and return the raw response for custom handling
264    async fn send_raw(self) -> Result<reqwest::Response>;
265}
266
267/// A `reqwest::RequestBuilder` wrapper that automatically retries failed
268/// requests using an exponential back-off strategy powered by the `backon`
269/// crate. Use [`RequestBuilderExt::with_retry`] to construct one.
270pub struct RetriableRequestBuilder {
271    inner: reqwest::RequestBuilder,
272    backoff: ExponentialBuilder,
273    retry_if: fn(&AlienError<ErrorData>) -> bool,
274}
275
276impl RetriableRequestBuilder {
277    /// Overrides the default back-off settings.
278    pub fn backoff(mut self, backoff: ExponentialBuilder) -> Self {
279        self.backoff = backoff;
280        self
281    }
282
283    /// Retries only the errors `retry_if` accepts, instead of every retryable one. For a request
284    /// that must not be sent twice once the service may have acted on it.
285    pub fn retry_only_when(mut self, retry_if: fn(&AlienError<ErrorData>) -> bool) -> Self {
286        self.retry_if = retry_if;
287        self
288    }
289
290    /// Determine if a given error is retry-able using the retryable field.
291    fn is_retryable_error(e: &AlienError<ErrorData>) -> bool {
292        e.retryable
293    }
294
295    /// Creates a default exponential back-off (max 3 attempts, up to 20s).
296    fn default_backoff() -> ExponentialBuilder {
297        ExponentialBuilder::default()
298            .with_max_times(3)
299            .with_max_delay(Duration::from_secs(20))
300            .with_jitter()
301    }
302
303    /// Execute the request, applying retries, and parse the body as JSON.
304    pub async fn send_json<T: DeserializeOwned + Send + 'static>(self) -> Result<T> {
305        let backoff = self.backoff;
306        let retry_if = self.retry_if;
307        let builder = self.inner;
308
309        let retryable = move || {
310            let attempt_builder = builder.try_clone();
311            async move {
312                let attempt_builder = attempt_builder.ok_or_else(|| {
313                    AlienError::new(ErrorData::GenericError {
314                        message: "Request retry preparation failed".into(),
315                    })
316                })?;
317
318                let (client, request, body_string) = build_and_extract_body(attempt_builder)?;
319                let new_builder = reqwest::RequestBuilder::from_parts(client, request);
320
321                #[cfg(target_arch = "wasm32")]
322                {
323                    let resp = new_builder.send().await.into_alien_error().context(
324                        ErrorData::HttpRequestFailed {
325                            message: "Network error during HTTP request".to_string(),
326                        },
327                    )?;
328                    handle_json_response(resp, body_string).await
329                }
330                #[cfg(not(target_arch = "wasm32"))]
331                {
332                    let resp = new_builder.send().await.into_alien_error().context(
333                        ErrorData::HttpRequestFailed {
334                            message: "Network error during HTTP request".to_string(),
335                        },
336                    )?;
337                    handle_json_response(resp, body_string).await
338                }
339            }
340        };
341
342        retryable.retry(backoff).when(retry_if).await
343    }
344
345    /// Execute the request, applying retries, and parse the body as XML.
346    pub async fn send_xml<T: DeserializeOwned + Send + 'static>(self) -> Result<T> {
347        let backoff = self.backoff;
348        let retry_if = self.retry_if;
349        let builder = self.inner;
350
351        let retryable = move || {
352            let attempt_builder = builder.try_clone();
353            async move {
354                let attempt_builder = attempt_builder.ok_or_else(|| {
355                    AlienError::new(ErrorData::GenericError {
356                        message: "Request retry preparation failed".into(),
357                    })
358                })?;
359
360                let (client, request, body_string) = build_and_extract_body(attempt_builder)?;
361                let new_builder = reqwest::RequestBuilder::from_parts(client, request);
362
363                #[cfg(target_arch = "wasm32")]
364                {
365                    let resp = new_builder.send().await.into_alien_error().context(
366                        ErrorData::HttpRequestFailed {
367                            message: "Network error during HTTP request".to_string(),
368                        },
369                    )?;
370                    handle_xml_response(resp, body_string).await
371                }
372                #[cfg(not(target_arch = "wasm32"))]
373                {
374                    let resp = new_builder.send().await.into_alien_error().context(
375                        ErrorData::HttpRequestFailed {
376                            message: "Network error during HTTP request".to_string(),
377                        },
378                    )?;
379                    handle_xml_response(resp, body_string).await
380                }
381            }
382        };
383
384        retryable.retry(backoff).when(retry_if).await
385    }
386
387    /// Execute the request, applying retries, without parsing the response body.
388    pub async fn send_no_response(self) -> Result<()> {
389        let backoff = self.backoff;
390        let retry_if = self.retry_if;
391        let builder = self.inner;
392
393        let retryable = move || {
394            let attempt_builder = builder.try_clone();
395            async move {
396                let attempt_builder = attempt_builder.ok_or_else(|| {
397                    AlienError::new(ErrorData::GenericError {
398                        message: "Request retry preparation failed".into(),
399                    })
400                })?;
401
402                let (client, request, body_string) = build_and_extract_body(attempt_builder)?;
403                let new_builder = reqwest::RequestBuilder::from_parts(client, request);
404
405                #[cfg(target_arch = "wasm32")]
406                {
407                    let resp = new_builder.send().await.into_alien_error().context(
408                        ErrorData::HttpRequestFailed {
409                            message: "Network error during HTTP request".to_string(),
410                        },
411                    )?;
412                    handle_no_response(resp, body_string).await
413                }
414                #[cfg(not(target_arch = "wasm32"))]
415                {
416                    let resp = new_builder.send().await.into_alien_error().context(
417                        ErrorData::HttpRequestFailed {
418                            message: "Network error during HTTP request".to_string(),
419                        },
420                    )?;
421                    handle_no_response(resp, body_string).await
422                }
423            }
424        };
425
426        retryable.retry(backoff).when(retry_if).await
427    }
428
429    /// Execute the request, applying retries, and return the raw response
430    pub async fn send_raw(self) -> Result<reqwest::Response> {
431        let backoff = self.backoff;
432        let retry_if = self.retry_if;
433        let builder = self.inner;
434
435        let retryable = move || {
436            let attempt_builder = builder.try_clone();
437            async move {
438                let attempt_builder = attempt_builder.ok_or_else(|| {
439                    AlienError::new(ErrorData::GenericError {
440                        message: "Request retry preparation failed".into(),
441                    })
442                })?;
443
444                let (client, request, _body_string) = build_and_extract_body(attempt_builder)?;
445                let new_builder = reqwest::RequestBuilder::from_parts(client, request);
446
447                #[cfg(target_arch = "wasm32")]
448                {
449                    new_builder.send().await.into_alien_error().context(
450                        ErrorData::HttpRequestFailed {
451                            message: "Network error during HTTP request".to_string(),
452                        },
453                    )
454                }
455                #[cfg(not(target_arch = "wasm32"))]
456                {
457                    new_builder.send().await.into_alien_error().context(
458                        ErrorData::HttpRequestFailed {
459                            message: "Network error during HTTP request".to_string(),
460                        },
461                    )
462                }
463            }
464        };
465
466        retryable.retry(backoff).when(retry_if).await
467    }
468}
469
470#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
471#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
472impl RequestBuilderExt for reqwest::RequestBuilder {
473    fn with_retry(self) -> RetriableRequestBuilder {
474        RetriableRequestBuilder {
475            inner: self,
476            backoff: RetriableRequestBuilder::default_backoff(),
477            retry_if: RetriableRequestBuilder::is_retryable_error,
478        }
479    }
480
481    async fn send_json<T: DeserializeOwned + 'static>(self) -> Result<T> {
482        let (client, request, body_string) = build_and_extract_body(self)?;
483        let builder = reqwest::RequestBuilder::from_parts(client, request);
484
485        #[cfg(target_arch = "wasm32")]
486        {
487            let resp =
488                builder
489                    .send()
490                    .await
491                    .into_alien_error()
492                    .context(ErrorData::HttpRequestFailed {
493                        message: "Network error during HTTP request".to_string(),
494                    })?;
495            handle_json_response(resp, body_string).await
496        }
497        #[cfg(not(target_arch = "wasm32"))]
498        {
499            let resp =
500                builder
501                    .send()
502                    .await
503                    .into_alien_error()
504                    .context(ErrorData::HttpRequestFailed {
505                        message: "Network error during HTTP request".to_string(),
506                    })?;
507            handle_json_response(resp, body_string).await
508        }
509    }
510
511    async fn send_xml<T: DeserializeOwned + 'static>(self) -> Result<T> {
512        let (client, request, body_string) = build_and_extract_body(self)?;
513        let builder = reqwest::RequestBuilder::from_parts(client, request);
514
515        #[cfg(target_arch = "wasm32")]
516        {
517            let resp =
518                builder
519                    .send()
520                    .await
521                    .into_alien_error()
522                    .context(ErrorData::HttpRequestFailed {
523                        message: "Network error during HTTP request".to_string(),
524                    })?;
525            handle_xml_response(resp, body_string).await
526        }
527        #[cfg(not(target_arch = "wasm32"))]
528        {
529            let resp =
530                builder
531                    .send()
532                    .await
533                    .into_alien_error()
534                    .context(ErrorData::HttpRequestFailed {
535                        message: "Network error during HTTP request".to_string(),
536                    })?;
537            handle_xml_response(resp, body_string).await
538        }
539    }
540
541    async fn send_no_response(self) -> Result<()> {
542        let (client, request, body_string) = build_and_extract_body(self)?;
543        let builder = reqwest::RequestBuilder::from_parts(client, request);
544
545        #[cfg(target_arch = "wasm32")]
546        {
547            let resp =
548                builder
549                    .send()
550                    .await
551                    .into_alien_error()
552                    .context(ErrorData::HttpRequestFailed {
553                        message: "Network error during HTTP request".to_string(),
554                    })?;
555            handle_no_response(resp, body_string).await
556        }
557        #[cfg(not(target_arch = "wasm32"))]
558        {
559            let resp =
560                builder
561                    .send()
562                    .await
563                    .into_alien_error()
564                    .context(ErrorData::HttpRequestFailed {
565                        message: "Network error during HTTP request".to_string(),
566                    })?;
567            handle_no_response(resp, body_string).await
568        }
569    }
570
571    async fn send_raw(self) -> Result<reqwest::Response> {
572        let (client, request, _body_string) = build_and_extract_body(self)?;
573        let builder = reqwest::RequestBuilder::from_parts(client, request);
574
575        #[cfg(target_arch = "wasm32")]
576        {
577            builder
578                .send()
579                .await
580                .into_alien_error()
581                .context(ErrorData::HttpRequestFailed {
582                    message: "Network error during HTTP request".to_string(),
583                })
584        }
585        #[cfg(not(target_arch = "wasm32"))]
586        {
587            builder
588                .send()
589                .await
590                .into_alien_error()
591                .context(ErrorData::HttpRequestFailed {
592                    message: "Network error during HTTP request".to_string(),
593                })
594        }
595    }
596}
597
598#[cfg(test)]
599mod tests {
600    use super::*;
601    use alien_error::ContextError;
602    use std::{
603        io::{BufRead, BufReader, Write},
604        net::TcpListener,
605        sync::{
606            atomic::{AtomicUsize, Ordering},
607            Arc,
608        },
609        thread,
610    };
611    use tokio::io::{AsyncBufReadExt, AsyncWriteExt};
612
613    #[tokio::test]
614    async fn success_body_read_failure_does_not_replay_mutation() {
615        let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
616            .await
617            .expect("listen");
618        let address = listener.local_addr().expect("address");
619        let requests = Arc::new(AtomicUsize::new(0));
620        let seen = requests.clone();
621        let server = tokio::spawn(async move {
622            loop {
623                let (mut stream, _) = listener.accept().await.expect("accept");
624                {
625                    let mut reader = tokio::io::BufReader::new(&mut stream);
626                    loop {
627                        let mut line = String::new();
628                        assert!(reader.read_line(&mut line).await.expect("request header") > 0);
629                        if line == "\r\n" {
630                            break;
631                        }
632                    }
633                }
634                let request = seen.fetch_add(1, Ordering::SeqCst);
635                let response = if request == 0 {
636                    &b"HTTP/1.1 200 OK\r\nContent-Length: 10\r\nConnection: close\r\n\r\nshort"[..]
637                } else {
638                    &b"HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"[..]
639                };
640                stream.write_all(response).await.expect("response");
641                stream.shutdown().await.expect("close response");
642            }
643        });
644        let error = reqwest::Client::builder()
645            .no_proxy()
646            .timeout(Duration::from_secs(5))
647            .build()
648            .expect("client")
649            .post(format!("http://{address}/mutation"))
650            .with_retry()
651            .backoff(
652                ExponentialBuilder::default()
653                    .with_min_delay(Duration::from_millis(1))
654                    .with_max_delay(Duration::from_millis(1))
655                    .with_max_times(2),
656            )
657            .send_no_response()
658            .await
659            .expect_err("truncated body must fail");
660        assert_eq!(requests.load(Ordering::SeqCst), 1, "mutation was replayed");
661        assert!(!error.retryable);
662        assert_eq!(error.code, "HTTP_RESPONSE_BODY_READ_FAILED");
663        assert!(matches!(
664            error.error,
665            Some(ErrorData::HttpResponseBodyReadFailed { http_status: 200 })
666        ));
667        server.abort();
668        assert!(server.await.expect_err("server stopped").is_cancelled());
669    }
670
671    #[tokio::test]
672    async fn rejects_truncated_success_response_body() {
673        let listener = TcpListener::bind("127.0.0.1:0").expect("listen");
674        let address = listener.local_addr().expect("address");
675        let server = thread::spawn(move || {
676            let (mut stream, _) = listener.accept().expect("accept");
677            {
678                let mut reader = BufReader::new(&mut stream);
679                loop {
680                    let mut line = String::new();
681                    assert!(reader.read_line(&mut line).expect("request header") > 0);
682                    if line == "\r\n" {
683                        break;
684                    }
685                }
686            }
687            stream
688                .write_all(
689                    b"HTTP/1.1 200 OK\r\nContent-Length: 10\r\nConnection: close\r\n\r\nshort",
690                )
691                .expect("partial response");
692        });
693        let response = reqwest::Client::builder()
694            .no_proxy()
695            .timeout(Duration::from_secs(5))
696            .build()
697            .expect("client")
698            .get(format!("http://{address}/"))
699            .send()
700            .await
701            .expect("response headers");
702        assert_eq!(response.status(), reqwest::StatusCode::OK);
703        let error = handle_no_response(response, None)
704            .await
705            .expect_err("truncated body must fail");
706        assert_eq!(error.code, "HTTP_RESPONSE_BODY_READ_FAILED");
707        assert!(!error.retryable);
708        server.join().expect("server");
709    }
710
711    const SECRET: &str = "Sup3rSecret-MasterPassword!";
712
713    /// An `HttpResponseError` as the transport produces it for a failed create: its captured request
714    /// body carries the master password, in both the typed payload and the derived `context` snapshot.
715    fn http_error_with_secret_body() -> AlienError<ErrorData> {
716        AlienError::new(ErrorData::HttpResponseError {
717            message: "Request failed with HTTP 409: Conflict".to_string(),
718            url: "https://rds.amazonaws.com/".to_string(),
719            http_status: 409,
720            http_request_text: Some(format!(
721                "Action=CreateDBCluster&MasterUserPassword={SECRET}&Engine=aurora-postgresql"
722            )),
723            http_response_text: Some(
724                "<Error><Code>DBClusterAlreadyExists</Code></Error>".to_string(),
725            ),
726        })
727    }
728
729    /// AWS path: redaction runs while the HTTP error is still the head. The secret must not survive in
730    /// any serialized field — typed payload or `context` — while diagnostics are kept.
731    #[test]
732    fn redacts_request_body_when_http_error_is_head() {
733        // Precondition: the unredacted error genuinely carries the secret, so this test can fail.
734        let raw = serde_json::to_string(&http_error_with_secret_body()).unwrap();
735        assert!(raw.contains(SECRET), "fixture should carry the secret");
736
737        let err = redact_request_body::<()>(Err(http_error_with_secret_body())).unwrap_err();
738        let json = serde_json::to_string(&err).expect("serialize redacted error");
739        assert!(!json.contains(SECRET), "request body leaked: {json}");
740        // Non-secret diagnostics are retained.
741        assert!(json.contains("409"), "status dropped: {json}");
742        assert!(
743            json.contains("DBClusterAlreadyExists"),
744            "response text dropped: {json}"
745        );
746    }
747
748    /// GCP/Azure path: the transport maps the HTTP error to a non-internal head before redaction runs,
749    /// so the body now lives only in the `source` chain's `context`. The non-internal head does NOT
750    /// sanitize its source by itself (asserted as a precondition), so the whole-chain walk is
751    /// load-bearing here.
752    #[test]
753    fn redacts_request_body_when_http_error_is_wrapped_in_source() {
754        let wrapped = http_error_with_secret_body().context(ErrorData::RemoteResourceConflict {
755            resource_type: "DBCluster".to_string(),
756            resource_name: "stack-db".to_string(),
757            message: "already exists".to_string(),
758        });
759        let before = serde_json::to_string(&wrapped).unwrap();
760        assert!(
761            before.contains(SECRET),
762            "precondition: a non-internal head must NOT sanitize its source on its own"
763        );
764
765        let err = redact_request_body::<()>(Err(wrapped)).unwrap_err();
766        let json = serde_json::to_string(&err).expect("serialize redacted error");
767        assert!(
768            !json.contains(SECRET),
769            "request body leaked through source chain: {json}"
770        );
771        // The chain and its non-secret diagnostics are otherwise intact.
772        assert!(
773            json.contains("REMOTE_RESOURCE_CONFLICT"),
774            "head dropped: {json}"
775        );
776        assert!(
777            json.contains("DBClusterAlreadyExists"),
778            "response text dropped: {json}"
779        );
780    }
781}