From a5ac6bf52ff2351396faa96f53a0cc5d20de3bd2 Mon Sep 17 00:00:00 2001 From: lotherk Date: Sun, 22 Nov 2026 12:30:49 +0000 Subject: [PATCH] fix(cgi): normalize SCRIPT_NAME when mounted at root A CGI mounted at / produced scheme-relative // links. Strip trailing slashes from SCRIPT_NAME, emit root-relative links, and cover the root mount in the pages tests. Assisted-by: OpenCode:DeepSeek-V4.1-Flash --- cgi/gotea.c | 20 ++++++++++++++++---- cgi/tests/run.sh | 9 +++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/cgi/gotea.c b/cgi/gotea.c index 06b6359f52..326e3d1020 100644 --- a/cgi/gotea.c +++ b/cgi/gotea.c @@ -234,8 +234,14 @@ static void href_top(struct buf *b, const char *suffix) { b_puts(b, ""); } @@ -1928,16 +1934,22 @@ main(void) char hostbuf[PATHBUF]; char *decoded = NULL, *repo_path = NULL, *repo_name = NULL; char *path = NULL; + char script_buf[PATHBUF]; struct path p; struct repo *repos; struct palette pal; - size_t nrepos = 0, action_idx = 0; + size_t nrepos = 0, action_idx = 0, script_len; struct buf body; int status = 200; method = env_or("REQUEST_METHOD", "GET"); g_head_only = strcmp(method, "HEAD") == 0; - g_script = env_or("SCRIPT_NAME", "/cgi-bin/gotea"); + snprintf(script_buf, sizeof(script_buf), "%s", + env_or("SCRIPT_NAME", "/cgi-bin/gotea")); + script_len = strlen(script_buf); + while (script_len > 0 && script_buf[script_len - 1] == '/') + script_buf[--script_len] = '\0'; + g_script = script_buf; g_root = env_or("GOTEA_REPO_ROOT", "./repositories"); g_got = env_or("GOT_BIN", "got"); palette_defaults(&pal); diff --git a/cgi/tests/run.sh b/cgi/tests/run.sh index ef0f558081..c706376d35 100755 --- a/cgi/tests/run.sh +++ b/cgi/tests/run.sh @@ -197,6 +197,15 @@ pages_host=$(pages_run / wrong.test) expect_contains "$pages_host" 'Status: 404' "pages: host isolation" echo "PASS pages host" +pages_rootmount=$(env -i PATH=/usr/bin:/bin HOME="$tmp" \ + GOTEA_PAGES_HOST=pages.test HTTP_HOST=pages.test \ + GOTEA_PAGES_ROOT="$tmp/pages" GOTEA_PAGES_ALLOW="$tmp/allow" \ + SCRIPT_NAME=/ REQUEST_METHOD=GET PATH_INFO=/ "$bin") +expect_contains "$pages_rootmount" 'href="/owner/site/"' \ + "pages: root mount links" +expect_no "$pages_rootmount" 'href="//' "pages: no scheme-relative links" +echo "PASS pages root mount" + "$cgi/pages/install-hook.sh" "$tmp/repos/owner/site.git" "$tmp/pages" \ "$tmp/allow" "$bb" pages >/dev/null hookout=$(printf '%040d %s refs/heads/pages\n' 0 "$bb_rev" |