Skip to content

fix(matrix): use standard Base64 for E2EE attachment IV - #1788

Merged
caronc merged 6 commits into
caronc:masterfrom
vampywiz17:fix/matrix-e2ee-attachment-iv-base64
Oct 16, 2026
Merged

caronc merged 6 commits into
caronc:masterfrom
vampywiz17:fix/matrix-e2ee-attachment-iv-base64

Conversation

@vampywiz17

@vampywiz17 vampywiz17 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Description

Related issue (if applicable): None — fixes a Matrix E2EE attachment encoding bug.

Encrypted Matrix image/file attachments sometimes fail to render in Matrix clients even when the attachment event is successfully delivered. A strict client reports an error such as:

invalid value in `iv` field: invalid base64 encoding:
invalid symbol 95, offset 7

The Matrix encrypted attachment format uses Base64URL for the JWK encryption key (file.key.k), but requires standard, unpadded Base64 for the initialization vector (file.iv).

The existing encrypt_attachment() implementation uses base64.urlsafe_b64encode() for the IV too. Depending on the random IV, this generates - or _ characters, which are rejected by strict Base64 decoders. This explains why the problem occurs intermittently.

This PR changes the IV encoding to:

iv_b64 = base64.b64encode(iv).rstrip(b"=").decode()

The change does not modify AES-256-CTR encryption, generated IV bytes, JWK key encoding, or the ciphertext hash.

A regression test forces an IV whose standard Base64 representation contains / rather than _, verifies that the serialized IV uses standard Base64, and checks successful AES-256-CTR decryption.

Matrix specification: https://spec.matrix.org/latest/client-server-api/#sending-encrypted-attachments

Checklist

  • Documentation ticket created (if applicable): Not applicable; this restores compliance with the existing Matrix specification.
  • The change is tested and works locally. (Live integration testing pending.)
  • No commented-out code in this PR.
  • No lint errors (use tox -e lint and optionally tox -e format). (Not yet run locally.)
  • Test coverage added or updated (use tox -e qa). (Regression test added; execution pending.)

Testing

Anyone can help test as follows:

# Clone the proposed branch
git clone --branch fix/matrix-e2ee-attachment-iv-base64 \
    https://github.com/vampywiz17/apprise.git

cd apprise

# Run the focused regression test
tox -e qa -- -k encrypted_attachment_iv_standard_base64

# Check lint
tox -e lint

For integration testing, configure an E2EE-enabled Matrix room and send image attachments repeatedly.

Verify that attachments remain readable in Element and other strict Matrix clients, including when the generated IV contains + or /.

No live Matrix credentials are required for the regression test.

@vampywiz17 vampywiz17 changed the title Fix/matrix e2ee attachment iv base64 fix(matrix): use standard Base64 for E2EE attachment IV Oct 14, 2026
@codecov

codecov Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (5c5c3ac) to head (2714f41).
⚠ïļ Report is 11 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #1788   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          246       246           
  Lines        40290     40307   +17     
  Branches      6950      6957    +7     
=========================================
+ Hits         40290     40307   +17     
Files with missing lines Coverage Δ
apprise/plugins/matrix/e2ee.py 100.00% <100.00%> (Ãļ)

... and 6 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄ïļ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@caronc
caronc merged commit 8227752 into caronc:master Oct 16, 2026
20 checks passed
@caronc

caronc commented Oct 16, 2026

Copy link
Copy Markdown
Owner

Thank you again for your PR! 🚀

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.

2 participants