forked from Furumi/furumi-web
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
da28e775b3
commit
2c17c9e4df
1 file changed
+59
-10
@@ -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<T: DeserializeOwned>(request: Request) -> Result<T, Rejection> {
|
||||
/// 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<T: DeserializeOwned>(request: Request) -> Result<T, Rejection> {
|
||||
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<PlaylistPath>| {
|
||||
Path(path): Path<PlaylistPath>,
|
||||
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<ImportPath>| {
|
||||
Path(path): Path<ImportPath>,
|
||||
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(),
|
||||
|
||||
Reference in new issue
Block a user