Skip to content

Commit d52c223

Browse files
cliffhallclaude
andcommitted
time: accept only exact IANA timezone keys on every platform
On macOS, zoneinfo resolves a key such as europe/warsaw through the case-insensitive APFS tz database, so the server accepted it where Linux rejects it, and test_get_current_time_errors failed locally. Check each key against available_timezones() in the tool path and in the --local-timezone validation. Closes #5060 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 0763da6 commit d52c223

4 files changed

Lines changed: 48 additions & 4 deletions

File tree

‎src/time/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Requires MCP Python SDK 2.x (`mcp>=2.2,<3`).
2020
- `time` (string): Time in 24-hour format (HH:MM)
2121
- `target_timezone` (string): Target IANA timezone name
2222

23+
A timezone name, here and in `--local-timezone`, must be an exact IANA key, case included: `Europe/Warsaw` is accepted and `europe/warsaw` is rejected on every platform, including macOS, whose case-insensitive filesystem would otherwise resolve it.
24+
2325
## Installation
2426

2527
### Using uv (recommended)

‎src/time/src/mcp_server_time/server.py‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
# loop only; serving 2026-07-28 is #4853.
1010

1111
from datetime import datetime, timedelta
12+
from functools import cache
1213
from importlib.metadata import version
1314
from enum import Enum
1415
import json
1516
import sys
1617
from typing import Any
1718

18-
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
19+
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError, available_timezones
1920
from tzlocal import get_localzone_name # ← returns "Europe/Paris", etc.
2021

2122
import jsonschema
@@ -79,9 +80,30 @@ def get_local_tz(local_tz_override: str | None = None) -> ZoneInfo:
7980
return ZoneInfo("UTC")
8081

8182

83+
@cache
84+
def known_timezones() -> frozenset[str]:
85+
"""The exact IANA keys this host's tz database holds, read once."""
86+
return frozenset(available_timezones())
87+
88+
89+
def load_zoneinfo(timezone_name: str) -> ZoneInfo:
90+
"""`ZoneInfo(timezone_name)`, accepting only an exact IANA key.
91+
92+
zoneinfo looks a key up as a file path, so on a case-insensitive
93+
filesystem (macOS's APFS) `europe/warsaw` resolves to `Europe/Warsaw`
94+
while Linux rejects it (#5060). Checking the key against the database's
95+
own list makes every platform reject it. `ZoneInfo` runs first so a path
96+
or a directory keeps the error zoneinfo gives it.
97+
"""
98+
zone = ZoneInfo(timezone_name)
99+
if timezone_name not in known_timezones():
100+
raise ZoneInfoNotFoundError(f"No time zone found with key {timezone_name}")
101+
return zone
102+
103+
82104
def get_zoneinfo(timezone_name: str) -> ZoneInfo:
83105
try:
84-
return ZoneInfo(timezone_name)
106+
return load_zoneinfo(timezone_name)
85107
except Exception as e:
86108
raise MCPError(code=INVALID_PARAMS, message=f"Invalid timezone: {str(e)}")
87109

@@ -173,7 +195,7 @@ async def serve(local_timezone: str | None = None) -> None:
173195
# bad key (unknown name, a tzdata directory, a path, a null byte), and
174196
# its messages can carry a filesystem path, so none of them is shown.
175197
try:
176-
ZoneInfo(local_timezone)
198+
load_zoneinfo(local_timezone)
177199
except (ZoneInfoNotFoundError, ValueError, OSError):
178200
sys.exit(
179201
f"Error: invalid --local-timezone {local_timezone!r}: "

‎src/time/tests/test_protocol.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ async def test_empty_local_timezone_override_is_treated_as_absent() -> None:
250250
"Not/AZone", # unknown name: ZoneInfoNotFoundError
251251
"America", # a tzdata directory, not a zone: OSError
252252
"../etc/passwd", # outside TZPATH: ValueError
253+
"europe/warsaw", # not the exact key, on any filesystem (#5060)
253254
],
254255
)
255256
async def test_invalid_local_timezone_fails_before_the_transport_opens(

‎src/time/tests/test_server.py‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
from unittest.mock import patch
55
from zoneinfo import ZoneInfo
66

7-
from mcp_server_time.server import TimeServer, get_local_tz
7+
from mcp_server_time.server import (
8+
TimeServer,
9+
get_local_tz,
10+
get_zoneinfo,
11+
known_timezones,
12+
)
813

914

1015
@pytest.mark.parametrize(
@@ -525,3 +530,17 @@ def test_get_local_tz_various_timezones(mock_get_localzone, timezone_name):
525530
result = get_local_tz()
526531
assert str(result) == timezone_name
527532
assert isinstance(result, ZoneInfo)
533+
534+
535+
def test_get_zoneinfo_rejects_a_key_the_tz_database_does_not_list():
536+
# #5060: on a case-insensitive filesystem `ZoneInfo` resolves a key that
537+
# is not an exact IANA name. Simulate that on any platform: `ZoneInfo`
538+
# accepts "Europe/Warsaw", but the database's key list does not hold it.
539+
without_warsaw = known_timezones() - {"Europe/Warsaw"}
540+
with patch("mcp_server_time.server.known_timezones", return_value=without_warsaw):
541+
with pytest.raises(
542+
MCPError,
543+
match="Invalid timezone: 'No time zone found with key Europe/Warsaw'",
544+
):
545+
get_zoneinfo("Europe/Warsaw")
546+
assert str(get_zoneinfo("Europe/Warsaw")) == "Europe/Warsaw"

0 commit comments

Comments
 (0)