From a7f09ffe101395a3b02ef122345a9a2f88658e83 Mon Sep 17 00:00:00 2001 From: Andre Devay Date: Sun, 29 Nov 2026 15:25:06 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Ignore=20invalid=20If-Modified-S?= =?UTF-8?q?ince=20in=20catalogd=20metas=20handler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit checkIfModifiedSince called httpError when the If-Modified-Since header could not be parsed, which wrote a 500 response, and then returned condNone, so handleV1Metas went on to stream the catalog into the same response. Clients got a 500 status whose body was "500 Internal Server Error" followed by the catalog content. RFC 9110 section 13.1.3 says a recipient must ignore If-Modified-Since when its value is not a valid HTTP-date, and that is what the upstream net/http code this file was copied from does (and what the /api/v1/all endpoint already does through http.ServeContent). Drop the httpError call and the now unused ResponseWriter parameter. Add handler tests for a 304 on an unchanged catalog, a 200 when it changed, and a 200 with the plain catalog body for an invalid date. Signed-off-by: Andre Devay --- internal/catalogd/server/handlers_test.go | 79 +++++++++++++++++++ .../server/http_preconditions_check.go | 6 +- 2 files changed, 82 insertions(+), 3 deletions(-) diff --git a/internal/catalogd/server/handlers_test.go b/internal/catalogd/server/handlers_test.go index 3b5ec604c1..788d9c0043 100644 --- a/internal/catalogd/server/handlers_test.go +++ b/internal/catalogd/server/handlers_test.go @@ -4,12 +4,16 @@ import ( "bytes" "context" "encoding/json" + "io" "io/fs" "net/http" "net/http/httptest" "net/url" + "os" + "path/filepath" "strings" "testing" + "time" "github.com/graphql-go/graphql" "go.uber.org/mock/gomock" @@ -263,3 +267,78 @@ func TestAllowedMethodsHandler_POSTOnlyForGraphQL(t *testing.T) { t.Error("POST should be allowed for GraphQL endpoint at router level") } } + +func TestHandleV1Metas_IfModifiedSince(t *testing.T) { + const catalogContent = `{"schema":"olm.package","name":"foo"}` + "\n" + modTime := time.Date(2025, time.January, 2, 3, 4, 5, 0, time.UTC) + + for _, tc := range []struct { + name string + ifModifiedSince string + expectedCode int + expectedBody string + }{ + { + name: "not modified since the given time", + ifModifiedSince: modTime.Format(http.TimeFormat), + expectedCode: http.StatusNotModified, + expectedBody: "", + }, + { + name: "modified since the given time", + ifModifiedSince: modTime.Add(-time.Hour).Format(http.TimeFormat), + expectedCode: http.StatusOK, + expectedBody: catalogContent, + }, + { + // RFC 9110 section 13.1.3: a recipient MUST ignore the + // If-Modified-Since header field if the field value is not a + // valid HTTP-date. + name: "invalid date is ignored", + ifModifiedSince: "not-a-date", + expectedCode: http.StatusOK, + expectedBody: catalogContent, + }, + } { + t.Run(tc.name, func(t *testing.T) { + catalogFile, err := os.Create(filepath.Join(t.TempDir(), "catalog.jsonl")) + if err != nil { + t.Fatal(err) + } + if _, err := catalogFile.WriteString(catalogContent); err != nil { + t.Fatal(err) + } + if _, err := catalogFile.Seek(0, io.SeekStart); err != nil { + t.Fatal(err) + } + if err := os.Chtimes(catalogFile.Name(), modTime, modTime); err != nil { + t.Fatal(err) + } + catalogStat, err := os.Stat(catalogFile.Name()) + if err != nil { + t.Fatal(err) + } + + ctrl := gomock.NewController(t) + rootURL, _ := url.Parse("http://localhost/") + store := mockcatalogdserver.NewMockCatalogStore(ctrl) + store.EXPECT().GetCatalogData("test-catalog").Return(catalogFile, catalogStat, nil) + + handlers := server.NewCatalogHandlers(store, nil, rootURL, server.MetasHandlerEnabled, server.GraphQLQueriesDisabled) + handler := handlers.Handler() + + req := httptest.NewRequest(http.MethodGet, "/test-catalog/api/v1/metas", nil) + req.Header.Set("If-Modified-Since", tc.ifModifiedSince) + w := httptest.NewRecorder() + + handler.ServeHTTP(w, req) + + if w.Code != tc.expectedCode { + t.Errorf("Expected status %d, got %d", tc.expectedCode, w.Code) + } + if w.Body.String() != tc.expectedBody { + t.Errorf("Expected body %q, got %q", tc.expectedBody, w.Body.String()) + } + }) + } +} diff --git a/internal/catalogd/server/http_preconditions_check.go b/internal/catalogd/server/http_preconditions_check.go index ceabad2d30..acef7944f1 100644 --- a/internal/catalogd/server/http_preconditions_check.go +++ b/internal/catalogd/server/http_preconditions_check.go @@ -44,7 +44,7 @@ func checkPreconditions(w http.ResponseWriter, r *http.Request, modtime time.Tim return true } case condNone: - if checkIfModifiedSince(r, w, modtime) == condFalse { + if checkIfModifiedSince(r, modtime) == condFalse { writeNotModified(w) return true } @@ -52,14 +52,14 @@ func checkPreconditions(w http.ResponseWriter, r *http.Request, modtime time.Tim return false } -func checkIfModifiedSince(r *http.Request, w http.ResponseWriter, modtime time.Time) condResult { +func checkIfModifiedSince(r *http.Request, modtime time.Time) condResult { ims := r.Header.Get("If-Modified-Since") if ims == "" || isZeroTime(modtime) { return condTrue } t, err := parseTime(ims) if err != nil { - httpError(w, err) + // RFC 9110 section 13.1.3: an invalid HTTP-date must be ignored. return condNone } // The Last-Modified header truncates sub-second precision so