Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

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

Subb09eb4f2807
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)