|
| 1 | +package logs |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +// Official modelcontextprotocol/registry server names are namespace/name |
| 13 | +// (e.g. "io.github.evidai/polymarket-guard"). The per-server log filename is |
| 14 | +// derived from the server name, so an unsanitized "/" would turn |
| 15 | +// "server-io.github.evidai/polymarket-guard.log" into a nested directory |
| 16 | +// (server-io.github.evidai/) instead of a single flat log file (MCP-1111). |
| 17 | +func TestServerLogFilename_SanitizesPathSeparators(t *testing.T) { |
| 18 | + cases := []struct { |
| 19 | + name string |
| 20 | + server string |
| 21 | + expected string |
| 22 | + }{ |
| 23 | + {"plain", "github", "server-github.log"}, |
| 24 | + {"namespaced slash", "io.github.evidai/polymarket-guard", "server-io.github.evidai_polymarket-guard.log"}, |
| 25 | + {"windows backslash", "ns\\name", "server-ns_name.log"}, |
| 26 | + {"colon", "host:1234", "server-host_1234.log"}, |
| 27 | + {"spaces and slashes", "a b/c", "server-a_b_c.log"}, |
| 28 | + } |
| 29 | + for _, tc := range cases { |
| 30 | + t.Run(tc.name, func(t *testing.T) { |
| 31 | + got := serverLogFilename(tc.server) |
| 32 | + assert.Equal(t, tc.expected, got) |
| 33 | + // A sanitized filename must never contain an OS path separator. |
| 34 | + assert.NotContains(t, got, "/") |
| 35 | + assert.NotContains(t, got, "\\") |
| 36 | + assert.Equal(t, filepath.Base(got), got, "sanitized filename must be a single path element") |
| 37 | + }) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// Regression: creating a logger for a namespaced (slash-bearing) server name |
| 42 | +// must produce a single flat log file, not a nested directory, and the tail |
| 43 | +// reader must round-trip the same raw name back to that file. |
| 44 | +func TestCreateUpstreamServerLogger_NamespacedNameFlatFile(t *testing.T) { |
| 45 | + // Use os.MkdirTemp (not t.TempDir) with a best-effort cleanup: the lumberjack |
| 46 | + // writer keeps the log file handle open for the lifetime of the logger, and |
| 47 | + // Windows cannot remove an open file. t.TempDir's cleanup asserts RemoveAll |
| 48 | + // succeeds and would fail the test on Windows; a non-asserting cleanup mirrors |
| 49 | + // the existing TestE2E_LogRotation pattern. |
| 50 | + logDir, err := os.MkdirTemp("", "mcpproxy-logtest-*") |
| 51 | + require.NoError(t, err) |
| 52 | + t.Cleanup(func() { _ = os.RemoveAll(logDir) }) |
| 53 | + const serverName = "io.github.evidai/polymarket-guard" |
| 54 | + |
| 55 | + cfg := DefaultLogConfig() |
| 56 | + cfg.LogDir = logDir |
| 57 | + cfg.EnableFile = true |
| 58 | + cfg.EnableConsole = false |
| 59 | + |
| 60 | + logger, err := CreateUpstreamServerLogger(cfg, serverName) |
| 61 | + require.NoError(t, err) |
| 62 | + logger.Info("hello from polymarket-guard") |
| 63 | + _ = logger.Sync() |
| 64 | + |
| 65 | + // The flat file exists directly in logDir. |
| 66 | + flatPath := filepath.Join(logDir, "server-io.github.evidai_polymarket-guard.log") |
| 67 | + _, err = os.Stat(flatPath) |
| 68 | + require.NoError(t, err, "expected a single flat log file at %s", flatPath) |
| 69 | + |
| 70 | + // No nested directory was created from the "/" in the server name. |
| 71 | + nestedDir := filepath.Join(logDir, "server-io.github.evidai") |
| 72 | + _, err = os.Stat(nestedDir) |
| 73 | + assert.True(t, os.IsNotExist(err), "no nested directory should be created from a namespaced server name") |
| 74 | + |
| 75 | + // The tail reader resolves the same raw name back to the flat file. |
| 76 | + lines, err := ReadUpstreamServerLogTail(cfg, serverName, 10) |
| 77 | + require.NoError(t, err) |
| 78 | + require.NotEmpty(t, lines, "tail reader must round-trip the namespaced server name to its flat log file") |
| 79 | + assert.Contains(t, lines[len(lines)-1], "hello from polymarket-guard") |
| 80 | + |
| 81 | + // The exported helper (used by the out-of-package read sites in internal/server |
| 82 | + // and cmd/mcpproxy) MUST resolve a namespaced name to the same flat file the |
| 83 | + // writer created — otherwise daemon/CLI log reads 404 for slash-bearing names |
| 84 | + // (the read-site divergence Codex flagged on PR #604). |
| 85 | + assert.Equal(t, flatPath, filepath.Join(logDir, ServerLogFilename(serverName)), |
| 86 | + "ServerLogFilename must resolve to the writer's flat file for a namespaced name") |
| 87 | +} |
0 commit comments