fix(server): keep well-known 404s out of the frontend cache policy (#6497)

Co-authored-by: suzhiguo7 <suzhiguo7@users.noreply.github.com>
This commit is contained in:
Su
2026-10-26 21:05:32 +08:00
committed by GitHub
parent c2bab22460
commit bb09eb4f28
2 changed files with 41 additions and 0 deletions
+5
View File
@@ -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.")
}
+36
View File
@@ -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)