From 7f95c54933a0f1392f40614c6400c6c2e1a38b0d Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Sat, 06 Jan 2024 01:38:32 +0000 Subject: [PATCH] Ensure referenced repository exists before executing route handler --- Cargo.lock | 2 +- src/database/schema/repository.rs | 6 ++++++ src/methods/repo/mod.rs | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 236dd48..cae5da3 100644 --- a/Cargo.lock +++ a/Cargo.lock @@ -237,7 +237,7 @@ "heck", "proc-macro2", "quote", - "syn", + "syn 2.0.43", ] [[package]] diff --git a/src/database/schema/repository.rs b/src/database/schema/repository.rs index 8eb48e5..42b0f62 100644 --- a/src/database/schema/repository.rs +++ a/src/database/schema/repository.rs @@ -33,6 +33,12 @@ pub type YokedRepository = Yoked>; impl Repository<'_> { + pub fn exists>(database: &sled::Db, path: P) -> bool { + database + .contains_key(TreePrefix::repository_id(path)) + .unwrap_or_default() + } + pub fn fetch_all(database: &sled::Db) -> Result> { database .scan_prefix([TreePrefix::Repository as u8]) diff --git a/src/methods/repo/mod.rs b/src/methods/repo/mod.rs index 1b818eb..8bf8040 100644 --- a/src/methods/repo/mod.rs +++ a/src/methods/repo/mod.rs @@ -123,6 +123,16 @@ let uri = uri_parts.into_iter().collect::().clean(); let path = scan_path.join(&uri); + let db = request + .extensions() + .get::() + .expect("db extension missing"); + if path.as_os_str().is_empty() + || !crate::database::schema::repository::Repository::exists(db, &uri) + { + return RepositoryNotFound.into_response(); + } + request.extensions_mut().insert(ChildPath(child_path)); request.extensions_mut().insert(Repository(uri)); request.extensions_mut().insert(RepositoryPath(path)); @@ -160,6 +170,14 @@ } pub type Result = std::result::Result; + +pub struct RepositoryNotFound; + +impl IntoResponse for RepositoryNotFound { + fn into_response(self) -> Response { + (StatusCode::NOT_FOUND, "Repository not found").into_response() + } +} pub struct Error(anyhow::Error); -- rgit 0.1.3