From 8ecab78448dcbea8bdd727471f54dabb99094179 Mon Sep 17 00:00:00 2001 From: Bryce Kwon Date: Sat, 22 Aug 2026 10:34:55 -0700 Subject: Keep error pages out of the cache --- source/cgit.c | 5 ++--- source/ui-shared.c | 6 ++++++ tests/t0020-validate-cache.sh | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/source/cgit.c b/source/cgit.c index ac8922b..12981c2 100644 --- a/source/cgit.c +++ b/source/cgit.c @@ -82,9 +82,8 @@ static void isolate_git_environment(void) */ static NORETURN void die_routine(const char *msg, va_list params) { - // A page is rendered with stdout pointed at the cache file, so the - // message would otherwise be written there instead of to the visitor. - cache_abandon_fill(); + // The error page abandons any cache fill in progress, so the message + // reaches the visitor rather than the cache file stdout points at. cgit_vprint_error_page(400, "Bad request", msg, params); exit(0); } diff --git a/source/ui-shared.c b/source/ui-shared.c index 066c25d..05fa30b 100644 --- a/source/ui-shared.c +++ b/source/ui-shared.c @@ -10,6 +10,7 @@ #define USE_THE_REPOSITORY_VARIABLE +#include "cache.h" #include "cgit.h" #include "html.h" #include "shared.h" @@ -1293,6 +1294,11 @@ void cgit_print_error_page(int code, const char *msg, const char *fmt, ...) void cgit_vprint_error_page(int code, const char *msg, const char *fmt, va_list ap) { + // An error page describes a condition that may not outlast the slot + // holding it. A request naming a commit not yet pushed would cache its + // 404 under the never-expiring static ttl and keep serving it after + // the push, so the error is rendered straight to the visitor instead. + cache_abandon_fill(); ctx.page.status = code; ctx.page.statusmsg = msg; cgit_print_layout_start(); diff --git a/tests/t0020-validate-cache.sh b/tests/t0020-validate-cache.sh index 0cf7433..feb6275 100755 --- a/tests/t0020-validate-cache.sh +++ b/tests/t0020-validate-cache.sh @@ -154,4 +154,18 @@ test_expect_success 'an ordinary key still fills a slot' ' test_line_count = 1 key.slots ' +# An error page reports a condition the repository may grow out of, and a +# request pinned to an object id would cache it under the never-expiring +# static ttl. Asking for a commit that does not exist yet must therefore +# leave nothing behind, or the real page could never appear once it does. +test_expect_success 'an error page leaves no slot behind' ' + rm -rf cache3 && mkdir cache3 && + CGIT_CONFIG="$PWD/bigkeyrc" \ + QUERY_STRING="url=bigpage/commit/&id=0123456789abcdef0123456789abcdef01234567" \ + cgit >error.out && + grep "Bad commit reference" error.out && + ls cache3 >error.slots && + test_line_count = 0 error.slots +' + test_done -- cgit v2.8.0