Upload folder using huggingface_hub
Browse files- app/validator/engine.py +18 -1
- app/validator/mx_intel.py +24 -0
- tests/test_ml_mx_gate.py +105 -0
app/validator/engine.py
CHANGED
|
@@ -21,6 +21,7 @@ from .dns_check import check_dns
|
|
| 21 |
from .disposable import check_disposable, domain_in_blocklist, domain_is_known
|
| 22 |
from .mailbox_provider import MailboxProvider, resolve_provider
|
| 23 |
from ..ml.runtime import get_model
|
|
|
|
| 24 |
from .retry_queue import is_greylist_result
|
| 25 |
from .scoring import calculate_score
|
| 26 |
|
|
@@ -228,11 +229,21 @@ class EmailValidationEngine:
|
|
| 228 |
|
| 229 |
# ML advisory (single path): unknown domains only, fail-open. Mirrors
|
| 230 |
# the batch pipeline so both surfaces agree on the same address.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
ml_model = get_model()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
if (
|
| 233 |
ml_model is not None
|
| 234 |
and not domain_is_known(domain)
|
| 235 |
and deep
|
|
|
|
| 236 |
):
|
| 237 |
try:
|
| 238 |
preds = ml_model.predict([f"john.smith [SEP] {domain}"])
|
|
@@ -530,14 +541,20 @@ class EmailValidationEngine:
|
|
| 530 |
# Runs only where the curated lists have no answer - the exact gap
|
| 531 |
# a static blocklist cannot close. Any failure here is silently
|
| 532 |
# ignored: the deterministic verdict stands.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 533 |
ml_map: dict[str, tuple[str, float]] = {}
|
| 534 |
ml_model = get_model()
|
| 535 |
if ml_model is not None:
|
| 536 |
unknown_domains = sorted(
|
| 537 |
{
|
| 538 |
domain
|
| 539 |
-
for _, _, _, domain,
|
| 540 |
if not domain_is_known(domain)
|
|
|
|
|
|
|
| 541 |
}
|
| 542 |
)
|
| 543 |
if unknown_domains:
|
|
|
|
| 21 |
from .disposable import check_disposable, domain_in_blocklist, domain_is_known
|
| 22 |
from .mailbox_provider import MailboxProvider, resolve_provider
|
| 23 |
from ..ml.runtime import get_model
|
| 24 |
+
from .mx_intel import mx_is_trusted
|
| 25 |
from .retry_queue import is_greylist_result
|
| 26 |
from .scoring import calculate_score
|
| 27 |
|
|
|
|
| 229 |
|
| 230 |
# ML advisory (single path): unknown domains only, fail-open. Mirrors
|
| 231 |
# the batch pipeline so both surfaces agree on the same address.
|
| 232 |
+
# Skipped when the MX runs on a trusted enterprise platform: the model
|
| 233 |
+
# learned 'unfamiliar domain string = disposable' from an imbalanced
|
| 234 |
+
# dataset and flags legitimate small businesses on Google Workspace /
|
| 235 |
+
# M365 — the deterministic MX intelligence is the better signal there.
|
| 236 |
ml_model = get_model()
|
| 237 |
+
mx_host = (
|
| 238 |
+
dns_result.mx_records[0]["host"]
|
| 239 |
+
if dns_result.mx_records
|
| 240 |
+
else None
|
| 241 |
+
)
|
| 242 |
if (
|
| 243 |
ml_model is not None
|
| 244 |
and not domain_is_known(domain)
|
| 245 |
and deep
|
| 246 |
+
and not mx_is_trusted(mx_host)
|
| 247 |
):
|
| 248 |
try:
|
| 249 |
preds = ml_model.predict([f"john.smith [SEP] {domain}"])
|
|
|
|
| 541 |
# Runs only where the curated lists have no answer - the exact gap
|
| 542 |
# a static blocklist cannot close. Any failure here is silently
|
| 543 |
# ignored: the deterministic verdict stands.
|
| 544 |
+
# Domains on trusted enterprise MX (Google Workspace / M365 /
|
| 545 |
+
# gateways) are excluded: disposable domains essentially never run
|
| 546 |
+
# there, and the model's 'unfamiliar string' bias would otherwise
|
| 547 |
+
# flag legitimate businesses as disposable.
|
| 548 |
ml_map: dict[str, tuple[str, float]] = {}
|
| 549 |
ml_model = get_model()
|
| 550 |
if ml_model is not None:
|
| 551 |
unknown_domains = sorted(
|
| 552 |
{
|
| 553 |
domain
|
| 554 |
+
for _, _, _, domain, dns in pending
|
| 555 |
if not domain_is_known(domain)
|
| 556 |
+
and dns.mx_records
|
| 557 |
+
and not mx_is_trusted(dns.mx_records[0]["host"])
|
| 558 |
}
|
| 559 |
)
|
| 560 |
if unknown_domains:
|
app/validator/mx_intel.py
CHANGED
|
@@ -78,6 +78,30 @@ def classify_mx(mx_host: str) -> MxPolicy:
|
|
| 78 |
return MxPolicy(provider="unknown", trusts_rcpt=False, accepts_all=False)
|
| 79 |
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
def parse_rcpt_code(response: str) -> int | None:
|
| 82 |
"""Extract the numeric SMTP status from a response line, or None."""
|
| 83 |
match = _RCPT_CODE_RE.match(response or "")
|
|
|
|
| 78 |
return MxPolicy(provider="unknown", trusts_rcpt=False, accepts_all=False)
|
| 79 |
|
| 80 |
|
| 81 |
+
# Platforms where a CUSTOM domain requires a paid/business account. A
|
| 82 |
+
# disposable domain essentially never runs on these — they cost money and
|
| 83 |
+
# verify identity — while disposable domains live on cheap self-hosted or
|
| 84 |
+
# free-tier MX. When the MX is one of these, the domain-string classifier's
|
| 85 |
+
# opinion is unnecessary and, given an imbalanced training set, harmful: it
|
| 86 |
+
# flagged a legitimate small business (Google Workspace MX) as disposable.
|
| 87 |
+
_TRUSTED_MX_PROVIDERS = frozenset({
|
| 88 |
+
"google", # Google Workspace (paid for custom domains)
|
| 89 |
+
"microsoft365", # Microsoft 365 / Exchange Online
|
| 90 |
+
"mimecast", # enterprise email security gateway
|
| 91 |
+
"proofpoint", # enterprise email security gateway
|
| 92 |
+
"barracuda", # enterprise email security gateway
|
| 93 |
+
"zoho", # Zoho Mail (paid for custom domains)
|
| 94 |
+
"proton", # Proton Business
|
| 95 |
+
"fastmail", # Fastmail (paid custom domain)
|
| 96 |
+
"apple", # iCloud+ Custom Email Domain
|
| 97 |
+
})
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def mx_is_trusted(mx_host: str) -> bool:
|
| 101 |
+
"""True when the MX runs on a paid enterprise/business mail platform."""
|
| 102 |
+
return classify_mx(mx_host).provider in _TRUSTED_MX_PROVIDERS
|
| 103 |
+
|
| 104 |
+
|
| 105 |
def parse_rcpt_code(response: str) -> int | None:
|
| 106 |
"""Extract the numeric SMTP status from a response line, or None."""
|
| 107 |
match = _RCPT_CODE_RE.match(response or "")
|
tests/test_ml_mx_gate.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import unittest
|
| 2 |
+
from unittest.mock import AsyncMock, MagicMock, patch
|
| 3 |
+
|
| 4 |
+
from app.models import DNSResult, ValidationStatus
|
| 5 |
+
from app.validator.engine import EmailValidationEngine
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def _dns_with_mx(host):
|
| 9 |
+
return DNSResult(
|
| 10 |
+
domain_exists=True,
|
| 11 |
+
has_mx_records=True,
|
| 12 |
+
mx_records=[{"host": host, "priority": 10}],
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def _clean():
|
| 17 |
+
from app.models import DisposableResult
|
| 18 |
+
|
| 19 |
+
return DisposableResult(
|
| 20 |
+
is_disposable=False, is_free_provider=False, is_role_based=False
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class NullCache:
|
| 25 |
+
async def get_dns(self, domain):
|
| 26 |
+
return None
|
| 27 |
+
|
| 28 |
+
async def put_dns(self, domain, result):
|
| 29 |
+
return None
|
| 30 |
+
|
| 31 |
+
async def get_verdict(self, email, deep):
|
| 32 |
+
return None
|
| 33 |
+
|
| 34 |
+
async def put_verdict(self, email, deep, smtp, catch_all):
|
| 35 |
+
return None
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
class FakeProvider:
|
| 39 |
+
async def verify_many(self, pairs):
|
| 40 |
+
return ({e: SMTPResult(verified=True, smtp_code=250) for e, _ in pairs}, {})
|
| 41 |
+
|
| 42 |
+
async def verify(self, email, mx_host):
|
| 43 |
+
return SMTPResult(verified=True, smtp_code=250)
|
| 44 |
+
|
| 45 |
+
async def catch_all(self, domain, mx_host):
|
| 46 |
+
return (False, 0.9)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def _fake_model(answer):
|
| 50 |
+
m = MagicMock()
|
| 51 |
+
m.predict = MagicMock(return_value=[("DISPOSABLE", 0.99)])
|
| 52 |
+
return m
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
class TrustedMxGateTests(unittest.IsolatedAsyncioTestCase):
|
| 56 |
+
"""A domain running on a trusted enterprise mail platform (Google Workspace,
|
| 57 |
+
M365, ...) is almost never disposable — disposable domains live on cheap
|
| 58 |
+
hosts. The ML model learned 'unfamiliar domain string = disposable' from an
|
| 59 |
+
imbalanced dataset, so for trusted-MX domains the model's opinion must be
|
| 60 |
+
ignored: the deterministic MX intelligence is the better signal."""
|
| 61 |
+
|
| 62 |
+
def setUp(self):
|
| 63 |
+
self.patchers = [
|
| 64 |
+
patch("app.validator.engine.get_model",
|
| 65 |
+
return_value=_fake_model("DISPOSABLE")),
|
| 66 |
+
]
|
| 67 |
+
for p in self.patchers:
|
| 68 |
+
p.start()
|
| 69 |
+
|
| 70 |
+
def tearDown(self):
|
| 71 |
+
for p in self.patchers:
|
| 72 |
+
p.stop()
|
| 73 |
+
|
| 74 |
+
def _engine(self):
|
| 75 |
+
return EmailValidationEngine(mailbox=FakeProvider(), cache=NullCache())
|
| 76 |
+
|
| 77 |
+
async def test_google_workspace_mx_skips_ml_single(self):
|
| 78 |
+
engine = self._engine()
|
| 79 |
+
with patch("app.validator.engine.check_dns",
|
| 80 |
+
new=AsyncMock(return_value=_dns_with_mx("aspmx.l.google.com"))):
|
| 81 |
+
result = await engine.validate("info@thefreshfactory.co", deep=True)
|
| 82 |
+
|
| 83 |
+
self.assertIsNone(result.disposable.ml_label)
|
| 84 |
+
self.assertNotIn(
|
| 85 |
+
"ML_DISPOSABLE_SUSPECT",
|
| 86 |
+
{k for k in result.scoring.breakdown},
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
async def test_unknown_mx_still_gets_ml_signal(self):
|
| 90 |
+
engine = self._engine()
|
| 91 |
+
with patch("app.validator.engine.check_dns",
|
| 92 |
+
new=AsyncMock(return_value=_dns_with_mx("mx.sketchy-host.test"))):
|
| 93 |
+
result = await engine.validate("a@sketchy.test", deep=True)
|
| 94 |
+
|
| 95 |
+
self.assertEqual(result.disposable.ml_label, "DISPOSABLE")
|
| 96 |
+
|
| 97 |
+
async def test_m365_mx_skips_ml_bulk(self):
|
| 98 |
+
engine = self._engine()
|
| 99 |
+
with patch(
|
| 100 |
+
"app.validator.engine.check_dns",
|
| 101 |
+
new=AsyncMock(return_value=_dns_with_mx("x.mail.protection.outlook.com")),
|
| 102 |
+
):
|
| 103 |
+
results = await engine.validate_batch(["a@contoso.com"], deep=True)
|
| 104 |
+
|
| 105 |
+
self.assertIsNone(results[0].disposable.ml_label)
|