Skip to content

Commit

Permalink
Ensure referenced repository exists before executing route handler
Browse files Browse the repository at this point in the history
  • Loading branch information
w4 committed Jan 6, 2024
1 parent deb257b commit 7f95c54
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/database/schema/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ pub struct Repository<'a> {
pub type YokedRepository = Yoked<Repository<'static>>;

impl Repository<'_> {
pub fn exists<P: AsRef<Path>>(database: &sled::Db, path: P) -> bool {
database
.contains_key(TreePrefix::repository_id(path))
.unwrap_or_default()
}

pub fn fetch_all(database: &sled::Db) -> Result<BTreeMap<String, YokedRepository>> {
database
.scan_prefix([TreePrefix::Repository as u8])
Expand Down
18 changes: 18 additions & 0 deletions src/methods/repo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ where
let uri = uri_parts.into_iter().collect::<PathBuf>().clean();
let path = scan_path.join(&uri);

let db = request
.extensions()
.get::<sled::Db>()
.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));
Expand Down Expand Up @@ -161,6 +171,14 @@ impl Deref for RepositoryPath {

pub type Result<T, E = Error> = std::result::Result<T, E>;

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

impl From<Arc<anyhow::Error>> for Error {
Expand Down

0 comments on commit 7f95c54

Please sign in to comment.