Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

fix: validate cached version timestamp types (#5351)

Harsh Kashyapc88ccf928fdd
diff --git a/fastmcp_slim/fastmcp/utilities/version_check.py b/fastmcp_slim/fastmcp/utilities/version_check.py
index 1b1c9178a..1db1121ff 100644
--- a/fastmcp_slim/fastmcp/utilities/version_check.py
+++ b/fastmcp_slim/fastmcp/utilities/version_check.py
@@ -38,8 +38,16 @@ def _read_cache(include_prereleases: bool = False) -> tuple[str | None, float]:
try:
data = json.loads(cache_path.read_text())
- return data.get("latest_version"), data.get("timestamp", 0)
- except (json.JSONDecodeError, OSError):
+ version = data.get("latest_version")
+ timestamp = data.get("timestamp", 0)
+ if (
+ not isinstance(version, str)
+ or isinstance(timestamp, bool)
+ or not isinstance(timestamp, int | float)
+ ):
+ return None, 0
+ return version, timestamp
+ except (json.JSONDecodeError, OSError, AttributeError):
return None, 0
diff --git a/tests/utilities/test_version_check.py b/tests/utilities/test_version_check.py
index 6198a5279..06069cee4 100644
--- a/tests/utilities/test_version_check.py
+++ b/tests/utilities/test_version_check.py
@@ -73,6 +73,21 @@ class TestReadCache:
assert version is None
assert timestamp == 0
+ @pytest.mark.parametrize("timestamp", ["invalid", None, [], {}, True])
+ def test_invalid_cache_timestamp_is_ignored(
+ self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch, timestamp: object
+ ):
+ cache_file = tmp_path / "version_cache.json"
+ cache_file.write_text(
+ json.dumps({"latest_version": "4.0.0", "timestamp": timestamp})
+ )
+ monkeypatch.setattr(
+ "fastmcp.utilities.version_check._get_cache_path",
+ lambda include_prereleases=False: cache_file,
+ )
+
+ assert _read_cache() == (None, 0)
+
class TestWriteCache:
def test_write_cache_creates_file(