1
Fork 0
mirror of https://github.com/thegeneralist01/archivr synced 2026-12-04 18:56:44 +01:00

fix: reject bare local-path locators on captures

core classifies any existing path as a local file, so a USER could capture and
read back arbitrary server files with a locator lacking the file:// prefix.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
TheGeneralist 2026-10-09 12:06:25 +02:00
parent 884788319e
commit 4dc7f7ee27
Signed by: thegeneralist01
SSH key fingerprint: SHA256:pp9qddbCNmVNoSjevdvQvM5z0DHN7LTa8qBMbcMq/R4
3 changed files with 25 additions and 0 deletions

View file

@ -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 {

View file

@ -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();

View file

@ -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<std::path::PathBuf> = 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");