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
This commit is contained in:
lotherk committed 2026-11-22 12:30:49 +00:00
1 parent a5bc30bfaa
commit a5ac6bf52f
2 files changed
+25 -4

No files matched your search

+16 -4
View File
@@ -234,8 +234,14 @@ static void
href_top(struct buf *b, const char *suffix)
{
b_puts(b, "<a href=\"");
b_url_path(b, g_script);
b_puts(b, suffix);
if (*g_script != '\0') {
b_url_path(b, g_script);
b_puts(b, suffix);
} else if (*suffix != '\0') {
b_puts(b, suffix);
} else {
b_putc(b, '/');
}
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);
+9
View File
@@ -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" |