From bb09eb4f2807d7bd13b2ea897f15fe0ea0e2f2ea Mon Sep 17 00:00:00 2001 From: Su <202412200713@nuist.edu.cn> Date: Mon, 26 Oct 2026 21:05:32 +0800 Subject: [PATCH] fix(server): keep well-known 404s out of the frontend cache policy (#6497) Co-authored-by: suzhiguo7 --- server/frontend/frontend.go | 5 +++++ server/frontend/frontend_test.go | 36 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/server/frontend/frontend.go b/server/frontend/frontend.go index a29cd5046..2be538b87 100644 --- a/server/frontend/frontend.go +++ b/server/frontend/frontend.go @@ -94,6 +94,11 @@ func shouldSkipFrontendStatic(requestPath string) bool { } return hasPathPrefix(requestPath, "/api") || hasPathPrefix(requestPath, "/file") || + // RFC 8615 reserves this namespace for protocol metadata. The SPA + // fallback already refuses it (see shouldServeFrontendHTML), but the + // 404 that results would otherwise inherit the static asset cache + // policy and be stored by clients and CDNs for an hour. + hasPathPrefix(requestPath, "/.well-known") || requestPath == "/memos.api" || strings.HasPrefix(requestPath, "/memos.api.") } diff --git a/server/frontend/frontend_test.go b/server/frontend/frontend_test.go index 90f9f32b5..b87566b7f 100644 --- a/server/frontend/frontend_test.go +++ b/server/frontend/frontend_test.go @@ -165,6 +165,42 @@ func TestFrontendService_HiddenPathsDoNotFallbackToIndex(t *testing.T) { require.Contains(t, rec.Header().Get("Content-Type"), "text/html") } +// TestFrontendService_DiscoveryRoutesSkipFrontendCacheHeaders pins the cache +// policy for well-known URIs. The SPA fallback already refuses them, but the +// resulting 404 used to inherit the static asset policy, so clients and CDNs +// could store an absent discovery endpoint for an hour. +func TestFrontendService_DiscoveryRoutesSkipFrontendCacheHeaders(t *testing.T) { + ctx := context.Background() + testStore := teststore.NewTestingStore(ctx, t) + + e := http.NewServeMux() + NewFrontendService(&profile.Profile{}, testStore).Serve(ctx, e, http.HandlerFunc(httphandler.NotFound)) + + for _, requestPath := range []string{ + "/.well-known/oauth-protected-resource", + "/.well-known/oauth-protected-resource/mcp", + "/.well-known/oauth-authorization-server", + "/.well-known/security.txt", + } { + req := httptest.NewRequest(http.MethodGet, requestPath, nil) + rec := httptest.NewRecorder() + e.ServeHTTP(rec, req) + + require.Equal(t, http.StatusNotFound, rec.Code, requestPath) + require.Empty(t, rec.Header().Get("Cache-Control"), requestPath) + require.Empty(t, rec.Header().Get("Pragma"), requestPath) + require.Empty(t, rec.Header().Get("Expires"), requestPath) + } + + // SPA routes keep the frontend HTML policy, so the exclusion stays narrow. + req := httptest.NewRequest(http.MethodGet, "/memos/abc", nil) + rec := httptest.NewRecorder() + e.ServeHTTP(rec, req) + + require.Equal(t, http.StatusOK, rec.Code) + require.Equal(t, frontendHTMLCacheControl, rec.Header().Get("Cache-Control")) +} + func TestFrontendService_SkipsDynamicRoutes(t *testing.T) { ctx := context.Background() testStore := teststore.NewTestingStore(ctx, t)