Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3243fda9f8
|
No files matched your search
+1
-1
@@ -6,4 +6,4 @@ EMAIL_SERVER=example.com
|
||||
# DATA_DIR=/data
|
||||
# AGENT_URL=http://unsubscriber-agent:8080
|
||||
# AGENT_ALLOWED_DOMAINS=example.org,gmail.com
|
||||
# AGENT_ALLOWED_SENDERS=jens@example.org
|
||||
# AGENT_ALLOWED_SENDERS=example.org,jens@gmail.com
|
||||
@@ -56,7 +56,9 @@ of entries: a bare domain (`wagnersnetz.de`) allows every address of that domain
|
||||
as full addresses, never their domain: the recipient is read from the forwarded text, and a newsletter body with a
|
||||
line like `To: stranger@gmail.com` would otherwise make the agent type a stranger's address.
|
||||
`AGENT_ALLOWED_SENDERS` (comma separated) lists the forwarders whose mails become cases; mails from other
|
||||
senders are ignored. With the agent on and an empty list nothing is processed.
|
||||
senders are ignored. Entries work as above: a bare domain allows every sender of that domain (not of its
|
||||
subdomains), a full address exactly that sender. Use a domain only for domains whose mail you control, since the
|
||||
From header decides. With the agent on and an empty list nothing is processed.
|
||||
|
||||
The agent container needs `LLM_BASE_URL` (Open WebUI, `/api/chat/completions` is appended), `LLM_MODEL`,
|
||||
`LLM_API_KEY` and `BLOCKED_NETS`, optionally `MAX_STEPS` (20) and `CASE_TIMEOUT` (300). `BLOCKED_NETS` is required:
|
||||
|
||||
@@ -18,6 +18,14 @@ def _allowed(address: str, entries) -> bool:
|
||||
return address in entries or address.rsplit("@", 1)[-1] in entries # a domain entry never contains "@"
|
||||
|
||||
|
||||
def sender_allowed(address: str, entries) -> bool:
|
||||
"""Whether a forwarder (the address of the From header) is on the list; entries as in _allowed. Anything that
|
||||
is not one plain address is refused, so that odd forms like "@allowed.example" or a quoted local part cannot
|
||||
pass by their last part."""
|
||||
address = address.lower()
|
||||
return bool(_ADDRESS.fullmatch(address)) and _allowed(address, entries)
|
||||
|
||||
|
||||
def original_recipient(text: str, forwarder: str, allowed_domains) -> str:
|
||||
"""First allowed address from the quoted 'To:'/'An:' lines, else the forwarder, else ''.
|
||||
|
||||
|
||||
@@ -296,6 +296,32 @@ def test_mail_from_unknown_sender_is_ignored(store):
|
||||
assert store.get("<evil@x>") is None and b"2" in imap.folders["INBOX"]
|
||||
|
||||
|
||||
def test_sender_entry_without_at_allows_the_whole_domain(store):
|
||||
imap = FakeIMAP({b"1": mail_bytes("<a@x>", sender="Jens <JENS@Wagnersnetz.de>"),
|
||||
b"2": mail_bytes("<b@x>", sender="philipp@wagnersnetz.de"),
|
||||
b"3": mail_bytes("<c@x>", sender="someone@gmx.de")})
|
||||
agent = Agent(OK, OK, OK)
|
||||
summary = worker(store, imap, agent, allowed_senders=("Wagnersnetz.de", "someone@gmx.de")).run_once()
|
||||
assert len(agent.calls) == 3 and summary["ignored"] == 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize("sender", [
|
||||
"mallory@evil.example",
|
||||
"mallory@sub.wagnersnetz.de", # a domain entry does not cover its subdomains
|
||||
"mallory@wagnersnetz.de.evil.example",
|
||||
"mallory@evilwagnersnetz.de",
|
||||
"other@gmx.de", # a full address allows exactly that one
|
||||
"@wagnersnetz.de", # not a plain address: must not pass by its last part
|
||||
"a b@wagnersnetz.de",
|
||||
'<"mallory@evil.example"@wagnersnetz.de>',
|
||||
])
|
||||
def test_sender_outside_the_allowed_domain_is_ignored(store, sender):
|
||||
imap = FakeIMAP({b"1": mail_bytes("<a@x>", sender=sender)})
|
||||
agent = Agent(OK)
|
||||
summary = worker(store, imap, agent, allowed_senders=("wagnersnetz.de", "someone@gmx.de")).run_once()
|
||||
assert agent.calls == [] and summary["ignored"] == 1 and store.all() == []
|
||||
|
||||
|
||||
def test_no_allowed_senders_with_agent_means_nobody(store):
|
||||
imap = FakeIMAP({b"1": mail_bytes("<a@x>")})
|
||||
agent = Agent(OK)
|
||||
|
||||
@@ -8,7 +8,7 @@ import time
|
||||
from email.utils import parseaddr
|
||||
|
||||
from cases import case_key
|
||||
from forwarded import original_recipient
|
||||
from forwarded import original_recipient, sender_allowed
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -73,7 +73,7 @@ class Worker:
|
||||
without += 1
|
||||
continue
|
||||
forwarder = parseaddr(message.get("From", ""))[1]
|
||||
if (self.allowed_senders and forwarder.lower() not in self.allowed_senders) or (
|
||||
if (self.allowed_senders and not sender_allowed(forwarder, self.allowed_senders)) or (
|
||||
not self.allowed_senders and self.agent is not None): # fail closed
|
||||
ignored += 1
|
||||
continue
|
||||
|
||||
Reference in new issue
Block a user