Przeglądaj źródła

fix(variable): replace regex with RFC-compliant email validation

Replaced regex-based email validation with email-validator library.
Regex cannot properly validate emails per RFC 5322/5321 - it fails
on valid addresses like "John Doe"@example.com, user+tag@example.com.

Changes:
- Added email-validator>=2.0.0 dependency to pyproject.toml
- Removed EMAIL_REGEX constant
- Updated _convert_email() to use validate_email() function
- Returns normalized email addresses
- Provides better error messages for invalid emails

Fixes #1481
xcad 5 miesięcy temu
rodzic
commit
19d3b62c44
2 zmienionych plików z 9 dodań i 5 usunięć
  1. 8 5
      cli/core/template/variable.py
  2. 1 0
      pyproject.toml

+ 8 - 5
cli/core/template/variable.py

@@ -1,10 +1,11 @@
 from __future__ import annotations
 
 import logging
-import re
 from typing import TYPE_CHECKING, Any
 from urllib.parse import urlparse
 
+from email_validator import EmailNotValidError, validate_email
+
 if TYPE_CHECKING:
     from cli.core.template.variable_section import VariableSection
 
@@ -12,7 +13,6 @@ logger = logging.getLogger(__name__)
 
 TRUE_VALUES = {"true", "1", "yes", "on"}
 FALSE_VALUES = {"false", "0", "no", "off"}
-EMAIL_REGEX = re.compile(r"^[^@\s]+@[^@\s]+\.[^@\s]+$")
 
 
 class Variable:
@@ -221,9 +221,12 @@ class Variable:
         val = str(value).strip()
         if not val:
             return None
-        if not EMAIL_REGEX.fullmatch(val):
-            raise ValueError("value must be a valid email address")
-        return val
+        try:
+            # Validate email using RFC 5321/5322 compliant parser
+            validated = validate_email(val, check_deliverability=False)
+            return validated.normalized
+        except EmailNotValidError as exc:
+            raise ValueError(f"value must be a valid email address: {exc}") from exc
 
     def to_dict(self) -> dict[str, Any]:
         """Serialize Variable to a dictionary for storage."""

+ 1 - 0
pyproject.toml

@@ -22,6 +22,7 @@ dependencies = [
     "PyYAML>=6.0",
     "python-frontmatter>=1.0.0",
     "Jinja2>=3.0",
+    "email-validator>=2.0.0",
 ]
 
 [project.scripts]