Allow callers to redact encoded JSON request bodies from logs (#64003)

## Why

Callers need a way to keep encoded JSON payloads out of request
diagnostics while preserving what the server receives.

## What changed

Add `EncodedJsonBody::omit_from_logs()` to replace the trace body with
`<redacted>` and show only the byte length in debug output. Preserve the
setting through private JSON enrichment and request preparation. The
setting leaves the wire payload and redirect policy unchanged.

## Testing

Add a regression test that checks trace redaction and debug output for
both plain and enriched bodies before and after preparation, and
verifies that decompressing the wire body yields the expected bytes.

GitOrigin-RevId: 5b1ae2c810242f8c7f35bcdf653af9127360c29c
This commit is contained in:
jif
2026-11-25 22:47:37 +00:00
committed by copyberry
parent 44d359767f
commit 30d71606a1
2 changed files with 41 additions and 2 deletions
+17 -2
View File
@@ -18,12 +18,13 @@ pub struct EncodedJsonBody {
trace_bytes: Option<Bytes>,
prepared: bool,
private: bool,
omit_from_logs: bool,
}
impl fmt::Debug for EncodedJsonBody {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut body = f.debug_struct("EncodedJsonBody");
if self.private {
if self.private || self.omit_from_logs {
return body
.field("byte_len", &self.bytes.len())
.finish_non_exhaustive();
@@ -43,6 +44,7 @@ impl EncodedJsonBody {
trace_bytes: None,
prepared: false,
private: false,
omit_from_logs: false,
})
}
@@ -63,10 +65,19 @@ impl EncodedJsonBody {
trace_bytes: Some(self.bytes.clone()),
prepared: false,
private: true,
omit_from_logs: self.omit_from_logs,
}
}
/// Suppress body diagnostics without changing the wire bytes or redirect policy.
pub fn omit_from_logs(&mut self) {
self.omit_from_logs = true;
}
pub(crate) fn trace_bytes(&self) -> &[u8] {
if self.omit_from_logs {
return b"<redacted>";
}
self.trace_bytes.as_ref().unwrap_or(&self.bytes)
}
}
@@ -158,6 +169,8 @@ impl Request {
self.body,
Some(RequestBody::Json(_) | RequestBody::EncodedJson(_))
);
let omit_from_logs =
matches!(&self.body, Some(RequestBody::EncodedJson(body)) if body.omit_from_logs);
let private_trace = match &self.body {
Some(RequestBody::EncodedJson(body)) if body.private => body.trace_bytes.clone(),
_ => None,
@@ -165,7 +178,8 @@ impl Request {
let private = private_trace.is_some();
let trace_bytes = if private {
private_trace
} else if self.compression != RequestCompression::None
} else if !omit_from_logs
&& self.compression != RequestCompression::None
&& tracing::enabled!(target: "codex_http_client::transport", tracing::Level::TRACE)
{
match self.body.as_ref() {
@@ -186,6 +200,7 @@ impl Request {
trace_bytes,
prepared: true,
private,
omit_from_logs,
})),
(false, Some(body)) => Some(RequestBody::Raw(body)),
(_, None) => None,
@@ -126,3 +126,27 @@ impl Write for TestLogWriter {
Ok(())
}
}
#[test]
fn omitted_body_stays_out_of_logs_after_enrichment_and_compression()
-> Result<(), Box<dyn std::error::Error>> {
let mut body = crate::EncodedJsonBody::encode(&json!({"secret": "body-marker"}))?;
body.omit_from_logs();
let enriched =
body.with_private_json(json!({"secret": "body-marker", "extra": true}).to_string());
for body in [body, enriched] {
let expected = body.shared_bytes().clone();
let mut req = Request::new(Method::POST, "https://example.com".into())
.with_compression(crate::RequestCompression::Zstd);
req.body = Some(RequestBody::EncodedJson(body));
let prepared = req.clone().into_prepared()?;
for req in [&req, &prepared] {
pretty_assertions::assert_eq!(request_body_for_trace(req), "<redacted>");
assert!(!format!("{req:?}").contains("body-marker"));
}
let wire = prepared.prepare_wire_body()?;
let bytes = zstd::stream::decode_all(wire.body.as_deref().expect("wire body"))?;
pretty_assertions::assert_eq!(bytes.as_slice(), expected.as_ref());
}
Ok(())
}