mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-09-04 02:01:48 +03:00
fix(signal): honor wildcard in inbound allowlists
This commit is contained in:
@@ -862,6 +862,7 @@ class SignalChannel(BaseChannel):
|
|||||||
return False, chat_id
|
return False, chat_id
|
||||||
if (
|
if (
|
||||||
self.config.group.policy == "allowlist"
|
self.config.group.policy == "allowlist"
|
||||||
|
and "*" not in self.config.group.allow_from
|
||||||
and chat_id not in self.config.group.allow_from
|
and chat_id not in self.config.group.allow_from
|
||||||
):
|
):
|
||||||
self.logger.info(
|
self.logger.info(
|
||||||
@@ -1061,6 +1062,9 @@ class SignalChannel(BaseChannel):
|
|||||||
def _sender_matches_allowlist(cls, sender_id: str, allow_list: list[str]) -> bool:
|
def _sender_matches_allowlist(cls, sender_id: str, allow_list: list[str]) -> bool:
|
||||||
"""Return True if any normalized variant of sender_id is on allow_list.
|
"""Return True if any normalized variant of sender_id is on allow_list.
|
||||||
|
|
||||||
|
A ``"*"`` entry allows every sender, matching the channel-wide
|
||||||
|
allowlist contract.
|
||||||
|
|
||||||
Both ``sender_id`` and each allow_list entry can be a single
|
Both ``sender_id`` and each allow_list entry can be a single
|
||||||
identifier or a pipe-joined composite of several (e.g.
|
identifier or a pipe-joined composite of several (e.g.
|
||||||
``"+1234567890|uuid-abc"``); both sides are split on ``|`` and each
|
``"+1234567890|uuid-abc"``); both sides are split on ``|`` and each
|
||||||
@@ -1070,6 +1074,8 @@ class SignalChannel(BaseChannel):
|
|||||||
"""
|
"""
|
||||||
if not allow_list:
|
if not allow_list:
|
||||||
return False
|
return False
|
||||||
|
if "*" in allow_list:
|
||||||
|
return True
|
||||||
sender_variants: set[str] = set()
|
sender_variants: set[str] = set()
|
||||||
for part in str(sender_id).split("|"):
|
for part in str(sender_id).split("|"):
|
||||||
sender_variants.update(cls._normalize_signal_id(part))
|
sender_variants.update(cls._normalize_signal_id(part))
|
||||||
|
|||||||
@@ -790,6 +790,14 @@ class TestHandleDataMessageDM:
|
|||||||
await ch._handle_receive_notification(params)
|
await ch._handle_receive_notification(params)
|
||||||
assert len(handled) == 1
|
assert len(handled) == 1
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_dm_allowlist_wildcard_preserves_content(self):
|
||||||
|
ch, handled = self._make_dm_channel(policy="allowlist", allow_from=["*"])
|
||||||
|
params = _dm_envelope(source_number="+19995550001", message="wildcard DM")
|
||||||
|
await ch._handle_receive_notification(params)
|
||||||
|
assert len(handled) == 1
|
||||||
|
assert handled[0]["content"] == "wildcard DM"
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_dm_allowlist_rejected_triggers_pairing(self):
|
async def test_dm_allowlist_rejected_triggers_pairing(self):
|
||||||
# Denied DM senders go through super()._handle_message which checks
|
# Denied DM senders go through super()._handle_message which checks
|
||||||
@@ -1025,6 +1033,16 @@ class TestHandleDataMessageGroup:
|
|||||||
await ch._handle_receive_notification(params)
|
await ch._handle_receive_notification(params)
|
||||||
assert len(handled) == 1
|
assert len(handled) == 1
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_group_allowlist_wildcard_preserves_content(self):
|
||||||
|
ch, handled = self._make_group_channel(
|
||||||
|
policy="allowlist", allow_from=["*"], require_mention=False
|
||||||
|
)
|
||||||
|
params = _group_envelope(group_id="grp==", source_name="Alice", message="wildcard group")
|
||||||
|
await ch._handle_receive_notification(params)
|
||||||
|
assert len(handled) == 1
|
||||||
|
assert "[Alice]: wildcard group" in handled[0]["content"]
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_group_allowlist_rejected(self):
|
async def test_group_allowlist_rejected(self):
|
||||||
ch, handled = self._make_group_channel(policy="allowlist", allow_from=["other=="])
|
ch, handled = self._make_group_channel(policy="allowlist", allow_from=["other=="])
|
||||||
|
|||||||
Reference in New Issue
Block a user