Skip to content

Commit 374eb76

Browse files
committed
Guardrails around cache
1 parent 38cbab8 commit 374eb76

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/gitfetch/cache.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,27 @@ def _is_cache_expired(self, cached_at: datetime) -> bool:
255255
Returns:
256256
True if expired, False otherwise
257257
"""
258-
expiry_time = datetime.now() - timedelta(minutes=self.cache_expiry_minutes)
258+
# Defensive handling: ensure cache_expiry_minutes is a reasonable int
259+
# and avoid passing an extremely large integer to timedelta which can
260+
# raise OverflowError on some platforms (assuming on 32-bit builds).
261+
try:
262+
minutes = int(self.cache_expiry_minutes)
263+
except Exception:
264+
minutes = 15
265+
266+
# Enforce sensible bounds: minimum 1 minute, cap to MAX_MINUTES
267+
# (10 years expressed in minutes). This prevents OverflowError while
268+
# still allowing very long cache durations when intentionally set.
269+
MAX_MINUTES = 5256000 # 10 years
270+
minutes = max(1, min(minutes, MAX_MINUTES))
271+
272+
try:
273+
expiry_time = datetime.now() - timedelta(minutes=minutes)
274+
except OverflowError:
275+
# In the unlikely event timedelta still overflows, treat cache as
276+
# non-expired (safe default) to avoid crashing the program.
277+
return False
278+
259279
return cached_at < expiry_time
260280

261281
def list_cached_accounts(self) -> list[tuple[str, datetime]]:

0 commit comments

Comments
 (0)