From 8ed9f4e63884fc4abd57f073378863353db715eb Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Mon, 23 Dec 2024 14:47:15 -0600 Subject: [PATCH] Update dependencies, fix type issues, and update email address parsing --- .pre-commit-config.yaml | 2 +- pyproject.toml | 2 +- src/market_server/schema.py | 46 +++++++++++++++++++++++-------------- src/market_server/server.py | 4 ++-- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 84c6ba9..b445a08 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -30,7 +30,7 @@ repos: hooks: - id: black - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.8.3 + rev: v0.8.4 hooks: - id: ruff types: [file] diff --git a/pyproject.toml b/pyproject.toml index c0cb1a0..4d4e168 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,7 +31,7 @@ keywords = ["minecraft", "mineos", "api", "server", "lua"] dependencies = [ "httpx~=0.28.0", "hypercorn[trio]~=0.17.3", - "quart~=0.19.9", + "quart~=0.20.0", "quart-trio~=0.11.1", "trio~=0.27.0", "Werkzeug~=3.1.2", diff --git a/src/market_server/schema.py b/src/market_server/schema.py index 41062a6..dcb44ab 100644 --- a/src/market_server/schema.py +++ b/src/market_server/schema.py @@ -27,14 +27,13 @@ import inspect import math +import re import time import traceback import uuid from collections import Counter, deque -from email import message_from_string -from email.errors import HeaderParseError -from email.headerregistry import Address, UniqueAddressHeader -from email.policy import default as email_default_policy +from email.headerregistry import Address +from email.utils import parseaddr from enum import IntEnum from secrets import token_urlsafe from typing import TYPE_CHECKING, Any, Final, NamedTuple, cast @@ -59,6 +58,10 @@ 7: "The Unlicense", } +EMAIL_REGEX: Final = re.compile( + r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$", +) + class PUBLICATION_CATEGORY(IntEnum): # noqa: N801 """Publication category enums.""" @@ -308,22 +311,31 @@ class Publication(NamedTuple): def parse_email_address(string: str) -> Address | None: """Parse email address from string.""" - try: - msg = message_from_string(f"To: {string}", policy=email_default_policy) - to = msg["to"] - except (IndexError, HeaderParseError): + # Strip whitespace from the input string + string = string.strip() + + # Check if the string matches the email regex + if not EMAIL_REGEX.match(string): return None - if not to: + + # Use parseaddr to extract the email address and name + name, email = parseaddr(string) + + # Check if the email is valid + if not email: return None - if not isinstance(to, UniqueAddressHeader): - raise RuntimeError( - f"During email parsing, got {to!r} ({type(to)}) instead of UniqueAddressHeader as email address group.", - ) - value = to.addresses[0] - assert isinstance(value, Address) - if not value.username or not value.domain: + + # Create an Address object + try: + address = Address(addr_spec=email, display_name=name) + except ValueError: return None - return value + + # Validate the Address object + if not address.username or not address.domain: + return None + + return address def parse_int(string: str | int) -> int | None: diff --git a/src/market_server/server.py b/src/market_server/server.py index f021a99..a8b5fd8 100644 --- a/src/market_server/server.py +++ b/src/market_server/server.py @@ -200,7 +200,7 @@ def find_ip() -> str: schema_v_2_04 = schema.Version_2_04(DATA_PATH) -@app.route("/MineOSAPI//