Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

fix(reports): validate custom ranges against the app calendar (#1215)

Tássio Noronha5f213068734d
diff --git a/backend/app/api/reports.py b/backend/app/api/reports.py
index 84a1f60f..61608c10 100644
--- a/backend/app/api/reports.py
+++ b/backend/app/api/reports.py
@@ -5,6 +5,7 @@ from typing import Optional
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.ext.asyncio import AsyncSession
+from app.core.app_clock import app_today
from app.core.database import get_async_session
from app.core.workspace_context import WorkspaceContext, current_workspace
from app.schemas.report import ReportResponse
@@ -67,7 +68,12 @@ def _resolve_custom_range(
status_code=422,
detail="end_date must be on or after start_date",
)
- if end_date > date.today():
+ # The app's calendar, not the host clock: the workspace's request runs
+ # on its own timezone (see the `current_workspace` dependency), and the
+ # service below builds the same window from `app_today()`. Comparing
+ # against a different calendar would reject the workspace's own today
+ # or accept a date the workspace has not reached yet.
+ if end_date > app_today():
raise HTTPException(
status_code=422,
detail="end_date must be on or before today",
diff --git a/backend/tests/test_report_service.py b/backend/tests/test_report_service.py
index b5f2dc51..2d4a4c9c 100644
--- a/backend/tests/test_report_service.py
+++ b/backend/tests/test_report_service.py
@@ -2,7 +2,8 @@ import uuid
from datetime import date, datetime, timedelta, timezone
from decimal import Decimal
from types import SimpleNamespace
-from unittest.mock import AsyncMock
+from unittest.mock import AsyncMock, patch
+from zoneinfo import ZoneInfo
import pytest
from fastapi import HTTPException
@@ -26,7 +27,9 @@ from app.api.reports import (
_add_years,
_financial_year_start_month,
_reject_unsupported_fiscal_year_report,
+ _resolve_custom_range,
)
+from app.core.app_clock import today_in, use_resolved_timezone
from app.services.report_service import (
_add_months,
_asset_value_at,
@@ -2833,6 +2836,9 @@ async def test_historical_api_custom_range_boundaries(
return cls(2026, 9, 12)
monkeypatch.setattr(reports_api, 'date', FixedDate)
+ # The guard reads "today" from the app's calendar, not from the module's
+ # date class, so both seams get the same fixed day.
+ monkeypatch.setattr(reports_api, 'app_today', lambda: FixedDate.today())
report = ReportResponse(
summary=ReportSummary(primary_value=0, change_amount=0, change_percent=None,
breakdowns=[]),
@@ -2895,3 +2901,64 @@ async def test_custom_dates_override_indian_yearly_ytd(
assert service.call_args.kwargs['financial_year_start_month'] == 4
finally:
app.dependency_overrides.pop(current_workspace, None)
+
+
+# ---------------------------------------------------------------------------
+# _resolve_custom_range: "today" must come from the app's calendar
+# (workspace or application timezone), never the host clock. The service
+# below builds its windows from `app_today()`, so a guard that compares
+# against a different calendar either rejects a legitimate "today" or
+# accepts a future date (issue #840).
+# ---------------------------------------------------------------------------
+
+
+def test_custom_range_accepts_end_date_that_is_app_today():
+ host_today = date.today()
+ # The workspace runs one day ahead of the host clock: its "today" is
+ # what the host only sees as tomorrow, and a report through the app's
+ # today is a normal, current request.
+ with patch("app.api.reports.app_today", return_value=host_today + timedelta(days=1)):
+ start = host_today
+ end = host_today + timedelta(days=1)
+ assert _resolve_custom_range(start, end) == (start, end)
+
+
+def test_custom_range_rejects_end_date_after_app_today():
+ host_today = date.today()
+ # The workspace runs one day behind the host clock: the host's today
+ # is still in the future for the workspace, so it is not a valid end.
+ with patch("app.api.reports.app_today", return_value=host_today - timedelta(days=1)):
+ with pytest.raises(HTTPException) as exc:
+ _resolve_custom_range(host_today - timedelta(days=1), host_today)
+ assert exc.value.detail == "end_date must be on or before today"
+
+
+def _host_and_shifted_zone() -> tuple[ZoneInfo, date, date]:
+ """A zone whose calendar date differs from the host's right now.
+
+ Kiritimati (UTC+14) and Pago Pago (UTC-11) are 25 hours apart, so they
+ always sit on different days, and one of the two always differs from
+ the host's day no matter which offset the host runs on.
+ """
+ host_today = date.today()
+ for name in ("Pacific/Kiritimati", "Pacific/Pago_Pago"):
+ zone = ZoneInfo(name)
+ if today_in(zone) != host_today:
+ return zone, today_in(zone), host_today
+ pytest.fail("expected a zone on a different calendar day from the host")
+
+
+def test_custom_range_guard_follows_resolved_timezone():
+ zone, zone_today, host_today = _host_and_shifted_zone()
+ with use_resolved_timezone(zone):
+ if zone_today > host_today:
+ # Workspace ahead of the host: asking for the workspace's own
+ # today must be accepted.
+ start = zone_today - timedelta(days=7)
+ assert _resolve_custom_range(start, zone_today) == (start, zone_today)
+ else:
+ # Workspace behind the host: the host's today is a future date
+ # for the workspace and must be rejected.
+ with pytest.raises(HTTPException) as exc:
+ _resolve_custom_range(zone_today, host_today)
+ assert exc.value.detail == "end_date must be on or before today"