Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changelog/private-body-receipts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"mpp": patch
---

Mark successful body-bound payment responses as private while preserving existing
`Cache-Control` directives, preventing shared caches from storing payment receipts.
24 changes: 23 additions & 1 deletion src/server/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,19 @@ where
let req = Request::from_parts(parts, http_body_util::Full::new(body));
let mut resp = inner.call(req).await?;

// Receipt responses MUST be Cache-Control: private (spec §11.10).
let existing_cc = resp
.headers()
.get_all(header::CACHE_CONTROL)
.iter()
.filter_map(|v| v.to_str().ok())
.collect::<Vec<_>>()
.join(", ");
let cache_control = with_private_cache_control(Some(existing_cc.as_str()));
if let Ok(val) = HeaderValue::from_str(&cache_control) {
resp.headers_mut().insert(header::CACHE_CONTROL, val);
}

if let Ok(val) = HeaderValue::from_str(&receipt_header) {
resp.headers_mut().insert(PAYMENT_RECEIPT_HEADER, val);
}
Expand Down Expand Up @@ -875,7 +888,12 @@ mod tests {
fn call(&mut self, req: Request<http_body_util::Full<Bytes>>) -> Self::Future {
Box::pin(async move {
let body = req.into_body().collect().await.unwrap().to_bytes();
Ok(Response::new(body.to_vec()))
let mut response = Response::new(body.to_vec());
response.headers_mut().insert(
header::CACHE_CONTROL,
HeaderValue::from_static("max-age=60"),
);
Ok(response)
})
}
}
Expand Down Expand Up @@ -936,6 +954,10 @@ mod tests {
resp.headers().get(PAYMENT_RECEIPT_HEADER).unwrap(),
"mock-receipt-token"
);
assert_eq!(
resp.headers().get(header::CACHE_CONTROL).unwrap(),
"max-age=60, private"
);
assert_eq!(
verify_body.lock().unwrap().as_deref(),
Some(br#"{"query":"paid"}"#.as_slice())
Expand Down
Loading