Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(channels): close first-contact bot pairing after a setup window

rubenluetke10-beep4362ceeb27d5
diff --git a/jarvis/channels/base.py b/jarvis/channels/base.py
index 8ce4d97d63..7946a5a20b 100644
--- a/jarvis/channels/base.py
+++ b/jarvis/channels/base.py
@@ -14,7 +14,21 @@ from uuid import UUID, uuid4
from jarvis.core.protocols import ChannelAdapter
-__all__ = ["ChannelAdapter", "ChannelMessage", "ChannelSession"]
+__all__ = [
+ "FIRST_CONTACT_PAIRING_WINDOW_S",
+ "ChannelAdapter",
+ "ChannelMessage",
+ "ChannelSession",
+]
+
+#: How long after a chat channel comes up (app start, or the fresh instance a
+#: marketplace connect builds) its EMPTY allowlist may still be claimed by the
+#: first direct message. Messaging the bot is prompting Jarvis with its tools,
+#: and a bot's username is public, so an unbounded first-contact claim let any
+#: stranger who found the bot before its owner did take it over for good. The
+#: owner pairs right after connecting; after this window an empty allowlist
+#: stays closed until the next start or reconnect.
+FIRST_CONTACT_PAIRING_WINDOW_S = 600.0
def _now_ns() -> int:
diff --git a/jarvis/channels/discord.py b/jarvis/channels/discord.py
index 1b5a26b421..8b23ddebea 100644
--- a/jarvis/channels/discord.py
+++ b/jarvis/channels/discord.py
@@ -33,7 +33,11 @@ from collections.abc import AsyncIterator
from typing import TYPE_CHECKING, Any
from uuid import UUID, uuid4
-from jarvis.channels.base import ChannelMessage, ChannelSession
+from jarvis.channels.base import (
+ FIRST_CONTACT_PAIRING_WINDOW_S,
+ ChannelMessage,
+ ChannelSession,
+)
from jarvis.channels.manager import ChannelContext, ChannelStartError
from jarvis.core.bus import EventBus
from jarvis.core.config import DiscordConfig, get_secret
@@ -124,6 +128,10 @@ class DiscordChannel:
self._event_handler_ref: Any = None
self._bot_user_id: int = 0
self._started = False
+ # First-contact pairing is open only for a short window after this
+ # instance is built (see FIRST_CONTACT_PAIRING_WINDOW_S).
+ self._pairing_closes_at = time.monotonic() + FIRST_CONTACT_PAIRING_WINDOW_S
+ self._pairing_closed_logged = False
@classmethod
def from_context(cls, ctx: ChannelContext) -> DiscordChannel:
@@ -364,6 +372,17 @@ class DiscordChannel:
except (TypeError, ValueError):
return False
+ if time.monotonic() > self._pairing_closes_at:
+ if not self._pairing_closed_logged:
+ self._pairing_closed_logged = True
+ log.warning(
+ "Discord first-user pairing refused: the pairing window closed "
+ "%.0f s after the channel started. Message the bot right after "
+ "connecting it, or add your user id to allowed_user_ids.",
+ FIRST_CONTACT_PAIRING_WINDOW_S,
+ )
+ return False
+
self._cfg.allowed_user_ids.append(user_id)
try:
from jarvis.core.config_writer import add_discord_allowed_user_id
diff --git a/jarvis/channels/telegram.py b/jarvis/channels/telegram.py
index 7e896554af..f0b13e3467 100644
--- a/jarvis/channels/telegram.py
+++ b/jarvis/channels/telegram.py
@@ -22,7 +22,11 @@ from collections.abc import AsyncIterator
from typing import TYPE_CHECKING, Any
from uuid import UUID, uuid4
-from jarvis.channels.base import ChannelMessage, ChannelSession
+from jarvis.channels.base import (
+ FIRST_CONTACT_PAIRING_WINDOW_S,
+ ChannelMessage,
+ ChannelSession,
+)
from jarvis.channels.manager import ChannelContext, ChannelStartError
from jarvis.core.bus import EventBus
from jarvis.core.config import TelegramConfig, get_secret, set_secret
@@ -112,6 +116,10 @@ class TelegramChannel:
self._event_handler_ref: Any = None
self._bot_username: str = ""
self._started = False
+ # First-contact pairing is open only for a short window after this
+ # instance is built (see FIRST_CONTACT_PAIRING_WINDOW_S).
+ self._pairing_closes_at = time.monotonic() + FIRST_CONTACT_PAIRING_WINDOW_S
+ self._pairing_closed_logged = False
@classmethod
def from_context(cls, ctx: ChannelContext) -> TelegramChannel:
@@ -351,6 +359,17 @@ class TelegramChannel:
except (TypeError, ValueError):
return False
+ if time.monotonic() > self._pairing_closes_at:
+ if not self._pairing_closed_logged:
+ self._pairing_closed_logged = True
+ log.warning(
+ "Telegram first-user pairing refused: the pairing window closed "
+ "%.0f s after the channel started. Message the bot right after "
+ "connecting it, or add your user id to allowed_user_ids.",
+ FIRST_CONTACT_PAIRING_WINDOW_S,
+ )
+ return False
+
self._cfg.allowed_user_ids.append(user_id)
try:
from jarvis.core.config_writer import add_telegram_allowed_user_id
diff --git a/tests/unit/channels/test_discord_channel.py b/tests/unit/channels/test_discord_channel.py
index 2a92a75dc2..bc118357a0 100644
--- a/tests/unit/channels/test_discord_channel.py
+++ b/tests/unit/channels/test_discord_channel.py
@@ -182,6 +182,34 @@ def test_pair_first_dm_claims_empty_allowlist(
assert ch._cfg.allowed_user_ids == [777] # noqa: SLF001
+def test_pair_first_dm_refused_after_the_pairing_window(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """A stranger who finds the bot after the owner's setup window closed must
+ not claim it: messaging the bot is prompting Jarvis with its tools."""
+ persisted: list[int] = []
+ monkeypatch.setattr(
+ "jarvis.core.config_writer.add_discord_allowed_user_id",
+ lambda user_id: persisted.append(user_id),
+ )
+ cfg = DiscordConfig(enabled=True, allowed_user_ids=[], pair_on_first_dm=True)
+ ch = _make_channel(cfg)
+ ch._pairing_closes_at = time.monotonic() - 1.0 # noqa: SLF001
+ assert ch._pair_first_dm(_make_message(user_id=666)) is False # noqa: SLF001
+ assert ch._cfg.allowed_user_ids == [] # noqa: SLF001
+ assert persisted == []
+
+
+def test_pairing_window_spans_the_documented_length() -> None:
+ from jarvis.channels.base import FIRST_CONTACT_PAIRING_WINDOW_S
+
+ before = time.monotonic()
+ ch = _make_channel(DiscordConfig(enabled=True, allowed_user_ids=[]))
+ closes_at = ch._pairing_closes_at # noqa: SLF001
+ assert before + FIRST_CONTACT_PAIRING_WINDOW_S <= closes_at
+ assert closes_at <= time.monotonic() + FIRST_CONTACT_PAIRING_WINDOW_S
+
+
def test_pair_first_dm_off_when_allowlist_nonempty() -> None:
cfg = DiscordConfig(enabled=True, allowed_user_ids=[1], pair_on_first_dm=True)
ch = _make_channel(cfg)
diff --git a/tests/unit/channels/test_telegram_channel.py b/tests/unit/channels/test_telegram_channel.py
index ff544a1097..64b285d49e 100644
--- a/tests/unit/channels/test_telegram_channel.py
+++ b/tests/unit/channels/test_telegram_channel.py
@@ -222,6 +222,26 @@ async def test_on_msg_pairs_first_private_user_and_inserts_inbox(
assert msg.metadata["telegram_chat_id"] == 999
+async def test_on_msg_refuses_first_contact_after_the_pairing_window(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """After the setup window an empty allowlist stays closed: a stranger who
+ finds the public bot later must not become its owner."""
+ paired: list[int] = []
+ monkeypatch.setattr(
+ "jarvis.core.config_writer.add_telegram_allowed_user_id",
+ lambda user_id: paired.append(user_id),
+ )
+ cfg = TelegramConfig(enabled=True, allowed_user_ids=[])
+ ch = _make_channel(cfg)
+ ch._pairing_closes_at = time.monotonic() - 1.0 # noqa: SLF001
+ await ch._on_telegram_msg(_make_update(user_id=666), _ctx=None) # noqa: SLF001
+ assert paired == []
+ assert ch._cfg.allowed_user_ids == [] # noqa: SLF001
+ assert ch._inbox.qsize() == 0 # noqa: SLF001
+
+
@pytest.mark.asyncio
async def test_start_command_replies_without_entering_inbox(
monkeypatch: pytest.MonkeyPatch,