Files
Shadowbroker/backend/tests/mesh/test_dm_welcome_resend.py
BigBodyCobainandClaude Opus 5.5 90d3fa370b Make secure mail work between two fresh nodes
Mail between two nodes that had just exchanged contact addresses never
arrived readable. Fixes along the whole path, each covered by a test:

- Status: the web UI's own requests get the full wormhole status, so it
  stops treating a ready node as not ready.
- Invites: the owner node named in an invite is asked first, with an
  onion-sized timeout; an import that could not reach it finishes in the
  background and replaces the keyless contact it left behind.
- Aliases: no one-sided mailbox alias is made up on compose; both sides
  address mail by node id until an alias is agreed.
- Signing: each mailbox request is signed once, keeping polls under the
  sign rate limit.
- MLS: the welcome is sent with the mail until the peer answers, and the
  receiver joins as the identity whose key package the sender used, with
  that identity's DH key.
- Sender seal: a seal addressed to the node's DM identity opens even when
  the browser names the node's transport identity.
- Gate rooms: the chat controller restarted the live stream and the room
  long-poll on every status refresh, about 60 requests a second, which
  exhausted local ports; it now reacts only to access changes, and the
  stream waits a minute between tries when there is no admin session.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-02 05:17:48 -07:00

64 lines
2.2 KiB
Python

"""Mail on a session we started carries its welcome until the peer answers.
The MLS welcome used to ride only on the very first message. When that send
failed after sealing (peer offline, relay refused), every later message went
out without it, and the peer could never join the session to read any of
them.
"""
from unittest.mock import patch
import pytest
from services.mesh import mesh_dm_mls as mls
class _Client:
def dm_session_welcome(self, handle):
return b"welcome-for-%d" % handle
def dm_decrypt(self, handle, ciphertext):
return ciphertext
@pytest.fixture
def session(monkeypatch):
binding = mls._SessionBinding(
session_id=mls._session_id("me", "peer"),
local_alias="me",
remote_alias="peer",
role="initiator",
session_handle=7,
created_at=1,
)
monkeypatch.setattr(mls, "_SESSIONS", {binding.session_id: binding})
monkeypatch.setattr(mls, "_load_state", lambda: None)
monkeypatch.setattr(mls, "_save_state", lambda: None)
monkeypatch.setattr(mls, "_privacy_client", lambda: _Client())
monkeypatch.setattr(mls, "_seal_welcome_for_public_key", lambda payload, pub: b"sealed:" + payload)
monkeypatch.setattr(mls, "_require_private_transport", lambda: (True, ""))
monkeypatch.setattr(mls, "_unpad_plaintext", lambda data: data)
monkeypatch.setattr(mls, "_lock_dm_format", lambda *a, **k: None)
return binding
def test_initiator_resends_welcome_until_the_peer_answers(session):
welcome = mls.pending_session_welcome("me", "peer", "peer-dh")
assert mls._unb64(welcome) == b"sealed:welcome-for-7"
assert mls.decrypt_dm("me", "peer", mls._b64(b"hi"), "")["ok"] is True
assert session.peer_confirmed is True
assert mls.pending_session_welcome("me", "peer", "peer-dh") == ""
def test_no_welcome_from_the_joining_side_or_without_a_key(session):
assert mls.pending_session_welcome("me", "peer", "") == ""
session.role = "responder"
assert mls.pending_session_welcome("me", "peer", "peer-dh") == ""
assert mls.pending_session_welcome("me", "stranger", "peer-dh") == ""
def test_confirmation_survives_a_restart(session):
session.peer_confirmed = True
assert mls._serialize_session(session)["peer_confirmed"] is True