Skip to content

fix(matrix): hard-fail E2EE when python-olm missing + stable MATRIX_DEVICE_ID - #5517

Closed
kshitijk4poor wants to merge 1 commit into
NousResearch:mainfrom
kshitijk4poor:fix/matrix-e2ee-hard-fail-stable-device
Closed

kshitijk4poor wants to merge 1 commit into
NousResearch:mainfrom
kshitijk4poor:fix/matrix-e2ee-hard-fail-stable-device

Conversation

@kshitijk4poor

Copy link
Copy Markdown
Contributor

Problem

Matrix E2EE silently doesn't work in encrypted rooms even when MATRIX_ENCRYPTION=true is set. Two root causes:

1. Silent degradation when E2EE deps missing

When matrix-nio is installed without the [e2e] extra (no python-olm/libolm), nio.crypto.ENCRYPTION_ENABLED is False and client.olm is never initialized. The adapter logged warnings but connect() returned True — bot appeared online but could never decrypt messages in encrypted rooms.

2. Unstable device identity across restarts

Without a stable device_id, each restart creates a new device identity. Other Matrix clients see it as 'unknown device' and refuse to share Megolm session keys, so decryption fails even when the crypto backend is correctly installed.

Fix

Hard-fail when E2EE deps are missing:

  • New _check_e2ee_deps() function checks nio.crypto.ENCRYPTION_ENABLED
  • check_matrix_requirements() fails at startup if encryption is requested but deps are missing — clear error message tells users exactly what to install
  • connect() double-checks and refuses to start if crypto store fails to load
  • No more silent fallback to plaintext-only mode when E2EE was explicitly requested

Stable device identity via MATRIX_DEVICE_ID:

  • New env var MATRIX_DEVICE_ID lets users pin a stable device ID
  • Passed to nio.AsyncClient constructor and restore_login()
  • Configured device_id takes precedence over whoami-resolved device_id
  • Plumbed through gateway/config.py and hermes_cli/config.py

Files changed

File Change
gateway/platforms/matrix.py Hard-fail logic, _check_e2ee_deps(), MATRIX_DEVICE_ID support
gateway/config.py Plumb MATRIX_DEVICE_ID into platform extras
hermes_cli/config.py Add MATRIX_DEVICE_ID to OPTIONAL_ENV_VARS
tests/gateway/test_matrix.py 12 new tests covering hard-fail, device_id, config plumbing

Testing

  • All 118 Matrix tests pass
  • Full suite: 2313 passed, 1 pre-existing failure (test_session_race_guard)

Note

This fixes the check_matrix_requirements() and connect() paths. The deeper E2EE verification flow (SAS device verification, recovery key support) is a separate feature request tracked in #3521 comments.

Closes #3521

…EVICE_ID

Two issues caused Matrix E2EE to silently not work in encrypted rooms:

1. When matrix-nio is installed without the [e2e] extra (no python-olm /
   libolm), nio.crypto.ENCRYPTION_ENABLED is False and client.olm is
   never initialized. The adapter logged warnings but returned True from
   connect(), so the bot appeared online but could never decrypt messages.
   Now: check_matrix_requirements() and connect() both hard-fail with a
   clear error message when MATRIX_ENCRYPTION=true but E2EE deps are
   missing.

2. Without a stable device_id, the bot gets a new device identity on each
   restart. Other clients see it as "unknown device" and refuse to share
   Megolm session keys. Now: MATRIX_DEVICE_ID env var lets users pin a
   stable device identity that persists across restarts and is passed to
   nio.AsyncClient constructor + restore_login().

Changes:
- gateway/platforms/matrix.py: add _check_e2ee_deps(), hard-fail in
  connect() and check_matrix_requirements(), MATRIX_DEVICE_ID support
  in constructor + restore_login
- gateway/config.py: plumb MATRIX_DEVICE_ID into platform extras
- hermes_cli/config.py: add MATRIX_DEVICE_ID to OPTIONAL_ENV_VARS

Closes NousResearch#3521
@teknium1

teknium1 commented Apr 6, 2026

Copy link
Copy Markdown
Collaborator

Merged via PR #5665. Your commit was cherry-picked with authorship preserved. Thanks @kshitijk4poor!

@kshitijk4poor
kshitijk4poor deleted the fix/matrix-e2ee-hard-fail-stable-device branch August 5, 2026 07:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Matrix E2EE: Bot Cannot Decrypt Messages and Device Verification is Non-Functional

2 participants