From 8e07452604161c761fbabaf0a0207b25cb3bd6de Mon Sep 17 00:00:00 2001 From: stumpylog <797416+stumpylog@users.noreply.github.com> Date: Thu, 19 Nov 2026 09:12:37 -0800 Subject: [PATCH] Fix: reject malformed zone OCR language codes Garbage values went straight to tesseract and looked like an empty read. --- src/documents/serialisers.py | 10 +++++ src/documents/tests/test_api_ocr_templates.py | 44 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py index 17aa489af4..abf2cc3ff8 100644 --- a/src/documents/serialisers.py +++ b/src/documents/serialisers.py @@ -3770,6 +3770,16 @@ class OcrTemplateZoneSerializer(serializers.ModelSerializer): ) return value + def validate_ocr_language(self, value: str) -> str: + # Tesseract language codes joined by "+", e.g. "deu+eng", "chi_sim" or + # "script/Latin". + code = r"[A-Za-z0-9_]+(/[A-Za-z0-9_]+)?" + if not re.fullmatch(rf"{code}(\+{code})*", value): + raise serializers.ValidationError( + "Use Tesseract language codes joined by '+', e.g. 'deu+eng'.", + ) + 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 diff --git a/src/documents/tests/test_api_ocr_templates.py b/src/documents/tests/test_api_ocr_templates.py index 6891b957ad..6afa5c8837 100644 --- a/src/documents/tests/test_api_ocr_templates.py +++ b/src/documents/tests/test_api_ocr_templates.py @@ -1116,6 +1116,50 @@ class TestOcrTemplateValidation: if expected == status.HTTP_400_BAD_REQUEST: assert "custom_field" in response.json()["zones"]["0"] + @pytest.mark.parametrize( + ("ocr_language", "expected"), + [ + pytest.param("eng", status.HTTP_201_CREATED, id="single"), + pytest.param("deu+eng", status.HTTP_201_CREATED, id="several"), + pytest.param("chi_sim", status.HTTP_201_CREATED, id="underscore"), + pytest.param( + "script/Latin+eng", + status.HTTP_201_CREATED, + id="script-model", + ), + pytest.param("/tmp/x", status.HTTP_400_BAD_REQUEST, id="absolute-path"), + pytest.param("../eng", status.HTTP_400_BAD_REQUEST, id="parent-path"), + pytest.param("", status.HTTP_400_BAD_REQUEST, id="blank"), + pytest.param("deu eng", status.HTTP_400_BAD_REQUEST, id="space"), + pytest.param("deu+", status.HTTP_400_BAD_REQUEST, id="trailing-plus"), + pytest.param("deu,eng", status.HTTP_400_BAD_REQUEST, id="comma"), + ], + ) + def test_ocr_language_must_be_tesseract_codes( + self, + admin_client: APIClient, + text_zone_payload: Callable[..., dict[str, Any]], + ocr_language: str, + expected: int, + ) -> None: + """ + GIVEN: + - A zone with well-formed or malformed Tesseract language codes + WHEN: + - Create is attempted + THEN: + - Only codes joined by "+" are accepted + """ + response = admin_client.post( + ENDPOINT, + template_payload(zones=[text_zone_payload(ocr_language=ocr_language)]), + format="json", + ) + + assert response.status_code == expected + if expected == status.HTTP_400_BAD_REQUEST: + assert "ocr_language" in response.json()["zones"]["0"] + @pytest.mark.parametrize( ("date_format", "expected"), [