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/// The exponential back-off the cloud clients retry with unless a request overrides it: up to
268/// 3 retries, each delay up to 20s.
269pub fn default_retry_backoff() -> ExponentialBuilder {
270    let backoff = ExponentialBuilder::default()
271        .with_max_times(3)
272        .with_max_delay(Duration::from_secs(20))
273        .with_jitter();
274    // Mocked tests answer at once, so waiting between retries only adds time. The retries
275    // still happen, so every request a test expects is still sent.
276    if cfg!(feature = "no-retry-delay") {
277        backoff.with_min_delay(Duration::ZERO)
278    } else {
279        backoff
280    }
281}
282
283/// A `reqwest::RequestBuilder` wrapper that automatically retries failed
284/// requests using an exponential back-off strategy powered by the `backon`
285/// crate. Use [`RequestBuilderExt::with_retry`] to construct one.
286pub struct RetriableRequestBuilder {
287    inner: reqwest::RequestBuilder,
288    backoff: ExponentialBuilder,
289    retry_if: fn(&AlienError<ErrorData>) -> bool,
290}
291
292impl RetriableRequestBuilder {
293    /// Overrides the default back-off settings.
294    pub fn backoff(mut self, backoff: ExponentialBuilder) -> Self {
295        self.backoff = backoff;
296        self
297    }
298
299    /// Retries only the errors `retry_if` accepts, instead of every retryable one. For a request
300    /// that must not be sent twice once the service may have acted on it.
301    pub fn retry_only_when(mut self, retry_if: fn(&AlienError<ErrorData>) -> bool) -> Self {
302        self.retry_if = retry_if;
303        self
304    }
305
306    /// Determine if a given error is retry-able using the retryable field.
307    fn is_retryable_error(e: &AlienError<ErrorData>) -> bool {
308        e.retryable
309    }
310
311    /// Execute the request, applying retries, and parse the body as JSON.
312    pub async fn send_json<T: DeserializeOwned + Send + 'static>(self) -> Result<T> {
313        let backoff = self.backoff;
314        let retry_if = self.retry_if;
315        let builder = self.inner;
316
317        let retryable = move || {
318            let attempt_builder = builder.try_clone();
319            async move {
320                let attempt_builder = attempt_builder.ok_or_else(|| {
321                    AlienError::new(ErrorData::GenericError {
322                        message: "Request retry preparation failed".into(),
323                    })
324                })?;
325
326                let (client, request, body_string) = build_and_extract_body(attempt_builder)?;
327                let new_builder = reqwest::RequestBuilder::from_parts(client, request);
328
329                #[cfg(target_arch = "wasm32")]
330                {
331                    let resp = new_builder.send().await.into_alien_error().context(
332                        ErrorData::HttpRequestFailed {
333                            message: "Network error during HTTP request".to_string(),
334                        },
335                    )?;
336                    handle_json_response(resp, body_string).await
337                }
338                #[cfg(not(target_arch = "wasm32"))]
339                {
340                    let resp = new_builder.send().await.into_alien_error().context(
341                        ErrorData::HttpRequestFailed {
342                            message: "Network error during HTTP request".to_string(),
343                        },
344                    )?;
345                    handle_json_response(resp, body_string).await
346                }
347            }
348        };
349
350        retryable.retry(backoff).when(retry_if).await
351    }
352
353    /// Execute the request, applying retries, and parse the body as XML.
354    pub async fn send_xml<T: DeserializeOwned + Send + 'static>(self) -> Result<T> {
355        let backoff = self.backoff;
356        let retry_if = self.retry_if;
357        let builder = self.inner;
358
359        let retryable = move || {
360            let attempt_builder = builder.try_clone();
361            async move {
362                let attempt_builder = attempt_builder.ok_or_else(|| {
363                    AlienError::new(ErrorData::GenericError {
364                        message: "Request retry preparation failed".into(),
365                    })
366                })?;
367
368                let (client, request, body_string) = build_and_extract_body(attempt_builder)?;
369                let new_builder = reqwest::RequestBuilder::from_parts(client, request);
370
371                #[cfg(target_arch = "wasm32")]
372                {
373                    let resp = new_builder.send().await.into_alien_error().context(
374                        ErrorData::HttpRequestFailed {
375                            message: "Network error during HTTP request".to_string(),
376                        },
377                    )?;
378                    handle_xml_response(resp, body_string).await
379                }
380                #[cfg(not(target_arch = "wasm32"))]
381                {
382                    let resp = new_builder.send().await.into_alien_error().context(
383                        ErrorData::HttpRequestFailed {
384                            message: "Network error during HTTP request".to_string(),
385                        },
386                    )?;
387                    handle_xml_response(resp, body_string).await
388                }
389            }
390        };
391
392        retryable.retry(backoff).when(retry_if).await
393    }
394
395    /// Execute the request, applying retries, without parsing the response body.
396    pub async fn send_no_response(self) -> Result<()> {
397        let backoff = self.backoff;
398        let retry_if = self.retry_if;
399        let builder = self.inner;
400
401        let retryable = move || {
402            let attempt_builder = builder.try_clone();
403            async move {
404                let attempt_builder = attempt_builder.ok_or_else(|| {
405                    AlienError::new(ErrorData::GenericError {
406                        message: "Request retry preparation failed".into(),
407                    })
408                })?;
409
410                let (client, request, body_string) = build_and_extract_body(attempt_builder)?;
411                let new_builder = reqwest::RequestBuilder::from_parts(client, request);
412
413                #[cfg(target_arch = "wasm32")]
414                {
415                    let resp = new_builder.send().await.into_alien_error().context(
416                        ErrorData::HttpRequestFailed {
417                            message: "Network error during HTTP request".to_string(),
418                        },
419                    )?;
420                    handle_no_response(resp, body_string).await
421                }
422                #[cfg(not(target_arch = "wasm32"))]
423                {
424                    let resp = new_builder.send().await.into_alien_error().context(
425                        ErrorData::HttpRequestFailed {
426                            message: "Network error during HTTP request".to_string(),
427                        },
428                    )?;
429                    handle_no_response(resp, body_string).await
430                }
431            }
432        };
433
434        retryable.retry(backoff).when(retry_if).await
435    }
436
437    /// Execute the request, applying retries, and return the raw response
438    pub async fn send_raw(self) -> Result<reqwest::Response> {
439        let backoff = self.backoff;
440        let retry_if = self.retry_if;
441        let builder = self.inner;
442
443        let retryable = move || {
444            let attempt_builder = builder.try_clone();
445            async move {
446                let attempt_builder = attempt_builder.ok_or_else(|| {
447                    AlienError::new(ErrorData::GenericError {
448                        message: "Request retry preparation failed".into(),
449                    })
450                })?;
451
452                let (client, request, _body_string) = build_and_extract_body(attempt_builder)?;
453                let new_builder = reqwest::RequestBuilder::from_parts(client, request);
454
455                #[cfg(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                #[cfg(not(target_arch = "wasm32"))]
464                {
465                    new_builder.send().await.into_alien_error().context(
466                        ErrorData::HttpRequestFailed {
467                            message: "Network error during HTTP request".to_string(),
468                        },
469                    )
470                }
471            }
472        };
473
474        retryable.retry(backoff).when(retry_if).await
475    }
476}
477
478#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
479#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
480impl RequestBuilderExt for reqwest::RequestBuilder {
481    fn with_retry(self) -> RetriableRequestBuilder {
482        RetriableRequestBuilder {
483            inner: self,
484            backoff: default_retry_backoff(),
485            retry_if: RetriableRequestBuilder::is_retryable_error,
486        }
487    }
488
489    async fn send_json<T: DeserializeOwned + 'static>(self) -> Result<T> {
490        let (client, request, body_string) = build_and_extract_body(self)?;
491        let builder = reqwest::RequestBuilder::from_parts(client, request);
492
493        #[cfg(target_arch = "wasm32")]
494        {
495            let resp =
496                builder
497                    .send()
498                    .await
499                    .into_alien_error()
500                    .context(ErrorData::HttpRequestFailed {
501                        message: "Network error during HTTP request".to_string(),
502                    })?;
503            handle_json_response(resp, body_string).await
504        }
505        #[cfg(not(target_arch = "wasm32"))]
506        {
507            let resp =
508                builder
509                    .send()
510                    .await
511                    .into_alien_error()
512                    .context(ErrorData::HttpRequestFailed {
513                        message: "Network error during HTTP request".to_string(),
514                    })?;
515            handle_json_response(resp, body_string).await
516        }
517    }
518
519    async fn send_xml<T: DeserializeOwned + 'static>(self) -> Result<T> {
520        let (client, request, body_string) = build_and_extract_body(self)?;
521        let builder = reqwest::RequestBuilder::from_parts(client, request);
522
523        #[cfg(target_arch = "wasm32")]
524        {
525            let resp =
526                builder
527                    .send()
528                    .await
529                    .into_alien_error()
530                    .context(ErrorData::HttpRequestFailed {
531                        message: "Network error during HTTP request".to_string(),
532                    })?;
533            handle_xml_response(resp, body_string).await
534        }
535        #[cfg(not(target_arch = "wasm32"))]
536        {
537            let resp =
538                builder
539                    .send()
540                    .await
541                    .into_alien_error()
542                    .context(ErrorData::HttpRequestFailed {
543                        message: "Network error during HTTP request".to_string(),
544                    })?;
545            handle_xml_response(resp, body_string).await
546        }
547    }
548
549    async fn send_no_response(self) -> Result<()> {
550        let (client, request, body_string) = build_and_extract_body(self)?;
551        let builder = reqwest::RequestBuilder::from_parts(client, request);
552
553        #[cfg(target_arch = "wasm32")]
554        {
555            let resp =
556                builder
557                    .send()
558                    .await
559                    .into_alien_error()
560                    .context(ErrorData::HttpRequestFailed {
561                        message: "Network error during HTTP request".to_string(),
562                    })?;
563            handle_no_response(resp, body_string).await
564        }
565        #[cfg(not(target_arch = "wasm32"))]
566        {
567            let resp =
568                builder
569                    .send()
570                    .await
571                    .into_alien_error()
572                    .context(ErrorData::HttpRequestFailed {
573                        message: "Network error during HTTP request".to_string(),
574                    })?;
575            handle_no_response(resp, body_string).await
576        }
577    }
578
579    async fn send_raw(self) -> Result<reqwest::Response> {
580        let (client, request, _body_string) = build_and_extract_body(self)?;
581        let builder = reqwest::RequestBuilder::from_parts(client, request);
582
583        #[cfg(target_arch = "wasm32")]
584        {
585            builder
586                .send()
587                .await
588                .into_alien_error()
589                .context(ErrorData::HttpRequestFailed {
590                    message: "Network error during HTTP request".to_string(),
591                })
592        }
593        #[cfg(not(target_arch = "wasm32"))]
594        {
595            builder
596                .send()
597                .await
598                .into_alien_error()
599                .context(ErrorData::HttpRequestFailed {
600                    message: "Network error during HTTP request".to_string(),
601                })
602        }
603    }
604}
605
606#[cfg(test)]
607mod tests {
608    use super::*;
609    use alien_error::ContextError;
610    use std::{
611        io::{BufRead, BufReader, Write},
612        net::TcpListener,
613        sync::{
614            atomic::{AtomicUsize, Ordering},
615            Arc,
616        },
617        thread,
618    };
619    use tokio::io::{AsyncBufReadExt, AsyncWriteExt};
620
621    #[tokio::test]
622    async fn success_body_read_failure_does_not_replay_mutation() {
623        let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
624            .await
625            .expect("listen");
626        let address = listener.local_addr().expect("address");
627        let requests = Arc::new(AtomicUsize::new(0));
628        let seen = requests.clone();
629        let server = tokio::spawn(async move {
630            loop {
631                let (mut stream, _) = listener.accept().await.expect("accept");
632                {
633                    let mut reader = tokio::io::BufReader::new(&mut stream);
634                    loop {
635                        let mut line = String::new();
636                        assert!(reader.read_line(&mut line).await.expect("request header") > 0);
637                        if line == "\r\n" {
638                            break;
639                        }
640                    }
641                }
642                let request = seen.fetch_add(1, Ordering::SeqCst);
643                let response = if request == 0 {
644                    &b"HTTP/1.1 200 OK\r\nContent-Length: 10\r\nConnection: close\r\n\r\nshort"[..]
645                } else {
646                    &b"HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"[..]
647                };
648                stream.write_all(response).await.expect("response");
649                stream.shutdown().await.expect("close response");
650            }
651        });
652        let error = reqwest::Client::builder()
653            .no_proxy()
654            .timeout(Duration::from_secs(5))
655            .build()
656            .expect("client")
657            .post(format!("http://{address}/mutation"))
658            .with_retry()
659            .backoff(
660                ExponentialBuilder::default()
661                    .with_min_delay(Duration::from_millis(1))
662                    .with_max_delay(Duration::from_millis(1))
663                    .with_max_times(2),
664            )
665            .send_no_response()
666            .await
667            .expect_err("truncated body must fail");
668        assert_eq!(requests.load(Ordering::SeqCst), 1, "mutation was replayed");
669        assert!(!error.retryable);
670        assert_eq!(error.code, "HTTP_RESPONSE_BODY_READ_FAILED");
671        assert!(matches!(
672            error.error,
673            Some(ErrorData::HttpResponseBodyReadFailed { http_status: 200 })
674        ));
675        server.abort();
676        assert!(server.await.expect_err("server stopped").is_cancelled());
677    }
678
679    #[tokio::test]
680    async fn rejects_truncated_success_response_body() {
681        let listener = TcpListener::bind("127.0.0.1:0").expect("listen");
682        let address = listener.local_addr().expect("address");
683        let server = thread::spawn(move || {
684            let (mut stream, _) = listener.accept().expect("accept");
685            {
686                let mut reader = BufReader::new(&mut stream);
687                loop {
688                    let mut line = String::new();
689                    assert!(reader.read_line(&mut line).expect("request header") > 0);
690                    if line == "\r\n" {
691                        break;
692                    }
693                }
694            }
695            stream
696                .write_all(
697                    b"HTTP/1.1 200 OK\r\nContent-Length: 10\r\nConnection: close\r\n\r\nshort",
698                )
699                .expect("partial response");
700        });
701        let response = reqwest::Client::builder()
702            .no_proxy()
703            .timeout(Duration::from_secs(5))
704            .build()
705            .expect("client")
706            .get(format!("http://{address}/"))
707            .send()
708            .await
709            .expect("response headers");
710        assert_eq!(response.status(), reqwest::StatusCode::OK);
711        let error = handle_no_response(response, None)
712            .await
713            .expect_err("truncated body must fail");
714        assert_eq!(error.code, "HTTP_RESPONSE_BODY_READ_FAILED");
715        assert!(!error.retryable);
716        server.join().expect("server");
717    }
718
719    const SECRET: &str = "Sup3rSecret-MasterPassword!";
720
721    /// An `HttpResponseError` as the transport produces it for a failed create: its captured request
722    /// body carries the master password, in both the typed payload and the derived `context` snapshot.
723    fn http_error_with_secret_body() -> AlienError<ErrorData> {
724        AlienError::new(ErrorData::HttpResponseError {
725            message: "Request failed with HTTP 409: Conflict".to_string(),
726            url: "https://rds.amazonaws.com/".to_string(),
727            http_status: 409,
728            http_request_text: Some(format!(
729                "Action=CreateDBCluster&MasterUserPassword={SECRET}&Engine=aurora-postgresql"
730            )),
731            http_response_text: Some(
732                "<Error><Code>DBClusterAlreadyExists</Code></Error>".to_string(),
733            ),
734        })
735    }
736
737    /// AWS path: redaction runs while the HTTP error is still the head. The secret must not survive in
738    /// any serialized field — typed payload or `context` — while diagnostics are kept.
739    #[test]
740    fn redacts_request_body_when_http_error_is_head() {
741        // Precondition: the unredacted error genuinely carries the secret, so this test can fail.
742        let raw = serde_json::to_string(&http_error_with_secret_body()).unwrap();
743        assert!(raw.contains(SECRET), "fixture should carry the secret");
744
745        let err = redact_request_body::<()>(Err(http_error_with_secret_body())).unwrap_err();
746        let json = serde_json::to_string(&err).expect("serialize redacted error");
747        assert!(!json.contains(SECRET), "request body leaked: {json}");
748        // Non-secret diagnostics are retained.
749        assert!(json.contains("409"), "status dropped: {json}");
750        assert!(
751            json.contains("DBClusterAlreadyExists"),
752            "response text dropped: {json}"
753        );
754    }
755
756    /// GCP/Azure path: the transport maps the HTTP error to a non-internal head before redaction runs,
757    /// so the body now lives only in the `source` chain's `context`. The non-internal head does NOT
758    /// sanitize its source by itself (asserted as a precondition), so the whole-chain walk is
759    /// load-bearing here.
760    #[test]
761    fn redacts_request_body_when_http_error_is_wrapped_in_source() {
762        let wrapped = http_error_with_secret_body().context(ErrorData::RemoteResourceConflict {
763            resource_type: "DBCluster".to_string(),
764            resource_name: "stack-db".to_string(),
765            message: "already exists".to_string(),
766        });
767        let before = serde_json::to_string(&wrapped).unwrap();
768        assert!(
769            before.contains(SECRET),
770            "precondition: a non-internal head must NOT sanitize its source on its own"
771        );
772
773        let err = redact_request_body::<()>(Err(wrapped)).unwrap_err();
774        let json = serde_json::to_string(&err).expect("serialize redacted error");
775        assert!(
776            !json.contains(SECRET),
777            "request body leaked through source chain: {json}"
778        );
779        // The chain and its non-secret diagnostics are otherwise intact.
780        assert!(
781            json.contains("REMOTE_RESOURCE_CONFLICT"),
782            "head dropped: {json}"
783        );
784        assert!(
785            json.contains("DBClusterAlreadyExists"),
786            "response text dropped: {json}"
787        );
788    }
789}