|
| 1 | +"""Installation provenance checks for the Telegram MCP server. |
| 2 | +
|
| 3 | +The PyPI distribution name ``telegram-mcp`` is currently occupied by an |
| 4 | +unrelated project. This guard can only protect executions that reach this |
| 5 | +repository's code; it cannot run when a user launches the third-party package. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import json |
| 11 | +from dataclasses import dataclass |
| 12 | +from importlib import metadata |
| 13 | +from pathlib import Path |
| 14 | +from urllib.parse import unquote, urlparse |
| 15 | + |
| 16 | +DISTRIBUTION_NAME = "telegram-mcp" |
| 17 | + |
| 18 | + |
| 19 | +class UnsafeInstallationError(RuntimeError): |
| 20 | + """Raised when installed package metadata points at the wrong project.""" |
| 21 | + |
| 22 | + |
| 23 | +@dataclass(frozen=True) |
| 24 | +class DistributionIdentity: |
| 25 | + """Small, testable view of Python package metadata.""" |
| 26 | + |
| 27 | + name: str |
| 28 | + version: str |
| 29 | + authors: tuple[str, ...] = () |
| 30 | + maintainers: tuple[str, ...] = () |
| 31 | + urls: tuple[str, ...] = () |
| 32 | + summary: str = "" |
| 33 | + direct_url: str = "" |
| 34 | + |
| 35 | + @classmethod |
| 36 | + def from_distribution(cls, dist: metadata.Distribution) -> "DistributionIdentity": |
| 37 | + package_metadata = dist.metadata |
| 38 | + urls = tuple(package_metadata.get_all("Project-URL") or ()) |
| 39 | + homepage = package_metadata.get("Home-page") |
| 40 | + if homepage: |
| 41 | + urls += (homepage,) |
| 42 | + |
| 43 | + authors = tuple( |
| 44 | + value |
| 45 | + for value in ( |
| 46 | + package_metadata.get("Author"), |
| 47 | + package_metadata.get("Author-email"), |
| 48 | + ) |
| 49 | + if value |
| 50 | + ) |
| 51 | + maintainers = tuple( |
| 52 | + value |
| 53 | + for value in ( |
| 54 | + package_metadata.get("Maintainer"), |
| 55 | + package_metadata.get("Maintainer-email"), |
| 56 | + ) |
| 57 | + if value |
| 58 | + ) |
| 59 | + |
| 60 | + read_text = getattr(dist, "read_text", None) |
| 61 | + direct_url = read_text("direct_url.json") if callable(read_text) else "" |
| 62 | + |
| 63 | + return cls( |
| 64 | + name=package_metadata.get("Name") or getattr(dist, "name", DISTRIBUTION_NAME), |
| 65 | + version=package_metadata.get("Version") or dist.version, |
| 66 | + authors=authors, |
| 67 | + maintainers=maintainers, |
| 68 | + urls=urls, |
| 69 | + summary=package_metadata.get("Summary", ""), |
| 70 | + direct_url=direct_url, |
| 71 | + ) |
| 72 | + |
| 73 | + |
| 74 | +def _project_root_declares_distribution_name(path: Path) -> bool: |
| 75 | + pyproject_path = path / "pyproject.toml" |
| 76 | + if not pyproject_path.is_file(): |
| 77 | + return False |
| 78 | + |
| 79 | + try: |
| 80 | + pyproject_text = pyproject_path.read_text(encoding="utf-8") |
| 81 | + except OSError: |
| 82 | + return False |
| 83 | + |
| 84 | + return f'name = "{DISTRIBUTION_NAME}"' in pyproject_text |
| 85 | + |
| 86 | + |
| 87 | +def _direct_url_json(direct_url: str) -> dict: |
| 88 | + if not direct_url: |
| 89 | + return {} |
| 90 | + |
| 91 | + try: |
| 92 | + direct_url_data = json.loads(direct_url) |
| 93 | + except json.JSONDecodeError: |
| 94 | + return {} |
| 95 | + |
| 96 | + return direct_url_data if isinstance(direct_url_data, dict) else {} |
| 97 | + |
| 98 | + |
| 99 | +def _direct_url_is_explicit_source_install(direct_url: str) -> bool: |
| 100 | + direct_url_data = _direct_url_json(direct_url) |
| 101 | + if not direct_url_data: |
| 102 | + return False |
| 103 | + |
| 104 | + raw_url = str(direct_url_data.get("url", "")).strip() |
| 105 | + if not raw_url: |
| 106 | + return False |
| 107 | + |
| 108 | + parsed_url = urlparse(raw_url) |
| 109 | + |
| 110 | + if parsed_url.scheme == "file": |
| 111 | + source_path = Path(unquote(parsed_url.path)).resolve() |
| 112 | + return _project_root_declares_distribution_name(source_path) |
| 113 | + |
| 114 | + vcs_info = direct_url_data.get("vcs_info") |
| 115 | + return isinstance(vcs_info, dict) and bool(vcs_info.get("vcs")) |
| 116 | + |
| 117 | + |
| 118 | +def _looks_like_explicit_source_install(identity: DistributionIdentity) -> bool: |
| 119 | + return _direct_url_is_explicit_source_install(identity.direct_url) |
| 120 | + |
| 121 | + |
| 122 | +def _format_unsafe_installation_message(identity: DistributionIdentity) -> str: |
| 123 | + authors = ", ".join(identity.authors) or "unknown" |
| 124 | + maintainers = ", ".join(identity.maintainers) or "unknown" |
| 125 | + urls = "; ".join(identity.urls) or "unknown" |
| 126 | + |
| 127 | + return ( |
| 128 | + "Refusing to start: the installed 'telegram-mcp' distribution was not " |
| 129 | + "installed from an explicit source checkout.\n" |
| 130 | + f"Detected distribution: name={identity.name!r}, version={identity.version!r}, " |
| 131 | + f"authors={authors!r}, maintainers={maintainers!r}, urls={urls!r}.\n" |
| 132 | + "The 'telegram-mcp' name on PyPI is currently owned by a different project. " |
| 133 | + "This guard requires a source checkout or installer-recorded direct " |
| 134 | + "git/file URL. Run this server from a cloned checkout or install a " |
| 135 | + "trusted repository explicitly with: " |
| 136 | + 'pip install "git+https://github.com/chigwell/telegram-mcp.git"' |
| 137 | + ) |
| 138 | + |
| 139 | + |
| 140 | +def assert_safe_distribution(distribution_name: str = DISTRIBUTION_NAME) -> None: |
| 141 | + """Abort when the installed distribution metadata is not this project. |
| 142 | +
|
| 143 | + Source checkouts that have not been installed as a distribution are allowed: |
| 144 | + ``uv --directory /path/to/telegram-mcp run main.py`` can run from source |
| 145 | + without package metadata. If metadata for ``telegram-mcp`` is present, it |
| 146 | + must come from an explicit git or file install recorded by the installer. |
| 147 | + """ |
| 148 | + |
| 149 | + try: |
| 150 | + dist = metadata.distribution(distribution_name) |
| 151 | + except metadata.PackageNotFoundError: |
| 152 | + return |
| 153 | + |
| 154 | + identity = DistributionIdentity.from_distribution(dist) |
| 155 | + if _looks_like_explicit_source_install(identity): |
| 156 | + return |
| 157 | + |
| 158 | + raise UnsafeInstallationError(_format_unsafe_installation_message(identity)) |
0 commit comments