Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/secfoo/mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from __future__ import annotations

import json
import os
import re
import shutil
import subprocess
Expand Down Expand Up @@ -176,14 +177,22 @@ 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:
for result in results:
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


Expand Down
33 changes: 33 additions & 0 deletions tests/test_mcp.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import annotations

import json
import stat
import subprocess
import sys

import pytest

Expand Down Expand Up @@ -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 ----------


Expand Down
Loading