diff --git a/pkgs/id/crates/id-web/src/routes.rs b/pkgs/id/crates/id-web/src/routes.rs index c2ac75efee..c1664e3bce 100644 --- a/pkgs/id/crates/id-web/src/routes.rs +++ b/pkgs/id/crates/id-web/src/routes.rs @@ -1068,6 +1068,16 @@ fn safe_blob_content_type(filename: &str) -> (&'static str, bool) { } } +/// Reduce a multipart upload filename to a plain file name (#10585). +/// +/// A client can send a path (`../../x.png`, `C:\fakepath\x.png`) or a +/// padded name; uploads are stored as plain names, so everything before +/// the final path separator is dropped and the remainder trimmed. The +/// result is validated against the canonical-name rule before use. +fn normalize_upload_filename(name: &str) -> &str { + name.rsplit(['/', '\\']).next().unwrap_or(name).trim() +} + /// Reconcile an uploaded file's name with its validated content type /// (#908): a name whose extension disagrees with the type the upload /// was validated as gets that extension replaced (or appended when it @@ -3399,6 +3409,201 @@ mod upload_router_tests { assert_eq!((oks, conflicts), (1, 1)); } + /// POST a JSON `{ "name": ... }` body to a file route. + async fn post_name(router: Router, uri: &str, name: &str) -> (StatusCode, String) { + let response = router + .oneshot( + Request::builder() + .method("POST") + .uri(uri) + .header("content-type", "application/json") + .body(Body::from(serde_json::json!({ "name": name }).to_string())) + .unwrap(), + ) + .await + .unwrap(); + let status = response.status(); + let bytes = to_bytes(response.into_body(), usize::MAX).await.unwrap(); + (status, String::from_utf8_lossy(&bytes).into_owned()) + } + + const NON_CANONICAL_NAMES: [&str; 4] = [ + "a/../b.md", + "../escape.md", + "/rooted.md", + "doc.md.archive.1234567890", + ]; + + // #10549: create holds web names to the canonical rule. A padded name + // is normalized to its trimmed form; rooted/traversal names and the + // reserved archive namespace are refused before any tag write. + #[tokio::test] + async fn create_rejects_non_canonical_names_and_trims_padding() { + let (router, state, _dir, _endpoint, _docs) = router_with_state().await; + + let (status, body) = post_name(router.clone(), "/api/new", " padded.md ").await; + assert_eq!(status, StatusCode::OK, "{body}"); + assert!( + upload_name_owner(&state.store, "padded.md") + .await + .unwrap() + .is_some() + ); + assert!( + upload_name_owner(&state.store, " padded.md ") + .await + .unwrap() + .is_none() + ); + + for name in NON_CANONICAL_NAMES { + let (status, body) = post_name(router.clone(), "/api/new", name).await; + assert_eq!(status, StatusCode::BAD_REQUEST, "{name}: {body}"); + assert!( + upload_name_owner(&state.store, name) + .await + .unwrap() + .is_none(), + "{name} must not be stored" + ); + } + } + + // #10549: save refuses the same non-canonical shapes before it + // publishes, archives, or writes any tag; a valid name still saves. + #[tokio::test] + async fn save_rejects_non_canonical_names() { + let (router, state, _dir, _endpoint, _docs) = router_with_state().await; + let doc = serde_json::json!({ + "type": "doc", + "content": [{"type": "paragraph", "content": [{"type": "text", "text": "hi"}]}], + }); + let save = |name: &str| { + let router = router.clone(); + let body = serde_json::json!({ + "doc_id": iroh_blobs::Hash::from_bytes([42; 32]).to_string(), + "name": name, + "doc": doc.clone(), + }) + .to_string(); + async move { + let response = router + .oneshot( + Request::builder() + .method("POST") + .uri("/api/save") + .header("content-type", "application/json") + .body(Body::from(body)) + .unwrap(), + ) + .await + .unwrap(); + response.status() + } + }; + + for name in NON_CANONICAL_NAMES { + let status = save(name).await; + assert_eq!(status, StatusCode::BAD_REQUEST, "{name}"); + assert!( + upload_name_owner(&state.store, name) + .await + .unwrap() + .is_none(), + "{name} must not be stored" + ); + } + + assert_eq!(save("notes.md").await, StatusCode::OK); + assert!( + upload_name_owner(&state.store, "notes.md") + .await + .unwrap() + .is_some() + ); + } + + // #10573: soft delete and restore hold their name to the canonical + // rule before the `deleted` tag is written, and normalize padding to + // the trimmed (stored) name. + #[tokio::test] + async fn delete_and_restore_reject_non_canonical_names() { + let (router, state, _dir, _endpoint, _docs) = router_with_state().await; + let (status, body) = post_name(router.clone(), "/api/new", "notes.md").await; + assert_eq!(status, StatusCode::OK, "{body}"); + + for name in NON_CANONICAL_NAMES { + for uri in ["/api/delete", "/api/restore"] { + let (status, body) = post_name(router.clone(), uri, name).await; + assert_eq!(status, StatusCode::BAD_REQUEST, "{uri} {name}: {body}"); + let tags = state + .tag_store + .get_by_key(&state.tag_store.global, name.as_bytes(), b"deleted") + .await + .unwrap(); + assert!(tags.is_empty(), "{uri} {name} wrote a deleted tag"); + } + } + + let (status, body) = post_name(router.clone(), "/api/delete", " notes.md ").await; + assert_eq!(status, StatusCode::OK, "{body}"); + assert!(body.contains("\"name\":\"notes.md\""), "body: {body}"); + let tags = state + .tag_store + .get_by_key(&state.tag_store.global, b"notes.md", b"deleted") + .await + .unwrap(); + assert_eq!(tags.len(), 1, "padded delete marks the trimmed name"); + + let (status, body) = post_name(router.clone(), "/api/restore", " notes.md ").await; + assert_eq!(status, StatusCode::OK, "{body}"); + let tags = state + .tag_store + .get_by_key(&state.tag_store.global, b"notes.md", b"deleted") + .await + .unwrap(); + assert!(tags.is_empty(), "padded restore clears the trimmed name"); + } + + // #10585: a multipart filename carrying a path or padding is stored as + // its plain, canonical file name (drop the leading path, trim), never + // verbatim. + #[tokio::test] + async fn upload_normalizes_path_shaped_and_padded_filenames() { + let (router, state, _dir, _endpoint, _docs) = router_with_state().await; + for (sent, stored) in [ + ("../../x.png", "x.png"), + ("/abs.png", "abs.png"), + ("C:\\fakepath\\ie.png", "ie.png"), + (" padded.png ", "padded.png"), + ] { + let (status, body) = upload( + router.clone(), + multipart_body_named(sent, "image/png", b"\x89PNG\r\n\x1a\nbytes"), + ) + .await; + assert_eq!(status, StatusCode::OK, "{sent}: {body}"); + assert!( + body.contains(&format!("\"name\":\"{stored}\"")), + "{sent}: {body}" + ); + assert!( + upload_name_owner(&state.store, stored) + .await + .unwrap() + .is_some(), + "{sent} stored as {stored}" + ); + assert!( + upload_name_owner(&state.store, sent) + .await + .unwrap() + .is_none(), + "{sent} stored verbatim" + ); + } + } + // #1549: a second upload under the same filename with different // bytes must not silently un-name the first: the second is stored // uniquified, both stay listed and resolvable, and the first name diff --git a/pkgs/id/crates/id-web/src/routes/file_handlers.rs b/pkgs/id/crates/id-web/src/routes/file_handlers.rs index 612445e2c5..0b7925ac17 100644 --- a/pkgs/id/crates/id-web/src/routes/file_handlers.rs +++ b/pkgs/id/crates/id-web/src/routes/file_handlers.rs @@ -2,6 +2,39 @@ use super::*; +/// Response for a file name refused by the canonical-name rule. +fn name_rejection_response(err: &FileOpError) -> Response { + match err { + FileOpError::EmptyName => { + (StatusCode::BAD_REQUEST, "File name cannot be empty").into_response() + } + FileOpError::InvalidName(name) => ( + StatusCode::BAD_REQUEST, + format!("Invalid file name: {name}"), + ) + .into_response(), + FileOpError::ReservedArchiveName(name) => ( + StatusCode::BAD_REQUEST, + format!("File name is reserved for archives: {name}"), + ) + .into_response(), + _ => (StatusCode::BAD_REQUEST, "Invalid file name").into_response(), + } +} + +/// Apply the canonical-name rule to a web request's file name. +/// +/// The web is a forgiving surface, so the caller's name is trimmed first; +/// the stored name is that trimmed result. What remains is then held to +/// the same rule every `FileOps` entry point enforces (#10549, #10573): +/// rooted or drive-prefixed names, empty/`.`/`..` components, padded +/// names, and the reserved archive namespace are refused with a 4xx +/// instead of entering the tag/metadata namespace. +fn canonical_web_name(name: &mut String) -> Result<(), Response> { + *name = name.trim().to_owned(); + FileOps::validate_name(name).map_err(|err| name_rejection_response(&err)) +} + /// Save the current editor document to the blob store. /// /// Converts the `ProseMirror` JSON to the appropriate format based on filename, @@ -11,8 +44,11 @@ use super::*; /// Rate-limited: rejects saves within the cooldown period (default 5s) per file. pub(super) async fn save_handler( State(state): State, - Json(req): Json, + Json(mut req): Json, ) -> Response { + if let Err(response) = canonical_web_name(&mut req.name) { + return response; + } tracing::info!( "[routes] save_handler: doc_id={}, name={}", req.doc_id, @@ -211,13 +247,12 @@ pub(super) async fn save_handler( /// adds it as a blob, and creates a tag for it. pub(super) async fn new_file_handler( State(state): State, - Json(req): Json, + Json(mut req): Json, ) -> Response { - tracing::info!("[routes] new_file_handler: name={}", req.name); - - if req.name.trim().is_empty() { - return (StatusCode::BAD_REQUEST, "File name cannot be empty").into_response(); + if let Err(response) = canonical_web_name(&mut req.name) { + return response; } + tracing::info!("[routes] new_file_handler: name={}", req.name); // #8407: creating a name that is already in use must conflict, not // silently replace the existing file's content with an empty blob. @@ -447,11 +482,21 @@ pub(super) async fn upload_handler( } // Get filename from the field, or generate one. A supplied name is - // reconciled with the validated content type (#908) so it cannot - // masquerade as a different type than the bytes were checked as. + // reduced to its plain file-name form (#10585: clients can send + // `../../x.png` or `C:\fakepath\x.png`), reconciled with the validated + // content type (#908) so it cannot masquerade as a different type than + // the bytes were checked as, and then held to the canonical-name rule + // every `FileOps` entry point enforces. let ext = mime_to_extension(&content_type); - let filename = match field.file_name().map(ToOwned::to_owned) { - Some(name) if !name.trim().is_empty() => reconcile_upload_filename(&name, ext), + let filename = match field.file_name().map(normalize_upload_filename) { + Some(name) if !name.is_empty() => { + let filename = reconcile_upload_filename(name, ext); + if let Err(err) = FileOps::validate_name(&filename) { + tracing::info!("[routes] Rejected upload filename: {err}"); + return name_rejection_response(&err); + } + filename + } _ => { let ts = SystemTime::now() .duration_since(UNIX_EPOCH) @@ -1166,13 +1211,12 @@ pub(super) async fn download_handler(Json(req): Json) -> Respon /// Request body for soft-deleting or hard-deleting a file. pub(super) async fn delete_handler( State(state): State, - Json(req): Json, + Json(mut req): Json, ) -> Response { - tracing::info!("[routes] delete_handler: name={}", req.name); - - if req.name.trim().is_empty() { - return (StatusCode::BAD_REQUEST, "File name cannot be empty").into_response(); + if let Err(response) = canonical_web_name(&mut req.name) { + return response; } + tracing::info!("[routes] delete_handler: name={}", req.name); // Verify the file exists match state.store.tags().get(&req.name).await { @@ -1217,13 +1261,12 @@ pub(super) async fn delete_handler( /// Restore a soft-deleted file by removing the "deleted" tag. pub(super) async fn restore_handler( State(state): State, - Json(req): Json, + Json(mut req): Json, ) -> Response { - tracing::info!("[routes] restore_handler: name={}", req.name); - - if req.name.trim().is_empty() { - return (StatusCode::BAD_REQUEST, "File name cannot be empty").into_response(); + if let Err(response) = canonical_web_name(&mut req.name) { + return response; } + tracing::info!("[routes] restore_handler: name={}", req.name); // Verify the file exists, distinguishing an absent file from a lookup error. match state.store.tags().get(&req.name).await {