diff --git a/crates/archivr-core/src/capture.rs b/crates/archivr-core/src/capture.rs index 5823fe8..4891b11 100644 --- a/crates/archivr-core/src/capture.rs +++ b/crates/archivr-core/src/capture.rs @@ -419,6 +419,13 @@ fn expand_shorthand_to_url(path: &str, source: &Source) -> String { path.to_string() } +/// True when `locator` would be captured as a file on the local filesystem — +/// a `file://` URI or any bare path that exists. Callers that accept locators +/// from untrusted clients use this to refuse local reads. +pub fn locator_is_local_path(locator: &str) -> bool { + determine_source(locator) == Source::Local +} + // INFO: yt-dlp supports a lot of sites; so, when archiving (for example) a website, the user // -> should be asked whether they want to archive the whole website or just the video(s) on it. fn determine_source(path: &str) -> Source { diff --git a/crates/archivr-server/src/jobs.rs b/crates/archivr-server/src/jobs.rs index 489aecd..6481cc4 100644 --- a/crates/archivr-server/src/jobs.rs +++ b/crates/archivr-server/src/jobs.rs @@ -708,6 +708,16 @@ mod tests { let (s, _) = capture_locator(&f, &alice, &format!("file://{}", outside.display())).await; assert_eq!(s, StatusCode::BAD_REQUEST); + // A bare path (no file:// prefix) is classified as a local file by core and must + // be refused too: absolute, and relative to the server's cwd. + let (s, body) = capture_locator(&f, &alice, &outside.display().to_string()).await; + assert_eq!(s, StatusCode::BAD_REQUEST); + assert!(body["error"].as_str().unwrap().contains("local paths are not accepted")); + let (s, _) = capture_locator(&f, &alice, "/etc/hosts").await; + assert_eq!(s, StatusCode::BAD_REQUEST); + let (s, _) = capture_locator(&f, &alice, "Cargo.toml").await; + assert_eq!(s, StatusCode::BAD_REQUEST); + // Rejected requests must not leave job rows behind. { let conn = database::open_or_initialize(&f.archive_path).unwrap(); diff --git a/crates/archivr-server/src/routes.rs b/crates/archivr-server/src/routes.rs index 22c745f..400c212 100644 --- a/crates/archivr-server/src/routes.rs +++ b/crates/archivr-server/src/routes.rs @@ -1651,6 +1651,14 @@ async fn capture_handler( // Canonicalize both sides to prevent path-traversal via `..` components in the locator. // Same pattern as artifact serving. The staged file must already exist on disk // (it was written by upload_handler), so canonicalize() will resolve symlinks correctly. + // A bare path (absolute, or relative to the server's cwd) is classified as a local + // file by core, so the file:// check alone would let it through; staged uploads + // are always submitted as file:// locators, so bare paths have no legitimate use here. + if !locator.starts_with("file://") && capture::locator_is_local_path(&locator) { + return Err(ApiError::bad_request( + "local paths are not accepted as locators; upload the file and capture the returned file:// locator", + )); + } let staged_upload_path: Option = if locator.starts_with("file://") { let file_path = std::path::PathBuf::from(locator.trim_start_matches("file://")); let staging_dir = archive_paths.store_path.join("temp").join("uploads");