From 2eb27cfe77e6600e0562d85cc519e00c394653b7 Mon Sep 17 00:00:00 2001 From: Sanapach Date: Thu, 19 Nov 2026 21:52:31 +0200 Subject: [PATCH] Accept only application/json bodies on podcast endpoints Co-Authored-By: Claude Opus 5.5 --- docs/development.md | 4 +++ src/podcasts/http.rs | 72 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/docs/development.md b/docs/development.md index 253eb8e..84d3670 100644 --- a/docs/development.md +++ b/docs/development.md @@ -153,6 +153,10 @@ authenticates and maps statuses), so the Postgres tests drive it directly: | `POST /episodes/{id}/playback` | source, resume position; with `start` a new session (explicit, above `carried_generation`); records an unknown handed-off reference first | | `POST /episodes/{id}/progress` | `checkpoint`/`pause`/`stop`/`handoff` at the session generation, `seek` explicit, completion once (`ended` or `reaches_completion`), listening session record | +Request bodies must be `application/json` (415 otherwise, like cot's `Json` +extractor): other types need no CORS preflight and would let a same-site page +post with the session cookie. + In the browser (`templates/player/scripts.html`), `_playEpisodeLocal` asks for the playback, loads the publisher URL (or the proxy: mixed content, or a failed direct load) and seeks before playing; when the source cannot seek to diff --git a/src/podcasts/http.rs b/src/podcasts/http.rs index 13adb40..a3a6afb 100644 --- a/src/podcasts/http.rs +++ b/src/podcasts/http.rs @@ -107,8 +107,29 @@ fn unauthorized() -> cot::response::Response { /// Largest JSON body accepted (a playback request carries one episode reference). const MAX_JSON_BODY: usize = 64 * 1024; -/// Request body as JSON (`null` when empty); malformed JSON is a 400. +/// Whether a `Content-Type` value is `application/json` (parameters such +/// as `charset` allowed). +fn is_json_content_type(value: Option<&str>) -> bool { + value + .and_then(|value| value.split(';').next()) + .is_some_and(|essence| essence.trim().eq_ignore_ascii_case("application/json")) +} + +/// Request body as JSON (`{}` when empty); malformed JSON is a 400. +/// +/// A body must be sent as `application/json` (415 otherwise), like cot's +/// `Json` extractor requires on the other player endpoints: a cross-origin +/// page can only send that type after a CORS preflight, while `text/plain` +/// and form bodies need none and would carry the session cookie from a +/// same-site page. `navigator.sendBeacon` sends a Blob typed +/// `application/json`. async fn json_body(request: cot::request::Request) -> Result { + let json = is_json_content_type( + request + .headers() + .get(CONTENT_TYPE) + .and_then(|value| value.to_str().ok()), + ); let bytes = request .into_body() .into_bytes_limited(MAX_JSON_BODY) @@ -117,6 +138,9 @@ async fn json_body(request: cot::request::Request) -> Result, body: &'static str) -> cot::request::Request { + let mut builder = cot::http::Request::builder() + .method("POST") + .uri("/api/podcasts/feeds"); + if let Some(value) = content_type { + builder = builder.header(CONTENT_TYPE, value); + } + builder.body(Body::fixed(body)).unwrap() + } + + #[tokio::test] + async fn json_bodies_need_the_json_content_type() { + let url = r#"{"url": "https://podcasts.example/feed.xml"}"#; + for content_type in [ + "application/json", + "application/json; charset=UTF-8", + "Application/JSON", + ] { + let body = json_body(request(Some(content_type), url)).await.unwrap(); + assert_eq!(body["url"], "https://podcasts.example/feed.xml"); + } + // What a page can send cross-origin without a CORS preflight. + for content_type in [ + Some("text/plain"), + Some("text/plain;charset=UTF-8"), + Some("application/x-www-form-urlencoded"), + Some("multipart/form-data; boundary=x"), + None, + ] { + let error = json_body(request(content_type, url)).await.unwrap_err(); + assert_eq!(error.status, 415, "{content_type:?}"); + } + // An empty body (a plain POST) is an empty object whatever the type. + let empty = json_body(request(None, "")).await.unwrap(); + assert_eq!(empty, serde_json::json!({})); + let malformed = json_body(request(Some("application/json"), "{")) + .await + .unwrap_err(); + assert_eq!(malformed.status, 400); + } +}