diff --git a/src/secfoo/mcp.py b/src/secfoo/mcp.py index a35b273..ba3fbe4 100644 --- a/src/secfoo/mcp.py +++ b/src/secfoo/mcp.py @@ -19,6 +19,7 @@ from __future__ import annotations import json +import os import re import shutil import subprocess @@ -176,7 +177,8 @@ def sync_cursor(servers: list[MCPServerConfig]) -> list[SyncResult]: if newly_added: root["mcpServers"] = existing - CURSOR_MCP_CONFIG_PATH.write_text(json.dumps(root, indent=2), encoding="utf-8") + # Same 0600 as claude-mcp-config.json: env and header values are secrets. + write_private_text(CURSOR_MCP_CONFIG_PATH, json.dumps(root, indent=2)) for name in newly_added: proc = _run_cli(["agent", "mcp", "enable", name]) if proc.returncode != 0: @@ -184,6 +186,13 @@ def sync_cursor(servers: list[MCPServerConfig]) -> list[SyncResult]: if result.server_name == name: result.ok = False result.message += f" (enable failed: {proc.stderr.strip()})" + elif os.name == "posix": + # Nothing new to write, but an older secfoo may have left our + # servers' secrets in a 0644 file: tighten it on this re-run too. + try: + CURSOR_MCP_CONFIG_PATH.chmod(0o600) + except OSError: + pass # e.g. not our file; the sync itself still succeeded return results diff --git a/tests/test_mcp.py b/tests/test_mcp.py index 7f2ba29..1f279b5 100644 --- a/tests/test_mcp.py +++ b/tests/test_mcp.py @@ -1,7 +1,9 @@ from __future__ import annotations import json +import stat import subprocess +import sys import pytest @@ -203,6 +205,37 @@ def test_sync_cursor_keeps_other_top_level_keys(tmp_path, monkeypatch): assert "Atlassian-Rovo-MCP" in payload["mcpServers"] +@pytest.mark.skipif(sys.platform == "win32", reason="POSIX permission bits") +@pytest.mark.parametrize("existing_mode", [None, 0o644], ids=["new-file", "existing-0644"]) +def test_sync_cursor_writes_config_owner_only(tmp_path, monkeypatch, existing_mode): + # The file holds each server's env and headers (bearer keys, API tokens). + cursor_path = tmp_path / "mcp.json" + if existing_mode is not None: + cursor_path.write_text(json.dumps({"mcpServers": {}})) + cursor_path.chmod(existing_mode) + monkeypatch.setattr(mcp, "CURSOR_MCP_CONFIG_PATH", cursor_path) + monkeypatch.setattr(mcp.subprocess, "run", lambda cmd, **kw: subprocess.CompletedProcess(cmd, 0, "", "")) + + results = mcp.sync_cursor([REMOTE_SERVER]) + assert results[0].ok is True + assert stat.S_IMODE(cursor_path.stat().st_mode) == 0o600 + assert json.loads(cursor_path.read_text())["mcpServers"]["example"]["headers"] == {"Authorization": "Bearer abc"} + + +@pytest.mark.skipif(sys.platform == "win32", reason="POSIX permission bits") +def test_sync_cursor_tightens_an_existing_file_even_when_nothing_is_added(tmp_path, monkeypatch): + # A file an older secfoo wrote at 0644 must not stay world-readable just + # because every server is already configured on the re-run. + cursor_path = tmp_path / "mcp.json" + cursor_path.write_text(json.dumps({"mcpServers": {"example": {"headers": {"Authorization": "Bearer abc"}}}})) + cursor_path.chmod(0o644) + monkeypatch.setattr(mcp, "CURSOR_MCP_CONFIG_PATH", cursor_path) + + results = mcp.sync_cursor([REMOTE_SERVER]) + assert results == [mcp.SyncResult("example", True, "already configured, skipped")] + assert stat.S_IMODE(cursor_path.stat().st_mode) == 0o600 + + # ---------- dispatch ----------