From a5bc30bfaaaec9ca3e7d2bc418d9cdebdf51bdf6 Mon Sep 17 00:00:00 2001 From: lotherk Date: Sun, 22 Nov 2026 12:29:28 +0000 Subject: [PATCH] fix(cgi): match pages host via SERVER_NAME fallback CGI servers are not required to set HTTP_HOST. Fall back to SERVER_NAME:SERVER_PORT, omitting 80 and 443, and document the pages host contract. Also omit the redundant Status header on 200 file responses. Assisted-by: OpenCode:DeepSeek-V4.1-Flash --- README.md | 14 ++++++++------ cgi/gotea.c | 21 ++++++++++++++++++++- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 3621f2ec7b..89541e0ed2 100644 --- a/README.md +++ b/README.md @@ -111,12 +111,14 @@ GOTEA_PAGES_ALLOW=/etc/gotea/pages.allow \ ./cgi/gotea-cgi ``` -`GOTEA_PAGES_HOST` must match `HTTP_HOST` exactly; other hosts get 404. Only -allowlisted repositories are served, and the pages host never exposes the main -UI. Keep private repositories out of the allowlist: the publisher has no -visibility check. Optional publisher settings are `GOTEA_PAGES_BRANCH` (default -`pages`), `GOTEA_PAGES_THEME`, `GOTEA_PAGES_SUBDIR`, and `GOTEA_PAGES_STRICT=1` -to fail the push when publishing fails. +`GOTEA_PAGES_HOST` must match `HTTP_HOST` exactly. Without `HTTP_HOST`, the CGI +falls back to `SERVER_NAME:SERVER_PORT` (ports 80 and 443 are omitted). Other +hosts get 404. Only allowlisted repositories are served, and the pages host +never exposes the main UI. Keep private repositories out of the allowlist: the +publisher has no visibility check. Optional publisher settings are +`GOTEA_PAGES_BRANCH` (default `pages`), `GOTEA_PAGES_THEME`, +`GOTEA_PAGES_SUBDIR`, and `GOTEA_PAGES_STRICT=1` to fail the push when +publishing fails. bb is vendored under `third_party/bb` (MIT, upstream commit in `third_party/bb/NOTICE`), so a checkout builds without an installed bb. diff --git a/cgi/gotea.c b/cgi/gotea.c index 95214ebc08..06b6359f52 100644 --- a/cgi/gotea.c +++ b/cgi/gotea.c @@ -1862,6 +1862,24 @@ done: return 0; } +static const char * +request_host(char *buf, size_t len) +{ + const char *host = getenv("HTTP_HOST"); + const char *name, *port; + + if (host != NULL && *host != '\0') + return host; + name = env_or("SERVER_NAME", ""); + port = getenv("SERVER_PORT"); + if (port != NULL && *port != '\0' && strcmp(port, "80") != 0 && + strcmp(port, "443") != 0 && strchr(name, ':') == NULL) { + snprintf(buf, len, "%s:%s", name, port); + return buf; + } + return name; +} + static int is_action(const char *s) { @@ -1907,6 +1925,7 @@ main(void) { const char *method, *decoded_env, *palette_file; const char *pages_host, *http_host; + char hostbuf[PATHBUF]; char *decoded = NULL, *repo_path = NULL, *repo_name = NULL; char *path = NULL; struct path p; @@ -1936,7 +1955,7 @@ main(void) pages_host = getenv("GOTEA_PAGES_HOST"); if (pages_host != NULL && *pages_host != '\0') { - http_host = env_or("HTTP_HOST", env_or("SERVER_NAME", "")); + http_host = request_host(hostbuf, sizeof(hostbuf)); if (strcasecmp(http_host, pages_host) != 0) { b_init(&body); g_pages_mode = 1;