From f81ab41c1be90359d3f3ffbcf2323ddf59d95548 Mon Sep 17 00:00:00 2001 From: stumpylog <797416+stumpylog@users.noreply.github.com> Date: Thu, 19 Nov 2026 09:09:40 -0800 Subject: [PATCH] Fix: reject zone date formats that cannot read a full date A bad format silently fell back to auto-detection, and one without a year read every date as 1900. --- src/documents/serialisers.py | 21 ++++++++ src/documents/tests/test_api_ocr_templates.py | 49 +++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py index 337cb164f8..17aa489af4 100644 --- a/src/documents/serialisers.py +++ b/src/documents/serialisers.py @@ -3770,6 +3770,27 @@ class OcrTemplateZoneSerializer(serializers.ModelSerializer): ) return value + def validate_date_format(self, value: str) -> str: + # The format must read back a full date: without a day, month or year + # strptime fills in 1 or 1900 instead of failing. A missing year is + # rejected before parsing, since Python deprecates parsing a day of month + # without one. + if value: + if not re.search(r"%[Yy]", value.replace("%%", "")): + raise serializers.ValidationError( + "The date format must include the day, month and year.", + ) + sample = datetime(2024, 3, 15) + try: + round_trip = datetime.strptime(sample.strftime(value), value) + except ValueError as e: + raise serializers.ValidationError(f"Invalid date format: {e}") from e + if round_trip.date() != sample.date(): + raise serializers.ValidationError( + "The date format must read back a full date (day, month and year).", + ) + return value + def validate_validation_regex(self, value: str) -> str: if value: try: diff --git a/src/documents/tests/test_api_ocr_templates.py b/src/documents/tests/test_api_ocr_templates.py index 54f6848cd4..6891b957ad 100644 --- a/src/documents/tests/test_api_ocr_templates.py +++ b/src/documents/tests/test_api_ocr_templates.py @@ -1116,6 +1116,55 @@ class TestOcrTemplateValidation: if expected == status.HTTP_400_BAD_REQUEST: assert "custom_field" in response.json()["zones"]["0"] + @pytest.mark.parametrize( + ("date_format", "expected"), + [ + pytest.param("", status.HTTP_201_CREATED, id="blank"), + pytest.param("%d.%m.%Y", status.HTTP_201_CREATED, id="day-month-year"), + pytest.param("%m/%d/%y", status.HTTP_201_CREATED, id="two-digit-year"), + pytest.param("%d %B %Y", status.HTTP_201_CREATED, id="month-name"), + pytest.param("%d.%m", status.HTTP_400_BAD_REQUEST, id="no-year"), + pytest.param( + "%d.%m.%%Y", + status.HTTP_400_BAD_REQUEST, + id="escaped-percent-is-not-a-year", + ), + pytest.param("%m.%Y", status.HTTP_400_BAD_REQUEST, id="no-day"), + pytest.param("%Y", status.HTTP_400_BAD_REQUEST, id="year-only"), + pytest.param("date", status.HTTP_400_BAD_REQUEST, id="no-directive"), + pytest.param( + "%d.%m.%Y %Q", + status.HTTP_400_BAD_REQUEST, + id="bad-directive", + ), + ], + ) + def test_date_format_must_read_a_full_date( + self, + admin_client: APIClient, + text_zone_payload: Callable[..., dict[str, Any]], + date_format: str, + expected: int, + ) -> None: + """ + GIVEN: + - A zone with a blank date format, one with a day, month and year, + or one missing a part or using an unknown directive + WHEN: + - Create is attempted + THEN: + - Only blank and complete formats are accepted + """ + response = admin_client.post( + ENDPOINT, + template_payload(zones=[text_zone_payload(date_format=date_format)]), + format="json", + ) + + assert response.status_code == expected + if expected == status.HTTP_400_BAD_REQUEST: + assert "date_format" in response.json()["zones"]["0"] + @pytest.mark.parametrize( ("pattern", "expected"), [