From 884fbf7fb219a982f413a3ae6dceb09379b815e1 Mon Sep 17 00:00:00 2001 From: stumpylog <797416+stumpylog@users.noreply.github.com> Date: Thu, 19 Nov 2026 09:00:05 -0800 Subject: [PATCH] Fix: reject invalid zone validation regexes on save A bad pattern made every extraction fail the regex check. --- src/documents/serialisers.py | 10 ++++ src/documents/tests/test_api_ocr_templates.py | 56 ++++++++++++++++++- 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py index 59f33d87db..ede858700e 100644 --- a/src/documents/serialisers.py +++ b/src/documents/serialisers.py @@ -3770,6 +3770,16 @@ class OcrTemplateZoneSerializer(serializers.ModelSerializer): ) return value + def validate_validation_regex(self, value: str) -> str: + if value: + try: + validate_regex_pattern(value) + except ValueError as e: + raise serializers.ValidationError( + f"Invalid regular expression: {e}", + ) from e + return value + def validate_page(self, value): if value is not None and value != -1 and value < 1: raise serializers.ValidationError( diff --git a/src/documents/tests/test_api_ocr_templates.py b/src/documents/tests/test_api_ocr_templates.py index fb439605dd..6a4d4af4fb 100644 --- a/src/documents/tests/test_api_ocr_templates.py +++ b/src/documents/tests/test_api_ocr_templates.py @@ -1056,6 +1056,61 @@ class TestOcrTemplateValidation: ) assert response.status_code == status.HTTP_400_BAD_REQUEST + @pytest.mark.parametrize( + ("pattern", "expected"), + [ + pytest.param("", status.HTTP_201_CREATED, id="blank"), + pytest.param(r"INV-\d+", status.HTTP_201_CREATED, id="valid"), + pytest.param("INV-(", status.HTTP_400_BAD_REQUEST, id="unbalanced"), + pytest.param("[a-", status.HTTP_400_BAD_REQUEST, id="unclosed-set"), + ], + ) + def test_validation_regex_must_compile( + self, + admin_client: APIClient, + text_zone_payload: Callable[..., dict[str, Any]], + pattern: str, + expected: int, + ) -> None: + """ + GIVEN: + - A zone with a blank, valid or invalid validation regex + WHEN: + - Create is attempted + THEN: + - Blank and valid patterns are accepted, invalid ones are rejected + on the zone's validation_regex field + """ + response = admin_client.post( + ENDPOINT, + template_payload(zones=[text_zone_payload(validation_regex=pattern)]), + format="json", + ) + + assert response.status_code == expected + if expected == status.HTTP_400_BAD_REQUEST: + assert "validation_regex" in response.json()["zones"]["0"] + + def test_zone_preview_rejects_an_invalid_validation_regex( + self, + admin_client: APIClient, + mock_preview: MagicMock, + ) -> None: + """ + GIVEN: + - A zone preview request with an invalid validation regex + WHEN: + - The preview is requested + THEN: + - 400 is returned and no extraction runs + """ + document = DocumentFactory(mime_type="image/png") + + response = preview_zone(admin_client, document, validation_regex="INV-(") + + assert response.status_code == status.HTTP_400_BAD_REQUEST + mock_preview.assert_not_called() + @pytest.mark.parametrize( "formats", [ @@ -2223,7 +2278,6 @@ class TestZonePreview: pytest.param("", "123", None, id="no-regex"), pytest.param(r"\d+", "abc", False, id="no-match"), pytest.param(r"\d+", None, None, id="no-value"), - pytest.param("[invalid", "123", False, id="invalid-regex"), ], ) def test_reports_regex_failures_and_empty_results(