httpd: Return 404 for missing repos and objects

Requesting a repo the node doesn't have, or a commit, blob or path that
doesn't exist, returned 500 on the raw and git routes. Asking for a
directory as a file, or the reverse, did the same on the JSON API too.

`read_blob` now uses `blob_at` from radicle, which gives us the
not-found error for directories.
This commit is contained in:
Yorgos Saslis
2026-11-25 16:19:16 +01:00
committed by Rūdolfs Ošiņš
parent 7241e790ba
commit b7977fa4d8
7 changed files with 132 additions and 14 deletions
+6
View File
@@ -2,6 +2,12 @@
## Unreleased
### radicle-httpd
- **404 for missing repos and objects**: Raw, git and API routes return 404
instead of 500 for repos the node doesn't have, missing commits, blobs and
paths, and paths of the wrong type
## radicle-httpd + radicle-search 0.31.0
This release defines a backward compatibility policy in Radicle Explorer for
@@ -5,6 +5,8 @@ use serde_json::json;
use radicle::git::raw::ErrorExt;
use crate::error::surf_not_found_message;
/// Errors relating to the API backend.
#[derive(Debug, thiserror::Error)]
pub enum Error {
@@ -108,18 +110,17 @@ pub enum Error {
impl IntoResponse for Error {
fn into_response(self) -> Response {
let message = self.to_string();
let surf_not_found = match &self {
Error::Surf(e) => surf_not_found_message(e),
_ => None,
};
let (status, msg) = match self {
Error::NotFound => (StatusCode::NOT_FOUND, None),
Error::CobStore(e @ radicle::cob::store::Error::NotFound(_, _)) => {
(StatusCode::NOT_FOUND, Some(e.to_string()))
}
Error::Crypto(msg) => (StatusCode::BAD_REQUEST, Some(msg.to_string())),
Error::Surf(radicle_surf::Error::Git(e)) if e.is_not_found() => {
(StatusCode::NOT_FOUND, Some(e.message().to_owned()))
}
Error::Surf(radicle_surf::Error::Directory(
e @ radicle_surf::fs::error::Directory::PathNotFound(_),
)) => (StatusCode::NOT_FOUND, Some(e.to_string())),
Error::Surf(_) if surf_not_found.is_some() => (StatusCode::NOT_FOUND, surf_not_found),
Error::Git2(e) if e.is_not_found() => {
(StatusCode::NOT_FOUND, Some(e.message().to_owned()))
}
@@ -1076,12 +1076,7 @@ fn read_blob(
commit: Oid,
path: &str,
) -> Result<BlobData, Error> {
let tree = storage.backend.find_commit(commit.into())?.tree()?;
let entry = tree.get_path(std::path::Path::new(path))?;
let blob = entry
.to_object(&storage.backend)?
.into_blob()
.map_err(|_| radicle::git::raw::Error::from_str("path does not point to a blob"))?;
let blob = storage.blob_at(commit, path)?;
if blob.content().len() > MAX_BODY_LIMIT {
return Ok(BlobData::TooLarge);
@@ -2407,6 +2402,38 @@ mod routes {
assert_eq!(body["cobs"]["issues"]["open"], json!(1));
}
#[tokio::test]
async fn test_repos_commit_not_in_repo() {
let tmp = tempfile::tempdir().unwrap();
let app = super::router(seed(tmp.path()));
let missing = "1111111111111111111111111111111111111111";
for path in [
format!("/repos/{RID}/tree/{missing}/dir1"),
format!("/repos/{RID}/blob/{missing}/README"),
format!("/repos/{RID}/blob/{HEAD}/dir1"),
format!("/repos/{RID}/tree/{HEAD}/README"),
] {
let response = get(&app, &path).await;
assert_eq!(response.status(), StatusCode::NOT_FOUND, "{path}");
}
for path in [
format!("/repos/{RID}/commits/{missing}"),
format!("/repos/{RID}/tree/{missing}/"),
] {
let response = get(&app, path).await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
assert_eq!(
response.json().await,
json!({
"error": format!("object not found - no match for id ({missing})"),
"code": 404
})
);
}
}
#[tokio::test]
async fn test_repos_blob() {
let tmp = tempfile::tempdir().unwrap();
@@ -3,6 +3,9 @@ use std::process::ExitStatus;
use axum::http;
use axum::response::{IntoResponse, Response};
use radicle::git::raw::ErrorExt;
use radicle_surf::fs::error::Directory;
/// Errors relating to the Git backend.
#[derive(Debug, thiserror::Error)]
pub enum GitError {
@@ -53,6 +56,8 @@ impl GitError {
GitError::ServiceUnavailable(_) => http::StatusCode::SERVICE_UNAVAILABLE,
GitError::Id(_) => http::StatusCode::NOT_FOUND,
GitError::NotFound => http::StatusCode::NOT_FOUND,
GitError::Storage(err) if err.is_not_found() => http::StatusCode::NOT_FOUND,
GitError::Repository(err) if err.is_not_found() => http::StatusCode::NOT_FOUND,
_ => http::StatusCode::INTERNAL_SERVER_ERROR,
}
}
@@ -115,11 +120,34 @@ impl RawError {
match self {
RawError::BadRequest => http::StatusCode::BAD_REQUEST,
RawError::SurfFile(_) | RawError::NotFound => http::StatusCode::NOT_FOUND,
RawError::Surf(err) if surf_not_found_message(err).is_some() => {
http::StatusCode::NOT_FOUND
}
RawError::Git(err) if err.is_not_found() => http::StatusCode::NOT_FOUND,
RawError::Storage(err) if err.is_not_found() => http::StatusCode::NOT_FOUND,
RawError::Repository(err) if err.is_not_found() => http::StatusCode::NOT_FOUND,
_ => http::StatusCode::INTERNAL_SERVER_ERROR,
}
}
}
pub(crate) fn surf_not_found_message(err: &radicle_surf::Error) -> Option<String> {
match err {
radicle_surf::Error::Git(err) => git_not_found_message(err),
radicle_surf::Error::Directory(
err @ (Directory::PathNotFound(_) | Directory::InvalidType(..)),
) => Some(err.to_string()),
radicle_surf::Error::ToCommit(err) => err
.downcast_ref::<radicle_surf::Error>()
.and_then(surf_not_found_message),
_ => None,
}
}
fn git_not_found_message(err: &radicle::git::raw::Error) -> Option<String> {
err.is_not_found().then(|| err.message().to_owned())
}
impl IntoResponse for RawError {
fn into_response(self) -> Response {
tracing::error!("{}", self);
+17 -1
View File
@@ -210,7 +210,7 @@ mod routes {
use axum_listener::DualAddr;
use radicle::identity::RepoId;
use crate::test::{self, get, get_with_headers, RID};
use crate::test::{self, get, get_with_headers, RID, RID_NOT_IN_STORAGE};
#[tokio::test]
async fn test_info_request() {
@@ -318,4 +318,20 @@ mod routes {
let response = get(&app, "/heartwood.git/info/refs").await;
assert_eq!(response.status(), StatusCode::OK);
}
#[tokio::test]
async fn test_info_refs_repo_not_in_storage() {
let tmp = tempfile::tempdir().unwrap();
let ctx = test::seed(tmp.path());
let app = super::router(ctx.profile().to_owned(), Arc::new(HashMap::new())).layer(
MockConnectInfo(DualAddr::Tcp(SocketAddr::from(([0, 0, 0, 0], 8080)))),
);
let response = get(
&app,
format!("/{RID_NOT_IN_STORAGE}.git/info/refs?service=git-upload-pack"),
)
.await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
}
}
+40 -1
View File
@@ -397,7 +397,7 @@ mod routes {
use axum::http::StatusCode;
use crate::test::{self, get, HEAD, RID, RID_PRIVATE};
use crate::test::{self, get, HEAD, RID, RID_NOT_IN_STORAGE, RID_PRIVATE};
use radicle::storage::ReadStorage;
#[tokio::test]
@@ -421,6 +421,45 @@ mod routes {
assert_eq!(response.status(), StatusCode::NOT_FOUND);
}
#[tokio::test]
async fn test_file_handler_repo_not_in_storage() {
let tmp = tempfile::tempdir().unwrap();
let ctx = test::seed(tmp.path());
let app = super::router(ctx.profile().to_owned(), Arc::new(HashMap::new()));
let response = get(&app, format!("/{RID_NOT_IN_STORAGE}/head/README")).await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
let response = get(&app, format!("/{RID_NOT_IN_STORAGE}/{HEAD}/README")).await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
let response = get(&app, format!("/{RID_NOT_IN_STORAGE}/archive/master.tar.gz")).await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
}
#[tokio::test]
async fn test_object_not_in_repo() {
let tmp = tempfile::tempdir().unwrap();
let ctx = test::seed(tmp.path());
let app = super::router(ctx.profile().to_owned(), Arc::new(HashMap::new()));
let missing = "1111111111111111111111111111111111111111";
let response = get(&app, format!("/{RID}/{missing}/README")).await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
let response = get(&app, format!("/{RID}/blobs/{missing}")).await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
let response = get(&app, format!("/{RID}/{HEAD}/does-not-exist")).await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
let response = get(&app, format!("/{RID}/{HEAD}/dir1")).await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
let response = get(&app, format!("/{RID}/head/dir1")).await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
}
#[tokio::test]
async fn test_archive_tar_zst() {
let tmp = tempfile::tempdir().unwrap();
@@ -29,6 +29,7 @@ use crate::api::Context;
pub const RID: &str = "rad:z4FucBZHZMCsxTyQE1dfE2YR59Qbp";
pub const RID_PRIVATE: &str = "rad:zLuTzcmoWMcdK37xqArS8eckp9vK";
pub const RID_NOT_IN_STORAGE: &str = "rad:zWzu5sgdan7wuErGDRz1u4JTFEF7";
pub const HEAD: &str = "e8c676b9e3b42308dc9d218b70faa5408f8e58ca";
pub const PARENT: &str = "ee8d6a29304623a78ebfa5eeed5af674d0e58f83";
pub const INITIAL_COMMIT: &str = "f604ce9fd5b7cc77b7609beda45ea8760bee78f7";