Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a config option to drop emails if user-supplied content contains a given keyword. #535

Merged
merged 4 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/535.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a config option to drop emails if user-supplied content contains a given keyword.
8 changes: 8 additions & 0 deletions sydent/config/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ def parse_config(self, cfg: ConfigParser) -> bool:
third_party_invite_room_blocklist = cfg.get(
"email", "email.third_party_invite_room_blocklist", fallback=""
)
third_party_invite_keyword_blocklist = cfg.get(
"email", "email.third_party_invite_keyword_blocklist", fallback=""
)
self.third_party_invite_homeserver_blocklist = {
server
for server in third_party_invite_homeserver_blocklist.split("\n")
Expand All @@ -86,6 +89,11 @@ def parse_config(self, cfg: ConfigParser) -> bool:
for room_id in third_party_invite_room_blocklist.split("\n")
if room_id # filter out empty lines
}
self.third_party_invite_keyword_blocklist = {
keyword
for keyword in third_party_invite_keyword_blocklist.split("\n")
if keyword
}

self.email_sender_ratelimit_burst = cfg.getint(
"email", "email.ratelimit_sender.burst", fallback=5
Expand Down
4 changes: 4 additions & 0 deletions sydent/http/servlets/store_invite_servlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ def render_POST(self, request: Request) -> JsonDict:
sender, "Limit exceeded for this sender"
)

for keyword in self.sydent.config.email.third_party_invite_keyword_blocklist:
if keyword in [medium, address, roomId, sender]:
raise MatrixRestError(403, "M_UNAUTHORIZED", "Invite not allowed")

globalAssocStore = GlobalAssociationStore(self.sydent)
mxid = globalAssocStore.getMxid(medium, normalised_address)
if mxid:
Expand Down
20 changes: 19 additions & 1 deletion tests/test_invites.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from sydent.db.invite_tokens import JoinTokenStore
from sydent.http.httpclient import FederationHttpClient
from sydent.http.servlets.store_invite_servlet import StoreInviteServlet
from tests.utils import make_sydent
from tests.utils import make_request, make_sydent


class ThreepidInvitesTestCase(unittest.TestCase):
Expand All @@ -19,6 +19,7 @@ def setUp(self):
# Used by test_invited_email_address_obfuscation
"email.third_party_invite_username_obfuscate_characters": "6",
"email.third_party_invite_domain_obfuscate_characters": "8",
"email.third_party_invite_keyword_blocklist": "evil\nbad",
},
}
self.sydent = make_sydent(test_config=config)
Expand Down Expand Up @@ -102,6 +103,23 @@ def test_invited_email_address_obfuscation(self):

self.assertEqual(redacted_address, "...@1...")

def test_third_party_invite_keyword_block_works(self):
invite_config = {
"medium": "email",
"address": "evil",
"room_id": "bad",
"sender": "@bad_dude:badness.org",
}
request, channel = make_request(
self.sydent.reactor,
"POST",
"/_matrix/identity/v2/store-invite",
invite_config,
)
store_invite_servlet = StoreInviteServlet(self.sydent)
request.render(store_invite_servlet)
self.assertEqual(channel.code, 403)


class ThreepidInvitesNoDeleteTestCase(unittest.TestCase):
"""Test that invite tokens are not deleted when that is disabled."""
Expand Down