From ddd97f4fa6f0d96342b4cf3eb1f76baf0488da70 Mon Sep 17 00:00:00 2001 From: Nabeel Hassan <60441195+nabeelbaghoor@users.noreply.github.com> Date: Sat, 21 Nov 2026 05:23:49 +0500 Subject: [PATCH 1/2] fix(mcp): write ~/.cursor/mcp.json owner-only (0600) --- src/secfoo/mcp.py | 3 ++- tests/test_mcp.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/secfoo/mcp.py b/src/secfoo/mcp.py index a35b273..6d1c6a3 100644 --- a/src/secfoo/mcp.py +++ b/src/secfoo/mcp.py @@ -176,7 +176,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: diff --git a/tests/test_mcp.py b/tests/test_mcp.py index 7f2ba29..4a54587 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,23 @@ 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"} + + # ---------- dispatch ---------- From ec2166b1c2767ba62e52d45cfa0df306e8fd02ab Mon Sep 17 00:00:00 2001 From: Satyanarayana Vuppala Date: Sat, 21 Nov 2026 08:26:06 +0000 Subject: [PATCH 2/2] fix(mcp): tighten an existing ~/.cursor/mcp.json even when nothing is added The file is only rewritten when a new server is added, so a user whose older secfoo left it at 0644 kept a world-readable file on every re-run where all servers were already configured -- the usual upgrade path. chmod it to 0600 in that case too (POSIX only; an OSError never fails the sync). Co-Authored-By: Claude Opus 5.5 --- src/secfoo/mcp.py | 8 ++++++++ tests/test_mcp.py | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/secfoo/mcp.py b/src/secfoo/mcp.py index 6d1c6a3..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 @@ -185,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 4a54587..1f279b5 100644 --- a/tests/test_mcp.py +++ b/tests/test_mcp.py @@ -222,6 +222,20 @@ def test_sync_cursor_writes_config_owner_only(tmp_path, monkeypatch, existing_mo 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 ----------