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 hidden paths out of the SPA fallback

johnnyjoygh3eeac84ea277
diff --git a/server/frontend/frontend.go b/server/frontend/frontend.go
index 1efc05326..a29cd5046 100644
--- a/server/frontend/frontend.go
+++ b/server/frontend/frontend.go
@@ -121,8 +121,17 @@ func setFrontendCacheHeaders(w http.ResponseWriter, requestPath string) {
w.Header().Set("Cache-Control", cacheControl)
}
+// shouldServeFrontendHTML reports whether an unknown path is an SPA route
+// that loads index.html. Paths with a file extension are assets, and paths
+// with a hidden segment are never app routes: no username, memo UID, or fixed
+// route begins with a dot. This keeps the SPA out of "/.well-known/", which
+// RFC 8615 reserves for registered metadata that clients probe and expect a
+// 404 for when the server does not offer it, and out of probes like "/.git/".
func shouldServeFrontendHTML(requestPath string) bool {
- return requestPath == "/" || requestPath == "/index.html" || path.Ext(requestPath) == ""
+ if requestPath == "/" || requestPath == "/index.html" {
+ return true
+ }
+ return path.Ext(requestPath) == "" && !strings.Contains(requestPath, "/.")
}
func hasPathPrefix(requestPath, prefix string) bool {
diff --git a/server/frontend/frontend_test.go b/server/frontend/frontend_test.go
index 652717fbf..90f9f32b5 100644
--- a/server/frontend/frontend_test.go
+++ b/server/frontend/frontend_test.go
@@ -134,6 +134,37 @@ func TestFrontendService_MissingAssetDoesNotFallbackToIndex(t *testing.T) {
require.Equal(t, http.StatusNotFound, rec.Code)
}
+func TestFrontendService_HiddenPathsDoNotFallbackToIndex(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))
+
+ // OAuth Protected Resource Metadata discovery (RFC 9728) probes these
+ // paths and reads a 404 as "not advertised"; HTML would break the client.
+ for _, requestPath := range []string{
+ "/.well-known/oauth-protected-resource",
+ "/.well-known/oauth-protected-resource/mcp",
+ "/.well-known/oauth-authorization-server",
+ "/.git/config",
+ "/u/.hidden",
+ } {
+ req := httptest.NewRequest(http.MethodGet, requestPath, nil)
+ rec := httptest.NewRecorder()
+ e.ServeHTTP(rec, req)
+
+ require.Equal(t, http.StatusNotFound, rec.Code, requestPath)
+ require.NotContains(t, rec.Header().Get("Content-Type"), "text/html", requestPath)
+ }
+
+ req := httptest.NewRequest(http.MethodGet, "/memos/abc", nil)
+ rec := httptest.NewRecorder()
+ e.ServeHTTP(rec, req)
+ require.Equal(t, http.StatusOK, rec.Code)
+ require.Contains(t, rec.Header().Get("Content-Type"), "text/html")
+}
+
func TestFrontendService_SkipsDynamicRoutes(t *testing.T) {
ctx := context.Background()
testStore := teststore.NewTestingStore(ctx, t)
diff --git a/server/test/startup_test.go b/server/test/startup_test.go
index 9c53d7321..9337ed8da 100644
--- a/server/test/startup_test.go
+++ b/server/test/startup_test.go
@@ -318,6 +318,22 @@ func TestStartupServesEveryRegisteredRouter(t *testing.T) {
})
require.NotEqual(t, http.StatusNotFound, status, "mcp route should be mounted")
})
+
+ t.Run("oauth discovery is not advertised", func(t *testing.T) {
+ // MCP clients probe RFC 9728 metadata before trusting the endpoint.
+ // The MCP server authenticates with personal access tokens, so the
+ // well-known paths must 404 instead of falling back to the SPA.
+ for _, requestPath := range []string{"/.well-known/oauth-protected-resource", "/.well-known/oauth-protected-resource/mcp"} {
+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, inst.baseURL+requestPath, nil)
+ require.NoError(t, err)
+ resp, err := inst.client.Do(req)
+ require.NoError(t, err)
+ resp.Body.Close()
+
+ require.Equal(t, http.StatusNotFound, resp.StatusCode, requestPath)
+ require.NotContains(t, resp.Header.Get("Content-Type"), "text/html", requestPath)
+ }
+ })
}
// TestStartupFreshInstallRoundTrip covers the new-install path end to end: