Skip to content

Commit 1b20c2f

Browse files
committed
httpd: Return 404 for unknown archive refs
/raw/<rid>/archive/<ref> passed the git2 error from resolve_reference_from_short_name straight through, so a typo in the branch name or a commit SHA in place of a ref yielded a 500. Map ErrorCode::NotFound to 404 like we already do for the object lookup. Signed-off-by: Jörg Thalheim <joerg@thalheim.io>
1 parent 51edb93 commit 1b20c2f

1 file changed

Lines changed: 26 additions & 8 deletions

File tree

  • crates/radicle-httpd/src

‎crates/radicle-httpd/src/raw.rs‎

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,23 +208,28 @@ async fn archive_by_committish(
208208
let project = doc.project()?;
209209
let repo_name = project.name();
210210

211+
let not_found = |e: radicle::git::raw::Error| {
212+
if e.code() == ErrorCode::NotFound {
213+
Error::NotFound
214+
} else {
215+
Error::Git(e)
216+
}
217+
};
218+
211219
let oid = match committish {
212220
Committish::Oid(oid) => oid,
213221
Committish::Ref(refname) => repo
214222
.backend
215223
.resolve_reference_from_short_name(refname)
216-
.map(|reference| reference.target())?
224+
.map_err(not_found)?
225+
.target()
217226
.ok_or(Error::NotFound)?
218227
.into(),
219228
};
220229

221-
if let Err(e) = repo.backend.find_object(oid.into(), None) {
222-
return Err(if e.code() == ErrorCode::NotFound {
223-
Error::NotFound
224-
} else {
225-
Error::Git(e)
226-
});
227-
}
230+
repo.backend
231+
.find_object(oid.into(), None)
232+
.map_err(not_found)?;
228233

229234
// Build a prefix for the archive, which includes the
230235
// refname (if one was given):
@@ -392,6 +397,19 @@ mod routes {
392397
assert_eq!(response.status(), StatusCode::NOT_FOUND);
393398
}
394399

400+
#[tokio::test]
401+
async fn test_archive_unknown_ref() {
402+
let tmp = tempfile::tempdir().unwrap();
403+
let ctx = test::seed(tmp.path());
404+
let app = super::router(ctx.profile().to_owned(), Arc::new(HashMap::new()));
405+
406+
let response = get(&app, format!("/{RID}/archive/master.tar.gz")).await;
407+
assert_eq!(response.status(), StatusCode::OK);
408+
409+
let response = get(&app, format!("/{RID}/archive/does-not-exist.tar.gz")).await;
410+
assert_eq!(response.status(), StatusCode::NOT_FOUND);
411+
}
412+
395413
#[tokio::test]
396414
async fn test_alias_resolution() {
397415
let tmp = tempfile::tempdir().unwrap();

0 commit comments

Comments
 (0)