From 6f4b1ffafa3544e25a68cacf81eec132a3482792 Mon Sep 17 00:00:00 2001 From: Seyyed Date: Fri, 23 Oct 2026 21:23:45 +0330 Subject: [PATCH 1/2] fix(backend): support Windows timezone and CAPTCHA fonts --- hesabixAPI/app/services/captcha_service.py | 43 +++++++++++++-- hesabixAPI/pyproject.toml | 1 + hesabixAPI/tests/test_captcha_font.py | 61 ++++++++++++++++++++++ 3 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 hesabixAPI/tests/test_captcha_font.py diff --git a/hesabixAPI/app/services/captcha_service.py b/hesabixAPI/app/services/captcha_service.py index 294fbd19..80d20f01 100755 --- a/hesabixAPI/app/services/captcha_service.py +++ b/hesabixAPI/app/services/captcha_service.py @@ -1,9 +1,12 @@ from __future__ import annotations import base64 +import hashlib import io +import os import secrets from datetime import datetime, timedelta +from pathlib import Path from PIL import Image, ImageDraw, ImageFont, ImageFilter from sqlalchemy.orm import Session @@ -12,11 +15,44 @@ from adapters.db.models.captcha import Captcha from app.core.settings import get_settings from app.services.auth_security_event_service import log_auth_security_event from app.services.system_settings_service import get_captcha_auth_security_effective -import hashlib + _ALPHANUM_CHARSET = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ" +_API_ROOT = Path(__file__).resolve().parents[2] +_BUNDLED_CAPTCHA_FONT = _API_ROOT / "assets" / "fonts" / "Vazirmatn-Bold.ttf" + + +def _captcha_font_candidates() -> tuple[Path, ...]: + candidates = [ + Path("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"), + Path("/usr/share/fonts/dejavu/DejaVuSans-Bold.ttf"), + ] + + windows_dir = os.environ.get("WINDIR") + if windows_dir: + windows_fonts = Path(windows_dir) / "Fonts" + candidates.extend( + [ + windows_fonts / "arialbd.ttf", + windows_fonts / "segoeuib.ttf", + ] + ) + + candidates.append(_BUNDLED_CAPTCHA_FONT) + return tuple(candidates) + + +def _load_captcha_font(size: int): + for font_path in _captcha_font_candidates(): + try: + if font_path.is_file(): + return ImageFont.truetype(str(font_path), size) + except (OSError, ValueError): + continue + + return ImageFont.load_default() def _generate_numeric_code(length: int) -> str: return "".join(str(secrets.randbelow(10)) for _ in range(length)) @@ -46,10 +82,7 @@ def _render_image(code: str, strong: bool) -> Image.Image: bg2 = 242 + secrets.randbelow(8) img = Image.new("RGB", (width, height), (bg0, bg1, bg2)) draw = ImageDraw.Draw(img) - try: - font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf" - font = ImageFont.truetype(font_path, 26) - except Exception: + font = _load_captcha_font(26) font = ImageFont.load_default() for _ in range(8 if strong else 3): xy = [(secrets.randbelow(width), secrets.randbelow(height)) for _ in range(2)] diff --git a/hesabixAPI/pyproject.toml b/hesabixAPI/pyproject.toml index cd0b8d38..67f853a1 100755 --- a/hesabixAPI/pyproject.toml +++ b/hesabixAPI/pyproject.toml @@ -41,6 +41,7 @@ dependencies = [ "simpleeval>=0.9.14", "croniter>=2.0.0", "pytz>=2024.1", + "tzdata>=2025.1; sys_platform == 'win32'", "paramiko>=3.4.0", "qrcode>=7.4.2", "basalam-sdk>=0.1.0", diff --git a/hesabixAPI/tests/test_captcha_font.py b/hesabixAPI/tests/test_captcha_font.py new file mode 100644 index 00000000..ce45ac7b --- /dev/null +++ b/hesabixAPI/tests/test_captcha_font.py @@ -0,0 +1,61 @@ +from pathlib import Path + +from app.services import captcha_service + + +def test_captcha_font_candidates_keep_linux_font_first( + monkeypatch, +) -> None: + monkeypatch.delenv("WINDIR", raising=False) + + candidates = captcha_service._captcha_font_candidates() + + assert candidates[0] == Path( + "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf" + ) + assert candidates[-1] == captcha_service._BUNDLED_CAPTCHA_FONT + + +def test_captcha_font_candidates_include_windows_fonts( + monkeypatch, +) -> None: + windows_dir = r"C:\Windows" + monkeypatch.setenv("WINDIR", windows_dir) + + candidates = captcha_service._captcha_font_candidates() + fonts_dir = Path(windows_dir) / "Fonts" + + assert fonts_dir / "arialbd.ttf" in candidates + assert fonts_dir / "segoeuib.ttf" in candidates + + +def test_load_captcha_font_skips_missing_candidate( + monkeypatch, + tmp_path, +) -> None: + missing_font = tmp_path / "missing.ttf" + available_font = tmp_path / "available.ttf" + available_font.write_bytes(b"test-font") + + expected_font = object() + loaded: list[tuple[Path, int]] = [] + + def fake_truetype(path: str, size: int): + loaded.append((Path(path), size)) + return expected_font + + monkeypatch.setattr( + captcha_service, + "_captcha_font_candidates", + lambda: (missing_font, available_font), + ) + monkeypatch.setattr( + captcha_service.ImageFont, + "truetype", + fake_truetype, + ) + + font = captcha_service._load_captcha_font(26) + + assert font is expected_font + assert loaded == [(available_font, 26)] \ No newline at end of file -- 2.43.0 From 793f035cd7bfcba810368f799bdd703deed518d3 Mon Sep 17 00:00:00 2001 From: Seyyed Date: Sat, 24 Oct 2026 17:18:09 +0330 Subject: [PATCH 2/2] fix(backend): correct CAPTCHA font loading --- hesabixAPI/app/services/captcha_service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hesabixAPI/app/services/captcha_service.py b/hesabixAPI/app/services/captcha_service.py index 80d20f01..d6936837 100755 --- a/hesabixAPI/app/services/captcha_service.py +++ b/hesabixAPI/app/services/captcha_service.py @@ -83,7 +83,7 @@ def _render_image(code: str, strong: bool) -> Image.Image: img = Image.new("RGB", (width, height), (bg0, bg1, bg2)) draw = ImageDraw.Draw(img) font = _load_captcha_font(26) - font = ImageFont.load_default() + for _ in range(8 if strong else 3): xy = [(secrets.randbelow(width), secrets.randbelow(height)) for _ in range(2)] fill = (180 + secrets.randbelow(40), 185 + secrets.randbelow(40), 192 + secrets.randbelow(40)) -- 2.43.0