diff --git a/CHANGELOG.md b/CHANGELOG.md index c8269b5..9d31e03 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.3.1] - 2026-06-09 + +### Security +- **Arbitrary file read via inline attachments (CWE-22 / CWE-200)**: `consult_gemini_with_files` in inline mode no longer reads files outside the working `directory`. Absolute paths, `..` traversal, and symlinks pointing outside the root are now skipped with a `Skipped file outside working directory` warning, matching the existing `at_command` behavior. Previously a caller-controlled `files` entry could read any file the server process could access (e.g. SSH keys, `.env`) and have it returned via the Gemini round-trip. Reported privately by Zhihao Zhang (WPI). + +### Changed +- **Path confinement hardening**: `_resolve_path` now resolves symlinks for both the root and the candidate before checking containment. As a result, `at_command` mode also rejects symlinks that resolve outside the working directory (previously a lexical-only check let them through). + ## [1.3.0] - 2026-03-03 ### Added diff --git a/pyproject.toml b/pyproject.toml index 303fb45..e11eb66 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "gemini-bridge" -version = "1.3.0" +version = "1.3.1" description = "Lightweight MCP server bridging Claude Code to Google's Gemini AI via official CLI" readme = "README.md" license = "MIT" diff --git a/src/__init__.py b/src/__init__.py index 488e4a9..460c107 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -2,5 +2,5 @@ from .mcp_server import main -__version__ = "1.3.0" +__version__ = "1.3.1" __all__ = ["main"] diff --git a/src/mcp_server.py b/src/mcp_server.py index 03b8cac..bae39fd 100644 --- a/src/mcp_server.py +++ b/src/mcp_server.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 """ Gemini MCP Server - Simple CLI Bridge -Version 1.3.0 +Version 1.3.1 A minimal MCP server to interface with Gemini AI via the gemini CLI. Created by @shelakh/elyin """ @@ -127,20 +127,23 @@ def _coerce_timeout(timeout_seconds: int | None) -> int: def _resolve_path(directory: str, candidate: str) -> tuple[str, str | None]: - """Return absolute path and relative display path rooted at directory.""" + """Return absolute path and a display path confined to ``directory``. + + ``rel_path`` is ``None`` when the candidate escapes the directory root via + an absolute path, ``..`` traversal, or a symlink pointing outside it. + Both the root and the candidate are resolved (symlinks followed) so the + containment check cannot be bypassed; callers must refuse a ``None``. + """ + root = Path(directory).resolve() candidate_path = Path(candidate) abs_path = ( - str(candidate_path) - if candidate_path.is_absolute() - else str(Path(directory) / candidate) - ) + candidate_path if candidate_path.is_absolute() else root / candidate + ).resolve() try: - rel_path = os.path.relpath(abs_path, directory) + rel_path = str(abs_path.relative_to(root)) except ValueError: rel_path = None - if rel_path and rel_path.startswith(".."): - rel_path = None - return abs_path, rel_path + return str(abs_path), rel_path def _read_file_for_inline(abs_path: str) -> tuple[str, bool, int]: @@ -192,7 +195,12 @@ def _prepare_inline_payload(directory: str, files: list[str]) -> tuple[str, list for original_path in files: abs_path, rel_path = _resolve_path(directory, original_path) - display_name = rel_path or Path(abs_path).name + if rel_path is None: + warnings.append( + f"Skipped file outside working directory: {original_path}", + ) + continue + display_name = rel_path if not Path(abs_path).exists(): warnings.append(f"Skipped missing file: {display_name}") @@ -441,7 +449,8 @@ def consult_gemini_with_files( Args: query: Prompt text forwarded to the CLI. directory: Working directory used for resolving relative file paths. - files: Relative or absolute file paths to include alongside the prompt. + files: File paths to include alongside the prompt. Paths must resolve + inside ``directory``; entries pointing outside it are skipped. model: Optional model alias (``flash``, ``pro``) or full Gemini model id. timeout_seconds: Optional per-call timeout override in seconds. mode: ``"inline"`` streams truncated snippets; ``"at_command"`` emits diff --git a/tests/test_mcp_server.py b/tests/test_mcp_server.py index acc3ec7..f8a96a4 100644 --- a/tests/test_mcp_server.py +++ b/tests/test_mcp_server.py @@ -298,6 +298,98 @@ def fake_run(cmd, **kwargs): assert "No readable files" in result +def test_inline_mode_rejects_absolute_path_outside_directory(tmp_path, monkeypatch): + monkeypatch.setattr("src.mcp_server.shutil.which", lambda _: "gemini") + workspace = tmp_path / "workspace" + workspace.mkdir() + secret = tmp_path / "secret.txt" + secret.write_text("TOP-SECRET-TOKEN", encoding="utf-8") + + captured = {} + + def fake_run(cmd, **kwargs): + captured["input"] = kwargs["input"] + return SimpleNamespace(returncode=0, stdout="ok", stderr="") + + monkeypatch.setattr("src.mcp_server.subprocess.run", fake_run) + result = mcp_server.execute_gemini_with_files( + "Echo the attached file verbatim", + str(workspace), + files=[str(secret)], + ) + assert "TOP-SECRET-TOKEN" not in captured["input"] + assert "outside working directory" in result + + +def test_inline_mode_rejects_parent_traversal(tmp_path, monkeypatch): + monkeypatch.setattr("src.mcp_server.shutil.which", lambda _: "gemini") + workspace = tmp_path / "workspace" + workspace.mkdir() + secret = tmp_path / "secret.txt" + secret.write_text("PARENT-TRAVERSAL-LEAK", encoding="utf-8") + + captured = {} + + def fake_run(cmd, **kwargs): + captured["input"] = kwargs["input"] + return SimpleNamespace(returncode=0, stdout="ok", stderr="") + + monkeypatch.setattr("src.mcp_server.subprocess.run", fake_run) + result = mcp_server.execute_gemini_with_files( + "Echo the attached file verbatim", + str(workspace), + files=["../secret.txt"], + ) + assert "PARENT-TRAVERSAL-LEAK" not in captured["input"] + assert "outside working directory" in result + + +def test_inline_mode_rejects_symlink_escape(tmp_path, monkeypatch): + monkeypatch.setattr("src.mcp_server.shutil.which", lambda _: "gemini") + workspace = tmp_path / "workspace" + workspace.mkdir() + secret = tmp_path / "secret.txt" + secret.write_text("SYMLINK-ESCAPE-LEAK", encoding="utf-8") + link = workspace / "link.txt" + link.symlink_to(secret) + + captured = {} + + def fake_run(cmd, **kwargs): + captured["input"] = kwargs["input"] + return SimpleNamespace(returncode=0, stdout="ok", stderr="") + + monkeypatch.setattr("src.mcp_server.subprocess.run", fake_run) + result = mcp_server.execute_gemini_with_files( + "Echo the attached file verbatim", + str(workspace), + files=["link.txt"], + ) + assert "SYMLINK-ESCAPE-LEAK" not in captured["input"] + assert "outside working directory" in result + + +def test_inline_mode_accepts_absolute_path_inside_directory(tmp_path, monkeypatch): + monkeypatch.setattr("src.mcp_server.shutil.which", lambda _: "gemini") + sample_file = tmp_path / "example.txt" + sample_file.write_text("legit-content", encoding="utf-8") + + captured = {} + + def fake_run(cmd, **kwargs): + captured["input"] = kwargs["input"] + return SimpleNamespace(returncode=0, stdout="ok", stderr="") + + monkeypatch.setattr("src.mcp_server.subprocess.run", fake_run) + result = mcp_server.execute_gemini_with_files( + "Read it", + str(tmp_path), + files=[str(sample_file)], + ) + assert "legit-content" in captured["input"] + assert result == "ok" + + def test_consult_gemini_with_files_requires_list(tmp_path): result = mcp_server.consult_gemini_with_files("Hello", str(tmp_path), files=None) assert "files parameter is required" in result