mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-11-20 12:36:53 +00:00
Fix: reject malformed zone OCR language codes
Garbage values went straight to tesseract and looked like an empty read.
This commit is contained in:
1 parent
f81ab41c1b
commit
8e07452604
2 files changed
+54
No files matched your search
@@ -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
|
||||
|
||||
@@ -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"),
|
||||
[
|
||||
|
||||
Reference in new issue
Block a user