From 2c17c9e4df71070700c911d1f5a92c82a8e9ab07 Mon Sep 17 00:00:00 2001 From: Sanapach Date: Thu, 26 Nov 2026 19:28:39 +0200 Subject: [PATCH] Accept cancel and recreate of an import only as JSON requests Both took any POST, so a form of a sibling site that receives the SameSite=Lax session cookie could cancel or requeue an import. Like every other change of an import they now need application/json, which a form cannot send without a preflight; the player already sends it. Co-Authored-By: Claude Opus 5.5 --- src/library_import/handlers.rs | 69 +++++++++++++++++++++++++++++----- 1 file changed, 59 insertions(+), 10 deletions(-) diff --git a/src/library_import/handlers.rs b/src/library_import/handlers.rs index a1965be..a8a98cc 100644 --- a/src/library_import/handlers.rs +++ b/src/library_import/handlers.rs @@ -192,12 +192,20 @@ struct PlaylistPath { playlist_id: String, } -async fn recreate(services: &Services, user_id: i64, path: &PlaylistPath) -> Response { +async fn recreate( + services: &Services, + user_id: i64, + path: &PlaylistPath, + request: &Request, +) -> Response { let (Some(import_id), Some(playlist_id)) = (positive_id(&path.id), positive_id(&path.playlist_id)) else { return not_found(); }; + if let Err(rejection) = require_json(request) { + return rejection.respond(); + } let pool = services.pool().await?; match store::recreate_target(&pool, user_id, import_id, playlist_id).await { Ok(RecreateOutcome::Requested { queued }) => { @@ -293,21 +301,28 @@ impl Rejection { } } -/// Reads a JSON body of at most [`MAX_REQUEST_BYTES`]. Only `application/json` -/// is accepted: with SameSite=Lax cookies a cross-site form cannot send it. -async fn read_json(request: Request) -> Result { +/// Only `application/json` is accepted for a change: with SameSite=Lax +/// cookies a form of another site cannot send it without a preflight. +fn require_json(request: &Request) -> Result<(), Rejection> { let is_json = request .headers() .get(cot::http::header::CONTENT_TYPE) .and_then(|value| value.to_str().ok()) .and_then(|value| value.split(';').next()) .is_some_and(|value| value.trim().eq_ignore_ascii_case("application/json")); - if !is_json { - return Err(Rejection::new( + if is_json { + Ok(()) + } else { + Err(Rejection::new( StatusCode::UNSUPPORTED_MEDIA_TYPE, "expected application/json", - )); + )) } +} + +/// Reads a JSON body of at most [`MAX_REQUEST_BYTES`], see [`require_json`]. +async fn read_json(request: Request) -> Result { + require_json(&request)?; let Ok(bytes) = request .into_body() .into_bytes_limited(MAX_REQUEST_BYTES) @@ -1265,11 +1280,12 @@ impl App for ImportApp { move |auth_ctx: auth::AuthContext, session: Session, db: Database, - Path(path): Path| { + Path(path): Path, + request: Request| { let services = services.clone(); async move { let user = require_user!(auth_ctx, session, db); - recreate(&services, user.id, &path).await + recreate(&services, user.id, &path, &request).await } }, ) @@ -1278,13 +1294,17 @@ impl App for ImportApp { move |auth_ctx: auth::AuthContext, session: Session, db: Database, - Path(path): Path| { + Path(path): Path, + request: Request| { let services = services.clone(); async move { let user = require_user!(auth_ctx, session, db); let Some(id) = import_id(&path) else { return not_found(); }; + if let Err(rejection) = require_json(&request) { + return rejection.respond(); + } match store::request_cancel(&services.pool().await?, user.id, id).await { Ok(CancelOutcome::Cancelled) => { TokenVault::shared().forget_import(id); @@ -1441,6 +1461,35 @@ mod tests { ); } + /// Cancel and recreate take no body but still refuse what a form can send. + #[test] + fn bodiless_changes_accept_only_json() { + let form = |content_type: Option<&str>| { + let mut request = cot::http::Request::builder() + .method("POST") + .uri("/api/imports/5/cancel"); + if let Some(content_type) = content_type { + request = request.header("Content-Type", content_type); + } + request.body(Body::empty()).unwrap() + }; + for content_type in [ + None, + Some("application/x-www-form-urlencoded"), + Some("multipart/form-data; boundary=x"), + Some("text/plain"), + ] { + let rejection = require_json(&form(content_type)).unwrap_err(); + assert_eq!( + rejection.status, + StatusCode::UNSUPPORTED_MEDIA_TYPE, + "{content_type:?}" + ); + } + assert!(require_json(&form(Some("application/json"))).is_ok()); + assert!(require_json(&form(Some("Application/JSON; charset=utf-8"))).is_ok()); + } + fn export_request(selection: &[&str]) -> ImportRequest { ImportRequest { provider: "spotify".into(),