From 63809b0d848156d8266ba23624b7ff64b556288d Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Tue, 31 Oct 2023 14:19:23 +0100 Subject: [PATCH 1/3] bug(gitlab): Sanitize the tarball name in get_repo_ref GitLab enforces that the tarball's *filename* does not contain any slashes: it replaces them with dashes. We need to do the same, so our redirects will point to something that exists. Signed-off-by: Gergely Nagy --- src/api/v1/gitlab/endpoints/get_repo_ref.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/api/v1/gitlab/endpoints/get_repo_ref.rs b/src/api/v1/gitlab/endpoints/get_repo_ref.rs index 1ab5a52..8a13813 100644 --- a/src/api/v1/gitlab/endpoints/get_repo_ref.rs +++ b/src/api/v1/gitlab/endpoints/get_repo_ref.rs @@ -21,9 +21,10 @@ pub async fn get_repo_ref( let git_ref_name = git_ref .strip_suffix(".tar.gz") .expect("couldn't strip .tar.gz suffix"); + let git_ref_dashed_name = git_ref_name.replace('/', "-"); let uri = format!( "https://{}/{}/{}/-/archive/{}/{}-{}.tar.gz", - host, user, repo, git_ref_name, repo, git_ref_name, + host, user, repo, git_ref_name, repo, git_ref_dashed_name, ); Redirect::to(&uri).into_response() } else { From cfa0f97448e22a82acdcc81ebeb59e4d654f1562 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Mon, 30 Oct 2023 21:06:04 +0100 Subject: [PATCH 2/3] bug(api): handle branch names with slashes in them When dealing with branches, don't capture just one slice of the path as the branch name, but every part of the path from that point on. This lets us easily deal with branches that have slashes in their names. Since the branch is always the last part of the URI, and the `/b/` or `/branch/` part routes things here anyway, capturing the rest of the path is safe. Only the path is captured, query strings are not, those aren't part of the routing. Fixes #19. Signed-off-by: Gergely Nagy --- src/api/v1/forgejo/routes.rs | 4 ++-- src/api/v1/github/routes.rs | 4 ++-- src/api/v1/gitlab/routes.rs | 4 ++-- src/api/v1/sourcehut/routes.rs | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/api/v1/forgejo/routes.rs b/src/api/v1/forgejo/routes.rs index db0713e..54201cb 100644 --- a/src/api/v1/forgejo/routes.rs +++ b/src/api/v1/forgejo/routes.rs @@ -9,8 +9,8 @@ use axum::{routing::get, Router}; pub fn get_routes() -> Router { Router::new() - .route("/:host/:user/:repo/b/:branch", get(get_repo_ref)) - .route("/:host/:user/:repo/branch/:branch", get(get_repo_ref)) + .route("/:host/:user/:repo/b/*branch", get(get_repo_ref)) + .route("/:host/:user/:repo/branch/*branch", get(get_repo_ref)) .route("/:host/:user/:repo/v/:version", get(get_repo_ref)) .route("/:host/:user/:repo/version/:version", get(get_repo_ref)) .route("/:host/:user/:repo/t/:version", get(get_repo_ref)) diff --git a/src/api/v1/github/routes.rs b/src/api/v1/github/routes.rs index e7b0d86..34c89b8 100644 --- a/src/api/v1/github/routes.rs +++ b/src/api/v1/github/routes.rs @@ -9,8 +9,8 @@ use axum::{routing::get, Router}; pub fn get_routes() -> Router { Router::new() - .route("/:user/:repo/b/:branch", get(get_repo_branch)) - .route("/:user/:repo/branch/:branch", get(get_repo_branch)) + .route("/:user/:repo/b/*branch", get(get_repo_branch)) + .route("/:user/:repo/branch/*branch", get(get_repo_branch)) .route("/:user/:repo/v/:version", get(get_repo_version)) .route("/:user/:repo/version/:version", get(get_repo_version)) .route("/:user/:repo/t/:version", get(get_repo_version)) diff --git a/src/api/v1/gitlab/routes.rs b/src/api/v1/gitlab/routes.rs index f273475..0e6307d 100644 --- a/src/api/v1/gitlab/routes.rs +++ b/src/api/v1/gitlab/routes.rs @@ -9,8 +9,8 @@ use axum::{routing::get, Router}; pub fn get_routes() -> Router { Router::new() - .route("/:host/:user/:repo/b/:branch", get(get_repo_ref)) - .route("/:host/:user/:repo/branch/:branch", get(get_repo_ref)) + .route("/:host/:user/:repo/b/*branch", get(get_repo_ref)) + .route("/:host/:user/:repo/branch/*branch", get(get_repo_ref)) .route("/:host/:user/:repo/v/:version", get(get_repo_ref)) .route("/:host/:user/:repo/version/:version", get(get_repo_ref)) .route("/:host/:user/:repo/t/:version", get(get_repo_ref)) diff --git a/src/api/v1/sourcehut/routes.rs b/src/api/v1/sourcehut/routes.rs index 320125d..12049b5 100644 --- a/src/api/v1/sourcehut/routes.rs +++ b/src/api/v1/sourcehut/routes.rs @@ -9,8 +9,8 @@ use super::endpoints::get_repo_ref; pub fn get_routes() -> Router { Router::new() - .route("/:host/:user/:repo/b/:version", get(get_repo_ref)) - .route("/:host/:user/:repo/branch/:version", get(get_repo_ref)) + .route("/:host/:user/:repo/b/*version", get(get_repo_ref)) + .route("/:host/:user/:repo/branch/*version", get(get_repo_ref)) .route("/:host/:user/:repo/v/:version", get(get_repo_ref)) .route("/:host/:user/:repo/version/:version", get(get_repo_ref)) .route("/:host/:user/:repo/t/:version", get(get_repo_ref)) From 9583f6c1700dc918fd497179d687b3c360261d72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christina=20S=C3=B8rensen?= Date: Tue, 31 Oct 2023 06:25:48 +0100 Subject: [PATCH 3/3] test(itest): add testing of slashes in branches --- justfile | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/justfile b/justfile index 13d8feb..d214dbb 100644 --- a/justfile +++ b/justfile @@ -214,6 +214,12 @@ itest: just run_test "http://localhost:3000/v1/next.forgejo.org/cafkafk/hello.tar.gz" just run_test "http://localhost:3000/v1/flakehub.com/cafkafk/hello/v/v0.0.1.tar.gz" + just run_test "http://localhost:3000/v1/codeberg/cafkafk/hello/b/a-/t/e/s/t/i/n/g/b/r/a/n/c/h-th@t-should-be-/ha/rd/to/d/e/a/l/wi/th.tar.gz" + just run_test "http://localhost:3000/v1/github/cafkafk/hello/b/a-/t/e/s/t/i/n/g/b/r/a/n/c/h-th@t-should-be-/ha/rd/to/d/e/a/l/wi/th.tar.gz" + just run_test "http://localhost:3000/v1/gitlab/gitlab.com/cafkafk/hello/b/a-/t/e/s/t/i/n/g/b/r/a/n/c/h-th@t-should-be-/ha/rd/to/d/e/a/l/wi/th.tar.gz" + just run_test "http://localhost:3000/v1/forgejo/next.forgejo.org/cafkafk/hello/b/a-/t/e/s/t/i/n/g/b/r/a/n/c/h-th@t-should-be-/ha/rd/to/d/e/a/l/wi/th.tar.gz" + just run_test "http://localhost:3000/v1/git.madhouse-project.org/cafkafk/hello/b/a-/t/e/s/t/i/n/g/b/r/a/n/c/h-th@t-should-be-/ha/rd/to/d/e/a/l/wi/th.tar.gz" + @echo "tests passsed :3" # Integration Testing of rime.cx (requires Nix) @@ -232,4 +238,6 @@ itest-live: just run_test "http://rime.cx/v1/next.forgejo.org/cafkafk/hello.tar.gz" just run_test "http://rime.cx/v1/flakehub.com/cafkafk/hello/v/v0.0.1.tar.gz" + just run_test "http://rime.cx/v1/git.madhouse-project.org/cafkafk/hello/b/a-/t/e/s/t/i/n/g/b/r/a/n/c/h-th@t-should-be-/ha/rd/to/d/e/a/l/wi/th.tar.gz" + @echo "tests passsed :3"