mirror of
https://github.com/usememos/memos.git
synced 2026-11-19 10:29:11 +01:00
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:
@@ -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.")
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user